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

03 Relational Model

Uploaded by

Noha Elzahar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

03 Relational Model

Uploaded by

Noha Elzahar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

The Relational Data Model and

Relational Database Constraints

Dr. Yousry Taha

(This is the instructor’s notes and student has to


read the textbook for complete material.)
Relational Data Model Concepts
1. The Relational Data Model represents the
database as a collection of relations (tables of
values).

2. The Relational Data Model is very widely used


for its simplicity and its mathematical basics
foundations.

3. Each table has rows which represents collections


of related values.
(Relational Model) - Dr. Yousry Taha 2
Relational Data Model
◼ A row is called a tuple.

◼ A column header is called an attribute.

◼ The table is called the relation.

◼ The data type describing the types of values


in each columns is called a domain.
(Relational Model) - Dr. Yousry Taha 3
Domains, Attributes, Tuples, and Relations
◼ A domain D is a set of atomic values which means it
is indivisible for this relation.
i.e -Names: The set of names of persons.
-Grade_point_average: Possible values of computed
grade point average; each must be a real number
between 0 and 4.
-Local_phone_numbers; The set of 7-digit phone
numbers valid within a particular are code.
◼ Names, Grade_point_average, etc. are called the
logical definitions of domains.
◼ A data type is also specified for each domain (integer,
real, characters, date, …)
(Relational Model) - Dr. Yousry Taha 4
Definitions
For the relational schema R(A1,A2,A3, …., An):
◼ R is the name of the relation
◼ A1,A2,A3, …., An are the attributes
◼ Each attribute Ai is the name of a role played by some
domain D (dom(Ai)) in the relation schema R.
◼ The degree of a relation is the number of attributes n
of its relation schema.
◼ Ex:
STUDENT(Name, SSN,HomePhone,Adrress,OfficePhone,Age,GPA)

(Relational Model) - Dr. Yousry Taha 5


Relational State
➢ A relation state r of the relation schema R(A1,A2,A3,
…., An) is denoted by r(R).
➢ r(R) is a set of n-tuples r = {t1,t2,t3, …., tm }
➢ Each n-tuple t is an ordered list of n values t = <
v1,v2,v3, …., vn>.
➢ Each value vi is an element of dom(Ai) or a special null
value.
➢ The value vi is referred to as t[Ai].
➢ Null values represent attributes whose values are
unknown or does not exist.
➢ Next figure represents an example of a relation.

(Relational Model) - Dr. Yousry Taha 6


Relation Name Attributes

STUDENT Name SSN HomePhone Address OfficePhone Age GPA

Benjamin Bayer 305-61-2435 373-1616 2918 Bluebonnet Lane null 19 3.21

Katherine Ashly 381-62-1245 375-4409 125 Kirby Road null 18 2.89

Tuples Dick Davidson 422-11-2320 Null 3452 Elgin Road 749-1253 25 3.53

Charles Cooper 489-22-1100 376-9821 265 Lark Lane 749-6492 28 3.93

Barbara Benson 533-69-1238 839-8461 7384 Fontana Lane null 19 3.25

The attributes and tuples of relation STUDENT

(Relational Model) - Dr. Yousry Taha 7


Relational State (cont)
A relation can also be restated as follows:

➢ r(R) is a mathematical relation of degree n on the domains


dom(A1), dom(A2), …..,dom(An)

➢ r(R) is a subset of the cartesian product of the domains.

➢ The cardinality of a domain Ai is denoted by |dom(Ai)|.

(Relational Model) - Dr. Yousry Taha 8


Characteristics of Relations

Ordering of tuples in a relation:


➢ Tuples ordering is not part of a relation definition
because it is defined as a set of tuples
➢ There is no preference for one logical ordering
over another.
➢ When a relation is implemented as a file, a
physical ordering may be specified on the records
of the file.
➢ Next figure represents STUDENT relation with
another order.
(Relational Model) - Dr. Yousry Taha 9
STUDENT Name SSN HomePhone Address OfficePhone Age GPA

Dick Davidson 422-11-2320 Null 3452 Elgin Road 749-1253 25 3.53

Barbara Benson 533-69-1238 839-8461 7384 Fontana Lane null 19 3.25

Charles Cooper 489-22-1100 376-9821 265 Lark Lane 749-6492 28 3.93

Katherine Ashly 381-62-1245 375-4409 125 Kirby Road null 18 2.89

