0% found this document useful (0 votes)
100 views50 pages

Relational Model: Lecture - 3

The document discusses key concepts of the relational model, including how tables represent data through relations, tuples, and attributes. It defines terminology like relation, attribute, domain, tuple, cardinality, and keys. It explains how to identify candidate keys, primary keys, and foreign keys. The goals are to understand how relational databases are structured and related through keys. Representing data as relational tables allows flexible querying and integrity constraints.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
100 views50 pages

Relational Model: Lecture - 3

The document discusses key concepts of the relational model, including how tables represent data through relations, tuples, and attributes. It defines terminology like relation, attribute, domain, tuple, cardinality, and keys. It explains how to identify candidate keys, primary keys, and foreign keys. The goals are to understand how relational databases are structured and related through keys. Representing data as relational tables allows flexible querying and integrity constraints.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 50

Relational Model

LECTURE - 3

Dr. Said Mirza Pahlevi, M.Eng.


Objectives
2

Terminology of relational model.

How tables are used to represent data.

Properties of database relations.

How to identify CK, PK, and FKs.

Meaning of entity integrity and referential integrity.

Purpose and advantages of views.

Dr. Said Mirza Pahlevi, M.Eng. Sekolah Tinggi Ilmu Statistik (STIS)
Relational Model Terminology
3

A relation  a table with columns and rows.


 Only applies to logical structure of the database, not the physical
structure.
 Hence, the external and conceptual levels.

Attribute  a named column of a relation.


 Used to hold information about objects to be represented in the
database.

Domain  the set of allowable values for one or more


attributes.
 Every attribute in a relation is defined on a domain.
 May be distinct for each attribute, or two or more attributes may
be defined on the same domain.
Dr. Said Mirza Pahlevi, M.Eng. Sekolah Tinggi Ilmu Statistik (STIS)
Relational Model Terminology
4

Tuple  a row of a relation.


 Can appear in any order & the relation is still the same

 It is called as extension/state of a relation

Intension  relation structure with domain and


any other restrictions on possible values

Degree  number of attributes in a relation


 Unary relation: a relation with 1 attribute
 Binary relation: a relation with 2 attributes

 Ternary relation: a relation with 3 attributes

 N-ary relation: a relation with more than 3 attributs

Dr. Said Mirza Pahlevi, M.Eng. Sekolah Tinggi Ilmu Statistik (STIS)
Relational Model Terminology
5

Cardinality is the number of tuples in a


relation.

Relational Database is a collection of


normalized relations with distinct relation
names.

Dr. Said Mirza Pahlevi, M.Eng. Sekolah Tinggi Ilmu Statistik (STIS)
Instances of Branch & Staff Relations
6

Dr. Said Mirza Pahlevi, M.Eng. Sekolah Tinggi Ilmu Statistik (STIS)
Examples of Attribute Domains
7

Dr. Said Mirza Pahlevi, M.Eng. Sekolah Tinggi Ilmu Statistik (STIS)
Alternative Terminologies
8

Formal terms Alternative 1 Alternative 2


Relation Table File
Tuple Row Record
Attribute Column Field

Dr. Said Mirza Pahlevi, M.Eng. Sekolah Tinggi Ilmu Statistik (STIS)
Mathematical Definition of Relation

Consider two sets, D1 & D2,


 Where D1 = {2, 4} and D2 = {1, 3, 5}.

Cartesian product, D1 × D2, is set of all


ordered pairs, where first element is member
of D1 and second element is member of D2.
 D1 × D2={(2, 1), (2, 3), (2, 5), (4, 1), (4, 3), (4, 5)}

Alternative way is to find all combinations of


elements with first from D1 & second from D2.
9
Dr. Said Mirza Pahlevi, M.Eng.
Mathematical Definition of Relation
10

Any subset of Cartesian product is a relation


 e.g., R = {(2, 1), (4, 1)}

May specify which pairs are in relation using


