Top Banner
The Relational The Relational Model 1 Model 1 Prof. Sin-Min Lee Prof. Sin-Min Lee Department of Mathematics Department of Mathematics and Computer Science and Computer Science Prof. Sin-Min Lee Department of Computer Science
60
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Relational Model

The Relational The Relational Model 1Model 1Prof. Sin-Min LeeProf. Sin-Min Lee

Department of Mathematics Department of Mathematics and Computer Scienceand Computer ScienceProf. Sin-Min Lee

Department of Computer Science

Page 2: Relational Model

History of Relational History of Relational ModelModel

• First proposed by a E.F. Codd in 1970. Codd proposed the Relational model in 1970.“ A relational model of data for large shared data banks.”

•He linked the representation of data with that of mathematical sets.

• First research started at IBM’s San Jose Research Laboratory. Prototype was called System R.

• Commercial RDBMS’s started to appear in late 1970’s and early 1980’s. Most well known is Oracle.

Page 3: Relational Model

Dr. Edgar F. Codd (1923-Dr. Edgar F. Codd (1923-2003)2003)

Codd completed his PhD at Codd completed his PhD at the University of Michigan in the University of Michigan in 1963, and presented a thesis 1963, and presented a thesis on the topic of a self-on the topic of a self-reproducing computer reproducing computer consisting of a large number consisting of a large number of simple identical cells, each of simple identical cells, each of which interacts in a of which interacts in a uniform manner with its four uniform manner with its four immediate neighbors. Codd immediate neighbors. Codd reported this work in a book reported this work in a book entitled entitled Cellular AutomataCellular Automata published by Academic Press published by Academic Press in 1968. in 1968.

Page 4: Relational Model

Edgar CoddEdgar Codd The The relational modelrelational model devised by Codd was devised by Codd was

explored during the 1970s, and commercial explored during the 1970s, and commercial relational database products began to emerge relational database products began to emerge in the 1980s, originally for mainframe in the 1980s, originally for mainframe systems and later for microcomputers. Edgar systems and later for microcomputers. Edgar Codd first wrote about the concept of Codd first wrote about the concept of relational databasesrelational databases in his paper "A in his paper "A Relational Model of Data for Large Shared Relational Model of Data for Large Shared Data Banks" in 1970. He was using the term Data Banks" in 1970. He was using the term "relation""relation" in the strict mathematical sense of in the strict mathematical sense of a table with a few special properties. Thus a table with a few special properties. Thus Codd was describing a system where all of Codd was describing a system where all of the the database database - data, structure, rules - is - data, structure, rules - is housed in simple tables of rows and columns. housed in simple tables of rows and columns.

Page 5: Relational Model

Edgar CoddEdgar Codd While this may seem obvious to us today, it While this may seem obvious to us today, it

was by no means obvious in 1970. Codd was by no means obvious in 1970. Codd went on to define relational databases more went on to define relational databases more completely where he laid out twelve completely where he laid out twelve principles of relational databases in 1974. principles of relational databases in 1974. His most recent work in 1990, expands the His most recent work in 1990, expands the list to 333 requirements.(list to 333 requirements.(CoddCodd 90 90))NormalizationNormalization for relational databases was for relational databases was introduced by Dr. E. F. Codd back in 1970 introduced by Dr. E. F. Codd back in 1970 when he wrote his original paper. The when he wrote his original paper. The concept has since been expounded upon by concept has since been expounded upon by other experts in the field. other experts in the field.

Read his obituary Read his obituary http://www.intosaiitaudit.org/intoit_articles/18p60top62.pdfhttp://www.intosaiitaudit.org/intoit_articles/18p60top62.pdf

Page 6: Relational Model

Read: Read: http://www.mercurynews.com/mld/merchttp://www.mercurynews.com/mld/merc

urynews/news/local/5676133.htm?1curynews/news/local/5676133.htm?1c IBM database developer dead at 79IBM database developer dead at 79

`RELATIONAL' MODEL IS BASIS OF TODAY'S TRANSACTIONS`RELATIONAL' MODEL IS BASIS OF TODAY'S TRANSACTIONS By Lisa M. KriegerBy Lisa M. Krieger Mercury NewsMercury News

