Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 405G: Introduction to Database Systems Lecture 5: Logical Design by Relational Model Instructor: Chen Qian.

Similar presentations


Presentation on theme: "CS 405G: Introduction to Database Systems Lecture 5: Logical Design by Relational Model Instructor: Chen Qian."— Presentation transcript:

1 CS 405G: Introduction to Database Systems Lecture 5: Logical Design by Relational Model Instructor: Chen Qian

2 12/13/20152 Today’s Outline Relational Model Constraints (unfinished part of last class) Update Operations Insertion Deletion Update Transaction Dealing with Constraint Violations

3 12/13/20153 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

4 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.

5 12/13/20155 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

6 12/13/20156 Key Example Enroll (sid: string, cid: string) What is the candidate key(s)? sid + cid Why

7 12/13/20157 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.

8 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.

9 Foreign Key constraints Only students listed in the Students relation should be allowed to enroll for courses. Enrolled Students Possible violation: Add to Enrolled. Possible violation: delete from Students.

10 12/13/201510 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

11 12/13/201511 Update Operations on Relations Update operations INSERT a tuple. DELETE a tuple. MODIFY a tuple. Constraints should not be violated in updates

12 12/13/201512 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

13 12/13/201513 Key concept: Transaction an atomic sequence of database actions (reads/writes) takes DB from one consistent state to another consistent state 1consistent state 2 transaction

14 12/13/201514 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: $200 savings: $1000 Transaction “transfer $100 from Saving to checking” checking: $300 savings: $900

15 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.

16 12/13/2015Luke Huan Univ. of Kansas16 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

17 Entity Set -> Relation 17 Relation: Beers(name, manf) Beers name manf

18 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 employeedepartment name addr name location Work Work(employee name, dept name, duration) duration

19 Relationship -> Relation The set of nondescriptive attributes is a candidate key, if there are no key constraints. employeedepartment name addr name location Work Work(employee name, dept name, duration) duration

20 Relationship -> Relation If there is a key constraint, the key of the entity with an arrow is the candidate key of the relation. employeedepartment name addr name location manage Manage(employee name, dept name, duration) duration

21 Relationship -> Relation 21 DrinkersBeers Likes 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)

22 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). DrinkersBeers name addr name manf Favorite

23 Combining Relations 23 Risk with Many-Many Relationships: Combining Drinkers with Likes would be a mistake. It leads to redundancy DrinkersBeers Likes name addr name manf name addr beer Sally 123 Maple Bud Sally 123 Maple Miller Redundancy

24 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.

25 12/13/201525 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 Buildings name year number capacity In Seats number L/R?

26 Example 26 LoginsHostsAt name time

27 Example 27 LoginsHostsAt name Hosts(hostName) Logins(loginName, hostName, time) At(loginName, hostName, hostName2) Must be the same time At becomes part of Logins

28 12/13/201528 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

29 12/13/201529 Ternary relationship types. (a) The SUPPLY relationship.

30 12/13/201530 Mapping the n-ary relationship type SUPPLY

31 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. 12/13/201531

32 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 12/13/201532

33 Next class Relational algebra (hard part!) 12/13/201533


Download ppt "CS 405G: Introduction to Database Systems Lecture 5: Logical Design by Relational Model Instructor: Chen Qian."

Similar presentations


Ads by Google