some condition for selection
 Second element is 1:
 R = {(x, y) | xD1, yD2, and y = 1}
 First element is always twice the second:
 S = {(x, y) | xD1, yD2, and x = 2y}

Dr. Said Mirza Pahlevi, M.Eng. Sekolah Tinggi Ilmu Statistik (STIS)
Mathematical Definition of Relation
11

Consider three sets D1, D2, D3 with Cartesian


Product D1 × D2 × D3
 D1={1, 3}, D2={2, 4}, D3={5, 6}
 D1 × D2 × D3 = {(1,2,5), (1,2,6), (1,4,5), (1,4,6),
(3,2,5), (3,2,6), (3,4,5), (3,4,6)}

Any subset of these ordered triples is a


relation.

Dr. Said Mirza Pahlevi, M.Eng. Sekolah Tinggi Ilmu Statistik (STIS)
Mathematical Definition of Relation
12

Cartesian product of n sets (D1, D2, . . ., Dn)


 D1 × D2 × . . . × Dn = {(d1, d2, . . . , dn) | d1D1,
d2D2, . . . , dnDn}
Usually written as:
n
X
Di
i =1
Any set of n-tuples from this Cartesian
product is a relation on the n sets.
To define the relations we have to specify sets
or domains
Dr. Said Mirza Pahlevi, M.Eng. Sekolah Tinggi Ilmu Statistik (STIS)
Relation Schema
13

Relation schema

 Named relation  a set of attribute & domain name pairs

 Let A1, A2, . . ., An be attributes with domains D1,


D2, . . ., Dn then set {A1:D1, A2:D2, . . ., An:Dn} is a
relation schema.

Dr. Said Mirza Pahlevi, M.Eng. Sekolah Tinggi Ilmu Statistik (STIS)
Relation
14

Relation R defined by relation schema S is

 a set of mappings from the attribute names to their


corresponding domains; It is a set of n-tuples:

 (A1:d1, A2:d2, . . ., An:dn) s.t. d1D1, d2D2, . . . , dnDn.

 Table notation  attribute names as column headings and


tuples as rows with form (d1, d2, …, dn), where diDi.

 Relation is any subset of Cartesian product of attribute


domains.
Dr. Said Mirza Pahlevi, M.Eng. Sekolah Tinggi Ilmu Statistik (STIS)
Relation Schema and Relation:Example
15

Relation Schema
 {branchNo:D1, street:D2, city:D3, postcode:D4}

Relation/relation instance
 {branchNo:B005, street:22 Deer Rd, city:London, postcode:SW1 4EH}
 {branchNo:B007, street:16 Argyll St, city:Aberdeen, postcode:AB2 3SU}

Dr. Said Mirza Pahlevi, M.Eng. Sekolah Tinggi Ilmu Statistik (STIS)
Database Relations
16

Relational database schema (relational schema)

 Set of relation schemas, each with a distinct name.

 If R1, R2, …, Rn are a set of relation schemas then


relational schema is R = {R1, R2, …, Rn}

Dr. Said Mirza Pahlevi, M.Eng. Sekolah Tinggi Ilmu Statistik (STIS)
Properties of Relations
17

Relation name is distinct from all other


relation names in relational schema.

Each cell of relation contains exactly one


atomic (single) value.

Each attribute has a distinct name.

Values of an attribute are all from the same


domain.
Dr. Said Mirza Pahlevi, M.Eng. Sekolah Tinggi Ilmu Statistik (STIS)
Properties of Relations
18

Each tuple is distinct; there are no duplicate


tuples.

Order of attributes has no significance.

Order of tuples has no significance, theoretically.

Dr. Said Mirza Pahlevi, M.Eng. Sekolah Tinggi Ilmu Statistik (STIS)
Relational Keys
19

Superkey
 An attribute, or set of attributes, that uniquely
identifies a tuple within a relation.

Candidate Key
 Superkey (K) such that no proper subset is a superkey
within the relation.
 In each tuple of R, values of K uniquely identify that
tuple (uniqueness).
 No proper subset of K has the uniqueness property