Edgar F. Codd, an IBM computer pioneer who created the Edgar F. Codd, an IBM computer pioneer who created the ``relational database model'' that underlies a $7 billion industry of ``relational database model'' that underlies a $7 billion industry of storing the world's online business data, died of heart failure at home storing the world's online business data, died of heart failure at home Friday in Williams Island, Fla. He was 79.Friday in Williams Island, Fla. He was 79.

Bank accounts, credit cards, stock trading, travel reservations, online Bank accounts, credit cards, stock trading, travel reservations, online auctions and innumerable other now-routine data transactions all auctions and innumerable other now-routine data transactions all rely on Codd's model, based on highly abstract and complex rely on Codd's model, based on highly abstract and complex mathematical theory.mathematical theory.

Before Codd's landmark research paper in 1970, it was possible to Before Codd's landmark research paper in 1970, it was possible to store lots of information -- but analyzing it was difficult, requiring store lots of information -- but analyzing it was difficult, requiring lines and lines of code for even simple tasks.lines and lines of code for even simple tasks.

Page 7: Relational Model

Codd’s Original PaperCodd’s Original Paper

““ A Relational Model of Data for Large A Relational Model of Data for Large Shared Data Banks”Shared Data Banks”

Communications of the ACM, Volume 13, Communications of the ACM, Volume 13, Number 6, June 1970Number 6, June 1970

Lower level (basement) of the new Martin Lower level (basement) of the new Martin Luther King, Jr. LibraryLuther King, Jr. Library

Get to roll the shelves apart to access Get to roll the shelves apart to access journals.journals.

Page 8: Relational Model

Codd’s ReasonsCodd’s Reasons

Data independence from database Data independence from database implementation such as machine implementation such as machine representationrepresentation

Natural structure of dataNatural structure of data Can be analyzed mathematically Can be analyzed mathematically

(Codd was a mathematician by (Codd was a mathematician by training)training)

Page 9: Relational Model

Alternative: Network Alternative: Network ModelModel

Charles A. Bachman 1973 ACM Charles A. Bachman 1973 ACM Turing Award Lecture “The Turing Award Lecture “The Programmer as Navigator”Programmer as Navigator”

Communications of the ACM, Communications of the ACM, Volume 16, Number 11, November Volume 16, Number 11, November 1973, pp. 653-6571973, pp. 653-657

Page 10: Relational Model
Page 11: Relational Model

INTRODUCTIONINTRODUCTIONThe relational model is the most used data model for commercial data-processing because it is simple to use and to maintain.

A relational data model is based on a collection of tables. The user of the database system may query these tables, insert new tuples, and update (modify) tuples. There are several languages for expressing these operations.

Page 12: Relational Model

Data Models

· Codd Codd suggests that suggests that any data any data model has model has three three components:components:

the data the data structures;structures;

the integrity the integrity constraints;constraints;

the data the data manipulation manipulation operators.operators.

Page 13: Relational Model
Page 14: Relational Model
Page 15: Relational Model

Relational Data StructureRelational Data Structure

EMPLOYEEEMPLOYEE

Emp# Name Emp Sex Mgr Emp#Emp# Name Emp Sex Mgr Emp#

E1 JonesE1 Jones MaleMale E65E65

E6 SmithE6 Smith MaleMale E28E28

E28E28 Jones FemaleJones Female --

Relation Attribute

Heading

Body

FemaleMale

Gender

Domain

Page 16: Relational Model

Basic StructureBasic Structure

account-numberaccount-number branch-namebranch-name balancebalance

A-101A-101 DowntownDowntown 500500

A-102A-102 PerryridgePerryridge 400400

A-201A-201 BrightonBrighton 900900

A-215A-215 MianusMianus 700700

The account table below represents a relation in the relational model. The three columns titles are the attributes and their domains.

Each row is called a tuple.

An account is a subset of the set of all possible tuples.

Page 17: Relational Model

The DomainThe Domain

Emp#E1E2E3

NameRed

BrownBlack

Mgr#-

E1E1

E1, E2, E3,E4Red, BrownBlack, Blue

