Relational model for Databases

31
Shriram Desai, [email protected] 7411001044 The Relational Model

description

Relational model for Databases of different companies

Transcript of Relational model for Databases

Page 1: Relational model for Databases

Shriram Desai,[email protected]

7411001044

The Relational Model

Page 2: Relational model for Databases

Why Relational Model?

Currently the most widely used Vendors: Oracle, Microsoft, IBM

Older models still used IBM’s IMS (hierarchical model)

Recent competitions Object Oriented Model: ObjectStore

Implementation standard for relational Model SQL (Structured Query Language) SQL 3: includes object-relational extensions

Page 3: Relational model for Databases

Murali Mani

Relational Model Structures

Relations (also called Tables) Attributes (also called Columns or Fields)

Note: Every attribute is simple (not composite or multi-valued)

Constraints Key and Foreign Key constraints (More constraints later)

Eg: Student Relation (The following 2 relations are equivalent)

sNumber sName

1 Dave

2 Greg

Student

sNumber sName

2 Greg

1 Dave

Student

Cardinality = 2Arity/Degree = 2

Page 4: Relational model for Databases

Murali Mani

Relational Model

Schema for a relation Eg: Student (sNumber, sName) PRIMARY KEY (Student) = <sNumber>

Schema for a database Schemas for all relations in the database

Tuples (Rows) The set of rows in a relation are the tuples of that

relation Note: Attribute values may be null

Page 5: Relational model for Databases

Murali Mani

Primary Key Constraints A set of attributes is a key for a relation if:

No two distinct tuples can have the same values in all key fields

A proper subset of the key attributes is not a key. Superkey: A proper subset of a superkey

may be a superkey If multiple keys, one of them is chosen to be

the primary key. Eg: PRIMARY KEY (Student) = <sNumber> Primary key attributes cannot take null values

Page 6: Relational model for Databases

Murali Mani

Candidate Keys (SQL: Unique)

Keys that are not primary keys are candidate keys.

Specified in SQL using UNIQUE Attribute of unique key may have null values ! Eg: Student (sNumber, sName)

PRIMARY KEY (Student) = <sNumber>

CANDIDATE KEY (Student) = <sName>

Page 7: Relational model for Databases

Murali Mani

Violation of key constraints A relation violates a primary key constraint if:

There is a row with null values for any attribute of primary key.

(or) There are 2 rows with same values for all attributes of primary key

Consider R (a, b) where a is unique. R violates the unique constraint if all of the following are true 2 rows in R have the same non-null values for a

Page 8: Relational model for Databases

Murali Mani

Keys: Example

sNumber sName address

1 Dave 144FL

2 Greg 320FL

Student

Primary Key: <sNumber>Candidate key: <sName>Some superkeys: {<sNumber, address>,

<sName>, <sNumber>, <sNumber, sName> <sNumber, sName, address>}

Page 9: Relational model for Databases

Murali Mani

Foreign Key Constraints

To specify an attribute (or multiple attributes) S1 of a relation R1 refers to the attribute (or attributes) S2 of another relation R2

Eg: Professor (pName, pOffice)

Student (sNumber, sName, advisor)

PRIMARY KEY (Professor) = <pName>

FOREIGN KEY Student (advisor) REFERENCES Professor (pName)

Page 10: Relational model for Databases

Murali Mani

Foreign Key Constraints

FOREIGN KEY R1 (S1) REFERENCES R2 (S2)

Like a logical pointer The values of S1 for any row of R1 must be

values of S2 for some row in R2 (null values are allowed)

S2 must be a key for R2 R2 can be the same as R1 (i.e., a relation

can have a foreign key referring to itself).

Page 11: Relational model for Databases

Murali Mani

Foreign Keys: ExamplesDept (dNumber, dName)Person (pNumber, pName, dept)

PRIMARY KEY (Dept) = <dNumber>PRIMARY KEY (Person) = <pNumber>FOREIGN KEY Person (dept) REFERENCES Dept (dNumber)

Persons working for Depts

Person and his/her father

Person (pNumber, pName, father)

PRIMARY KEY (Person) = <pNumber>FOREIGN KEY Person (father) REFERENCES Person (pNumber)

Page 12: Relational model for Databases

Murali Mani

Violation of Foreign Key constraints

Suppose we have: FOREIGN KEY R1 (S1) REFERENCES R2 (S2)

This constraint is violated if Consider a row in R1 with non-null values for all

attributes of S1 If there is no row in R2 which have these values

