We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 34
Chapter 5
The Relational Data Model and
Relational Database Constraints Chapter Outline ■ Relational Model Concepts ■ Relational Model Constraints and Relational Database Schemas ■ Update Operations and Dealing with Constraint Violations
Relational Model Concepts ■ 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 formal relational model in this chapter ■ In practice, there is a standard model based on SQL – this is described in Chapters 8 and 9
Informal Definitions ■ Informally, a relation looks like a table of values. ■ A relation contains a set of rows, or tuples. ■ The relational model does not distinguish entities from relationships. ■ It sees only “tables of facts.” ■ Each column is an attribute that gives an indication of the meaning of the data items in that column
Relation Schema and State ■ The Schema is a description of a Relation: ■ Denoted by R(A1, A2, .....An) ■ R is the name of the relation ■ The attributes of the relation are A1, A2, ..., An ■ The relation state r(R) refers to the present set of data/tuples in the relation. ■ Example: CUSTOMER (Cust-id, Cust-name, Address, Phone#) ■ CUSTOMER is the relation name ■ Defined over the four attributes: Cust-id, Cust-name, Address, Phone# ■ A CUSTOMER state may include 100 CUSTOMERS; another 250 CUSTOMERs.
Relations States Are Sets ■ All tuples in a relation state r(R) form a set. ■ By definition, there cannot be duplicates, or identical tuples, in a set. ■ By definition, set elements (tuples) are not ordered, even though tuples frequently appear to be in the tabular form. ■ Ordering of attributes however is important ■ We will consider the attributes in R(A1, A2, ...,
Characteristics Of Relations ■ Values in a tuple: ■ All values are considered atomic (indivisible). ■ No composite or multi-valued attributes. ■ A special null value is used to represent values that are not available. 3 reasons in reality: ■ Unknown ■ Inapplicable ■ Withheld ■ IMPORTANT: NULL ≠ NULL
Relational Database Schema ■ Relational Database Schema: ■ A set S of relation schemas that belong to the same database. ■ S is the name of the whole database schema ■ S = {R1, R2, ..., Rn} ■ R1, R2, …, Rn are the names of the individual relation schemas within the database S ■ Following slide shows a COMPANY database schema with 6 relation schemas
Relational Integrity Constraints ■ Constraints are conditions that must hold on all valid relation states. ■ There are three main types of constraints in the relational model: ■ Key constraints ■ Entity integrity constraints ■ Referential integrity constraints ■ Domain constraint ■ Every value in a tuple must be from the domain of its attribute (or it could be null, if allowed for that attribute)
Domain Constraints ■ The value of an attribute is limited to its domain. ■ A domain can impose rules on both formats and valid value ranges. ■ A salary value cannot be negative ■ 2006-02-15 is an incorrect address. ■ 2006-02-29 is an incorrect date. ■ Something must be wrong with a present employee born in 1800-01-01. ■ An employee’s name cannot be NULL. ■ This is called the NOT NULL constraint.
Key Constraints (continued) ■ If a relation has several candidate keys, one is chosen to be the primary key. ■ The primary key attributes are underlined. ■ Example: Consider the CAR relation schema: ■ CAR(State, Reg#, SerialNo, Make, Model, Year) ■ We chose SerialNo as the primary key ■ The primary key is used to reference the tuple from another tuple ■ General rule: choose as primary key the smallest of the candidate keys (in terms of space) ■ choice is sometimes subjective
Entity Integrity ■ The primary key attributes PK of each relation schema R in S cannot have null values ■ Because PK values are used to identify the individual tuples. ■ If PK has several attributes, null is not allowed in any of these attributes
Foreign Keys ■ Tuples in a referencing relation R1 use a set of attributes FK (called foreign key) that reference the primary key attributes PK of the referenced relation R2. ■ If a person owns a car, whose primary key is SerialNo, then the PERSON relation must have a CarSerialNo attribute to identify the car. FK={CarSerialNo}. ■ If the primary key of CAR is {state, Reg#}, then PERSON must have attributes CarState and CarReg#. FK={CarState, CareReg#}.
Referential Integrity (or foreign key) Constraint ■ Statement of the constraint ■ The value in 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.
Other Types of Constraints ■ Application Semantic Integrity Constraints: ■ Also called business rules or real world constraints ■ May not be expressed by a give database model ■ Example: “the max. no. of hours per employee for all projects he or she works on is 56 hrs per week” ■ Example: A department cannot have more than 100 employees unless it controls more than 5 projects.
■ SQL-99 allows Triggers and Assertions to
express for some of these ■ Heed the performance issue
Remedies to Constraint Violations ■ In case of integrity violation, several actions can be taken: ■ Cancel the operation that causes the violation (RESTRICT or REJECT option) ■ Trigger additional updates so the violation is corrected (CASCADE option, SET NULL option) ■ Execute a user-specified error-correction routine ■ Perform the operation but inform the user of the violation
Possible Violations for UPDATE ■ Constraint violations 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 and business rules constraints