SlideShare a Scribd company logo
1 of 31
The Relational Model 
Murali Mani
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 
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) 
Student 
Murali Mani 
 Constraints 
 Key and Foreign Key constraints (More constraints later) 
 Eg: Student Relation (The following 2 relations are 
equivalent) 
Student 
sNumber sName 
1 Dave 
2 Greg 
sNumber sName 
2 Greg 
1 Dave 
Cardinality = 2 
Arity/Degree = 2
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 
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 
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> 
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 
Murali Mani
Keys: Example 
Student 
sNumber sName address 
1 Dave 144FL 
2 Greg 320FL 
Primary Key: <sNumber> 
Candidate key: <sName> 
Some superkeys: {<sNumber, address>, 
<sName>, 
<sNumber>, 
<sNumber, sName> 
<sNumber, sName, address>} 
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 
Murali Mani 
(pName)
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). 
Murali Mani
Foreign Keys: Examples 
Dept (dNumber, dName) 
Person (pNumber, pName, dept) 
PRIMARY KEY (Dept) = <dNumber> 
PRIMARY KEY (Person) = 
<pNumber> 
FOREIGN KEY Person (dept) 
REFERENCES Dept (dNumber) 
Murali Mani 
Persons working for Depts 
Person and his/her father 
Person (pNumber, pName, father) 
PRIMARY KEY (Person) = <pNumber> 
FOREIGN KEY Person (father) 
REFERENCES Person (pNumber)
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. 
Murali Mani
Relational Model: Summary 
Murali Mani 
 Structures 
 Relations (Tables) 
 Attributes (Columns, Fields) 
 Constraints 
 Key 
 Primary key, candidate key (unique) 
 Super Key 
 Foreign Key