(irreducibility).

Dr. Said Mirza Pahlevi, M.Eng. Sekolah Tinggi Ilmu Statistik (STIS)
Relational Keys
20
Branch
Candidate keys
 Branch  branchNo,
postcode

Notes
 Relation instance Staff
cannot be used to
prove a candidate key
 Candidate key
identification
 Know the real world
meaning of attributes

Dr. Said Mirza Pahlevi, M.Eng. Sekolah Tinggi Ilmu Statistik (STIS)
Relational Keys
21

Primary Key
 Candidate key selected to identify tuples uniquely within
relation.

Alternate Keys
 Candidate keys that are not selected to be primary key.

Foreign Key
 Attribute, or set of attributes, within one relation that
matches candidate key of some (possibly same) relation.

Dr. Said Mirza Pahlevi, M.Eng. Sekolah Tinggi Ilmu Statistik (STIS)
Representing Relational DB Schemas
22

Give relation name followed by the attribute


names in parantheses; Normaly the primary
key is underlined

 The conceptual model or conceptual schema is the all


of such schemas for the database.

Example:
 Branch(branchNo, street, city, postcode)
 Staff(staffNo, fName, lName, position, sex, DOB,
salary, branchNo)
Dr. Said Mirza Pahlevi, M.Eng. Sekolah Tinggi Ilmu Statistik (STIS)
Integrity Constraints
23

Null

 Represents value for an attribute that is currently


unknown or not applicable for tuple.

 Deals with incomplete or exceptional data.

 Represents the absence of a value and is not the same


as zero or spaces, which are values.

Dr. Said Mirza Pahlevi, M.Eng. Sekolah Tinggi Ilmu Statistik (STIS)
Integrity Constraints
24

Entity Integrity
 In a base relation, no attribute of a primary key can be
null.
 Base relation corresponds to an entity in a conceptual
schema.

Referential Integrity
 If foreign key exists in a relation, either foreign key
value must match a candidate key value of some tuple
in its home relation or foreign key value must be
wholly null.

Dr. Said Mirza Pahlevi, M.Eng. Sekolah Tinggi Ilmu Statistik (STIS)
Integrity Constraints
25

General Constraints

 Additional rules specified by users or database


administrators that define or constrain some aspect of
the enterprise.

 e.g., setting upper limit of staffs work in a branch

Dr. Said Mirza Pahlevi, M.Eng. Sekolah Tinggi Ilmu Statistik (STIS)
Views
26

Base Relation
 Named relation corresponding to an entity in
conceptual schema, whose tuples are physically stored
in database.
View
 Dynamic result of one or more relational operations
operating on base relations to produce another relation.

Dr. Said Mirza Pahlevi, M.Eng. Sekolah Tinggi Ilmu Statistik (STIS)
Views
27

A virtual relation that does not necessarily


actually exist in the database but is produced
upon request, at time of request.

Contents of a view are defined as a query on


one or more base relations.

Views are dynamic, meaning that changes


made to base relations that affect view
attributes are immediately reflected in the view.

Dr. Said Mirza Pahlevi, M.Eng. Sekolah Tinggi Ilmu Statistik (STIS)
Purpose of Views
28

Provides powerful and flexible security


mechanism by hiding parts of database from
certain users.

Permits users to access data in a customized


way, so that same data can be seen by different
users in different ways, at same time.

Can simplify complex operations on base


relations.

Dr. Said Mirza Pahlevi, M.Eng. Sekolah Tinggi Ilmu Statistik (STIS)
Kuis-1
29

Buat relational schema dan contoh instance


dari masing-masing relation. Tentukan
primary key, candidate key dan foreign key-
nya. Dan berikan contoh view yang dapat
dibuat dari relational schema tersebut

 Rumah sakit

 Department store

 Universitas
Dr. Said Mirza Pahlevi, M.Eng. Sekolah Tinggi Ilmu Statistik (STIS)
Kuis-2
30

