Chapter 3 Outline • Relational Model Concepts • Relational Model Constraints and Relational Database Schemas • Update Operations and Dealing with Constraint Violations
Relational Model Concepts • The relational Model of Data is based on the concept of a Relation – Has a formal mathematical foundation provided by set theory and first order predicate logic • We review the essentials of the formal relational model in this chapter • In practice, there is a standard model based on SQL (Structured Query Language) – described in Chapters 4 and 5 • Note: There are several important differences between the formal model and the practical model, as we shall see
• The model was first proposed by Dr. E.F. Codd of
IBM Research 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 • Dr. Codd earned the coveted ACM Turing Award in 1981
Formal Definitions – Relation Schema • Relation Schema (or 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 – n is the cardinality of the relation • Example: CUSTOMER (Cust-id, Cust-name, Address, Phone#) – CUSTOMER is the relation name – The CUSTOMER relation schema (or just relation) has four attributes: Cust-id, Cust-name, Address, Phone# • Each attribute has a domain or a set of valid values. – For example, the domain of Cust-id can be 6 digit numbers.
Formal Definitions - Tuple • A tuple is an ordered set of values (enclosed in angled brackets ‘< … >’) • Each value is derived from an appropriate domain. • A row in the CUSTOMER relation is a 4-tuple and would consist of four values, for example: – <632895, "John Smith", "101 Main St. Atlanta, GA 30332", "(404) 894-2000"> – Called a 4-tuple because it has 4 values – In general, a particular relation will have n-tuples, where n is the number of attributes for the relation • A relation is a set of such tuples (rows)
Formal Definitions - Domain • A domain of values can have a logical definition: – Example: “USA_phone_numbers” are the set of 10 digit phone numbers valid in the U.S. • A domain also has a data-type or a format defined for it. – The USA_phone_numbers may have a format: (ddd)ddd-dddd where each d is a decimal digit. – Dates have various formats such as year, month, date formatted as yyyy-mm-dd, or as dd:mm:yyyy etc.
• The attribute name designates the role played by a domain in a
relation: – Used to interpret the meaning of the data elements corresponding to that attribute – Example: The domain Date may be used to define two attributes “Invoice-date” and “Payment-date” with different meanings (roles)
Formal Definitions – State of a Relation • Formally, a relation state r(R) is a subset of the Cartesian product of the domains of its attributes – each domain contains the set of all possible values the attribute can take. – The Cartesian product contains all possible tuples from the attribute domains – The relations state r(R) is the subset of tuples that represent valid information in the mini-world at a particular time
Formal Definitions - Summary • Formally (see Figure 3.1), – Given relation schema R(A1, A2, .........., An) – Relation state r(R) ⊂ dom(A1) X dom(A2) X ....X dom(An) • R(A1, A2, …, An) is the schema of the relation • R is the name of the relation • A1, A2, …, An are the attributes of the relation • r(R): is a specific state (or "instance" or “population”) of relation R – this is a set of tuples (rows) in the relation at a particular moment in time – r(R) = {t1, t2, …, tn} where each ti is an n-tuple – ti = <v1, v2, …, vn> where each vj element-of dom(Aj)
Formal Definitions - Example • Let R(A1, A2) be a relation schema: – Let dom(A1) = {0,1} – Let dom(A2) = {a,b,c} • Then: The Cartesian product dom(A1) X dom(A2) contains all possible tuples from these domains: {<0,a> , <0,b> , <0,c>, <1,a>, <1,b>, <1,c> } • The relation state r(R) ⊂ dom(A1) X dom(A2) • For example: One possible state r(R) could be {<0,a> , <0,b> , <1,c> } – This state has three 2-tuples: <0,a> , <0,b> , <1,c>
Relation Definitions Summary Informal Terms Formal Terms Table Relation Column Header Attribute All possible Column Domain Values or Data Type Row Tuple
Characteristics Of Relations (cont.) • Notation: – We refer to component values of a tuple t by: • t[Ai] or t.Ai • This is the value vi of attribute Ai for tuple t – Similarly, t[Au, Av, ..., Aw] refers to the subtuple of t containing the values of attributes Au, Av, ..., Aw, respectively in t
Relational Integrity Constraints • Constraints are conditions that must hold on all valid relation states. • Constraints are derived from the mini-world semantics • There are three main types of built-in constraints in the relational model (Explicit Constraints): – Key constraints – Entity integrity constraints – Referential integrity constraints • Another implicit constraint is the 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)
Key Constraints • Superkey SK of R: – Is a set of attributes SK of R with the following condition: • No two tuples in any valid relation state 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 • This condition must hold in any valid state r(R)
Key Constraints • Key K of R: – Is a "minimal" superkey – Formally, a key K is a superkey such that removal of any attribute from K results in a set of attributes that is not a superkey (or key) any more (does not possess the superkey uniqueness property) – Hence, a superkey with one attribute is always a key
Key Constraints (cont.) • Example: Consider the CAR relation schema: – CAR(State, Reg#, SerialNo, Make, Model, Year) – CAR has two keys (determined from the mini-world constraints): • Key1 = {State, Reg#} • Key2 = {SerialNo} – Both are also superkeys of CAR – However, {SerialNo, Make} is a superkey but not a key. • In general: – Any key is a superkey (but not vice versa) – Any set of attributes that includes a key is a superkey – A minimal superkey is also a key
Referential Integrity Constraint • A constraint involving two relations – The previous constraints (key, entity integrity) involve a single relation. • Used to specify a relationship among tuples in two relations: – The referencing relation and the referenced relation.
DELETE operation • DELETE one or more existing tuples from a relation • 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 (see Chapter 4 for more details) – RESTRICT option: reject the deletion – CASCADE option: propagate the deletion by automatically deleting the referencing tuples – SET NULL option: set the foreign keys of the referencing tuples to NULL (the foreign keys cannot have NOT NULL constraint) – One of the above options must be specified during database design for each referential integrity (foreign key) constraint
UPDATE operation • UPDATE modifies the values of attributes in one or more existing tuples in a relation • UPDATE may violate domain constraint and NOT NULL constraint on an attribute being modified • Other constraints may also be violated: – Updating the primary key (PK): • Similar to a DELETE followed by an INSERT • Need to specify similar options to DELETE • The CASCADE option propagates the new value of PK to the foreign keys of the referencing tuples automatically – Updating a foreign key (FK) m ay violate referential integrity – Updating an ordinary attribute (neither PK nor FK): • Can only violate domain or NOT NULL constraints
In-Class Exercise (Taken from Exercise 3.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.