for S2, then the FK constraint is violated.

Page 13: Relational model for Databases

Murali Mani

Relational Model: Summary

Structures Relations (Tables) Attributes (Columns, Fields)

Constraints Key

Primary key, candidate key (unique) Super Key Foreign Key

Page 14: Relational model for Databases

Murali Mani

ER schema Relational schemaSimple Algorithm

Entity type E Relation E’ Attribute of E Attribute as E’ Key for E Primary Key for E’

For relationship type R between E1, E2, …, En

Create separate relation R’ Attributes of R’ are primary keys of E1, E2, …, En and

attributes of R Primary Key for R’ is defined as:

If the maximum cardinality of any Ei is 1, primary key for R’ = primary key for Ei

Else, primary key for R’ = primary keys for E1, E2, …, En

Define “appropriate” foreign keys from R’ to E1, E2, …, En

Page 15: Relational model for Databases

Murali Mani

Simple algorithm: Example 1

Person (pNumber, pName)Dept (dNumber, dName)WorksFor (pNumber, dNumber, years)

Person

pNumber

pName

Dept

dNumber

dName

WorksFor

(1, *) (0, *)

years

PRIMARY KEY (Person) = <pNumber>PRIMARY KEY (Dept) = <dNumber>PRIMARY KEY (WorksFor) = <pNumber, dNumber>FOREIGN KEY WorksFor (pNumber) REFERENCES Person (pNumber)FOREIGN KEY WorksFor (dNumber) REFERENCES Dept (dNumber)

Page 16: Relational model for Databases

Murali Mani

Simple Algorithm: Example 2

Supplier

sName

sLoc

Consumer

cName

cLoc

Supply

price

Product

pName pNumber

qty

(1, *) (0, *)

(0, *)

PRIMARY Key (Supplier) = <sName> PRIMARY Key (Consumer) = <cName>PRIMARY Key (Product) = <pName>PRIMARY Key (Supply) = <supplier, consumer, product>FOREIGN KEY Supply (supplier) REFERENCES Supplier (sName)FOREIGN KEY Supply (consumer) REFERENCES Consumer (cName)FOREIGN KEY Supply (product) REFERENCES Product (pName)

Supplier (sName, sLoc)Consumer (cName, cLoc)Product (pName, pNumber)Supply (supplier, consumer,

product, price, qty)

Page 17: Relational model for Databases

Murali Mani

Simple Algorithm: Example 3

C onta ins

P art

pN am e pN um ber

subP artsuperP art

quantity

(0 , 1 )(0 , *)

PRIMARY KEY (Part) = <pNumber>PRIMARY KEY (Contains) = <subPart>

FOREIGN KEY Contains (superPart) REFERENCES Part (pNumber)FOREIGN KEY Contains (subPart) REFERENCES Part (pNumber)

Part (pName, pNumber)Contains (superPart, subPart, quantity)

Page 18: Relational model for Databases

Murali Mani

Decreasing the number of Relations

Technique 1

If the relationship type R contains an entity type, say E, whose maximum cardinality is 1, then R may be represented as attributes of E. If the cardinality of E is (1, 1), then no “new nulls” are

introduced If the cardinality of E is (0, 1) then “new nulls” may be

introduced.

Page 19: Relational model for Databases

Murali Mani

Example 1

Student

sNumber

sName

Professor

pNumber

pName

HasAdvisor

(1,1) (0, *)

years

Student (sNumber, sName, advisor, years)Professor (pNumber, pName)

PRIMARY KEY (Student) = <sNumber>PRIMARY KEY (Professor) = <pNumber>FOREIGN KEY Student (advisor) REFERENCES Professor (pNumber)

Note: advisor will never be null for a student

Page 20: Relational model for Databases

Murali Mani

Example 2

Person

pNumber

pName

Dept

dNumber

dName

WorksFor

(0,1) (0, *)

years

Person (pNumber, pName, dept, years)Dept (dNumber, dName)

PRIMARY KEY (Person) = <pNumber>PRIMARY KEY (Dept) = <dNumber>FOREIGN KEY Person (dept) REFERENCES Dept (dNumber)

Dept and years may be null for a person

Page 21: Relational model for Databases

Murali Mani

Example 3

C onta ins

P art

pN am e pN um ber

subP artsuperP art

quantity

(0 , 1 )(0 , *)

Part (pNumber, pname, superPart, quantity)

PRIMARY KEY (Part) = <pNumber>FOREIGN KEY Part (superPart) REFERENCES Part (pNumber)

