DBMS Unit - 2
DBMS Unit - 2
RELATIONAL MODEL
RELATIONAL ALGEBRA
• Atomic means that each value in the domain is indivisible as far as the relational model is
concerned.
• Usa_phone_numbers. The set of ten-digit phone numbers valid in the United States.
• Social_security_numbers. The set of valid nine-digit Social Security numbers. (This is a
unique identifier assigned to each person in the United States for employment, tax, and
benefits purposes.)
• Employee_ages. Possible ages of employees in a company; each must be an integer value
between 15 and 80.
• A data type or format is also specified for each domain.
* For example, the data type for the domain Usa_phone_numbers can be declared as a
character string of the form (ddd)ddd-dddd, where each d is a numeric (decimal) digit and
* The data type for Employee_ages is an integer number between 15 and 80.
Domains, Attributes, Tuples and Relations
• Relation Schema
• Relation schema R, denoted by R (A1, A2, .....An) is made up of a relation name R and a list of
attributes A1, A2, …, An
• Each attribute Ai is a name of a role played by some domain D in the relation schema R.
• D is called the domain of Ai and is denoted by dom(Ai).
• A relation schema is used to describe a relation; R is called the name of this relation
The terms relation intension for the relation schema R and relation
extension for a relation state r(R) are commonly used.
Mathematical Definition
A relation schema or relation state r(R) is a mathematical relation of degree n on
the domains dom(A1), dom(A2),…, dom(An) which is a subset of the Cartesian
product of the domains that defines R:
r(R) ⊆ (dom(A1) X dom(A2) X … X dom(An))
The Cartesian product specifies all possible combinations of values from the
underlying domains.
Alternative Definition
◦ A relation schema R = {A1, A2, …, An} is a set of attributes, and a relation state
r(R) is a finite set of mappings r = {t1, t2, …, tm}, where each tuple ti is a
mapping from R to D, and D is the union of the attribute domains; that is, D =
dom(A1) U dom(A2) U … U dom(An).
Values and NULLs in the
Tuples
All values are considered atomic (indivisible).
For example, the schema of the student entity has a Name, SSN,
HomePhone, Address, Officephone, Age and GPA. Each tuple in the
relation can then be interpreted as a fact or a particular instance of the
relation.
Example…
Relational Model Notations
Following notations are used in the Relational Model:
◦ A relation schema R of degree n is denoted by R(A1, A2, …, An).
◦ The letters Q, R, S denote relation names.
◦ The letters q, r, s denote relation states.
◦ The letters t, u, v denote tuples.
Relational Model Constraints and
Relational Database Schemas
Constraints on databases can generally be divided into three main categories:
Domain Constraints
Key
◦ A key K of a relation schema R is a super key of R with the additional property that removing
any attribute A from K leaves set of attributes K’ that is not a super key of R any more.
Candidate Key
◦ A relation schema may have more than one key. In this case, each of the keys is called a
candidate key.
Primary Key
◦ This is the candidate key whose values are used to identify tuples in the relation.
◦ The primary key of the relation schema are underlined.
Relational Databases and Relational Database
Schemas
Relational Database Schema
◦ A relational database schema S is a set of relation schemas S = {R1, R2, …, Rm}
and a set of integrity constraints IC.
A database state that does not obey all the integrity constraints is called
an invalid state, and a state that satisfies all the constraints in IC is called
a valid state.
Entity Integrity Constraints
The entity integrity constraint states that no primary key can be NULL.
Having NULL values for the primary key implies that we can not identify
some tuples.
For example, if two or more tuples had NULL for their primary keys, we
might not be able to distinguish them if we tried to reference them
from other relations.
Schema Diagram
Example Database State
Referential Integrity
Constraints
The referential integrity constraint is specified between two
relations and is used to maintain the consistency among
tuples in the two relations.
◦ The attributes in FK have the same domain(s) as the primary key attributes PK of R 2; the
attributes FK are said to reference or refer to the relation R 2.
◦ The 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 is NULL. In the former case, we have t 1[FK] = t2[PK], and we
say that the tuple t1 references or refers to the tuple t2.
In this definition, R1 is called the referencing relation and R2 is the referenced relation.`
Update Operations and Dealing with
Constraint Violations
The operations of the relational model can be categorized into retrievals
and update.
Eg:
Delete the WORKS_ON tuple with ESSN = ‘999887777’ and PNO = 10.
Eg:
Update the SALARY of the EMPLOYEE tuple with SSN = ‘999887777’ to 28000.
Update the SSN of the EMPLOYEE tuple with SSN = ‘999887777’ to ‘987654321’.
The Transaction Concept
A database application program running against a relational database typically runs a
series of transactions.
A transaction involves reading from the database as well as doing insertions, deletions
and updates to existing values in the database.
A single transaction may involve any number of retrieval operations that reads from the
database and any number of update operations.
• Relational algebra is the basic set of operations for the relational model
◦ SELECT is commutative:
◦ <condition1>( < condition2> (R)) = <condition2> ( < condition1> (R))
◦ The number of tuples in the result of a SELECT is less than (or equal to) the number of
tuples in the input relation R
The following query results refer to this
database state
Unary Relational Operations: PROJECT
PROJECT Operation is denoted by (pi)
This operation keeps certain columns (attributes) from a relation and
discards the other columns.
◦ PROJECT creates a vertical partitioning
◦ The list of specified columns (attributes) is kept in each tuple
◦ The other attributes in each tuple are discarded
Example: To list each employee’s first and last name and salary, the
following is used:
LNAME, FNAME,SALARY(EMPLOYEE)
Unary Relational Operations: PROJECT
(cont.)
Example: Suppose that we want to retrieve the name of the manager of each
department.
◦ To get the manager’s name, we need to combine each DEPARTMENT tuple with the
EMPLOYEE tuple whose SSN value matches the MGRSSN value in the department tuple.
The NATURAL JOIN or EQUIJOIN operation can also be specified among multiple tables,
leading to an n-way join
• Project-department-manager combined information
• This combines each project tuple with its controlling department tuple into a single Tuple
• Then combines that tuple with an employee tuple that is the department manager.
• Project_Dept
Example of NATURAL JOIN operation
Complete Set of Relational Operations
The set of operations including SELECT , PROJECT , UNION ,
DIFFERENCE - , RENAME , and CARTESIAN PRODUCT X is called a
complete set because any other relational algebra expression can be
expressed by a combination of these five operations.
For example:
◦ R S = (R S ) – ((R - S) (S - R))
◦ R <join condition>S = <join condition> (R X S)
Binary Relational Operations: DIVISION
DIVISION Operation
◦ The division operation is applied to two relations
◦ R(Z) S(X), where X subset Z. Let Y = Z - X (and hence Z = X
Y); that is, let Y be the set of attributes of R that are not attributes of S.
•The JOIN operations described earlier match tuples that satisfy the join condition.
For example, for a NATURAL JOIN operation R * S, only tuples from R that have
matching tuples in S—and vice versa—appear in the result
• Hence
(i)Tuples without a matching (or related) tuple are eliminated from the JOIN
result.
(ii)Tuples with NULL values in the join attributes are also eliminated.
•This type of join, where tuples with no match are eliminated, is known as an inner
join.
•This amounts to the loss of information if the user wants the result of the JOIN to
include all the tuples in one or more of the component relations.
Additional Relational Operations (cont.)
The left outer join operation keeps every tuple in the first or left
relation R in ; if no matching tuple is found in S, then the
attributes of S in the join result are filled or “padded” with null values.
A similar operation, right outer join, keeps every tuple in the second or
right relation S in the result of
A third operation, full outer join, denoted by keeps all tuples in
both the left and the right relations when no matching tuples are found,
padding them with null values as needed.
Additional Relational Operations (cont.)
• The LEFT OUTER JOIN operation keeps every tuple in the first, or left, relation R in R
S;
• if no matching tuple is found in S, then the attributes of S in the join result are filled or
padded with NULL values. The result of these operations is shown
Example Database State
Additional Relational Operations (cont.)
OUTER UNION Operations
◦ The outer union operation was developed to take the union of tuples from
two relations if the relations are not type compatible.
◦ This operation will take the union of tuples in two relations R(X, Y) and S(X, Z)
that are partially compatible, meaning that only some of their attributes, say
X, are type compatible.
◦ The attributes that are type compatible are represented only once in the
result, and those attributes that are not type compatible from either relation
are also kept in the result relation T(X, Y, Z).
Additional Relational Operations (cont.)
Example: An outer union can be applied to two relations
whose schemas are STUDENT(Name, SSN, Department,
Advisor) and INSTRUCTOR(Name, SSN, Department, Rank).
◦ Tuples from the two relations are matched based on having the
same combination of values of the shared attributes— Name,
SSN, Department.
◦ If a student is also an instructor, both Advisor and Rank will have a
value; otherwise, one of these two attributes will be null.
◦ The result relation STUDENT_OR_INSTRUCTOR will have the
following attributes:
STUDENT_OR_INSTRUCTOR (Name, SSN, Department,
Advisor, Rank)
Query 1. Retrieve the name and address of all employees who work for
the ‘Research’ department.
Query 2. For every project located in ‘Stafford’, list the project number,
the controlling department number, and the department manager’s last
name, address, and birth date.
Examples of Queries in Relational Algebra
Q1: Retrieve the name and address of all employees who work for the
‘Research’ department.
RESEARCH_DEPT DNAME=’Research’ (DEPARTMENT)
RESEARCH_EMPS (RESEARCH_DEPT DNUMBER= DNOEMPLOYEE EMPLOYEE)
RESULT FNAME, LNAME, ADDRESS (RESEARCH_EMPS)
If the chosen key of E is a composite, then the set of simple attributes that form it will
together form the primary key of R.
If multiple keys were identified for E during the conceptual design, the information
describing the attributes that form each additional key is kept in order to specify
secondary keys of relation R.
Mapping of Weak Entity
Types
For each weak entity type W in the ER schema with owner entity type E,
create a relation R and include all simple attributes of W as attributes of R.
If there is a weak entity type E2 whose owner is also a weak entity type E 1,
then E1 should be mapped before E2 to determine its primary key first.
Mapping of Binary 1:1 Relationship Types
For each binary 1:1 relationship type R in ER schema, identify the
relations S and T that correspond to the entity types participating in R.
Include as foreign key attributes in S the primary keys of the relations that
represent the participating entity types.
Also include any simple attributes of the n – ary relationship type as attributes
of S.
The primary key of S is usually a combination of all the foreign keys that
reference the relations representing the participating entity types.
Data Definition, Constraints,
and Schema Changes
Used to CREATE, DROP, and ALTER the descriptions of the tables
(relations) of a database