Presentation is loading. Please wait.

Presentation is loading. Please wait.

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 3 The Relational Data Model and Relational Database Constraints تنبيه.

Similar presentations


Presentation on theme: "Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 3 The Relational Data Model and Relational Database Constraints تنبيه."— Presentation transcript:

1 Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 3 The Relational Data Model and Relational Database Constraints تنبيه : شرائح العرض (Slides) هي وسيلة لتوضيح الدرس واداة من الادوات في ذلك. حيث المرجع الاساسي للمادة هي الكتاب المعتمد في وصف المقرر

2 Copyright © 2011 Ramez Elmasri and Shamkant B. Navathe Foreign Key A foreign key is a an attribute, or set of attributes, within one relation that matches the primary key of another relation A foreign key and primary key play an important role in performing data manipulation 2

3 Copyright © 2011 Ramez Elmasri and Shamkant B. Navathe 3 COMPANY Database Schema

4 Copyright © 2011 Ramez Elmasri and Shamkant B. Navathe 4 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.

5 Copyright © 2011 Ramez Elmasri and Shamkant B. Navathe 5 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. Referencing Referenced

6 Copyright © 2011 Ramez Elmasri and Shamkant B. Navathe 6 Referential Integrity Tuples in the referencing relation R1 have foreign key attributes FK 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.PK

7 Copyright © 2011 Ramez Elmasri and Shamkant B. Navathe 7 Referential Integrity Constraint Statement of the constraint The value in the foreign key FK of 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.

8 Copyright © 2011 Ramez Elmasri and Shamkant B. Navathe 8 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 primary key of the referenced table Next slide shows the COMPANY relational schema diagram

9 Copyright © 2011 Ramez Elmasri and Shamkant B. Navathe 9 Referential Integrity Constraints for COMPANY database

10 Copyright © 2011 Ramez Elmasri and Shamkant B. Navathe 10 Other Types of Constraints Semantic Integrity Constraints: based on application semantics and cannot be expressed by the model per se Example: “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

11 Copyright © 2011 Ramez Elmasri and Shamkant B. Navathe 11 Populated database state Each relation will have many tuples in its current relation state The relational database state is a union of all the individual relation states Whenever the database is changed, a new state arises Basic operations for changing the database: INSERT a new tuple in a relation DELETE an existing tuple from a relation MODIFY an attribute of an existing tuple Next slide shows an example state for the COMPANY database

12 Copyright © 2011 Ramez Elmasri and Shamkant B. Navathe 12 Populated database state for COMPANY

13 Copyright © 2011 Ramez Elmasri and Shamkant B. Navathe 13 Update Operations on Relations There are three basic update operations on relations INSERT a tuple. DELETE a tuple. MODIFY a tuple. Integrity constraints should not be violated by the update operations. Several update operations may have to be grouped together. Updates may propagate to cause other updates automatically. This may be necessary to maintain integrity constraints.

14 Copyright © 2011 Ramez Elmasri and Shamkant B. Navathe Insert Operation Insert operation provides a list of attribute values for a new tuple t that is to be inserted into relation R Insert can violate any of the four constraints Domain Constraints Can be violated if an attribute value is given that does not appear in the corresponding domain Key Constraints Can be violated if a key value in the new tuple already exist in another tuple Entity Integrity Can be violated if the primary key of the new tuple is null Referential Integrity Can be violated if the value of any foreign key in tuple refers to a tuple that does not exist in the referenced relation 14

15 Copyright © 2011 Ramez Elmasri and Shamkant B. Navathe 15 Update Operations on Relations In case of integrity violation, several actions can be taken: Cancel the operation that causes the violation (RESTRICT or 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

16 Copyright © 2011 Ramez Elmasri and Shamkant B. Navathe 16 Possible violations for each operation DELETE may violate only referential integrity: If the primary key value of the tuple being deleted is referenced from other tuples in the database Can be remedied by several actions: RESTRICT, CASCADE, SET NULL RESTRICT option: reject the deletion CASCADE option: propagate the new primary key value into the foreign keys of the referencing tuples SET NULL option: set the foreign keys of the referencing tuples to NULL One of the above options must be specified during database design for each foreign key constraint

17 Copyright © 2011 Ramez Elmasri and Shamkant B. Navathe 17 Possible violations for each operation UPDATE may violate domain constraint and NOT NULL constraint on an attribute being modified Any of the other constraints may also be violated, depending on the attribute being updated: Updating the primary key (PK): Similar to a DELETE followed by an INSERT Need to specify similar options to DELETE Updating a foreign key (FK): May violate referential integrity Updating an ordinary attribute (neither PK nor FK): Can only violate domain constraints

18 Copyright © 2011 Ramez Elmasri and Shamkant B. Navathe 18 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 Described the Relational Update Operations and Dealing with Constraint Violations


Download ppt "Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 3 The Relational Data Model and Relational Database Constraints تنبيه."

Similar presentations


Ads by Google