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

Chapter (4) Relational

This document discusses key concepts of the relational database model, including: - Relations are represented as tables with rows and columns. - Each relation has a schema defining its attributes and their domains. - Keys like the primary key and foreign keys uniquely identify rows and define relationships between relations. - Integrity constraints like entity integrity and referential integrity enforce data accuracy.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Chapter (4) Relational

This document discusses key concepts of the relational database model, including: - Relations are represented as tables with rows and columns. - Each relation has a schema defining its attributes and their domains. - Keys like the primary key and foreign keys uniquely identify rows and define relationships between relations. - Integrity constraints like entity integrity and referential integrity enforce data accuracy.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 36

Chapter 4

The Relational Model

Ahmed M. Zeki

ITIS 216
ITBIS 385
Objectives
 Terminology of relational model
 How tables are used to represent data
 Connection between mathematical relations
and relations in the relational model
 Properties of database relations
 How to identify CK, PK, and FKs
 Meaning of entity integrity and referential
integrity
 Purpose and advantages of views

2 / 36
Relational Model Terminology
 RDBMS

 The dominant data processing software in


use today

 Sales: US$6-10 billion per year

 Based on the relational data model proposed


by Codd 1970

3 / 36
Relational Model Terminology
 First generation of DBMS (the network and
hierarchical DBMS)

 Objective of RDBMS:
◦ To allow a high degree of data independence
◦ To provide substantial grounds of dealing with data
semantic, consistency, and redundancy problems

 Several hundreds RDBMSs

4 / 36
Relational Model Terminology
 Some extensions to the relational model:
◦ Capture more closely the meaning of data
◦ Support OO concepts
◦ Support deductive capabilities

 A relation (table or file) is a table with


columns (attributes or fields) and rows
(tuples or records)

 Only applies to logical structure of the


database, not the physical structure
5 / 36
Relational Model Terminology

6 / 36
Relational Model Terminology
 Attributes can appear in any order

 Domain is the set of allowable values for one


or more attributes

7 / 36
Relational Model Terminology
 The structure of the table together with a
specification of the domains and any other
restrictions on possible values is called its
intension, which is usually fixed unless the
meaning of a relation is changed to include
additional attributes.

 The tuples are called the extension (or state)


of a relation, which changes over time.

8 / 36
Relational Model Terminology
 Degree is the number of attributes in a
relation.
◦ Unary
◦ Binary
◦ Ternary
◦ n-ary

 The degree is a property of the intension of


the relation

 Cardinality is the number of tuples in a


relation. It is a property of the extension of
the relation.
9 / 36
Mathematical Definition of Relation
 Consider two sets, D1 & D2, where D1 = {2, 4} and D2
= {1, 3, 5}.
 Cartesian product, D ´ D , is set of all ordered pairs,
1 2
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 and second from D2.

10 / 36
Mathematical Definition of Relation
 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; e.g.
◦ 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}

11 / 36
Mathematical Definition of Relation
 Consider three sets D1, D2, D3 with Cartesian Product
D1 ´ D2 ´ D3; e.g.

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.

12 / 36
Mathematical Definition of Relation
 Cartesian product of n sets (D1, D2, . . ., Dn) is:

D1 ´ D2 ´ . . . ´ Dn = {(d1, d2, . . . , dn) | d1 ÎD1, d2 ÎD2, . . . ,


dnÎDn}
usually written as:
n
XDi
i=1

 Any set of n-tuples from this Cartesian product is a


relation on the n sets.

13 / 36
Relation Schema
 Relational Database is a collection of
normalized (appropriate structure) relations
with distinct relation names.

 Relation Schema: A named relation defined


by a set of attribute and domain name pairs.
◦ Let A1, A2, …, An be attributes with domains D1,
D2, ..., Dn. Then the set {A1:D1, A2:D2, …, An:Dn}
is a relation schema.

14 / 36
Relation Schema
◦ A relation R defined by a relation schema S is a set
of mapping from the attribute names to their
corresponding domains.

◦ R = (A1:d1, A2:d2, …, An:dn) such that d1  D1, d2  D2, …, dn  Dn


◦ Hence each element consists of an attribute and a
value for that attribute.
◦ A relation is a subset of the Cartesian product of
the domains of the attributes
◦ {(B005, 22 Deer Rd, London, SW1 4EH)}
◦ Or: {(branchNo: B005, street: 22 Deer Rd, city:
London, postcode: SW1 4EH)}, this is called a
relation instance.
15 / 36
Relation Schema
 Relational DB Schema: a set of relation
schemas, each with a distinct name.
 If R , R , …, R are a set of relation schemas,
1 2 n
then we can write the relational schema R as:
 R = { R , R , …, R }
1 2 n

