Presentation is loading. Please wait.

Presentation is loading. Please wait.

The Relational Data Model and Relational Database Constraints

Similar presentations


Presentation on theme: "The Relational Data Model and Relational Database Constraints"— Presentation transcript:

1 The Relational Data Model and Relational Database Constraints
Chapter 3 The Relational Data Model and Relational Database Constraints

2 Component Values Notation
We refer to 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

3 Component Values Notation
t1.Name OR t1[Name] = “Dick Davidson” t3[Gpa] = 3.93 t4[Ssn, Name] ={ , “Benjamin Bayer”}

4 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 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)

5 What is a key We want to be able to retrieve every single piece of data that we putted into the database So, it is important to specify one attribute or more than one attributes which make it possible to uniquely identify every row in a table This attribute (s) is called a key This uniqueness ensure that we are retrieving exactly the row we want. The key can not be NULL Selecting a good key, is considered a challenge in the database design

6 Super key (SK) Candidate key (CK) Primary key (PK) Foreign Key (FK)
Relational keys Super key (SK) Candidate key (CK) Primary key (PK) Foreign Key (FK)

7 Super Key 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) Every relation has one default super key which is the set of all its attributes

8 Super Key SK1 = {Name, Ssn, Home_phone, address, office_phone, Age, Gpa} SK2 = {Name, Ssn} SK3 = {Address, Ssn}

9 Candidate Key A superkey may contains additional attributes that are not necessarily for unique identification We are interested in identifying superkeys that contain only the attributes necessary for unique identification So, a candidate key is a superkey such that no proper subset is a superkey within the relation Example SK2 = {Name, Ssn}  Proper Subset = {Ssn} SK3 = {Address, Ssn}Proper Subset = {Ssn} Then SK2 and SK3 is not candidate key

10 Candidate Key A candidate key, k, for a relation R has two properties
Uniqueness In each tuple of R, the values of k uniquely identify that tuple Irreducibility No proper subset of k has the uniqueness property For example suppose we choose CK1 = {A1, A2, A3} as a candidate key and we discovered that CK2  CK1 = {A1, A2} achieve uniqueness then CK1 is not a candidate key Identifying the candidate key requires that we know the meaning of attributes in the real world so that we can decide whether duplicates are possible

11 Candidate Key Given the value of city , we can determine several branch office, So this attribute cannot be candidate key But given a branchNo we can determine at most one tuple Also, given a postcode we can determine at most one tuple Then branchNo and postcode are candidate keys

12 Primary Key A primary key is a candidate key that is selected to identify tuples in the relation If a relation has several candidate keys, the minimal one is chosen arbitrarily to be the primary key. In ER the primary key attributes are underlined.

13 Primary Key Example: Consider the CAR relation schema:
CAR(State, Reg#, SerialNo, Make, Model, Year) CAR has two keys: Key1 = {State, Reg#} Key2 = {SerialNo} Both are also candidate keys of CAR So a serialNo is selected as a primary key because it is minimal In general: Any key is a superkey (but not vice versa) for example {SerialNo, Make} is a superkey but not a key. Any set of attributes that includes a key is a superkey


Download ppt "The Relational Data Model and Relational Database Constraints"

Similar presentations


Ads by Google