Attributes

Domains

Employee

Person Name

Emp#

Page 18: Relational Model
Page 19: Relational Model
Page 20: Relational Model

Seven Characteristics of Seven Characteristics of a relationa relation

• The name of the relation is different from all others.

• Each cell of the relation contains only one value

• Each attribute (or field) has a name that is distinct.

• All the values of a particular attribute are from the same domain.

• The order of the attributes makes no difference.

• There are no duplicate tuples

• The order of the tuples makes no difference.

Page 21: Relational Model

Example of the Student Example of the Student table.table.

SocialNum FirstName LastName PhoneNum Class#556-34-2832 John Smith 924-1000 32245

839-32-1929 Jane Doe 924-1929 99839

312-39-5193 Some Body 555-1000 11021

493-33-2910 Any One 555-1020 49303

Primary Key

Page 22: Relational Model

Other terms...Other terms...

CardinalityCardinality = Number of rows = Number of rows DegreeDegree = Number of columns = Number of columns

Cardinality = 6

Degree = 5

Page 23: Relational Model
Page 24: Relational Model
Page 25: Relational Model
Page 26: Relational Model
Page 27: Relational Model
Page 28: Relational Model
Page 29: Relational Model
Page 30: Relational Model

Database SchemaDatabase Schema

Database SchemaDatabase Schema is the logical is the logical design of the database design of the database

Database instanceDatabase instance is a snapshot of is a snapshot of the data in the DB at a given the data in the DB at a given instance in timeinstance in time

Relation instanceRelation instance is the is the programming language notion of a programming language notion of a value of a variablevalue of a variable

Page 31: Relational Model

Database SchemaDatabase Schema

Relation schema consists of a list of attributes and their corresponding domain.

As a convention, uppercase letter are used so Account-schema=(account-number, branch-name, balance) This means that account is a relation on Account-schema by account(Account-schema)

Page 32: Relational Model

Database SchemaDatabase Schema

Relation instance is the set of values of a relation at a specific moment in time. This values may change in time causing a change in the relation as it is updated.

Page 33: Relational Model

The Relational Data ModelThe Relational Data Model

· DATA STRUCTURES - domain, DATA STRUCTURES - domain, attribute, relation, tuple, primary attribute, relation, tuple, primary key, degree, cardinality.key, degree, cardinality.

INTEGRITY CONSTRAINTS - entity INTEGRITY CONSTRAINTS - entity integrity and referential integrity.integrity and referential integrity.

DATA MANIPULATION DATA MANIPULATION OPERATIONS - defined through OPERATIONS - defined through relational algebra and equivalent relational algebra and equivalent relational calculus.relational calculus.

Page 34: Relational Model

KeysKeys Superkey Superkey is a set of one or more is a set of one or more

attributes that allow us to identify attributes that allow us to identify uniquely an entity in the entity set.uniquely an entity in the entity set.

Candidate Key Candidate Key are minimal superkey in are minimal superkey in an entity, one of those keys is selected to an entity, one of those keys is selected to be the primary keybe the primary key

Primary Key Primary Key is a candidate key that is is a candidate key that is chosen to identify entities within an entity chosen to identify entities within an entity setset

Foreign Key Foreign Key is a primary key of another is a primary key of another relation schemarelation schema

Page 35: Relational Model

KeysKeys

If K of R is a superkey for R, then the relation r(R) does not have two tuples with the same value. So if t1 and t2 are in r t1 = t2

Page 36: Relational Model

Find Candidate KeysFind Candidate Keys

R(A, B, C, D)R(A, B, C, D)1 2 3 41 1 3 43 2 1 44 1 2 3

{A, B} {A, C} {A, D}

{B, C} {B, D} {C, D}

X {A,C,D} {B, C, D}

X X

X

= okay = not okay

Page 37: Relational Model

How to determine keysHow to determine keys

Strong entity set:Strong entity set: the entity primary the entity primary key becomes the relation primary keykey becomes the relation primary key

Weak entity set:Weak entity set: the primary key of the primary key of the relation is the union of the strong the relation is the union of the strong entity set primary key and the entity set primary key and the discriminatordiscriminator