16 / 36
Properties of Relations
 Relation name is distinct from all other relation
names in relational schema
 Each cell of relation contains exactly one value
 Each attribute has a distinct name
 Values of an attribute are all from same domain
 Each tuple is distinct; there are no duplicate

tuples
 Order of attributes has no significance
 Order of tuples has no significance,

theoretically

17 / 36
Properties of Relations
 Relations do not contain repeating groups.

 A relation that satisfy this property is said to


be normalized or in first normal form (1NF).

 E.g It is illegal to store two postcodes for a


single branch.

18 / 36
Relational Keys
 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).
◦ There may be several keys for a relation. When a
key consists of more than one attribute, we call it
a composite key.

19 / 36
Relational Keys
 Primary Key: Candidate key selected to identify
tuples uniquely within the relation.
◦ A relation always has a primary key.

 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.
◦ When an attribute appears in more than one
relation, its appearance usually represents a
relationship between tuples of the two relations.

20 / 36
Relational Keys  Eg.
◦ We say that the
attribute
branchNo in the
Staff relation
targets the
primary key
attribute
branchNo in the
home relation
Brach. These
common
attributes play an
important role in
performing data
manipulation. 21 / 36
Representing Relational Schema
 Give the name of the relation followed by the
attribute names in parentheses. The primary
key is underlined
 Branch

◦ (branchNo, street, city, postcode)


 Staff
◦ (staffNo, fName, IName, position, sex, DOB, salary,
branchNo)
 PropertyForRent
◦ (propertyNo, street, city, postcode, type, rooms,
rent, ownerNo, staffNo, branchNo)

22 / 36
Representing Relational Schema
 Client
◦ (clientNo, fName, Iname, telNo, prefType, maxRent)
 PrivateOwner
◦ (ownerNo, fName, Iname, address, comment)
 Viewing
◦ (clientNo, propertyNo, viewData, comment)
 Registration
◦ (clientNo, branchNo, staffNo, dataJoined)

23 / 36
Integrity Constraints
 A data model has two other parts:
◦ A manipulative part: defining the types of
operation that are allowed on the data
◦ A set of integrity constraints: ensure that the data
is accurate
 Since every attribute has an associated
domain, there are constraints (called domain
constraints) that form restrictions on the set
of values allowed for the attributes of
relations.

24 / 36
Integrity Constraints
 In addition, there are two important integrity
rules:
◦ Constraints or restrictions that apply to all
instances of the DB.
 The two principal rules for the relational
model are known as:
◦ entity integrity
◦ referential integrity
 Other types of integrity constraint are:
◦ multiplicity
◦ general constraints

25 / 36
Integrity Constraints
 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.

26 / 36
Integrity Constraints
 Entity Integrity
◦ In a base relation, no attribute of a primary key can
be null.

 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.

27 / 36
Integrity Constraints
 General Constraints
◦ Additional rules specified by users or database
administrators that define or constrain some aspect
of the enterprise.
◦ Eg. Upper limit is 20

28 / 36
Views
 View is a virtual relation that does not
necessarily exist in the DB but is produced
upon request, at time of request.

 Contents of a view are defined as a query on


one or more base relations.

 Base Relation: A named relation


corresponding to an entity in the conceptual
schema, whose tuples are physically stored in
the DB.

29 / 36
Views
 Views are dynamic, meaning that changes
made to base relations that affect view
attributes are immediately reflected in the
view.

 An external model can consist of both base


(conceptual-level) relations and views derived
from the base.

 Provides powerful and flexible security


mechanism by hiding parts of database from
certain users.
30 / 36
Views
 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.

 A view should be designed to support the


external model that the user finds familier.

31 / 36
Views
 Examples:
◦ Some members of staff should see Staff tuples
without the salary attributes.
◦ Attributes may be renamed, or the order of
attributes changed.
◦ Some members of staff should see only property
records for those properties that they mange.

32 / 36
Views
 Views provides logical data independence

◦ Ex: if a new attribute is added to a relation, existing


users can be unaware of its existence if their views
are defined to exclude it.

◦ If an existing relation is rearranged or split up, a


view may be defined so that users can continue to
see their original views.

33 / 36
Views
 All updates to a base relation should be
immediately reflected in all views that
reference that base relation.

 If view is updated, underlying base relation


should reflect change.

34 / 36
Views
 There are restrictions on types of
modifications that can be made through
views:
◦ Updates are allowed if query involves a single
base relation and contains a candidate key of
base relation.
◦ Updates are not allowed involving multiple
base relations.
◦ Updates are not allowed involving
aggregation or grouping operations.

35 / 36
Views
 Classes of views are defined as:
◦ theoretically not updateable;
◦ theoretically updateable;
◦ partially updateable.

36 / 36

You might also like