Chapter 3: Relational Data Model and Relational Database Constraints PDF
Document Details
Uploaded by Deleted User
Riyadh University for Science and Technology
2024
Mohammed Badawy
Tags
Summary
This document is a chapter on relational data models and relational database constraints. It provides an overview of the relational model, its concepts, and various constraints. The document also includes examples and explains operations like insert, delete, and modify within the context of relational databases.
Full Transcript
Chapter 3 Relational Data Model and Relational Database Constraints Assoc. Prof. Mohammed Badawy [email protected] 28 October 2024 Chapter Outline ⚫ Relational Model Concepts ⚫ Relational Model Constraints and Relational Database Schemas ⚫ Update O...
Chapter 3 Relational Data Model and Relational Database Constraints Assoc. Prof. Mohammed Badawy [email protected] 28 October 2024 Chapter Outline ⚫ Relational Model Concepts ⚫ Relational Model Constraints and Relational Database Schemas ⚫ Update Operations and Dealing with Constraint Violations 2 Relational Model Concepts ⚫ The relational model of data is based on the concept of a Relation. ⚫ A Relation is a mathematical concept based on the ideas of sets. ⚫ The strength of the relational approach to data management comes from the formal foundation provided by the theory of relations. ⚫ We review the essentials of the relational approach in this chapter. 3 Relational Model Concepts ⚫ The model was first proposed by Tedd Codd of IBM in 1970 in the following paper: "A Relational Model for Large Shared Data Banks," Communications of the ACM, June 1970. The above paper caused a major revolution in the field of Database management and earned Ted Codd the coveted ACM Turing Award. 4 Informal Definitions ⚫ RELATION: A table of values – A relation may be thought of as a set of rows. – A relation may alternately be though of as a set of columns. – Each row represents a fact that corresponds to a real-world entity or relationship. – Each row has a value of an item or set of items that uniquely identifies that row in the table. – Sometimes row-ids or sequential numbers are assigned to identify the rows in the table. – Each column typically is called by its column name or column header or attribute name. 5 Formal Definitions ⚫ A Relation may be defined in multiple ways. ⚫ The Schema of a Relation: R (A1, A2,.....An) ⚫ Relation schema R is defined over attributes A1, A2,...An For Example - CUSTOMER (Cust-id, Cust-name, Address, Phone#) Here, CUSTOMER is a relation defined over the four attributes Cust-id, Cust-name, Address, Phone#, each of which has a domain or a set of valid values. For example, the domain of Cust-id is 6 digits numbers. 6 Formal Definitions ⚫ A tuple is an ordered set of values ⚫ Each value is derived from an appropriate domain. ⚫ Each row in the CUSTOMER table may be referred to as a tuple in the table and would consist of four values. is a tuple belonging to the CUSTOMER relation. ⚫ A relation may be regarded as a set of tuples (rows). ⚫ Columns in a table are also called attributes of the relation. 7 Definition Summary Informal Terms Formal Terms Table Relation Column Attribute/Domain Row Tuple Values in a column Domain Table Definition Schema of a Relation Populated Table Extension 8 Example 9 Note ❑Ordering of Tuples in a relation ❑Ordering of Values within a Tuple ❑Values and NULLs in the Tuples 10 Relational Integrity Constraints ⚫ Constraints are conditions that must hold on all valid relation instances. There are four types of constraints: 1. Domain constraints 2. Key constraints 3. Entity integrity constraints 4. Referential integrity constraints 11 Domain Constraints ⚫ Domain constraints specify that the value of each attribute A must be an atomic value from the domain dom(A). ⚫ The data types associated with domains typically include standard numeric data types for integers and real numbers. Characters, fixed-length strings, and variable-length strings are also available, as are date, time, timestamp, and money data types. ⚫ Other possible domain may be described by a subrange of values from a data type or as an enumerated data type where all possible values are explicitly listed. 12 Key Constraints ⚫ A relation is defined as a set of tuples. By definition, all elements of a set are distinct ⚫ This means that no two tuples can have the same combination of values for all their attributes. ⚫ Superkey of R: A set of attributes SK of R such that no two tuples in any valid relation instance 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]. ⚫ Every relation has at least one default superkey. ⚫ A superkey can have redundant attributes. 13 Key Constraints ⚫ Key of R: A "minimal" superkey; that is, a superkey K such that removal of any attribute from K results in a set of attributes that is not a superkey. ⚫ For example, consider the STUDENT relation. The attribute set {SSN} is a key of STUDENT because no two student tuples can have the same value for SSN. ⚫ Any set of attributes that include SSN—for example, {SSN, Name, Age}—is a superkey. ⚫ However, the superkey {SSN, Name, Age} is not a key of STUDENT because removing Name or Age or both from the set still leaves us with a superkey. ⚫ In general, a relation schema may have more than one key. In this case, each of the keys is called a candidate key. ⚫ It is common to designate one of the candidate keys as the primary key of the relation. 14 Entity Integrity ⚫ Entity Integrity: The primary key attributes PK of each relation schema R 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) ⚫ Note: Other attributes of R may be similarly constrained to disallow null values, even though they are not members of the primary key. 15 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. ⚫ 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. 16 Referential Integrity Constraint Statement of the constraint The value in the foreign key column (or columns) FK of the referencing relation R1 can be either: (1) a value of an existing primary key value of the 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. 17 18 19 20 21 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. 22 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 23 Insert Operation ⚫ The Insert operation provides a list of attribute values for a new tuple t that is to be inserted into a relation R. ⚫ Insert can violate any of the four types of constraints discussed before. ⚫ 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 t already exists in another tuple in the relation r(R). ⚫ Entity integrity can be violated if the primary key of the new tuple t is null. ⚫ Referential integrity can be violated if the value of any foreign key in t refers to a tuple that does not exist in the referenced relation. 24 Insert Operation: examples ⚫ Insert into EMPLOYEE. – Result: This insertion violates the entity integrity constraint (null for the primary key SSN), so it is rejected. ⚫ Insert into EMPLOYEE. – Result: This insertion violates the key constraint because another tuple with the same SSN value already exists in the EMPLOYEE relation, and so it is rejected. 25 Insert Operation: examples ⚫ Insert into EMPLOYEE. – Result: This insertion violates the referential integrity constraint specified on DNO because no DEPARTMENT tuple exists with DNUMBER = 7. ⚫ Insert into EMPLOYEE. – Result: This insertion satisfies all constraints, so it is acceptable. 26 Delete Operation ⚫ The Delete operation can violate only referential integrity, if the tuple being deleted is referenced by the foreign keys from other tuples in the database. ⚫ To specify deletion, a condition on the attributes of the relation selects the tuple (or tuples) to be deleted. 27 Delete Operation: examples ⚫ Delete the WORKS_ON tuple with ESSN ‘999887777’ and PNO = 10. – Result: This deletion is acceptable. ⚫ Delete the EMPLOYEE tuple with SSN = ‘999887777’. – Result: This deletion is not acceptable, because tuples in WORKS_ON refer to this tuple. Hence, if the tuple is deleted, referential integrity violations will result. ⚫ Delete the EMPLOYEE tuple with SSN = ‘333445555’. – Result: This deletion will result in even worse referential integrity violations, because the tuple involved is referenced by tuples from the EMPLOYEE, DEPARTMENT, WORKS_ON, and DEPENDENT relations. 28 Update Operation ⚫ The Update operation is used to change the values of one or more attributes in a tuple (or tuples) of some relation R. ⚫ It is necessary to specify a condition on the attributes of the relation to select the tuple (or tuples) to be modified. 29 Update Operation: examples ⚫ Update the SALARY of EMPLOYEE tuple with SSN = ‘999887777’ to 28000. – Result: Acceptable. ⚫ Update the DNO of EMPLOYEE tuple with SSN = ‘999887777’ to 1. – Result: Acceptable. ⚫ Update the DNO of EMPLOYEE tuple with SSN = ‘999887777’ to 7. – Result: Unacceptable, because it violates referential integrity. ⚫ Update the SSN of EMPLOYEE tuple with SSN = ‘999887777’ to ‘987654321’. – Result: Unacceptable, because it violates primary key and referential integrity constraints. 30