Se DB CH2
Se DB CH2
Relational Operations
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:
▪ The name of a relation schema such as STUDENT indicates the current set of
▪ 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]
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