Buat relational schema dan contoh instance


dari masing-masing relation. Tentukan
primary key, candidate key dan foreign key-
nya. Dan berikan contoh view yang dapat
dibuat dari relational schema tersebut

 Restoran

 Minimarket

 Perpustakaan
Dr. Said Mirza Pahlevi, M.Eng. Sekolah Tinggi Ilmu Statistik (STIS)
Relational Calculus
31

Dr. Said Mirza Pahlevi, M.Eng. Sekolah Tinggi Ilmu Statistik (STIS)
Relational Calculus
32

Relational calculus query specifies what is to be


retrieved rather than how to retrieve it.
 No description of how to evaluate a query.
 Not related to differential & integral calculus in math but
comes from predicate calculus.

In first-order logic (or predicate calculus), predicate


is a truth-valued function with arguments.

When we substitute values for the arguments,


function yields an expression, called a proposition,
which can be either true or false.
Dr. Said Mirza Pahlevi, M.Eng. Sekolah Tinggi Ilmu Statistik (STIS)
Relational Calculus
33

If predicate contains a variable (e.g. ‘x is a


member of staff’), there must be a range for x.

When we substitute some values of this range


for x, proposition may be true; for other
values, it may be false.

When applied to databases, relational calculus


has forms: tuple & domain relational calculus.

Dr. Said Mirza Pahlevi, M.Eng. Sekolah Tinggi Ilmu Statistik (STIS)
Tuple Relational Calculus
34

Interested in finding tuples for which a predicate is


true. Based on use of tuple variables.

Tuple variable is a variable that ‘ranges over’ a named


relation: i.e., variable whose only permitted values are
tuples of the relation.

Specify range of tuple variable S as Staff relation as:


 Staff(S)

To find set of all tuples S s.t. predicate P(S) is true:


 {S | P(S)}

Dr. Said Mirza Pahlevi, M.Eng. Sekolah Tinggi Ilmu Statistik (STIS)
Tuple Relational Calculus - Example
35

To find details of all staff earning more than


£10,000:
 {S | Staff(S)  S.salary > 10000}

To find a particular attribute, such as salary,


write:
 {S.salary | Staff(S)  S.salary > 10000}

Dr. Said Mirza Pahlevi, M.Eng. Sekolah Tinggi Ilmu Statistik (STIS)
Tuple Relational Calculus
36

Can use two quantifiers (pengukur) to tell how


many instances the predicate applies to:

 Existential quantifier  (‘there exists’)  an iterated OR


 If V range over R whose tuples are {t1, t2, …, tn} then
 V (p(V)) means: false OR p(t1) OR … OR p(tn)

 Universal quantifier  (‘for all’)  an iterated AND


 If V range over R whose tuples are {t1, t2, …, tn}
  V (p(V)) means: true AND p(t1) AND… AND p(tn)

Dr. Said Mirza Pahlevi, M.Eng. Sekolah Tinggi Ilmu Statistik (STIS)
Tuple Relational Calculus
37

Tuple variables qualified by  or  are called


bound variables, otherwise called free
variables.

Dr. Said Mirza Pahlevi, M.Eng. Sekolah Tinggi Ilmu Statistik (STIS)
Tuple Relational Calculus
38

Existential quantifier used in formulae that must be true for at


least one instance, e.g.,

 Staff(S)  (B) ( Branch(B)  (B.branchNo = S.branchNo)  B.city = ‘London’


)

 Means: there exists a Branch tuple with same branchNo as the branchNo of
the current Staff tuple, S, and is located in London’

Branch Staff

Dr. Said Mirza Pahlevi, M.Eng. Sekolah Tinggi Ilmu Statistik (STIS)
Tuple Relational Calculus
39

Universal quantifier is used in statements


about every instance, such as:
 (B) (B.city  ‘Paris’)

 Means: for all Branch tuples, the address is not in


Paris.

Can also use ~(B) (B.city = ‘Paris’) which


means there are no branches with an address
in Paris

