0% found this document useful (0 votes)
4 views38 pages

Se DB CH2

Chapter 2 discusses the relational data model, which organizes data into inter-related tables called relations, consisting of rows (tuples) and columns (attributes). It covers key concepts such as relational constraints, schema, keys, and integrity constraints, emphasizing the importance of unique identifiers for data integrity. The chapter also highlights the role of relational database management systems (RDBMS) and the use of SQL for defining database schemas.

Uploaded by

miki
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)
4 views38 pages

Se DB CH2

Chapter 2 discusses the relational data model, which organizes data into inter-related tables called relations, consisting of rows (tuples) and columns (attributes). It covers key concepts such as relational constraints, schema, keys, and integrity constraints, emphasizing the importance of unique identifiers for data integrity. The chapter also highlights the role of relational database management systems (RDBMS) and the use of SQL for defining database schemas.

Uploaded by

miki
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/ 38

Chapter-2

Relational Data Model

Prepared by: Marta G. (MSc.)


Contents
 Relational Model Concepts
 Relational Constraints & Relational Database Schema

 Relational Operations

 Relational Languages and Views

 Relational DBMS
Relational Model Concepts
❑ The relational model for database management is an approach to
logically represent and manage the data stored in a database.
❑ A database implemented and organized in terms of the relational model
is known as a relational database management system (RDBMS).
❑ In this model, the data is organized into a collection of two-
dimensional inter-related tables, also known as relations.
❑ Each relation is a collection of columns and rows, where the column
represents the attributes of an entity and the rows (or tuples)
represents the records.
Terminologies
• Relation: Two-dimensional table used to store a collection of data
elements.
• Tuple: Row of the relation, depicting a real-world entity.
• Attribute/Field: Column of the relation, depicting properties that
define the relation.
• Domain: Set of pre-defined atomic values that an attribute can take
i.e., it describes the legal values that an attribute can take.
• Eg. : Name : string of characters that represent name of persons.
Employee- age: possible ages of a company (20 – 70 years old)
• It also specifies the data type e.g. age must be positive integers
Cont…
 Relational Schema:
◦ It is the logical blueprint of the relation i.e., it describes the design
and the structure of the relation.
◦ Made up of a relation name R and a list of attributes A1, A2, .., An
◦ It contains the table name, its attributes, and their data types (optional):
 TABLE_NAME(ATTRIBUTE_1:TYPE_1,ATTRIBUTE_2TYPE_2, ...)
◦ For Student relation example, the relational schema will be:
 STUDENT(ROLL_NUMBER: INTEGER, NAME: string(20), CGPA: FLOAT)
 Degree/ arity : It is the total number of attributes present in the
relation schema. For the above example the degree of the relation is 3.
Cont…
 Cardinality : It specifies the number of entities involved in the
relation i.e., it is the total number of rows present in the relation.
 Relational Instance/ relation state: It is the collection of records
present in the relation at a given time (set of tuples at a given time) .
 Relation Key : It is an attribute or a group of attributes
that can be used to uniquely identify an entity in a table or
to determine the relationship between two tables. Relation
keys can be of 6 different types:
◦ Candidate Key, Super Key, Composite Key, Primary Key,
Alternate Key and Foreign Key
Note:
1. A Relation is a collection of rows (tuples) and columns
(attributes).
2. In a relation, the tuples depicts real-world entity, while the
attributes are the properties that define the relation.
3. Structure of the relation is described by the relational
schema.
4. Relational keys are used to uniquely identify a row in a
table or to determine the relationship between two tables.
Cont…
✓ Example: Domains, Attributes, Tuples, and Relations (all in one)
Characteristics of Relations:
 Ordering of Tuples within Relation is not important
◦ A relation is defined as a set of tuples.
◦ Mathematically, elements of a set have no order among them,
hence, tuples in a relation do not need to have any particular
order.
◦ Duplication is not allowed, that is why we use keys while
constructing relations.
◦ In other words, a relation is not sensitive to the ordering of tuples.
 Ordering of values within a tuple is important
Cont…
 Values & NULLs in the Tuples
◦ Each value in a tuple is an atomic value so, composite (e.g. Address)
and multivalued attributes are not allowed.
◦ This model is sometimes called the flat relational model.
◦ Much of the theory behind the relational model was developed with this
assumption in mind, which is called the first normal form assumption.
◦ Hence, multivalued attributes must be represented by separate
relations, and composite attributes are represented only by their simple
component attributes in the basic relational model.
◦ An important concept is that of NULL values which represents the
values of attributes that may be unknown or may not apply to a tuple.
◦ A special value called NULL is used in these cases.
Cont…
 Interpretation of a Relation