Benjamin Bayer 305-61-2435 373-1616 2918 Bluebonnet Lane null 19 3.21

STUDENT relation with a different order

(Relational Model) - Dr. Yousry Taha 10


Characteristics of Relations
Ordering of values within a tuple:
➢ The tuple is an ordered list, so the ordering of
values in a tuple is important.
➢ At a logical level, the order is not important as
long as the correspondence between the attribute
and its value is mentioned as:
t = < (Name, Dick), (SSN,422-11-
2320),(GPA,3.4), …>
➢ Each value in a tuple is an atomic value (no
multivalued or composite attributes).
(Relational Model) - Dr. Yousry Taha 11
Relational Model Notations
◼ R(A1,A2,A3, …., An) denotes a relation schema R
of n attributes.
◼ r(R) is a relation state.
◼ T[Ai] and t.Ai refer to the value vi in t for attribute
Ai.
◼ The letters Q,R,S denote relation names.
◼ The letters q,r,s denote relation states.
◼ The letters t,u,v denote tuples.
◼ The dot notation R.A (e.g. Student.Name or
Student.Age) can be used to identify the attributes.
(Relational Model) - Dr. Yousry Taha 12
Relational Constraints
◼ Domain Constraints:
– Each attribute A must be an atomic value from the
domain dom(A).
◼ Key Constraints:
– As a relation is defined as a set of tuple, thus no two
tuples can have the same combination of values for all
their attributes.
– Usually, there are other subset of attributes (superkey
SK) with this constraint: ti[SK] ≠ tj[SK]  i,j
– It is called uniqueness constraint that no two distinct
tuples in r can have the same values for SK.

(Relational Model) - Dr. Yousry Taha 13


Relational Constraints (cont)
◼ A superkey can have redundant attributes.
◼ A key K of R is a superkey of R with the
additional property that removing any
attribute A from K leaves a set of attributes
K’ that is not a superkey of R.
◼ For example, the attribute {SSN} in the
Student relation is a key.
◼ Any set includes {SSN} is a superkey of the
relation student.
(Relational Model) - Dr. Yousry Taha 14
Relational Constraints (cont)
◼ A relation schema may have more than one key.
– The keys are called the candidate keys.
– The primary key is one of the candidate keys which is
selected to identify tuples in the relation.
– The attributes that form the primary key are underlined
in the schema.

◼ Null Constraint:
– Not Null constraint specifies that an attribute must have
a valid value (e.g. Student name).

(Relational Model) - Dr. Yousry Taha 15


Primary key Candidate keys

CAR LicenseNumber EngineSerialNumber Make Model Year

Texas ABC-739 A69352 Ford Mustang 96

Florida TVP-347 B43696 Oldsmobile Cutlass 99

New York MPO-22 X83554 Oldsmobile Delta 95

California 432-TFY C43742 Mercedes 190-D 93

California RSK-629 Y82935 Toyota Camry 98

Texas RSK-629 U028365 Jaguar XJS 98

The CAR relation with two candidates keys.

(Relational Model) - Dr. Yousry Taha 16


Relational DB Schemas
◼ A relational DB schema is a set of schemas
S = {R1,R2,R3, …., Rm} and a set of integrity constraints
IC.
◼ DB schema is represented in next figure.
◼ DB state = {r1,r2,r3, …., rm} could be reviewed in the text
book.
◼ Data Definition Language (DDL) is used to define the
schema.
◼ The integrity constraints are specified on a DB schema
and are expected to hold on every DB state.
◼ Domain and key constraints are IC and also entity and
referential integrity which will be discussed.
(Relational Model) - Dr. Yousry Taha 17
EMPLOYEE
FNAME MINIT LNAME SSN BDATE ADDRESS SEX SALARY SUPERSSN DNO
DEPARTMENT
DNAME DNUMBER MGRSSN MGRSTARTDATE

DEPT_LOCATIONS
DNUMBER DLOCATION

PROJECT
PNAME PNUMBER PLOCATION DNUM

WORKS_ON

ESSN PNO HOURS


DEPENDENT
ESSN DEPENDENT_NAME SEX BDATE RELATIONSHIP

Schema diagram of the COMPANY relation


(Relational Model) - Dr. Yousry Taha 18
Integrity Constraints
◼ Entity integrity constraint:
– No primary key value can be null.
◼ Referential integrity constraint:
– It is specified between two relations.
– It is used to maintain consistency among tuples of the
two relations.
– It states that a tuple in one relation that refers to another
relation must refer to an existing tuple in that relation.
– The foreign key is a concept used to define the
referential integrity constraint.

