Understanding Relational Constraints in Database Systems

cs 405g introduction to database systems l.w
1 / 18
Embed
Share

Learn about the importance of relational constraints in database systems, including domain constraints, key constraints, entity integrity, and referential integrity. Explore examples of primary key constraints and foreign keys to ensure data integrity.

  • Database Systems
  • Relational Constraints
  • Data Integrity
  • Primary Key
  • Foreign Keys

Uploaded on | 1 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. CS 405G: Introduction to Database Systems Relational Constraints

  2. Topics Next Relational Integrity Constraint

  3. Relational Integrity Constraints Constraints are conditions that must hold on all valid relation instances. There are four main types of constraints: Domain constraints The value of an attribute must come from its domain Key constraints Entity integrity constraints Referential integrity constraints 1. 1. 2. 3. 4. 3/19/2025 3/19/2025 3 3

  4. Primary Key Constraints A set of fields is a candidate key for a relation if : 1. No two distinct tuples can have same values in all key fields, and 2. This is not true for any subset of the key. Part 2 false? A superkey. If there are >1 keys for a relation, one of the keys is chosen (by DBA) to be the primary key. 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. 3/19/2025 Jinze Liu @ University of Kentucky 4

  5. Key Example 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 3/19/2025 3/19/2025 5 5

  6. 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). Other attributes of R may be similarly constrained to disallow null values, even though they are not members of the primary key. 3/19/2025 3/19/2025 6 6

  7. 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 . 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. Can you name a data model w/o referential integrity? Links in HTML! 3/19/2025 Jinze Liu @ University of Kentucky 7

  8. Foreign Keys Only students listed in the Students relation should be allowed to enroll for courses. Enrolled Students sid 53666 Carnatic101 53666 Reggae203 53650 Topology112 53666 History105 cid grade C B A B sid name login age 18 18 19 gpa 3.4 3.2 3.8 53666 Jones 53688 Smith smith@eecs 53650 Smith smith@math jones@cs Or, use NULL as the value for the foreign key in the referencing tuple when the referenced tuple does not exist 3/19/2025 Jinze Liu @ University of Kentucky 8

  9. In-Class Exercise (Taken from Exercise 5.16) 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. 3/19/2025 3/19/2025 9 9

  10. In-Class Exercise 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. Jinze Liu @ University of Kentucky 10 3/19/2025

  11. 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 3/19/2025 3/19/2025 11 11

  12. Update Operations on Relations Update operations INSERT a tuple. DELETE a tuple. MODIFY a tuple. Constraints should not be violated in updates Jinze Liu @ University of Kentucky 12 3/19/2025

  13. Example We have the following relational schemas Student(sid: string, name: string, gpa: float) Course(cid: string, department: string) Enrolled(sid: string, cid: string, grade: character) We have the following sequence of database update operations. (assume all tables are empty before we apply any operations) INSERT< 1234 , John Smith , 3.5> into Student sid 1234 name gpa 3.5 John Smith Jinze Liu @ University of Kentucky 13 3/19/2025

  14. Example (Cont.) INSERT< 647 , EECS > into Courses INSERT< 1234 , 647 , B > into Enrolled UPDATE the grade in the Enrolled tuple with sid = 1234 and cid = 647 to A . DELETE the Enrolled tuple with sid 1234 and cid 647 sid 1234 name gpa 3.5 John Smith cid 647 department EECS sid 1234 1234 sid sid cid 647 647 cid cid grade B A grade grade Jinze Liu @ University of Kentucky 14 3/19/2025

  15. Exercise INSERT< 108 , MATH > into Courses INSERT< 1234 , 108 , B > into Enrolled INSERT< 1123 , Mary Carter , 3.8 > into Student sid 1234 1234 1123 sid name name gpa 3.5 3.5 3.8 gpa John Smith John Smith Mary Carter cid 647 department EECS cid 647 108 department EECS MATH sid cid grade sid 1234 cid 108 grade B Jinze Liu @ University of Kentucky 15 3/19/2025

  16. Exercise (cont.) A little bit tricky INSERT< 1125 , Bob Lee , good > into Student Fail due to domain constraint INSERT< 1123 , NULL, B > into Enrolled Fail due to entity integrity INSERT < 1233 , 647 , A > into Enrolled Failed due to referential integrity sid 1234 1123 name gpa 3.5 3.8 John Smith Mary Carter cid 647 108 department EECS MATH sid 1234 cid 108 grade B Jinze Liu @ University of Kentucky 16 3/19/2025

  17. Exercise (cont.) A more tricky one UPDATE the cid in the tuple from Course where cid = 108 to 109 sid 1234 1123 name gpa 3.5 3.8 John Smith Mary Carter cid 647 cid 647 108 109 department EECS department EECS MATH MATH sid 1234 1234 cid 108 109 grade B B sid cid grade Jinze Liu @ University of Kentucky 17 3/19/2025

  18. 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 Jinze Liu @ University of Kentucky 18 3/19/2025

Related


More Related Content