◦ The relation schema can be interpreted as a declaration
or a type of assertion.
 E.g; The schema of the STUDENT relation of (slide 8) asserts
that, in general, a student entity has aName, Ssn, Home-phone,
Address, Office-phone, Age, and Gpa.
◦ Each tuple in the relation can then be interpreted as a fact
or a particular instance of the assertion.
 E.g; The first tuple in (slide 8) asserts the fact that there is a
STUDENT whose name is Benjamin, Ssn is 305-61-2435, Age
is 19 and so on...
Cont…
 Notice that some relations may represent facts about
entities, whereas other relations may represent facts
about different relationships.
 e.g; A relation schema MAJORS (student-ssn,
department-code) asserts that students major in academic
disciplines (student & department relations ).
 A tuple in this relation relates a student to his or her
major discipline/field of study.
Relational Model Notation
▪ We can use the following sample notations for our operations:

▪ A relation schema R of degree n is denoted by R(A1, A2,.., An).

▪ The name of a relation schema such as STUDENT indicates the current set of

tuples in that (the student) relation.

▪ An attribute A can be qualified with the relation name R to which it belongs

by using the dot notation R.A.

▪ For example: STUDENT.Name, TEACHER.Age.

▪ This is because the same name may be used for two attributes in different

relations.
Cont…
 However, all attribute names in a particular relation must be
distinct.
 An n-tuple t in a relation r(R) is denoted by t = [v1, v2,...
vn], where vi is the value corresponding to attribute Ai.
◦ E,g; Consider tuple t = [’Barbara Benson’,‘533-69-1238’,
‘(817)839-8461’, ‘7384 Fontana Lane’, NULL, 19, 3.21 ]
◦ From the STUDENT relation in the example; we have: t[Name] =
[‘Barbara Benson’], and t[Ssn, Gpa, Age] = [‘533-69-1238’,3.21,
19].
Relational Model Constraints & Relational
Database Schemas
✓ In a relational database, there will typically be many relations, and
the tuples in those relations are usually related in various ways.
✓ The state of the whole database will correspond to the states of all
its relations at a particular point in time.
✓ There are generally many restrictions or constraints on the actual
values in a database state.
✓ Constraints are a restriction or limitation of data in the database
Con’t…
 Constraints on databases generally have three main categories:
1. Inherent model-based constraints or implicit constraints:
Constraints that are inherent in the data model
Example → the constraint that a relation cannot have
duplicate tuples is an inherent constraint.
2. Application-based /semantic constraints or business:
Constraints that cannot be directly expressed in the schemas of
the data model, and hence must be expressed and enforced by
the application programs some other way. How good the
relational database is?
Cont…
3. Schema-based constraints or explicit constraints:
Constraints that can be directly expressed in the schemas of
the data model, typically by specifying them in the DDL.
Eg.: Age of an employee has to be between 20 -45 years
so age is restricted. It directly related to relational schema
Cont…

 Schema-based constraints
1.Domain constraints,
2.Key constraints and constraints on NULLs,
3.Entity integrity constraints, and referential integrity
constraints
Cont…
 Domain Constraints:
◦ Domain constraints specify that within each tuple, the value of each
attribute has to be an atomic value/ indivisible and must have single
attribute name.
◦ This constraint checks the datatype of the values of each attribute
Key Constraints and Constraints on NULL Values
Key constraints
 An attribute that can uniquely identify each tuple in a
relation is called a key.
 A superkey specifies that no two tuples can have the same
value.
 It is any set of attributes that helps to identify each tuple
uniqely.
 Every relation has at least one superkey→set of all attribute.
Student table
RollNo Name Age Email
1 Yeabsira 18 [email protected]
2 Samuel 18 [email protected]
3 Haleta 20 [email protected]

SK = {RollNO}, {Email}, {RollNO, Name}, {RollNO,Age},


{RollNO, Email}, {Name, Email}, {Age, Email},
{RollNo, Name, Age, Email}
Cont…
 A key must satisfy two properties:
1. Two distinct tuples in any state of the relation cannot have
identical values for (all) the attributes in the relation. → This
uniqueness property applies for both key and a superkey.
2. It is a minimal superkey (candidate key) that is, a superkey
from which we cannot remove any attributes and still have the
uniqueness constraint hold.
▪ This minimality property is required for a key but is
optional for a superkey.
▪ For e.g. in previous relation RollNo or email can be a key
▪ Hence, a key is a superkey but not vice versa
Cont…
 A relational schema can have more than one key, each of