(Relational Model) - Dr. Yousry Taha 19


Integrity Constraints (cont)
◼ Foreign key:
A set of attributes FK in relation schema R1 is a
foreign key of R1 that references relation R2 if it
satisfies the following two rules:
1. The attributes in FK have the same domain(s) as the
primary key attribute PK of R2.

2. A value of FK in a tuple t1 of the current state r1(R1)


either occurs as a value of PK for some tuple t 2 in the
current state r2(R2) (t1[FK]=t2[PK]) or is null.

(Relational Model) - Dr. Yousry Taha 20


Integrity Constraints (cont)
◼ A foreign key can refer to its own relation.
◼ Foreign keys can be diagrammatically
displayed by drawing a directed arc from
the foreign key to its referenced relation (as
shown in next figure).
◼ Referential integrity constraints should be
specified on the DB schema and most
DBMS support them.
◼ The referential integrity constraints are
specified as a part of the data definition.
(Relational Model) - Dr. Yousry Taha 21
EMPLOYEE
FNAME MINIT LNAME SSN BDATE ADDRESS SEX SALARY SUPERSSN DNO

DEPARTMENT
DNAME DNUMBER MGRSSN MGRSTARTDATE

DEPT_LOCATIONS
DNUMBER DLOCATION

PROJECT
PNAME PNUMBER PLOCATION DNUM

WORKS_ON

ESSN PNO HOURS


DEPENDENT
ESSN DEPENDENT_NAME SEX BDATE RELATIONSHIP

Referential integrity constraints for the COMPANY relation


(Relational Model) - Dr. Yousry Taha 22
Update Operations
◼ The relational model operations can be
categorized into two categories:
➢ Retrieval operations.
➢ Update operations
• The basic update operations are:
➢ Insert
➢ Delete
➢ Update (or Modify)
• All integrity constraints specified on the relational
DB schema should not be violated.
(Relational Model) - Dr. Yousry Taha 23
Insert Operation
◼ The Insert operation provides a list of attribute
values for a new tuple that is to be inserted into a
relation R.

◼ The operation is rejected if it violates any of the


four types of constraints discussed before.

◼ Ex: Insert <‘Cecilia’,’F’,’Kolonsky’,null,’1960-


04-05’,….,null,4> into EMPLOYEE.
– This operation is rejected because it violates the entity
integrity constraint (primary key is null).

(Relational Model) - Dr. Yousry Taha 24


Insert Operation (cont)
Ex:
Insert <‘Cecilia’,’F’,’Kolonsky’,’677678989’,
….,’987654321’,7> into EMPLOYEE.
– This operation is rejected because it violates the
referential integrity constraint
(no DEPARTMENT tuple exists with DNUMBER = 7).

(Relational Model) - Dr. Yousry Taha 25


Delete Operation
◼ The Delete operation removes tuple(s) from the
relation R.
◼ The Delete operation can violate only the referential
integrity.
◼ Ex: Delete the WORKS_ON tuple with ESSN =
‘999887777’ and PNO = 10.
– This operation is acceptable (no violations).

◼ Ex: Delete the EMPLOYEE tuple with SSN =


‘999887777’.
– This operation is not acceptable because tuples in
WORKS_ON refer to this tuple.
(Relational Model) - Dr. Yousry Taha 26
Delete Operation (cont)
Ex: Delete the EMPLOYEE tuple with SSN = ‘333445555’.
– This deletion will result in even worse referential integrity
violations because the involved tuple is referenced by
tuples from the EMPLOYEE, DEPARTMENT,
WORKS_ON, and DEPENDANT relations.
◼ Three options are available if a deletion operation
causes a violation:
➢ Reject the operation.
➢ Cascade the deletion.
➢ Modify the referencing attribute values (set to null or
reference another valid tuple).

(Relational Model) - Dr. Yousry Taha 27


Update Operation
◼ Update operation is used to change the values
of one or more attributes in a tuple(s) in
relation R.
◼ Ex: Update the SALARY of the EMPLOYEE tuple with
SSN=‘999887777’ to 28000.
– This is an acceptable operation.
◼ Ex: Update the DNO of EMPLOYEE tuple with
SSN=‘999887777’ to 7
– Unacceptable because it violates the referential
integrity.

(Relational Model) - Dr. Yousry Taha 28

You might also like