CH 4-1 Relational Datamodel&constraints
CH 4-1 Relational Datamodel&constraints
CH 4-1 Relational Datamodel&constraints
3
Why is the relational model so popular?
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
6
Schema vs. Instance
the name of the relation and the set of
attributes is called the schema (or the
intension)
7
SCHEMA
Student (name, address, phone
Attribute number)
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
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}
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