the keys is called candidate key
 It is a set of attributes that uniquely identify the tuples in a
relation.
 We can have any number of candidate keys in a relation but
one is taken as primary key
 The key whose values are used to identify tuples in the
relation is a primary key (PK).
 It is usually selected by database designers.
 Eg: RollNo and Email are candidat keys for student relation
Cont….
 Reading Assignment: read more about candidate key &
surrogate key, and their advantages in real world.
Key Constraints and Constraints on NULL Values cont…

Constraints on NULL value


▪ Specifies whether null values are permitted or not (NOT
NULL)
Entity integrity constraint & Referential integrity constraint

Entity integrity constraint


 Specifies that no primary key value can be null
 Having NULL values for PK → we cannot identify some tuples.
 For example; If two or more tuples had NULL for their PKs, we may
not be able to distinguish them if we try to reference them from other
relations.
 Entity integrity constraints and NOT NULL are specified on
individual relations or tables.
Referential integrity constraint
 Specified between 2 relations and is used to maintain the consistency
among tuples in the two relations.
 States that a tuple in one relation that refers to another relation must
refer to an existing tuple in that relation.
Referential integrity constraint Cont…
STUDENT
SID SNAME DNO
1001 Yeabsira 2
1002 Samuel 1

The attribute of one relation that


refers to the primary key of another
relation is known as foreign key

DEPARTMENT DNO DNAME


1 CS
2 SE
3 IS
Referential Integrity constraint & Foreign Keys
 An attribute FK in a relation schema R1 is a FK of R1 that references
relation R2 if it satisfies the following rules:
1. The attribute in FK of R1 must have the same domain(s) as the primary key
attributes PK of R2 → the attributes FK are said to reference or refer to the relation
R2 or in other word It has to be the same domain as the attribute it refers
to
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 t2 in the current state r2(R2) or NULL.
 i.e. we have t1[FK] = t2[PK], and we say that the tuple t1 references or refers to
the tuple t2. (t represents tuple)
 In this definition, R1 is called the referencing relation and R2 is the referenced
relation i.e. for previous example student relation is referencing relation where
as department relation is referenced relation.
 If these two conditions hold, a referential integrity constraint from R1 to
R2 is said to hold.
Cont….
 In general, a relation schema may have more than one unique
identifier attribute (key).
◦ In this case, each of the keys is called a candidate key.
 E.g; A CAR relation (on next slide) has two candidate keys: License-
number and Engine-serial-number.
 It is common to designate one of the candidate keys as the PK of a
relation.
 The key whose values are used to identify tuples in the relation is a
primary key (PK).
 It is usually selected by database designers.
 We use the convention that the attributes that form the primary key of a
relation schema are underlined.
Cont….

fig: 2.2
Relational Databases & Relational Database
Schemas
 A relational database usually contains many relations, with
tuples in relations that 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,...} such that each ri is a state of Ri and such that
the ri relation states satisfy the integrity constraints specified
in IC.
 Fig 2.3 shows a relational database schema that we call
COMPANY = {EMPLOYEE, DEPARTMENT, DEPT-
LOCATIONS, PROJECT, WORKS-ON, DEPENDENT}.
Cont…
 Each relational DBMS must have a data definition language
(DDL) for defining a relational database schema.
 Current relational DBMSs mostly use SQL for this purpose.

Fig 2.3
Entity Integrity, Referential Integrity & Foreign Keys
 E.g; In fig 2.4, the attribute Dno of EMPLOYEE gives the
department number for which each employee works;
◦ Hence, its value in every EMPLOYEE tuple must match the
Dnumber value of some tuple in the DEPARTMENT relation.
Entity Integrity, Referential Integrity & Foreign Keys
 Referential integrity constraint diagrammatic representation

Fig 2.4
Database Design Models and Classification: RDBMS
A database model: is a model that determines the logical structure of a database
and fundamentally determines in which manner data can be stored, organized
and manipulated.
E.g;
Reading assignment

What are Composite Key and Alternate Key?


Which types of DBMSs are the Best In 2022 and 2023? Why?
Quize
1. Define the following terms:
Tuples, degree, domain, candidate
2. List at least two characteristics of relation
3. What are schema based constraints
4. List and describe at least 3 relation keys
5. What is Entity integrity constraint
Thank you !!!

You might also like