0% found this document useful (0 votes)
4 views

Chapter 5

Uploaded by

yordanosmussie11
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Chapter 5

Uploaded by

yordanosmussie11
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 36

Chapter 5

The Relational Data Model and


Relational Constraints
Outlines
• Relational Model Concepts
• Characteristics of Relations
• Relational Integrity Constraints
– Key Constraints
– Entity Integrity Constraints
– Referential Integrity Constraints
• Update Operations on Relations
• Relational Algebra Operations
Outlines (Continued)
• Relational Algebra Operations
– SELECT and PROJECT
– Set Operations
– JOIN Operations
– Additional Relational Operations
1 Relational Model Concepts
Database: a collection of relations

Relation (informally): A table of values. Each column the table has a


column header called an attribute. Each row is
called a tuple.
attribute
tuple
(entity or relationship)

Formal Relational Concept:

-- Domain: A set of atomic (indivisible) values. Domain Name


Data type(Format)
Continued
-- Attribute: A name to suggest the meaning that a domain plays in a
(role)

a particular relation. Each attribute Ai has a domain dom( Ai ).


E.G. Names (set of names of Persons) EMPNAME, MGRNAME

-- Relation schema: A relation name R and a set of attributes Ai


intension
that define the relation.
R: relation name
Denoted by: R(A1,A2,…, An ) where A ,A , …, A : attributes
1 2 n

Example: STUDENT (Name,SSN,BirthDate,Addr)


Continued

-- Degree of a relation: Its number of attributes n. n=4


-- Tuple t (of R(A1,A2,…,An)):
A (ordered) set of values t = <v1,v2,…,vn>
where each value vi is an element of dom(Ai).
Also called an n-tuple.
-- Relation instance r(R): A set of tuples.
extension
(state)
r(R) = { t1, t2, …, tm}, or alternatively

r(R)  dom(A1) × dom(A2) × … × dom(An)


Figure 7.1 The attributes and tuples of a relation STUDENT
degree=7

(student entity)
From the same domain play different role
* value unknown
* attribute does not apply to this tuple
* this tuple has no value for this attribute
2 Characteristics of Relations
Ordering of tuples in a relation r(R): No order
The tuples are not considered to be ordered, even though they
appear to be in the tabular form. c.f. sequential file

Ordering of attributes in a relation schema R (and of values within


each tuple):
We will consider the attributes in R(A1,A2,…,An) and the values in
t=<v1,v2,…,vn> to be ordered.
(However, a more general alternative definition of relation does not
require this ordering)
An ordered set of values t=<v1,…,vn> vs. a set of (<attr>,<value>) pairs
how to map composite & multivalued
Values in a tuple: All values are considered atomic (indivisible).
First normal form assumption
A special null value is used to represent values that are unknown
or inapplicable to certain tuples.
Interpretation: relations (entity, relationship) :*assertion
*predicate
Notation:
-- We refer to component values of a tuple t by t[Ai]=vi (the value
of attribute Ai for tuple t).
Similarly, t[Au, Av, …, Aw] refers to the subtuple of t containing
the values of attribute Au, Av, …, Aw respectively.
t= <‘Barbara Benson’, ‘533-69-1238’, ‘839-8461’,’7384 Fontana Lane’, null, 19,3.25>

t [Name]=<‘Barbara Benson’>
t [SSN, GPA, Age]=<‘533-69-1238’, 3.25, 19>
3 Relational Integrity Constraints
Constraints are conditions that must hold on all valid
relation instances. There are four main types of constrains:
Domain constraints, Key constraints, Entity integrity
constraints, and Referential integrity constraints.
functional dependencies, ...

Domain Constraints: the value of each attribute


v(A)  dom(A)
3.1 Key Constraints
Superkey of R: A set 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—the set of all its
attributes.
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.
Example: The CAR relation schema:
CAR(State, Reg#, SerialNo, Make, Model, Year)
has two keys Key1={State,Reg#}, Key2={SerialNo}, which are also
superkeys. {SerialNo, Make} is a superkey but not a key
a relation schema may have more than one key
If a relation has several candidate keys, one is chosen arbitrarily to
be the primary key. The primary key attributes are underlined.
For example, consider the STUDENT relation
of Figure 07.01. The attribute set {SSN} is a
key of STUDENT because no two student
tuples can have the same value for SSN (Note
4). Any set of attributes that includes 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.
3.2 Entity Integrity


Relational Database Schema: A set S of relation
schemas that belong to the same database and a set of
integrity constraints IC. S is the name of the database.
R={R1,R2,…,Rn}

relational database instance DB={r1,r2,…rn} s.t. ri satisfy IC (see 7-13,7-14)


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)
Key constraints and entity integrity constraints are specified on
individual relations.
• 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)
• Key constraints and entity integrity constraints are
specified on individual relations.
Figure 7.5 The COMPANY relational database schema; primary key are underlined.

