CS 405G: Introduction to Database Systems
Lecture 5: Logical Design by Relational ModelInstructor: Chen Qian
04/22/23 2
Today’s Outline
Relational Model Constraints (unfinished part of last class)
Update Operations Insertion Deletion Update Transaction Dealing with Constraint Violations
04/22/23 3
Relational Integrity Constraints
Integrity Constraints are conditions that must hold on all valid relation instances.
There are four main types of constraints:1. Domain constraints
1. The value of a attribute must come from its domain
2. Key constraints
3. Entity integrity constraints
4. Referential integrity constraints
Primary Key Constraints
A set of fields is a candidate key (abbreviated as key) for a relation if :1. No two distinct tuples can have same values in all key
fields, and
2. Property 1 is not true for any subset of the key.
What if Part 2 is false? A super key: a set of fields that contains a key.
If there are multiple keys for a relation, one of the keys is chosen (by DBA) to be the primary key.
04/22/23 5
Key Example
E.g., given a schema Student(sid: string, name: string, gpa: float) we have: sid is a key for Students. (What about name?) The set
{sid, gpa} is a superkey.
CAR (licence_num: string, Engine_serial_num: string, make: string, model: string, year: integer) What is the candidate key(s) Which one you may use as a primary key What are the super keys
04/22/23 6
Key Example
Enroll (sid: string, cid: string) What is the candidate key(s)?
sid + cid Why
04/22/23 7
Entity Integrity
Entity Integrity: The primary key attributes (PK) of each relation schema R cannot have null values in any tuple of r(R). Other attributes of R may be similarly constrained to
disallow null values, even though they are not members of the primary key.
Foreign Keys, Referential Integrity
Foreign key : Set of fields in one relation that is used to `refer’ to a tuple in another relation. (Must correspond to primary key of the second relation.) Like a `logical pointer’.
Foreign key constraint: The foreign key in the referencing relation must match the primary key of the referenced relation.
E.g. sid is a foreign key referring to Students: Student(sid: string, name: string, gpa: float) Enrolled(sid: string, cid: string, grade: string) If all foreign key constraints are enforced, referential
integrity is achieved, i.e., no dangling references.
Foreign Key constraints
Only students listed in the Students relation should be allowed to enroll for courses.
sid name login age gpa
53666 Jones jones@cs 18 3.453688 Smith smith@eecs 18 3.253650 Smith smith@math 19 3.8
sid cid grade53666 Carnatic101 C53666 Reggae203 B53650 Topology112 A53666 History105 B
EnrolledStudents
Possible violation: Add <50000, History105, B> to Enrolled. Possible violation: delete <53650, Smith, …> from Students.
04/22/23 10
Other Types of Constraints
Semantic Integrity Constraints: based on application semantics and cannot be
expressed by the model per se e.g., “the max. no. of hours per employee for all
projects he or she works on is 56 hrs per week” A constraint specification language may have to be
used to express these SQL-99 allows triggers and ASSERTIONS to
allow for some of these
04/22/23 11
Update Operations on Relations
Update operations INSERT a tuple. DELETE a tuple. MODIFY a tuple.
Constraints should not be violated in updates
04/22/23 12
Update Operations on Relations
In case of integrity violation, several actions can be taken: Cancel the operation that causes the violation
(REJECT option) Perform the operation but inform the user of the
violation Trigger additional updates so the violation is
corrected (CASCADE option, SET NULL option) Execute a user-specified error-correction routine
04/22/23 13
Key concept: Transaction
an atomic sequence of database actions (reads/writes) takes DB from one consistent state to another
consistent state 1 consistent state 2transaction
04/22/23 14
Example
Here, consistency is based on our knowledge of banking “semantics”
In general, up to writer of transaction to ensure transaction preserves consistency
DBMS provides (limited) automatic enforcement, via integrity constraints e.g., balances must be >= 0
checking: $200savings: $1000
Transaction“transfer $100 from Saving to checking”
checking: $300savings: $900
From E/R Diagrams to Relations
15
Called logical design (different from conceptual design)
Entity sets become relations with the same set of attributes.
Relationships become relations whose attributes are only: The keys of the connected entity sets. Attributes of the relationship itself.
04/22/23 Luke Huan Univ. of Kansas 16
Design principles
KISS Keep It Simple, Stupid
Avoid redundancy Redundancy wastes space, complicates updates and
deletes, promotes inconsistency Capture essential constraints, but don’t introduce
unnecessary restrictions Use your common sense
Entity Set -> Relation
17
Relation: Beers(name, manf)
Beers
name manf
Relationship -> Relation
18
To represent a relationship, the attributes of the relation include:
1. the primary key attributes of each participating entity set, becoming foreign keys.
2. the descriptive attributes of the relationship set
employee department
name addr name location
Work
Work(employee name, dept name, duration)
duration
Relationship -> Relation The set of nondescriptive attributes is a candidate
key, if there are no key constraints.
employee department
name addr name location
Work
Work(employee name, dept name, duration)
duration
Relationship -> Relation If there is a key constraint, the key of the entity with an arrow is the candidate
key of the relation.
employee department
name addr name location
manage
Manage(employee name, dept name, duration)
duration
Relationship -> Relation
21
Drinkers BeersLikes
Favorite
Favorite(drinker name, beer name)
Married
husband
wife
Married(husband name, wife name)
name addr name manf
Buddies
1 2
Buddies(name1, name2)
Likes(drinker name, beer name)
Combining Relations
22
It is OK to combine the relation for an entity-set E with the relation R for a many-one relationship from E to another entity set.
Example: Drinkers(name, addr) and Favorite(drinker, beer) combine to make Drinker1(name, addr, favBeer).
Drinkers Beers
name addr name manf
Favorite
Combining Relations
23
Risk with Many-Many Relationships: Combining Drinkers with Likes would be a mistake.
It leads to redundancy
Drinkers BeersLikes
name addr name manf
name addr beerSally 123 Maple BudSally 123 Maple Miller
Redundancy
Handling Weak Entity Sets
24
Relation for a weak entity set must include attributes for its complete key (including those belonging to other entity sets), as well as its own, nonkey attributes.
An identifying (double-diamond) relationship is redundant and yields no relation.
04/22/23 25
Translating weak entity sets
Remember the “borrowed” key attributes Watch out for attribute name conflicts
Building (building_name, year)Rooms (building_name, room_number, capacity)
Seats (building_name, room_number, seat_number, left_or_right)
Rooms In Buildingsname
year
number
capacity
In
Seatsnumber
L/R?
Example
26
Logins HostsAt
name name
time
Example
27
Logins HostsAt
name name
Hosts(hostName)Logins(loginName, hostName, time)At(loginName, hostName, hostName2)
Must be the same
time
At becomes part ofLogins
04/22/23 28
Mapping of N-ary Relationship Types
For each n-ary relationship type R, where n>2, create a new relationship to represent R.
Include all foreign keys of the participating entity types. include any attributes of the n-ary relationship
type
04/22/23 29
Ternary relationship types. (a) The SUPPLY relationship.
04/22/23 30
Mapping the n-ary relationship type SUPPLY
Some exercise
Consider the relations Students, Faculty, Courses, Rooms, Enrolled, Teaches, and Meets.
1. List all the foreign key constraints among these relations.
2. Give an example of a (plausible) constraint involving one or more of these relations that is not a primary key or foreign key constraint.
04/22/23 31
Some exercise
1. No foreign keys for Students, Faculty, Courses, Rooms Enrolled: sid and cid should both have FKCs placed on
them. (Real students must be enrolled in real courses.) Teaches: fid and cid Meets: cid and rno.
2. the length of sid, cid, and fid could be standardized; limits could be placed on the size of the numbers entered
into the credits, room/course capacity, and faculty salary; an enumerated type should be assigned to the grade field etc
04/22/23 32
Next class
Relational algebra (hard part!)
04/22/23 33