Note: superPart gives the superpart of a part, and it may be null

Page 22: Relational model for Databases

Murali Mani

Decreasing the number of Relations

Technique 2 (not recommended) If the relationship type R between E1 and E2 is

1:1, and the cardinality of E1 or E2 is (1, 1), then we can combine everything into 1 relation.

Let us assume the cardinality of E1 is (1, 1). We have one relation for E2, and move all attributes of E1 and for R to be attributes of E2. If the cardinality of E2 is (1, 1), no “new nulls” are

introduced If the cardinality of E2 is (0, 1) then “new nulls” may be

introduced.

Page 23: Relational model for Databases

Murali Mani

Example 1

Student

sNumber

sName

Professor

pNumber

pName

HasAdvisor

(0,1) (1,1)

years

Student (sNumber, sName, pNumber, pName, years)PRIMARY KEY (Student) = <sNumber>CANDIDATE KEY (Student) = <pNumber>

Note: pNumber, pName, and years can be null for students with no advisor

Page 24: Relational model for Databases

Murali Mani

Example 2

Student

sNumber

sName

Professor

pNumber

pName

HasAdvisor

(1,1) (1,1)

years

Student (sNumber, sName, pNumber, pName, years)PRIMARY KEY (Student) = <sNumber>CANDIDATE KEY (Student) = <pNumber>

Note: pNumber cannot be null for any student.

Page 25: Relational model for Databases

Murali Mani

Other details

Composite attribute in ER Include an attribute for every component of the

composite attribute. Multi-valued attribute in ER

We need a separate relation for any multi-valued attribute.

Identify appropriate attributes, keys and foreign key constraints.

Page 26: Relational model for Databases

Murali Mani

Composite and Multi-valued attributes in ER

Student

sNamesNumber

sAge

major

statestreet

address

city

Student (sNumber, sName, sAge, street, city, state)StudentMajor (sNumber, major)

PRIMARY KEY (Student) = <sNumber>PRIMARY KEY (StudentMajor) = <sNumber, major>FOREIGN KEY StudentMajor (sNumber) REFERENCES Student (sNumber)

Page 27: Relational model for Databases

Murali Mani

Weak entity types

Consider weak entity type E A relation for E, say E’ Attributes of E’ = attributes of E in ER + keys for

all indentifying entity types. Key for E’ = the key for E in ER + keys for all the

identifying entity types. Identify appropriate FKs from E’ to the identifying

entity types.

Page 28: Relational model for Databases

Murali Mani

Weak entity types: Example

Dept Offers Course

dNamedNumber cNamecNumber

(1, 1)(0, *)

Dept (dNumber, dName)Course (cNumber, dNumber, cName)

PRIMARY KEY (Dept) = <dNumber>PRIMARY KEY (Course) = <cNumber, dNumber>FOREIGN KEY Course (dNumber) REFERENCES Dept (dNumber)

Page 29: Relational model for Databases

Murali Mani

ISA Relationship types: Method 1

Student

GradStudent

sNamesNumber

ISA ISA

UGStudent

programyear

Student (sNumber, sName)UGStudent (sNumber, year)GradStudent (sNumber, program)

PRIMARY KEY (Student) = <sNumber>PRIMARY KEY (UGStudent) = <sNumber>PRIMARY KEY (GradStudent) = <sNumber>

FOREIGN KEY UGStudent (sNumber) REFERENCES Student (sNumber)FOREIGN KEY UGStudent (sNumber) REFERENCES Student (sNumber)An UGStudent will be represented

in both Student relation as well as UGStudent relation (similarly GradStudent)

Page 30: Relational model for Databases

Murali Mani

ISA Relationship types: Method 2

Student

GradStudent

sNamesNumber

ISA ISA

UGStudent

programyear

Student (sNumber, sName, year, program)PRIMARY KEY (Student) = <sNumber>

Note: There will be null values in the relation.

Page 31: Relational model for Databases

Murali Mani

ISA Relationship types: Method 3

Student

GradStudent

sNamesNumber

ISA ISA

UGStudent

programyear

Student (sNumber, sName)UGStudent (sNumber, sName, year)GradStudent (sNumber, sName, program)UGGradStudent (sNumber, sName, year, program)

PRIMARY KEY (Student) = <sNumber>PRIMARY KEY (UGStudent) = <sNumber>PRIMARY KEY (GradStudent) = <sNumber>PRIMARY KEY (UGGradStudent) = <sNumber>

Any student will be represented in only one of the relations as appropriate.