Allow attribute that represent the same real world concept to have
name that may or may not identical in different relation.
Allow attributes that represent different concept to have the same name
in different relations.
(to 7-16)
Figure 7.6 A relational database instance (state) of COMPANY schema
Continued
Note: Other attributes of R may be similarly constrained to
Disallow null values, even though they are not members of the
primary key.

Key Constraints and Entity Integrity Constraints are specified on


individual relations.
3.3 Referential Integrity
A tuple in one relation that refers to another relation must refer to an
existing tuple in it.
A constraint involving two relations (the previous constraints involve
a single relation). Arise from relationships among entities
EMPLOYEE(DNo) DEPARTMENT(DNUMBER)
Used to specify a relationship among tuples in two relations: the
referencing relation and the referenced relation.
(EMPLOYEE) (DEPARTMENT)

have the same domain


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].
FK can be null.
1. 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].
2. FK can be null.

Referential integrity constraints typically arise


from the relationships among the entities
represented by the relation schemas.
A referential integrity constraint can be displayed in a relational
database schema as a directed arc from R1 FK to R2. (see 7-19)

R1
referencing relation
‧‧‧ FK ‧‧‧
R2
PK referenced relation

Semantic integrity Constraint


˙ the salary of an employee > the salary of his boss
˙ the maximum work hours > number specified in worker law
a foreign key refers to its own relation

7-18
4 Update Operations on Relations
Operations: retrievals and updates
-- 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.
-- In case of integrity violation, several actions can be taken:
-cancel the operation that causes the violation
-perform the operation but inform the user of the violation
-trigger additional updates so the violation is corrected
-execute a user-specified error-correction routine
Insert operation

Domain Constraint:
if an attribute value is given that does not appear in the domain
Key Constraint:
if a key value in the new tuple t already exists in another tuple in
the relation.
Entity Integrity:
if the primary key of new tuple is null
Referential Integrity:
if the value of any foreign key in t refers to a tuple that does not
exist in the referenced relation.
(See 7-14)
1.Insert < ‘Cecilia’, ’F’, ‘ Kolowsky’, ‘677678989’, ’05-APR-50’,‘6357
Windy Lane, Katy, TX’, F, 28000, null, 4 > into EMPLOYEE.
acceptable
2.Insert <‘Alicia’,’J’,’Zelaya’,’999887777’,’05-APR-50’,’6357 Windy
Lane, Katy,TX’, F, 28000,’987654321’,4> into EMPLOYEE.
Violate key constraint.
3.Insert <‘Cecilia’,’F’,’Kolowsky’,null,’05-APR-50’,’6357 Windy Lane,
Katy,TX’, F, 28000, null,4> into EMPLOYEE.
Violate entity integrity constraint
4.Insert<‘Cecilia’, ‘F’, ‘Kolowsky’,’677678989’,’05-APR-50’,’6357
Windswept Katy, TX, F, 28000, ‘987654321’,7> into EMPLOYEE
Violate referential integrity constraint

EMPLOYEE ‧‧‧ SSN ‧‧‧ SUPER SSN DNo

DEPARTMENT ‧ ‧ DNUM MGRSSN ‧‧‧


Two options are available.
Reject the insertion
Correct the reason for rejecting the insertion.
(3) Provide an acceptable SSN.
(4) ‧ Change the value of DNo, or
‧ insert a DEPARTMENT tuple with DNUMER T=7
(Cascade back to EMPLOYEE relation)

EMPLOYEE
‧‧‧ SSN ‧‧‧ SUPER SSN DNo

‧ ‧ DNUM MGRSSN ‧‧‧


DEPARTMENT
DELETE OPERATION
Only referential integrity constraint may be violated.
× domain constraint
× key constraint
× entity integrity constraint

1. Delete the WORKS_ON tuple with ESSN=‘999887777’ and DNo=10