Relation set:Relation set: the union of the primary the union of the primary keys of the related entity sets becomes keys of the related entity sets becomes a superkey of the relationa superkey of the relation

Page 38: Relational Model

How to determine keysHow to determine keys

Combined tables:Combined tables: in a many-to-one, in a many-to-one, the primary key of the many the primary key of the many becomes the relation primary key. In becomes the relation primary key. In a one-to-one either primary key can a one-to-one either primary key can be usedbe used

Multivalued attributes:Multivalued attributes: the entity the entity primary key becomes the primary primary key becomes the primary keykey

Page 39: Relational Model

account-number

branch-namebalance

Schema Diagram

branch-name

branch-cityassets

customer-nameloan-number

customer-name

customer-streetcustomer-city

customer-nameaccount-number

loan-number

branch-nameamount

account depositor customer

branch loanborrowerdependency

relation shade indicates primary key

A database schema with primary and foreign key dependencies

primary

Page 40: Relational Model

Entity Integrity Entity Integrity

· No component of the Primary Key of a base No component of the Primary Key of a base relation is allowed to accept nulls.relation is allowed to accept nulls.

SurnameRed

BlackRed

Black

Given NameJohn

Fred

Salary$40,000$50,000$60,000$70,000

Page 41: Relational Model
Page 42: Relational Model
Page 43: Relational Model
Page 44: Relational Model
Page 45: Relational Model
Page 46: Relational Model
Page 47: Relational Model
Page 48: Relational Model

Foreign KeyForeign Key

· A foreign key is an attribute or attribute combination A foreign key is an attribute or attribute combination of one relation R2 whose values are required to of one relation R2 whose values are required to match those of the primary key of relation R1 where match those of the primary key of relation R1 where R1 and R2 are not necessarily distinct. Note that a R1 and R2 are not necessarily distinct. Note that a foreign key and the corresponding primary key foreign key and the corresponding primary key should be defined on the same domain(s).should be defined on the same domain(s).

Emp#e1e2e3

enameredblue

brown

Deptd1d2d3

Worksfordeptd1

d2

DnamePayTaxArt

Employee Dept

Foreign key

Page 49: Relational Model

Referential IntegrityReferential Integrity

If base relation R2 includes a foreign key FK If base relation R2 includes a foreign key FK matching the primary key PK of some base relation matching the primary key PK of some base relation R1 then every value of FK in R2 must either R1 then every value of FK in R2 must either

(a) be equal to the value of PK in some tuple of R1, (a) be equal to the value of PK in some tuple of R1, oror

(b) be wholly null.(b) be wholly null.

Note that PK and FK may comprise more Note that PK and FK may comprise more than one attribute and that R1 and R2 are not than one attribute and that R1 and R2 are not necessarily distinct.necessarily distinct.

( Stated more simply a foreign key should be a valid primary key value or null.)

Page 50: Relational Model

Foreign Key RulesForeign Key Rules

For each foreign key three rules need to be answered:For each foreign key three rules need to be answered:

Can the foreign key accept nulls ?Can the foreign key accept nulls ?

What should happen on an attempt to delete the target of a What should happen on an attempt to delete the target of a foreign key reference?foreign key reference?

What should happen on an attempt to update the target of a What should happen on an attempt to update the target of a foreign key reference ?foreign key reference ?

Emp#e1e2e3

enameredblue

brown

Deptd1d2d3

Worksfordeptd1

d2

DnamePayTaxArt

Employee Dept

Page 51: Relational Model

Foreign Key Rules

When should foreign key rules be checked ?When should foreign key rules be checked ?

