Shweta - DBMS - Chapter 5 - BSC (H) CS - Sem4
Shweta - DBMS - Chapter 5 - BSC (H) CS - Sem4
• The data elements in each row represent certain facts that correspond
to a real-world entity or relationship
In the formal model, rows are called tuples
• Each column has a column header that specify how to interpret the
Data Values in each row .
• Example:
CUSTOMER (Cust-id, Cust-name, Address, Phone#)
• CUSTOMER is the relation name
• Defined over the four attributes: Cust-id, Cust-name,
Address, Phone#
Table Relation
Row Tuple
• We will consider the attributes in R(A1, A2, ..., An) and the
values in t = <v1, v2, ..., vn> to be ordered .
• Notation:
• We refer to component values of a tuple t by:
• t [ Ai ] or t . Ai
• This is the value vi of attribute Ai for tuple t
RELATIONAL MODEL CONSTRAINTS
• Domain Constraints
• Key Constraints
• Constraints on Nulls
• Entity Integrity constraints
• Referential Integrity constraints
• Domain Constraints
Domain Constraints specify that within each tuple, the
value of each attribute Ai must be an atomic value from
the domain dom( Ai ).
KEY CONSTRAINT
• A relation is a set of Tuples .
• All elements of a set are distinct .
All tuples in a relation must be distinct .
No two tuples can have the same combination of values.
• Suppose we denote one subset of values by sk .
• Then for any two distinct tuples t1 and t2 in a relation state r of R ,
we have the constraint
t1[ sk ] != t2[ sk ]
Any such set of attributes sk is called a superkey
of R .
In a way we can say that …..
A superkey sk specifies the uniqueness constraint ….
i.e No two distinct tuples in any state r of R can have the same
value for sk.
Infact every relation has atleast one default superkey ---- the set
of all its attributes .
KEY CONSTRAINTS
• Superkey of R:
• Is a set of attributes SK of R with the following condition:
• No two tuples in any valid relation state r(R) will have the same value
for SK
• That is, for any distinct tuples t1 and t2 in r(R), t1[SK] t2[SK]
• This condition must hold in any valid state r(R)
• Key of R:
• A "minimal" superkey
• That is, a key is a superkey K such that removal of any attribute from K
results in a set of attributes that is not a superkey (does not possess the
superkey uniqueness property)
• Example
• Sno Pno Qty_sold
s1 p1 500
s1 p2 200
s1 p3 500
s2 p1 500
s2 p2 200
s2 p3 500
s3 p2 200
• General rule: Choose as primary key the smallest of the candidate keys
(in terms of size)
• Not always applicable – choice is sometimes subjective
CAR TABLE WITH TWO CANDIDATE KEYS –
LICENSENUMBER CHOSEN AS PRIMARY KEY
ENTITY INTEGRITY
• Entity Integrity:
• The primary key attributes PK of each relation schema R in S
cannot have null values in any tuple of r(R).
• This is because primary key values are used to identify the individual
tuples.
• t[PK] null for any tuple t in r(R)
• If PK has several attributes, null is not allowed in any of these attributes
• Note: Other attributes of R may be constrained to disallow null
values, even though they are not members of the primary key.
REFERENTIAL INTEGRITY
• A constraint involving two relations
• The previous constraints involve a single relation.
• Used to specify a relationship among tuples in two relations:
• The referencing relation and the referenced relation.
REFERENTIAL INTEGRITY
• Tuples in the referencing relation R1 have attributes FK (called
foreign key attributes) that reference the primary key attributes
PK of the referenced relation R2.
• A tuple t1 in R1 is said to reference a tuple t2 in R2 if t1[FK]
= t2[PK].
• A referential integrity constraint can be displayed in a relational
database schema as a directed arc from R1.FK to R2.
REFERENTIAL INTEGRITY (OR FOREIGN KEY)
CONSTRAINT
• Statement of the constraint
• The value in the foreign key column (or columns) FK of the
the referencing relation R1 can be either:
• (1) a value of an existing primary key value of a corresponding
primary key PK in the referenced relation R2, or
• (2) a null.
• In case (2), the FK in R1 should not be a part of its own primary
key.
DISPLAYING A RELATIONAL DATABASE SCHEMA
AND ITS CONSTRAINTS
• Each relation schema can be displayed as a row of attribute
names
• The name of the relation is written above the attribute names
• The primary key attribute (or attributes) will be underlined
• A foreign key (referential integrity) constraints is displayed as a
directed arc (arrow) from the foreign key attributes to the
referenced table
• Can also point the the primary key of the referenced relation for
clarity
• Next slide shows the COMPANY relational schema diagram
Referential Integrity Constraints for COMPANY database
SUMMARY
• Presented Relational Model Concepts
• Definitions
• Characteristics of relations
• Discussed Relational Model Constraints and Relational
Database Schemas
• Domain constraints‟
• Key constraints
• Entity integrity
• Referential integrity
IN-CLASS EXERCISE
(Taken from Exercise 5.15)
Consider the following relations for a database that keeps track of student
enrollment in courses and the books adopted for each course:
STUDENT(SSN, Name, Major, Bdate)
COURSE(Course#, Cname, Dept)
ENROLL(SSN, Course#, Quarter, Grade)
BOOK_ADOPTION(Course#, Quarter, Book_ISBN)
TEXT(Book_ISBN, Book_Title, Publisher, Author)
Draw a relational schema diagram specifying the foreign keys for this
schema.