Dr. Said Mirza Pahlevi, M.Eng. Sekolah Tinggi Ilmu Statistik (STIS)
Tuple Relational Calculus
40

Formulae should be unambiguous & make sense.

A (well-formed) formula is made out of atoms:


 R(Si ), where Si is a tuple variable and R is a relation
 Si.a1 q Sj.a2 where q = {<, ≤, ≥, >, =, ≠}
 Si.a1 q c where q = {<, ≤, ≥, >, =, ≠}

Can recursively build up formulae from atoms:


 An atom is a formula.
 If F1 and F2 are formulae, so are their conjunction, F1 Ù F2; disjunction,
F1 Ú F2; and negation, ~F1.
 If F is a formula with free variable X, then ($X)(F) and ("X)(F) are also
formulae.
Dr. Said Mirza Pahlevi, M.Eng. Sekolah Tinggi Ilmu Statistik (STIS)
Example - Tuple Relational Calculus
41

List the names of all managers who earn more


than £25,000.

{S.fName, S.lName | Staff(S) 


S.position = ‘Manager’  S.salary > 25000}

List the staff who manage properties for rent in


Glasgow.

{S | Staff(S)  ($P) ( PropertyForRent(P)  (P.staffNo =


S.staffNo) Ù P.city = ‘Glasgow’ )}

Dr. Said Mirza Pahlevi, M.Eng. Sekolah Tinggi Ilmu Statistik (STIS)
Example - Tuple Relational Calculus
42

 List the names of staff who currently do not


manage any properties.

{S.fName, S.lName | Staff(S)  (~($P)


PropertyForRent(PropertyForRent(P)(S.staffNo
Staff
= P.staffNo)))}
prope street city post rent owner staff branch
rtyNo code No No No

PA14 16 Holdead Aberden AB7 650 CO46 SA9 B007


5SU

PL94 6 Agryll St London NW2 400 CO87 SL41 B005

PG4 6 Lawrence Glasgow G11 350 CO40 B003


ST 9QX

PG36 2 Manor Rd Glasgow G32 375 CO93 SG37 B003


4QX

PG21 18 Dale Rd Glasgow G12 600 CO87 SG37 B003

PG16 5 Novar Dr Glasgow G12 450 CO93 SG14 B003


9AX

Dr. Said Mirza Pahlevi, M.Eng. Sekolah Tinggi Ilmu Statistik (STIS)
Example - Tuple Relational Calculus
43

List the names of clients who have viewed a


property for rent in Glasgow.

{C.fName, C.lName | Client(C) Ù ( ($V)($P)


( Viewing(V) Ù PropertyForRent(P) Ù
(C.clientNo = V.clientNo) Ù
(V.propertyNo=P.propertyNo) Ù
P.city =‘Glasgow’ ) )}

Dr. Said Mirza Pahlevi, M.Eng. Sekolah Tinggi Ilmu Statistik (STIS)
Latihan
44

List all the cities where there is both a branch


office and at least one property for rent!

PropertyForRent Branch
prope street city post rent owner staff branch branch street city postcode
rtyNo code No No No No

PA14 16 Holdead Aberden AB7 650 CO46 SA9 B007 B005 22 Deer London SW1 4EH
5SU Rd

PL94 6 Agryll St London NW2 400 CO87 SL41 B005 B007 16 Agryll Aberdeen AB2 3SU
St
PG4 6 Lawrence Glasgow G11 350 CO40 B003
B003 163 Glasgow G11 9QX
ST 9QX
Main ST
PG36 2 Manor Rd Glasgow G32 375 CO93 SG37 B003
B004 32 Bristol BS99 iNZ
4QX
Manse
PG21 18 Dale Rd Glasgow G12 600 CO87 SG37 B003 Rd

B002 56 Clove Glasgow NW10 6EU


PG16 5 Novar Dr Glasgow G12 450 CO93 SG14 B003 Dr
9AX

Dr. Said Mirza Pahlevi, M.Eng. Sekolah Tinggi Ilmu Statistik (STIS)
Jawaban
45

{B.city | Branch(B)  ( (P) (PropertyForRent(P)  B.city =


P.city) )}

PropertyForRent Branch
prope street city post rent owner staff branch branch street city postcode
rtyNo code No No No No

PA14 16 Holdead Aberden AB7 650 CO46 SA9 B007 B005 22 Deer London SW1 4EH
5SU Rd

PL94 6 Agryll St London NW2 400 CO87 SL41 B005 B007 16 Agryll Aberdeen AB2 3SU
St
PG4 6 Lawrence Glasgow G11 350 CO40 B003
B003 163 Glasgow G11 9QX
ST 9QX
Main ST
PG36 2 Manor Rd Glasgow G32 375 CO93 SG37 B003
B004 32 Bristol BS99 iNZ
4QX
Manse
PG21 18 Dale Rd Glasgow G12 600 CO87 SG37 B003 Rd

B002 56 Clove Glasgow NW10 6EU


PG16 5 Novar Dr Glasgow G12 450 CO93 SG14 B003 Dr
9AX

Dr. Said Mirza Pahlevi, M.Eng. Sekolah Tinggi Ilmu Statistik (STIS)
Domain Relational Calculus
46

Uses variables that take values from domains


instead of tuples of relations.

If F(d1, d2, . . . , dn) stands for a formula


composed of atoms and d1, d2, . . . , dn
represent domain variables, then:

 {d1, d2, . . . , dn | F(d1, d2, . . . , dn)}

 is a general domain relational calculus expression.

Dr. Said Mirza Pahlevi, M.Eng. Sekolah Tinggi Ilmu Statistik (STIS)
Example - Domain Relational Calculus
47

Find the names of all managers who earn more than


£25,000

{fN, lN | ($posn, sal)


(Staff (sN, fN, lN, posn, sex, DOB, sal, bN) 
posn = ‘Manager’  sal > 25000)}

Staff (sN, … , bN) ensures that domain variables are


restricted to be attributes of the same tuple
 posn=‘Manager’ is used instead of Staff.position =‘Manager’

($posn, sal) means ($posn), ($sal)

Dr. Said Mirza Pahlevi, M.Eng. Sekolah Tinggi Ilmu Statistik (STIS)
Example - Domain Relational Calculus
48

 List the staff who manage properties for


rent in Glasgow.

{sN, fN, lN, posn, sex, DOB, sal, bN |


($sN1,cty)(Staff(sN,fN,lN,posn,sex,DOB,sal,bN) 
PropertyForRent(pN, st, cty, pc, typ, rms, rnt, oN, sN1, bN1) Ù
 
(sN=sN1) Ù cty=‘Glasgow’)}
Staff

Dr. Said Mirza Pahlevi, M.Eng. Sekolah Tinggi Ilmu Statistik (STIS)
Example - Domain Relational Calculus
49

 List the names of staff who currently do


not manage any properties for rent.

{fN, lN | ($sN)
(Staff(sN,fN,lN,posn,sex,DOB,sal,bN) 
(~($sN1) (PropertyForRent(pN, st, cty, pc, typ,
rms, rnt, oN, sN1, bN1) Ù (sN=sN1))))}

Dr. Said Mirza Pahlevi, M.Eng. Sekolah Tinggi Ilmu Statistik (STIS)
Example - Domain Relational Calculus
50

 List the names of clients who have


viewed a property for rent in Glasgow.

{fN, lN | ($cN, cN1, pN, pN1, cty)


(Client(cN, fN, lN,tel, pT, mR) 
Viewing(cN1, pN1, dt, cmt) 
PropertyForRent(pN, st, cty, pc, typ,
rms, rnt,oN, sN, bN) Ù
(cN = cN1) Ù (pN = pN1) Ù cty = ‘Glasgow’)}

Dr. Said Mirza Pahlevi, M.Eng. Sekolah Tinggi Ilmu Statistik (STIS)

You might also like