Dept (Dept (Dept#Dept#, Dname, Budget), Dname, Budget)

Emp Emp (Emp#(Emp#, Ename, Salary, WorksforDept#), Ename, Salary, WorksforDept#)

WorksforDept# References Dept delete WorksforDept# References Dept delete cascades, update cascadescascades, update cascades

Depend (Depend (Emp#, DependnameEmp#, Dependname, Date-of-birth), Date-of-birth)

Emp# references Emp delete cascades, Emp# references Emp delete cascades, update cascadesupdate cascades

Page 52: Relational Model

Example of the Class Example of the Class table.table.

Class# SectionNum Professor

32245 2 Lee

11021 1 Agoston

Primary Key

Page 53: Relational Model

Class# SectionNum Professor

32245 2 Lee

11021 1 Agoston

SocialNum FirstName LastName PhoneNum Class#556-34-2832 John Smith 924-1000 32245

839-32-1929 Jane Doe 924-1929 99839

312-39-5193 Some Body 555-1000 11021

493-33-2910 Any One 555-1020 49303

Foreign key example

Page 54: Relational Model

Relational instances in Relational instances in the Student relationthe Student relation

The content of the table (a grouping of The content of the table (a grouping of rows) are called rows) are called relational instancesrelational instances

These instances are unordered, and no These instances are unordered, and no two rows can be exactly aliketwo rows can be exactly alike

A relationalinstance

Page 55: Relational Model

Integrity ConstraintsIntegrity Constraints All DBMS’ must have some form of ICs to All DBMS’ must have some form of ICs to

prevent invalid data from being entered.prevent invalid data from being entered. Domain constraints specify the set of Domain constraints specify the set of

values which may be used for each field.values which may be used for each field. Other constraints, such as key or tuple, Other constraints, such as key or tuple,

may limit which values from the domain may limit which values from the domain can be used for a given field in a given can be used for a given field in a given instance.instance.

Key constraints require that each set of Key constraints require that each set of fields in the key be unique for each entry.fields in the key be unique for each entry.

Page 56: Relational Model

Enforcing Integrity Enforcing Integrity ConstraintsConstraints

Each DBMS should have means to Each DBMS should have means to resolve invlaid entries such as:resolve invlaid entries such as: What happens if an entry that What happens if an entry that

duplicates a key entry is entered?duplicates a key entry is entered? What should be done if an entry of a What should be done if an entry of a

foreign key is deleted?foreign key is deleted? A foreign key is a key where at least one A foreign key is a key where at least one

field depends on a field from a different field depends on a field from a different table.table.

What happens when an invalid entry is What happens when an invalid entry is entered?entered?

Page 57: Relational Model

Relation schemaRelation schema Associated with each attribute is a Associated with each attribute is a set of values, called a domain, that set of values, called a domain, that can be assigned to the entry of a can be assigned to the entry of a tuple corresponding to the tuple corresponding to the attribute.attribute.

A relation schema is a set of A relation schema is a set of attributes.attributes.

Example EMP = { Name, SSN, Example EMP = { Name, SSN, DeptName, Salary, Birthdate }DeptName, Salary, Birthdate }

Convention Convention EMP(Name,SSN,DeptName,Salary,BiEMP(Name,SSN,DeptName,Salary,Birthdate)rthdate)

Page 58: Relational Model

IBM Relational Products

DB2 MVS/370 MVS/XASQL/DS VM/CMS DOS/VSEQMF front-end to DB2 and SQL/DSCSP application development tool

Numerous other RDBMS

ORACLE (SQL*Forms)INGRES from ASK Corp. (OSL,ABF)AIM/RDB from FujitsuINFORMIXVAXSQL/Rdb from DECNonStop SQL from Tandem

Relational DBMS Products

Microcomputer versions

ORACLEINGRESdBase IVmicroSQLpractically all micro DBMS

Page 59: Relational Model

• The relational model is based on set operations. Tables are sets of rows. The actual storage structure is hidden from the user. The relational model is just concerned with a logical view of the data, not the physical view. There are no pointers for the user to worry about. The only data are explicit values in tables. All data values in the cells of tables are Atomic (also known as Scalar). Exactly one data value and not a set or a repeating group is allowed in each cell.

• Relational databases are the most widely used in the world ( 90+%.)

• A mathematical viewpoint helped to shape a database industry.

• Future? Possibly Object Oriented Database model.

Page 60: Relational Model

Query LanguagesQuery LanguagesUsers use query languages to request information from the database SQL is the most spread.

Database uses two types of query languages:

Procedural language: the user instructs the system to perform a sequence of operations on the database

Nonprocedural language: the user describes the desired information without giving a specific procedure for obtain the information