ER schema ® Relational 
schema 
Simple 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 
Murali Mani
Simple algorithm: Example 1 
Person (pNumber, pName) 
Dept (dNumber, dName) 
WorksFor (pNumber, dNumber, years) 
Murali Mani 
Person 
pNumber 
pName 
Dept 
dNumber 
dName 
Works 
For 
(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)
Simple Algorithm: Example 2 
pName pNumber 
(1, *) (0, *) 
Murali Mani 
Supplier 
sName 
sLoc 
Supplier (sName, sLoc) 
Consumer (cName, cLoc) 
Product (pName, pNumber) 
Supply (supplier, consumer, 
Consumer 
product, price, qty) 
cName 
cLoc 
Supply 
price 
Product 
qty 
(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)
Simple Algorithm: Example 3 
p N a m e p N u m b e r 
s u p e r P a r t s u b P a r t 
( 0 , * ) ( 0 , 1 ) 
Part (pName, pNumber) 
Contains (superPart, subPart, quantity) 
Murali Mani 
P a r t 
C o n t a i n s 
q u a n t i t y 
PRIMARY KEY (Part) = <pNumber> 
PRIMARY KEY (Contains) = <subPart> 
FOREIGN KEY Contains (superPart) REFERENCES Part (pNumber) 
FOREIGN KEY Contains (subPart) REFERENCES Part (pNumber)
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. 
Murali Mani
Murali Mani 
Example 1 
Student 
sNumber 
sName 
Professor 
pNumber 
pName 
Has 
Advisor 
(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
Murali Mani 
Example 2 
Person 
pNumber 
pName 
Dept 
dNumber 
dName 
Works 
For 
(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
p N a m e p N u m b e r 
P a r t 
s u p e r P a r t s u b P a r t 
( 0 , * ) ( 0 , 1 ) 
Murali Mani 
Example 3 
C o n t a i n s 
q u a n t i t y 
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
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. 
Murali Mani
Murali Mani 
Example 1 
Student 
sNumber 
sName 
Professor 
pNumber 
pName 
Has 
Advisor 
(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
Murali Mani 
Example 2 
Student 
sNumber 
sName 
Professor 
pNumber 
pName 
Has 
Advisor 
(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.
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. 
Murali Mani
Composite and Multi-valued 
attributes in ER 
sNumber sName 
Student 
address 
Murali Mani 
sAge 
major 
street city 
state 
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)
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. 
Murali Mani
Weak entity types: Example 
Dept (dNumber, dName) 
Course (cNumber, dNumber, cName) 
PRIMARY KEY (Dept) = <dNumber> 
PRIMARY KEY (Course) = <cNumber, dNumber> 
FOREIGN KEY Course (dNumber) REFERENCES Dept (dNumber) 
Murali Mani
ISA Relationship types: 
Method 1 
year program 
Murali Mani 
sNumber sName 
Student 
ISA ISA 
GradStudent 
UGStudent 
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) 
An UGStudent will be represented REFERENCES Student (sNumber) 
in both Student relation as well as 
UGStudent relation (similarly 
GradStudent)
ISA Relationship types: 
Method 2 
sNumber sName 
Student 
ISA ISA 
year program 
Murali Mani 
GradStudent 
UGStudent 
Student (sNumber, sName, year, program) 
PRIMARY KEY (Student) = <sNumber> 
Note: There will be null values in the relation.
ISA Relationship types: 
Method 3 
year program 
Murali Mani 
sNumber sName 
Student 
ISA ISA 
GradStudent 
UGStudent 
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.

More Related Content

What's hot

4 the relational data model and relational database constraints
4 the relational data model and relational database constraints4 the relational data model and relational database constraints
4 the relational data model and relational database constraintsKumar
 
Chapter 2 Relational Data Model-part 2
Chapter 2 Relational Data Model-part 2Chapter 2 Relational Data Model-part 2
Chapter 2 Relational Data Model-part 2Eddyzulham Mahluzydde
 
24042020_normalization 1.pdf
24042020_normalization 1.pdf24042020_normalization 1.pdf
24042020_normalization 1.pdfShivani139202
 
7. Relational Database Design in DBMS
7. Relational Database Design in DBMS7. Relational Database Design in DBMS
7. Relational Database Design in DBMSkoolkampus
 
Functional dependencies in Database Management System
Functional dependencies in Database Management SystemFunctional dependencies in Database Management System
Functional dependencies in Database Management SystemKevin Jadiya
 
Formal Languages and Automata Theory unit 2
Formal Languages and Automata Theory unit 2Formal Languages and Automata Theory unit 2
Formal Languages and Automata Theory unit 2Srimatre K
 
FUNCTION DEPENDENCY AND TYPES & EXAMPLE
FUNCTION DEPENDENCY  AND TYPES & EXAMPLEFUNCTION DEPENDENCY  AND TYPES & EXAMPLE
FUNCTION DEPENDENCY AND TYPES & EXAMPLEVraj Patel
 
ER to relational Mapping: Data base design using ER to relational language. C...
ER to relational Mapping: Data base design using ER to relational language. C...ER to relational Mapping: Data base design using ER to relational language. C...
ER to relational Mapping: Data base design using ER to relational language. C...Raj vardhan
 
Chapter 2 Relational Data Model-part1
Chapter 2 Relational Data Model-part1Chapter 2 Relational Data Model-part1
Chapter 2 Relational Data Model-part1Eddyzulham Mahluzydde
 

What's hot (18)

Chapter3
Chapter3Chapter3
Chapter3
 
Chapter10
Chapter10Chapter10
Chapter10
 
Normalization1
Normalization1Normalization1
Normalization1
 
4 the relational data model and relational database constraints
4 the relational data model and relational database constraints4 the relational data model and relational database constraints
4 the relational data model and relational database constraints
 
Chapter 2 Relational Data Model-part 2
Chapter 2 Relational Data Model-part 2Chapter 2 Relational Data Model-part 2
Chapter 2 Relational Data Model-part 2
 
Differential equations intro
Differential equations introDifferential equations intro
Differential equations intro
 
Normalization
NormalizationNormalization
Normalization
 
24042020_normalization 1.pdf
24042020_normalization 1.pdf24042020_normalization 1.pdf
24042020_normalization 1.pdf
 
7. Relational Database Design in DBMS
7. Relational Database Design in DBMS7. Relational Database Design in DBMS
7. Relational Database Design in DBMS
 
Types of keys
Types of keysTypes of keys
Types of keys
 
Chapter 4
Chapter 4Chapter 4
Chapter 4
 
Ch 6
Ch 6Ch 6
Ch 6
 
Chapter05
Chapter05Chapter05
Chapter05
 
Functional dependencies in Database Management System
Functional dependencies in Database Management SystemFunctional dependencies in Database Management System
Functional dependencies in Database Management System
 
Formal Languages and Automata Theory unit 2
Formal Languages and Automata Theory unit 2Formal Languages and Automata Theory unit 2
Formal Languages and Automata Theory unit 2
 
FUNCTION DEPENDENCY AND TYPES & EXAMPLE
FUNCTION DEPENDENCY  AND TYPES & EXAMPLEFUNCTION DEPENDENCY  AND TYPES & EXAMPLE
FUNCTION DEPENDENCY AND TYPES & EXAMPLE
 
ER to relational Mapping: Data base design using ER to relational language. C...
ER to relational Mapping: Data base design using ER to relational language. C...ER to relational Mapping: Data base design using ER to relational language. C...
ER to relational Mapping: Data base design using ER to relational language. C...
 
Chapter 2 Relational Data Model-part1
Chapter 2 Relational Data Model-part1Chapter 2 Relational Data Model-part1
Chapter 2 Relational Data Model-part1
 

Viewers also liked

Viewers also liked (18)

De draak!!!
De draak!!!De draak!!!
De draak!!!
 
Ingrid Visser's Visit!
Ingrid Visser's Visit!Ingrid Visser's Visit!
Ingrid Visser's Visit!
 
Relational model
Relational modelRelational model
Relational model
 
Foto estafette 2012-2013
Foto estafette 2012-2013Foto estafette 2012-2013
Foto estafette 2012-2013
 
De draak in groente en fruit!
De draak in groente en fruit!De draak in groente en fruit!
De draak in groente en fruit!
 
Automobilnik
 Automobilnik Automobilnik
Automobilnik
 
management
managementmanagement
management
 
Now Hiring PowerPoint
Now Hiring PowerPointNow Hiring PowerPoint
Now Hiring PowerPoint
 
#AFCKesha
#AFCKesha#AFCKesha
#AFCKesha
 
Book week
Book weekBook week
Book week
 
Relational model
Relational modelRelational model
Relational model
 
Faltantes
FaltantesFaltantes
Faltantes
 
04cpp inh
04cpp inh04cpp inh
04cpp inh
 
Internship Reort presentation (Sajid Aslam)
Internship Reort presentation (Sajid Aslam) Internship Reort presentation (Sajid Aslam)
Internship Reort presentation (Sajid Aslam)
 
Pure Diamond Investments Presentation
Pure Diamond Investments PresentationPure Diamond Investments Presentation
Pure Diamond Investments Presentation
 
Asosiasi bunyi
Asosiasi bunyiAsosiasi bunyi
Asosiasi bunyi
 
Development of GM Crops (Methods) against insect diversity
Development of GM Crops (Methods) against insect diversityDevelopment of GM Crops (Methods) against insect diversity
Development of GM Crops (Methods) against insect diversity
 
redacción
redacciónredacción
redacción
 

Similar to Relational model

3 relational model
3 relational model3 relational model
3 relational modelUtkarsh De
 
The Relational Data Model and Relational Database Constraints Ch5 (Navathe 4t...
The Relational Data Model and Relational Database Constraints Ch5 (Navathe 4t...The Relational Data Model and Relational Database Constraints Ch5 (Navathe 4t...
The Relational Data Model and Relational Database Constraints Ch5 (Navathe 4t...Raj vardhan
 
relational model.pptx
relational model.pptxrelational model.pptx
relational model.pptxThangamaniR3
 
Module 2_Relational Algebra.pptx
Module 2_Relational Algebra.pptxModule 2_Relational Algebra.pptx
Module 2_Relational Algebra.pptxShashiKumarB5
 
Chapter 6 relational data model and relational
Chapter  6  relational data model and relationalChapter  6  relational data model and relational
Chapter 6 relational data model and relationalJafar Nesargi
 
Chapter 6 relational data model and relational
Chapter  6  relational data model and relationalChapter  6  relational data model and relational
Chapter 6 relational data model and relationalJafar Nesargi
 
The relational data model part[1]
The relational data model part[1]The relational data model part[1]
The relational data model part[1]Bashir Rezaie
 
Fundamentals of database system - Relational data model and relational datab...
Fundamentals of database system  - Relational data model and relational datab...Fundamentals of database system  - Relational data model and relational datab...
Fundamentals of database system - Relational data model and relational datab...Mustafa Kamel Mohammadi
 
4 the sql_standard
4 the  sql_standard4 the  sql_standard
4 the sql_standardUtkarsh De
 
4_RelationalDataModelAndRelationalMapping.pdf
4_RelationalDataModelAndRelationalMapping.pdf4_RelationalDataModelAndRelationalMapping.pdf
4_RelationalDataModelAndRelationalMapping.pdfLPhct2
 
Relation model part 1
Relation model part 1Relation model part 1
Relation model part 1Siti Ismail
 
2. relational model_150903
2. relational model_1509032. relational model_150903
2. relational model_150903Dika Handika
 

Similar to Relational model (20)

3 relational model
3 relational model3 relational model
3 relational model
 
3_Relational_Model.pdf
3_Relational_Model.pdf3_Relational_Model.pdf
3_Relational_Model.pdf
 
The Relational Data Model and Relational Database Constraints Ch5 (Navathe 4t...
The Relational Data Model and Relational Database Constraints Ch5 (Navathe 4t...The Relational Data Model and Relational Database Constraints Ch5 (Navathe 4t...
The Relational Data Model and Relational Database Constraints Ch5 (Navathe 4t...
 
relational model.pptx
relational model.pptxrelational model.pptx
relational model.pptx
 
Module 2_Relational Algebra.pptx
Module 2_Relational Algebra.pptxModule 2_Relational Algebra.pptx
Module 2_Relational Algebra.pptx
 
Lec02
Lec02Lec02
Lec02
 
Relational+algebra (1)
Relational+algebra (1)Relational+algebra (1)
Relational+algebra (1)
 
Ra Revision
Ra RevisionRa Revision
Ra Revision
 
Chapter 6 relational data model and relational
Chapter  6  relational data model and relationalChapter  6  relational data model and relational
Chapter 6 relational data model and relational
 
Chapter 6 relational data model and relational
Chapter  6  relational data model and relationalChapter  6  relational data model and relational
Chapter 6 relational data model and relational
 
Relational model
Relational modelRelational model
Relational model
 
The relational data model part[1]
The relational data model part[1]The relational data model part[1]
The relational data model part[1]
 
Fundamentals of database system - Relational data model and relational datab...
Fundamentals of database system  - Relational data model and relational datab...Fundamentals of database system  - Relational data model and relational datab...
Fundamentals of database system - Relational data model and relational datab...
 
4 the sql_standard
4 the  sql_standard4 the  sql_standard
4 the sql_standard
 
RDBMS
RDBMSRDBMS
RDBMS
 
4_RelationalDataModelAndRelationalMapping.pdf
4_RelationalDataModelAndRelationalMapping.pdf4_RelationalDataModelAndRelationalMapping.pdf
4_RelationalDataModelAndRelationalMapping.pdf
 
Relation model part 1
Relation model part 1Relation model part 1
Relation model part 1
 
2. relational model_150903
2. relational model_1509032. relational model_150903
2. relational model_150903
 
Ch2
Ch2Ch2
Ch2
 
DBMS CS3
DBMS CS3DBMS CS3
DBMS CS3
 

Relational model

  • 1. The Relational Model Murali Mani
  • 2. 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 Murali Mani
  • 3. Relational Model  Structures  Relations (also called Tables)  Attributes (also called Columns or Fields)  Note: Every attribute is simple (not composite or multi-valued) Student Murali Mani  Constraints  Key and Foreign Key constraints (More constraints later)  Eg: Student Relation (The following 2 relations are equivalent) Student sNumber sName 1 Dave 2 Greg sNumber sName 2 Greg 1 Dave Cardinality = 2 Arity/Degree = 2
  • 4. 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 Murali Mani
  • 5. 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 Murali Mani
  • 6. 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> Murali Mani
  • 7. 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 Murali Mani
  • 8. Keys: Example Student sNumber sName address 1 Dave 144FL 2 Greg 320FL Primary Key: <sNumber> Candidate key: <sName> Some superkeys: {<sNumber, address>, <sName>, <sNumber>, <sNumber, sName> <sNumber, sName, address>} Murali Mani
  • 9. 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 Murali Mani (pName)
  • 10. 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). Murali Mani
  • 11. Foreign Keys: Examples Dept (dNumber, dName) Person (pNumber, pName, dept) PRIMARY KEY (Dept) = <dNumber> PRIMARY KEY (Person) = <pNumber> FOREIGN KEY Person (dept) REFERENCES Dept (dNumber) Murali Mani Persons working for Depts Person and his/her father Person (pNumber, pName, father) PRIMARY KEY (Person) = <pNumber> FOREIGN KEY Person (father) REFERENCES Person (pNumber)
  • 12. 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. Murali Mani
  • 13. Relational Model: Summary Murali Mani  Structures  Relations (Tables)  Attributes (Columns, Fields)  Constraints  Key  Primary key, candidate key (unique)  Super Key  Foreign Key
  • 14. ER schema ® Relational schema Simple 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 Murali Mani
  • 15. Simple algorithm: Example 1 Person (pNumber, pName) Dept (dNumber, dName) WorksFor (pNumber, dNumber, years) Murali Mani Person pNumber pName Dept dNumber dName Works For (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)
  • 16. Simple Algorithm: Example 2 pName pNumber (1, *) (0, *) Murali Mani Supplier sName sLoc Supplier (sName, sLoc) Consumer (cName, cLoc) Product (pName, pNumber) Supply (supplier, consumer, Consumer product, price, qty) cName cLoc Supply price Product qty (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)
  • 17. Simple Algorithm: Example 3 p N a m e p N u m b e r s u p e r P a r t s u b P a r t ( 0 , * ) ( 0 , 1 ) Part (pName, pNumber) Contains (superPart, subPart, quantity) Murali Mani P a r t C o n t a i n s q u a n t i t y PRIMARY KEY (Part) = <pNumber> PRIMARY KEY (Contains) = <subPart> FOREIGN KEY Contains (superPart) REFERENCES Part (pNumber) FOREIGN KEY Contains (subPart) REFERENCES Part (pNumber)
  • 18. 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. Murali Mani
  • 19. Murali Mani Example 1 Student sNumber sName Professor pNumber pName Has Advisor (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
  • 20. Murali Mani Example 2 Person pNumber pName Dept dNumber dName Works For (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
  • 21. p N a m e p N u m b e r P a r t s u p e r P a r t s u b P a r t ( 0 , * ) ( 0 , 1 ) Murali Mani Example 3 C o n t a i n s q u a n t i t y 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
  • 22. 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. Murali Mani
  • 23. Murali Mani Example 1 Student sNumber sName Professor pNumber pName Has Advisor (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
  • 24. Murali Mani Example 2 Student sNumber sName Professor pNumber pName Has Advisor (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.
  • 25. 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. Murali Mani
  • 26. Composite and Multi-valued attributes in ER sNumber sName Student address Murali Mani sAge major street city state 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)
  • 27. 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. Murali Mani
  • 28. Weak entity types: Example Dept (dNumber, dName) Course (cNumber, dNumber, cName) PRIMARY KEY (Dept) = <dNumber> PRIMARY KEY (Course) = <cNumber, dNumber> FOREIGN KEY Course (dNumber) REFERENCES Dept (dNumber) Murali Mani
  • 29. ISA Relationship types: Method 1 year program Murali Mani sNumber sName Student ISA ISA GradStudent UGStudent 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) An UGStudent will be represented REFERENCES Student (sNumber) in both Student relation as well as UGStudent relation (similarly GradStudent)
  • 30. ISA Relationship types: Method 2 sNumber sName Student ISA ISA year program Murali Mani GradStudent UGStudent Student (sNumber, sName, year, program) PRIMARY KEY (Student) = <sNumber> Note: There will be null values in the relation.
  • 31. ISA Relationship types: Method 3 year program Murali Mani sNumber sName Student ISA ISA GradStudent UGStudent 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.