acceptable
2. Delete the EMPLOYEE tuple with SSN=‘999887777’
unacceptable.
Two tuples in WORKS_ON refer to this tuple.
3. Delete the EMPLOYEE tuple with SSN=‘333445555’
unacceptable
The tuple involved is referenced by tuples from EMPLOYEE,
DEPARTMENT, WORKS_ON and DEPENDENT relations.
7-24
Three options are available.
 Reject the deletion.
 Attempt to cascade (or propagate) the deletion
E.G. Delete the two offending tuples in(2).

 Modify the referencing attribute values that cause the violation.


Change to another valid tuple, or set to null. When a referential
integrity constraint is specified, the DBMS should allow the users to
specify which of the three options applies in case of a violation of
the constraint.

 Combine these three alternatives.


E.G. in (3) operation
WORKS_ON, DEPENDENT => automatically delete
EMPLOYEE => Set to null or change to another tuple
Modify operation
1. Modify the SALARY of the EMPLOYEE tuple with
SSN=‘999887777’ to 28000.
Acceptable
2. Modify the DNO of the EMPLOYEE tuple with
SSN=‘999887777’ to 1.
Acceptable
3. Modify the DNO of the EMPLOYEE tuple with
SSN=‘999887777’ to 7.
Unacceptable
(Violate referential integrity)
4. Modify the SSN of the EMPLOYEE tuple with
SSN=‘999887777’ to ‘987654321’.
Unacceptable
(Violate primary key and referential integrity constraints)
987654321
7
Modify attributes other than primary key and foreign key:
check correct data type & domain
Modify primary key:
check domain constraint, key constraint, entity integrity
constraint, referential integrity constraint.
Modify foreign key:
check referential integrity constraint
Defining Relations
 Deciding which attributes belong together in each relation
 Choosing appropriate names for the relations and their
attributes.
 Specifying the domains and data types of various
attributes.
 Identifying the candidate keys and choosing a primary key
for each relation.
 Specifying all foreign keys.
Name of relational database schema
DECLARE SCHEMA COMPANY;

domain name & data type


DECLARE DOMAIN PERSON_SSNS TYPE FIXED_CHAR (9) ;
DECLARE DOMAIN PERSON_NAMES TYPE VARIABLE_CHAR (15) ;
DECLARE DOMAIN PERSON_INITIALS TYPE ALPHABETIC_CHAR (1) ;
DECLARE DOMAIN DATES TYPE DATE ;
DECLARE DOMAIN ADDRESSES TYPE VARIABLE_CHAR (35) ;
DECLARE DOMAIN PERSON_SEX TYPE ENUMERATED {M, F} ;
DECLARE DOMAIN DEPT_SALARIES TYPE MONEY ;
DECLARE DOMAIN DEPT_NUMBERS TYPE INTEGER_RANGE [1,10] ;
DECLARE DOMAIN DEPT_NAMES TYPE VARIABLE_CHAR (20) ;

relations
DECLARE RELATION EMPLOYEE
FOR SCHEMA COMPANY
ATTRIBUTES FNAME DOMAIN PERSON_NAMES,
MINIT DOMAIN PERSON_INITIALS,
LNAME DOMAIN PERSON_NAMES,
SSN DOMAIN PERSON_SSNS,
BDATE DOMAIN DATES,
ADDRESS DOMAIN ADDRESSES,
SEX DOMAIN PERSON_SEX,
SALARY DOMAIN PERSON_SALARIES,
SUPERSSN DOMAIN PERSON_SSNS,
DNO DOMAIN DEPT_NUMBERS

CONSTRAINTS PRIMARY_KEY (SSN),


FOREIGN_KEY (SUPERSSN) REFERENCES EMPLOYEE,
FOREIGN_KEY (DNO) REFERENCES DEPARTMENT;
DECLARE RELATION DEPARTMENT
FOR SCHEMA COMPANY
ATTRIBUTES DNAME DOMAIN DEPT_NAMES,
DNUMBER DOMAIN DEPT_NUMBERS,
MGRSSN DOMAIN PERSON_SSNS,
MGRSTARTDATE DOMAIN DATES
CONSTRAINTS PRIMARY_KEY (DNUMBER),
KEY (DNAME),
FOREIGN_KEY (MGRSSN) REFERENCES EMPLOYEE;

You might also like