You are on page 1of 31

CHAPTER 4

Relational Data Model,


Relational Constraints and
Relational Algebra
Topics Covered
 Relational Model History
 Relational Model Characteristics
 Constraints on relational model
 Operations on relations
 Relational Algebra
 Select
 Project
 Rename
 Join
 Aggregate Functions
 Set theoretic operations
2
Relational Model History
 Introduced by Ted Codd in 1970
 Ted Codd was an IBM Research Fellow
 Laid the foundation for database theory
 Many database concepts & products
based on this model

3
Why is the relational model so popular?

 supported by a mathematical model


 relations (tables) are a good tool to use
when communicating information to users
and developers
 efficient implementations exist for the
storing of relational information in the form
of Relational DBMSs (RDBMSs)

4
What is a Relation?
 A Relation is a 2-dimensional table of values
(rows and columns)
 each row, or tuple, is a collection of related
facts
 the degree of the relation is the number of
attributes in the relation
 each column represents an attribute
 each row is an instance of the relation

5
What is a Relation (cont’d)?
 So, a relation is a big table of facts.
 Each column contains the same attribute data
with the same data type
 Each row describes a real-world instance of the
relation

 A Relational database contains one or more


relations (or tables).

6
Schema vs. Instance
 the name of the relation and the set of
attributes is called the schema (or the
intension)

 the current values in the relation represent


an instance (or extension) of the data

7
SCHEMA
Student (name, address, phone
Attribute number)

Name Address Phone Number


Bob Johnston 533-3333
St.
Tuple/ Mary Union St. 533-4444
Instance/
Fred Clarence 533-5555
Relation
St.
State
8
More formally……
 A domain D is a set of atomic values
 localphone number – The set of 8-digit numbers
 names – The set of names of persons
 date of birth – Possible dates of birth for people

 A relation schema R(A1, A2, …, An) is a:


 relation name (R)
 list of attributes (A1, A2, …, An)
 each attribute Ai is the name of a role played by some
domain D in the relation schema R
 a relation r(R) is a subset of dom(A1) X dom(A2) X … X
dom(An)
 each element in a relation, called a tuple, is a collection of
n values
9
Characteristics of Relations
 tuples have no particular order
 ordering of attributes not important
 all values belonging to a particular
attribute are from the same domain
 attributes are atomic
 attributes may have a null value

10
Types of Constraints
 Domain constraints
 Key constraints
 Integrity constraints
 EntityIntegrity Constraint
 Referential Integrity Constraint

11
Domain Constraints
 The value of each attribute, A, must be an
atomic value from the domain of A

 So, if an attribute is from the domain of a


phone number, then the attribute must be
a phone number.

12
Key constraints
 value of a key uniquely identifies a tuple in
a relation
 a superkey K is subset of attributes of R
such that:
 no 2 tuples have same values for K
 Every relation has at least one superkey;
what is it?

13
Key Constraints
Key: A key is defined as Superkey which
does not have redundant attribute. A key K
of Relation Schema R is a Superkey of R
with additional property that removing any
attribute A from K leaves a set of attributes
K1 that is not Superkey of R.
A Key is a minimal SuperKey
Candidate Keys
 A relation schema may have more than
one key, each of the keys is called a
candidate key.
 For Example, the CAR relation has two
candidate keys: Licensenumber and
EngineSerialNumber
Primary Key
 We choose one of the Candidate Key of
the relation to uniquely identify tuples in
relation. Such a key is called Primary Key
of relation.
 The Attributes that form a Primary key of a
relation are underlined.
Relational Databases and
Relational Schemas
 A Relational Database contains many relations,
with tuples in relations are related in various ways.
 A Relational Database Schema S is a set of
relation schemas S = {R1,R2,…,Rm} and a set of
integrity constraints IC.
 A Relational Database State DB of S is a set of
relation states DB = {r1,r2,…,rm} such that each ri is
a state of Ri and each ri satisfy the integrity
constraint specified in IC
Example
 Consider relational database schema for a
company specified as

COMPANY={EMPLOYEE,DEPARTMENT,DEPT
_LOCATIONS,PROJECT,WORKS_ON,DEPEN
DENT}

 Each of these relation schemas can be shown as


EMPLOYEE
FNAME LNAME SSN BDATE ADDRESS SEX SALARY SUPERSSN DNO

DEPARTMENT
DNAME DNUMBER MGRSSN MGRSTATE
Entity Integrity Constraint
 It states that no primary key value can be
null.
 Since if two or more tuple will have null
value for the primary key we will not be
able to distinguish them.
Referential Integrity Constraint
 It states that tuple in one relation that that refers
to another relation must refer to an existing tuple
in that relation.
 For Example, the attribute DNO of employee
gives the department number for which each
employee works hence it’s value in every
Employee tuple must match the DNUMBER
value of some tuple in DEPARTMENT relation.
 Referential Integrity constraint arise due to
relationship among the entities represented by
relation schemas.
 For example, here DNO refers to “works_for”
relationship between Employee and Department
entities.
 Referential Integrity constraint can be shown
diagrammatically by a directed arc from foreign
key to the relation it references.
Foreign Key
 A foreign Key specify referential integrity constraint
between two relation schemas R1 and R2. A set of
attributes FK in R1 is foreign key of R1 that references
R2 if it satisfies following two rules:
 The attributes in FK have same domains as primary
key attributes PK of R2
 A value FK in tuple t1 of current state r1(R1) either
occurs as a value of PK for some tuple t2 in current
state r2(R2) i.e. t1[FK]=t2[PK] or is null.
R1 is called referencing relation and R2 is called
referenced relation
EMPLOYEE
FNAME LNAME SSN BDATE ADDRESS SEX SALARY SUPERSSN DNO

DEPARTMENT
DNAME DNUMBER MGRSSN MGRSTATE
Database Operations
 There are three basic update operations
on relations:
 Insert – use to insert new tuple in a
relation
 Delete – use to delete tuple
 Modify – use to change values of some
attribute of existing tuple
Insert Operation
 It provides a list of attribute values for new
tuple t that is to be inserted into a relation
R.
 Insert Operation can violate Domain
constraint, Key constraint, Entity integrity
or referential integrity.
Contd…
 In case of violation of any of constraints
RDBMS rejects the insertion or may give
appropriate message for reason for
rejection.
Delete Operation
 It can violate only referential integrity, if tuple
being deleted is referenced by the foreign keys
from other tuples in database.
 To specify deletion, a condition on the attribute
of relation selects the tuple to be deleted.
Contd…
 If deletion operation causes a violation three
options are available:
 Reject the deletion.
 Cascade the deletion i.e. delete the tuple
that references the tuple being deleted.
 Modify the referencing attribute values that
cause violation, set it to null or any valid
tuple.
Update Operation
 Here we specify a condition on the
attribute of relation to select the tuples to
modify. For Example
 Update the salary of Employee tuple with
SSN=‘999887777’ to 28000
 Updating an attribute that is either a
primary key or foreign key leads to same
issues that we discussed in case of
insertion or deletion

You might also like