UE20CS301_Unit2_Slides
UE20CS301_Unit2_Slides
SYSTEM
S Nagasundari
Department of Computer Science and Engineering
DATABASE MANAGEMENT SYSTEM
S Nagasundari
Department of Computer Science and Engineering
2
DATABASE MANAGEMENT SYSTEM
Unit 2 : Relational Model
3
DATABASE MANAGEMENT SYSTEM
Unit 2: Relational Model
4
DATABASE MANAGEMENT SYSTEM
Relational Model : Reference
T1 : CHAPTER 5
5
DATABASE MANAGEMENT SYSTEM
Relational Model Concepts
• The model was first proposed by Dr. E.F. Codd of IBM Research
in 1970 in the following paper:
6
DATABASE MANAGEMENT SYSTEM
Relational Model Concepts
7
DATABASE MANAGEMENT SYSTEM
What is a flat file?
• The flat file refers to any file stored on a local file system,
as opposed to a more complex set of files, such as those
in a structured database.
• The data records in a flat file are typically stored
sequentially and without any metadata, such as the
indexes, keys, and relationships, that you would find in
database storage.
8
DATABASE MANAGEMENT SYSTEM
File/Database Structure
9
DATABASE MANAGEMENT SYSTEM
File/Database Structure
10
DATABASE MANAGEMENT SYSTEM
Database Structure ( Flat file)
11
DATABASE MANAGEMENT SYSTEM
What is wrong with this database?
12
DATABASE MANAGEMENT SYSTEM
What is wrong with this database?
13
DATABASE MANAGEMENT SYSTEM
Why is that a problem?
14
DATABASE MANAGEMENT SYSTEM
Why is that a problem?
15
DATABASE MANAGEMENT SYSTEM
Flat file Database Problems
16
DATABASE MANAGEMENT SYSTEM
Database Structure ( relational)
17
DATABASE MANAGEMENT SYSTEM
Normalising the Vet table
18
DATABASE MANAGEMENT SYSTEM
How does this solve the problem?
19
DATABASE MANAGEMENT SYSTEM
Informal Definitions
• Each column has a column header that gives an indication of the meaning of
the data items in that column
• In the formal model, the column header is called an
attribute name (or just attribute)
20
DATABASE MANAGEMENT SYSTEM
Example of a Relation
21
DATABASE MANAGEMENT SYSTEM
Informal Definitions
Key of a Relation:
• Each row has a value of a data item (or set of items) that
uniquely identifies that row in the table
• Called the key
• In the STUDENT table, SSN is the key
• Sometimes row-ids or sequential numbers are assigned as
keys to identify the rows in a table
• Called artificial key or surrogate key
22
DATABASE MANAGEMENT SYSTEM
Formal Definitions - Schema
23
DATABASE MANAGEMENT SYSTEM
Formal Definitions - Tuple
24
DATABASE MANAGEMENT SYSTEM
Formal Definitions - Domain
25
DATABASE MANAGEMENT SYSTEM
Formal Definitions - Domain
26
DATABASE MANAGEMENT SYSTEM
Formal Definitions - State
• The role these strings play in the CUSTOMER relation is that of the
name of a customer.
27
DATABASE MANAGEMENT SYSTEM
Formal Definitions - Summary
Formally,
• Given R(A1, A2, .........., An)
• r(R) ⊂ dom (A1) X dom (A2) X ....X dom(An)
30
DATABASE MANAGEMENT SYSTEM
Example-A Relation Student
31
DATABASE MANAGEMENT SYSTEM
Characteristics of Relations
33
DATABASE MANAGEMENT SYSTEM
Characteristics of Relations
Values in a tuple:
• All values are considered atomic (indivisible).
• Each value in a tuple must be from the domain of the attribute
for that column
• If tuple t = <v1, v2, …, vn> is a tuple (row) in the relation state
r of R(A1, A2, …, An)
• Then each vi must be a value from dom(Ai)
• A special null value is used to represent values that are unknown
or not available or inapplicable in certain tuples.
34
DATABASE MANAGEMENT SYSTEM
Characteristics of Relations
Notation:
• t[Ai] or t.Ai
35
THANK YOU
S Nagasundari
Department of Computer Science and Engineering
[email protected]
DATABASE MANAGEMENT
SYSTEM
S Nagasundari
Department of Computer Science and Engineering
9/1/2021 1
DATABASE MANAGEMENT SYSTEM
S Nagasundari
Department of Computer Science and Engineering
2
DATABASE MANAGEMENT SYSTEM
Unit 2 : Relational Model
CHAPTER 5
Constraints determine which values are permissible and which are not in the
database. They are of three main types:
1. Inherent or Implicit Constraints:
• Based on the data model itself. (E.g., relational model does not allow
a list as a value for any attribute)
• Constraints are conditions that must hold on all valid relation states.
• Integrity Constraints
• Standard numeric data types for integers (such as short integer, integer,
and long integer)
• Real numbers (float and double-precision float).
• Characters,
• Booleans,
• Fixed-length strings, Variable-length strings, as are date, time, timestamp,
and other special data types.
• Domains can also be described by a subrange of values from a data type or
• as an enumerated data type in which all possible values are explicitly listed
DATABASE MANAGEMENT SYSTEM
Key Constraints
• Is a set of attributes SK of R with the 1. Two distinct tuples in any state of the relation
• No two tuples in any valid relation attributes in the key. This uniqueness property
state r(R) will have the same value also applies to a superkey.
• Key2 = {SerialNo}
• If a relation has several candidate keys, one is • Also used to reference the tuple from
chosen arbitrarily to be the primary key. another tuple
• The primary key attributes are underlined. General rule: Choose as primary key the
• Example: Consider the CAR relation schema: smallest of the candidate keys (in terms
DB = {r1, r2, ..., rm} such that each ri is a state of Ri and such
that the ri relation states satisfy the integrity constraints
specified in IC.
Note: Not use the term instance since it also applies to single tuples.
DATABASE MANAGEMENT SYSTEM
Relational Database State
• A database state that does not obey all the integrity constraints is
called not valid, and
• A state that satisfies all the constraints in the defined set of integrity
constraints IC is called a valid state
DATABASE MANAGEMENT SYSTEM
Populated Database State
• Each relation will have many tuples in its current relation state
Example state
for the
COMPANY
database
schema
THANK YOU
S Nagasundari
Department of Computer Science and Engineering
[email protected]
DATABASE MANAGEMENT
SYSTEM
S Nagasundari
Department of Computer Science and Engineering
9/1/2021 1
DATABASE MANAGEMENT SYSTEM
S Nagasundari
Department of Computer Science and Engineering
2
DATABASE MANAGEMENT SYSTEM
Unit 2 : Relational Model
CHAPTER 5
• The conditions for a foreign key, given below, specify a referential integrity
constraint between the two relation schemas R1 and R2.
(2) a null.
In case (2), the FK in R1 should not be a part of its own primary key.
DATABASE MANAGEMENT SYSTEM
Displaying a relational database schema and its constraints
• State constraints
because they define the constraints that a valid state of the
database must satisfy.
• Transition constraints, can be defined to deal with state changes in
the database.
Example of a transition constraint is:
“the salary of an employee can only increase.”
Such constraints are typically enforced
by the application programs or specified using active rules and
triggers
DATABASE MANAGEMENT SYSTEM
Update Operations on Relations
Three basic operations that can change the states of relations in the
database
• INSERT a tuple.
• DELETE a tuple.
• MODIFY a tuple.
• Integrity constraints should not be violated by the update
operations.
• Domain constraint:
• Key constraint:
• Referential integrity:
• Entity integrity:
• Referential integrity:
• Entity integrity:
Example state
for the
COMPANY
database
schema
DATABASE MANAGEMENT SYSTEM
Possible Example violations for Insert operation
• Operation:
Insert <‘Cecilia’, ‘F’, ‘Kolonsky’, NULL, ‘1960-04-05’, ‘6357 Windy Lane,
Katy, TX’, F, 28000, NULL, 4> into EMPLOYEE.
• Operation:
Insert <‘Alicia’, ‘J’, ‘Zelaya’, ‘999887777’, ‘1960-04-05’, ‘6357 Windy
Lane, Katy, TX’, F, 28000, ‘987654321’, 4> into EMPLOYEE.
• Operation:
Insert <‘Cecilia’, ‘F’, ‘Kolonsky’, ‘677678989’, ‘1960-04-05’, ‘6357
Windswept, Katy, TX’, F, 28000, ‘987654321’, 7> into EMPLOYEE.
• Operation:
Insert <‘Cecilia’, ‘F’, ‘Kolonsky’, ‘677678989’, ‘1960-04-05’, ‘6357 Windy
Lane, Katy, TX’, F, 28000, NULL, 4> into EMPLOYEE.
• Operation 1: ask the user to provide a value for Ssn, and could then
accept the insertion if a valid Ssn value is provided.
• Operation 3: ask the user to change the value of Dno to some valid value
(or set it to NULL), or it could ask the user to insert a DEPARTMENT tuple
with Dnumber = 7 and could accept the original insertion only after such
an operation was accepted.
• Notice that in the latter case the insertion violation can cascade back to
the EMPLOYEE relation if the user attempts to insert a tuple for
department 7 with a value for Mgr_ssn that does not exist in the
EMPLOYEE relation.
DATABASE MANAGEMENT SYSTEM
Populated database state for
COMPANY
Example state
for the
COMPANY
database
schema
DATABASE MANAGEMENT SYSTEM
Possible violations for Delete operation
• Operation:
Delete the WORKS_ON tuple with Essn = ‘999887777’ and Pno = 10.
Result: This deletion is acceptable and deletes exactly one tuple.
• Operation:
Delete the EMPLOYEE tuple with Ssn = ‘999887777’.
Result: This deletion is not acceptable, because there are tuples
in WORKS_ON that refer to this tuple. Hence, if the tuple in
EMPLOYEE is deleted, referential integrity violations will result.
DATABASE MANAGEMENT SYSTEM
Possible violations for Delete operation
• Operation:
Delete the EMPLOYEE tuple with Ssn = ‘333445555’.
Result: This deletion will result in even worse referential integrity
violations, because the tuple involved is referenced by tuples
from the EMPLOYEE, DEPARTMENT, WORKS_ON, and
DEPENDENT relations.
DATABASE MANAGEMENT SYSTEM
Populated database state for
COMPANY
Example state
for the
COMPANY
database
schema
DATABASE MANAGEMENT SYSTEM
Possible violations for Update operation
• Operation:
Update the salary of the EMPLOYEE tuple with Ssn = ‘999887777’ to
28000.
Result: Acceptable.
• Operation:
Update the Dno of the EMPLOYEE tuple with Ssn = ‘999887777’ to 1.
Result: Acceptable.
DATABASE MANAGEMENT SYSTEM
Possible violations for Update operation
• Operation:
Update the Dno of the EMPLOYEE tuple with Ssn = ‘999887777’ to 7.
Result: Unacceptable, because it violates referential integrity.
• Operation:
Update the Ssn of the EMPLOYEE tuple with Ssn = ‘999887777’ to
‘987654321’.
Result: Unacceptable, because it violates primary key constraint by
repeating a value that already exists as a primary key in another
tuple; it violates referential integrity constraints because there are
other relations that refer to the existing value of Ssn.
DATABASE MANAGEMENT SYSTEM
Summary
S Nagasundari
Department of Computer Science and Engineering
[email protected]
DATABASE MANAGEMENT
SYSTEM
S Nagasundari
Department of Computer Science and Engineering
9/16/2021 1
DATABASE MANAGEMENT SYSTEM
S Nagasundari
Department of Computer Science and Engineering
2
DATABASE MANAGEMENT SYSTEM
Unit 2 : Relational Model
CHAPTER 9
Result
DATABASE MANAGEMENT SYSTEM
ER Conceptual schema diagram for Company Database
DATABASE MANAGEMENT SYSTEM
ER to Relational Mapping Algorithm ( Continued)
• For each weak entity type W in the ER schema with owner entity type
E, create a relation R & include all simple attributes (or simple
components of composite attributes) of W as attributes of R.
• Include the primary key SSN of the EMPLOYEE relation as a foreign key
attribute of DEPENDENT (renamed to ESSN).
Result
DATABASE MANAGEMENT SYSTEM
ER Conceptual schema diagram for Company Database
DATABASE MANAGEMENT SYSTEM
ER to Relational Mapping Algorithm ( Continued)
For each binary 1:1 relationship type R in the ER schema, identify the relations
S and T that correspond to the entity types participating in R.
Result
DATABASE MANAGEMENT SYSTEM
ER Conceptual schema diagram for Company Database
DATABASE MANAGEMENT SYSTEM
ER to Relational Mapping Algorithm ( Continued)
• For each regular binary 1:N relationship type R, identify the relation S
that represent the participating entity type at the N-side of the
relationship type.
Result
DATABASE MANAGEMENT SYSTEM
ER Conceptual schema diagram for Company Database
DATABASE MANAGEMENT SYSTEM
ER to Relational Mapping Algorithm ( Continued)
• For each regular binary M:N relationship type R, create a new relation S to
represent R. This is a relationship relation.
• Include as foreign key attributes in S the primary keys of the relations that
represent the participating entity types; their combination will form the
primary key of S.
• Also include any simple attributes of the M:N relationship type (or simple
components of composite attributes) as attributes of S.
DATABASE MANAGEMENT SYSTEM
ER to Relational Mapping Algorithm ( Continued)
Example: The M:N relationship type WORKS_ON from the ER diagram is mapped
by creating a relation WORKS_ON in the relational database schema.
• The primary keys of the PROJECT and EMPLOYEE relations are included as
foreign keys in WORKS_ON and renamed PNO and ESSN, respectively.
Result
DATABASE MANAGEMENT SYSTEM
ER Conceptual schema diagram for Company Database
DATABASE MANAGEMENT SYSTEM
ER to Relational Mapping Algorithm ( Continued)
Result
DATABASE MANAGEMENT SYSTEM
ER to Relational Mapping Algorithm ( Continued)
• For each n-ary relationship type R, where n>2, create a new relationship
S to represent R.
• Also include any simple attributes of the n-ary relationship type (or
simple components of composite attributes) as attributes of S.
S Nagasundari
Department of Computer Science and Engineering
[email protected]
DATABASE MANAGEMENT
SYSTEM
S Nagasundari
Department of Computer Science and Engineering
DATABASE MANAGEMENT SYSTEM
S Nagasundari
Department of Computer Science and Engineering
9/19/2021 2
DATABASE MANAGEMENT SYSTEM
Unit 2 : Relational Model
9/19/2021 3
DATABASE MANAGEMENT SYSTEM
Unit 2 : Relational Model
CHAPTER 8
9/19/2021 4
DATABASE MANAGEMENT SYSTEM
Unit 2 : Relational Model
Relational Algebra
• Unary Relational Operations
9/19/2021 5
DATABASE MANAGEMENT SYSTEM
Relational Algebra Overview
9/19/2021
objects in relational algebra are relations) 6
DATABASE MANAGEMENT SYSTEM
Relational Algebra Overview(Continued)
9/19/2021 7
DATABASE MANAGEMENT SYSTEM
Brief History of Origins of Algebra
9/19/2021 8
DATABASE MANAGEMENT SYSTEM
Relational Algebra Overview
9/19/2021 9
DATABASE MANAGEMENT SYSTEM
Database State for COMPANY
9/19/2021 10
DATABASE MANAGEMENT SYSTEM
Unary Relational Operations : Select
9/19/2021 11
DATABASE MANAGEMENT SYSTEM
Unary Relational Operations: SELECT
9/19/2021 13
DATABASE MANAGEMENT SYSTEM
Unary Relational Operations: SELECT(Continued)
9/19/2021 15
DATABASE MANAGEMENT SYSTEM
Unary Relational Operations: PROJECT
• Example: To list each employee’s first and last name and salary,
the following is used:
LNAME, FNAME,SALARY(EMPLOYEE)
9/19/2021 16
DATABASE MANAGEMENT SYSTEM
Unary Relational Operations: PROJECT
9/19/2021 17
DATABASE MANAGEMENT SYSTEM
Unary Relational Operations: PROJECT (contd.)
9/19/2021 19
DATABASE MANAGEMENT SYSTEM
Relational Algebra Expressions
• In the latter case, we must give names to the relations that hold the
intermediate results.
9/19/2021 20
DATABASE MANAGEMENT SYSTEM
Single expression Vs Sequence of Relational operations(Example)
• To retrieve the first name, last name, and salary of all employees who work
in department number 5, we must apply a select and a project operation
• DEP5_EMPS DNO=5(EMPLOYEE)
9/19/2021 21
DATABASE MANAGEMENT SYSTEM
Unary Relational Operations: Rename
9/19/2021 22
DATABASE MANAGEMENT SYSTEM
Unary Relational Operations: RENAME(continued)
The general RENAME operation can be expressed by any
of the following forms:
• S(R) changes:
• the relation name only to S
9/19/2021 24
DATABASE MANAGEMENT SYSTEM
Example of applying multiple operations and RENAME
9/19/2021 25
THANK YOU
S Nagasundari
Department of Computer Science and Engineering
[email protected]
DATABASE MANAGEMENT
SYSTEM
S Nagasundari
Department of Computer Science and Engineering
DATABASE MANAGEMENT SYSTEM
S Nagasundari
Department of Computer Science and Engineering
2
DATABASE MANAGEMENT SYSTEM
Unit 2 : Relational Model
3
DATABASE MANAGEMENT SYSTEM
Unit 2 : Relational Model
CHAPTER 8
4
DATABASE MANAGEMENT SYSTEM
Unit 2 : Relational Model
Relational Algebra
• Unary Relational Operations
5
DATABASE MANAGEMENT SYSTEM
Relational Algebra Operations from UNION Operation
6
DATABASE MANAGEMENT SYSTEM
Relational Algebra Operations from Set Theory : UNION
Example:
• To retrieve the social security numbers of all employees who either
work in department 5 (RESULT1 below) or directly supervise an
employee who works in department 5 (RESULT2 below)
8
DATABASE MANAGEMENT SYSTEM
Relational Algebra Operations from Set Theory
• R1(A1, A2, ..., An) and R2(B1, B2, ..., Bn) are type compatible if:
• they have the same number of attributes, and
• the domains of corresponding attributes are type compatible
(i.e. dom(Ai)=dom(Bi) for i=1, 2, ..., n).
9
DATABASE MANAGEMENT SYSTEM
Relational Algebra Operations from Set Theory : Intersection
INTERSECTION is denoted by
10
DATABASE MANAGEMENT SYSTEM
Relational Algebra Operations from Set Theory : SET DIFFERENCE(continued)
11
DATABASE MANAGEMENT SYSTEM
Example to illustrate the result of UNION, INTERSECT and DIFFERENCE
12
DATABASE MANAGEMENT SYSTEM
Some properties of UNION, INTERSECT and DIFFERENCE
• R S = S R, and R S = S R
• R–S≠S–R 13
DATABASE MANAGEMENT SYSTEM
Relational Algebra Operations from Set Theory : Cartesian Product
14
DATABASE MANAGEMENT SYSTEM
Relational Algebra Operations from Set Theory : Cartesian Product(contd.)
15
DATABASE MANAGEMENT SYSTEM
Relational Algebra Operations from Set Theory : Cartesian Product(contd.)
• Example (meaningful):
• FEMALE_EMPS SEX=’F’(EMPLOYEE)
• EMPNAMES FNAME, LNAME, SSN (FEMALE_EMPS)
• EMP_DEPENDENTS EMPNAMES x DEPENDENT
• ACTUAL_DEPS SSN=ESSN(EMP_DEPENDENTS)
• RESULT will now contain the name of female employees and their dependents
16
DATABASE MANAGEMENT SYSTEM
The Cartesian Product (Cross Product)
18
DATABASE MANAGEMENT SYSTEM
The Cartesian Product (Cross Product)
19
DATABASE MANAGEMENT SYSTEM
The Cartesian Product (Cross Product) Example
In order to create a new relation which pairs each singer with each role,
we need to use the relational operation Cartesian product.
Relational Algebra operation: Singers Cartesian product Roles giving
Singers-Roles
Source: https://www.cs.uct.ac.za/mit_notes/database/htmls/chp02.html#cartesian-product
20
DATABASE MANAGEMENT SYSTEM
The Cartesian Product (Cross Product) Example
21
DATABASE MANAGEMENT SYSTEM
Some Examples
22
DATABASE MANAGEMENT SYSTEM
Some Examples
23
DATABASE MANAGEMENT SYSTEM
Some Examples
Question:
b) Get the details about all flights from Chennai to New Delhi.
25
DATABASE MANAGEMENT SYSTEM
Some Examples
Get the details of flights that are scheduled on both dates 01/12/2020 and
02/12/2020 at 16:00 hours
Get the details of flights that are scheduled on either of the dates 01/12/2020 or
02/12/2020 or both at 16:00 hours.
26
THANK YOU
S Nagasundari
Department of Computer Science and Engineering
[email protected]
DATABASE MANAGEMENT
SYSTEM
S Nagasundari
Department of Computer Science and Engineering
DATABASE MANAGEMENT SYSTEM
S Nagasundari
Department of Computer Science and Engineering
DATABASE MANAGEMENT SYSTEM
Unit 2 : Relational Model
CHAPTER 8
Relational Algebra
• Unary Relational Operations
• where R and S can be any relations that result from general relational algebra
expressions.
DATABASE MANAGEMENT SYSTEM
Binary Relational Operations : JOIN(contd.)
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.
• DEPT_MGR DEPARTMENT MGRSSN=SSN EMPLOYEE
or
DATABASE MANAGEMENT SYSTEM
JOIN operation can be replaced by ( Recap)
DATABASE MANAGEMENT SYSTEM
Some properties of JOIN
• Theta can be any general boolean expression on the attributes of R and S; for
example:
A general join condition is of the form • Tuples whose join attributes are NULL
or for which the join condition is FALSE
<condition> AND <condition> AND … AND <condition>
do not appear in the result.
where each <condition> is of the form Ai θ Bj, • In that sense, the JOIN operation does
not necessarily preserve all the
• Ai is an attribute of R,
information in the participating
• Bj is an attribute of S, relations, because tuples that do not
get combined with matching ones in
• Ai and Bj have the same domain, and
the other relation do not appear in the
• θ (theta) is one of the comparison operators {=, <, result.
≤, >, ≥, ≠}.
DATABASE MANAGEMENT SYSTEM
Variations of JOINS
• EQUIJOIN
• NATURAL JOIN
DATABASE MANAGEMENT SYSTEM
Binary Relational Operations: EQUIJOIN
EQUIJOIN Operation
• The most common use of join involves join conditions with equality
comparisons only
• The standard definition of natural join requires that the two join attributes, or
each pair of corresponding join attributes, have the same name in both
relations
Example: To apply a natural join on the DNUMBER Another example: Q R(A,B,C,D) * S(C,D,E)
attributes of DEPARTMENT and DEPT_LOCATIONS, it is The implicit join condition includes each
sufficient to write: pair of attributes with the same name,
DEPT_LOCS DEPARTMENT * DEPT_LOCATIONS “AND”ed together:
R.C=S.C AND R.D.S.D
• Only attribute with the same name is DNUMBER
Result keeps only one attribute of each
• An implicit join condition is created based on
such pair:
this attribute:
Q(A,B,C,D,E)
DEPARTMENT.DNUMBER=DEPT_LOCATIONS.DNU
MBER
DATABASE MANAGEMENT SYSTEM
Join Operation
• For example:
• R S = (R S ) – ((R - S) (S - R))
• R <join condition>S = <join condition> (R X S)
DATABASE MANAGEMENT SYSTEM
Binary Relational Operations: Division Operation
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.
3. Apply the DIVISION operation to the two relations, which gives the
desired employees’ Social Security numbers
if a ssn project has 1 or more project then that ssn project numbers should we
equal to all the project numbers of the smith projects
DATABASE MANAGEMENT SYSTEM
Example of Division
DATABASE MANAGEMENT SYSTEM
Division Operation
Query Tree
Query Tree
• It represents the input relations of the query as leaf nodes of the tree,
represents the relational algebra operations as internal nodes.
• The execution terminates when the root node is executed and produces the
result relation for the query.
DATABASE MANAGEMENT SYSTEM
Query Tree Notation (continued)
Query Tree
• A tree gives a good visual feel of the complexity of the query and the
operations involved
• 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.
DATABASE MANAGEMENT SYSTEM
Relational Algebra Expression- Query Tree (continued)
DATABASE MANAGEMENT SYSTEM
Example of Query Tree
THANK YOU
S Nagasundari
Department of Computer Science and Engineering
[email protected]
DATABASE MANAGEMENT
SYSTEM
S Nagasundari
Department of Computer Science and Engineering
DATABASE MANAGEMENT SYSTEM
S Nagasundari
Department of Computer Science and Engineering
9/24/2021 2
DATABASE MANAGEMENT SYSTEM
Unit 2 : Relational Model
9/24/2021 3
DATABASE MANAGEMENT SYSTEM
Unit 2 : Relational Model
CHAPTER 8
9/24/2021 4
DATABASE MANAGEMENT SYSTEM
Unit 2 : Relational Model
Relational Algebra
• Unary Relational Operations
9/24/2021 5
DATABASE MANAGEMENT SYSTEM
Additional Relation Operation
• Generalized Projection
9/24/2021 6
DATABASE MANAGEMENT SYSTEM
Aggregate Function Operation
9/24/2021 7
DATABASE MANAGEMENT SYSTEM
Additional Relational Operations: Aggregate Functions and Grouping
• Examples of such functions include retrieving the average or total salary of all
employees or the total number of employee tuples.
9/24/2021 8
DATABASE MANAGEMENT SYSTEM
Additional Relational Operations: Aggregate Functions and Grouping
9/24/2021 9
DATABASE MANAGEMENT SYSTEM
Aggregate Function Operation
• The previous examples all summarized one or more attributes for a set
of tuples
• Example: For each department, retrieve the DNO, COUNT SSN, and
AVERAGE SALARY
9/24/2021 11
DATABASE MANAGEMENT SYSTEM
Using Grouping with Aggregation
9/24/2021 12
DATABASE MANAGEMENT SYSTEM
The aggregate function operation
9/24/2021 13
DATABASE MANAGEMENT SYSTEM
Results of GROUP BY and HAVING
9/24/2021 14
DATABASE MANAGEMENT SYSTEM
9/24/2021 15
THANK YOU
S Nagasundari
Department of Computer Science and Engineering
[email protected]
DATABASE MANAGEMENT
SYSTEM
S Nagasundari
Department of Computer Science and Engineering
DATABASE MANAGEMENT SYSTEM
S Nagasundari
Department of Computer Science and Engineering
2
DATABASE MANAGEMENT SYSTEM
Unit 2 : Relational Model
3
DATABASE MANAGEMENT SYSTEM
Unit 2 : Relational Model
CHAPTER 8
4
DATABASE MANAGEMENT SYSTEM
Unit 2 : Relational Model
Relational Algebra
• Unary Relational Operations
5
DATABASE MANAGEMENT SYSTEM
Additional Relational Operations
7
DATABASE MANAGEMENT SYSTEM
A two-level recursive query
8
DATABASE MANAGEMENT SYSTEM
Additional Relational Operations (continued)
• In NATURAL JOIN and EQUIJOIN, tuples without a matching (or related) tuple
are eliminated from the join result
• A set of operations, called OUTER joins, can be used when we want to keep
all the tuples in R, or all those in S, or all those in both relations in the result
of the join, regardless of whether or not they have matching tuples in the
other relation.
9
DATABASE MANAGEMENT SYSTEM
Additional Relational Operations (continued)
• The left outer join operation keeps every tuple in the first or left relation R in
• A similar operation, right outer join, keeps every tuple in the second or right
relation S in the result of R S.
• 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.
10
DATABASE MANAGEMENT SYSTEM
Example
Suppose that we want a list of all employee names as well as the name
of the departments they manage if they happen to manage a
department; if they do not manage one, we can indicate it with a NULL
value
11
DATABASE MANAGEMENT SYSTEM
The result of a LEFT OUTER JOIN operation
12
DATABASE MANAGEMENT SYSTEM
Additional Relational Operations (continued)
13
DATABASE MANAGEMENT SYSTEM
Additional Relational Operations (continued)
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:
S Nagasundari
Department of Computer Science and Engineering
[email protected]
DATABASE MANAGEMENT
SYSTEM
S Nagasundari
Department of Computer Science and Engineering
DATABASE MANAGEMENT SYSTEM
S Nagasundari
Department of Computer Science and Engineering
9/24/2021 2
DATABASE MANAGEMENT SYSTEM
Unit 2 : Relational Model
9/24/2021 3
DATABASE MANAGEMENT SYSTEM
Unit 2 : Relational Model
CHAPTER 8
9/24/2021 4
DATABASE MANAGEMENT SYSTEM
Unit 2 : Relational Model
Relational Algebra
• Unary Relational Operations
9/24/2021 5
DATABASE MANAGEMENT SYSTEM
Example Queries in Relational Algebra: Procedural Form
◼ 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)
9/24/2021 6
DATABASE MANAGEMENT SYSTEM
Example Queries in Relational Algebra: Single Expression
9/24/2021 7
DATABASE MANAGEMENT SYSTEM
Example Queries in Relational Algebra
9/24/2021 8
DATABASE MANAGEMENT SYSTEM
Example Queries in Relational Algebra
9/24/2021 9
DATABASE MANAGEMENT SYSTEM
Example Queries in Relational Algebra
Q5. List the names of all employees with two or more dependents
9/24/2021 10
DATABASE MANAGEMENT SYSTEM
Example Queries in Relational Algebra
Q7. List the names of managers who have at least one dependent.
9/24/2021 11
DATABASE MANAGEMENT SYSTEM
Example Queries in Relational Algebra
Consider a database with the following schema: • Eats contains information about
what type of pizza each person
Person ( name, age, gender ) name is a key (customer) eats (likes)
• Name is always the customer’s
Frequents ( name, pizzeria ) (name, pizzeria) is a key name
• Frequents record information
Eats ( name, pizza ) (name, pizza) is a key about pizzerias that each customer
frequents (visits)
Serves ( pizzeria, pizza, price ) (pizzeria, pizza) is a key
• Serves contains information about
Write relational algebra expressions for the following queries all possible pizza types for each
pizzeria
Source: http://openclassroom.stanford.edu/MainFolder/courses/cs145/old-site/docs/backup/ra-exercises.html
9/24/2021 12
DATABASE MANAGEMENT SYSTEM
Example Queries in Relational Algebra
1. Find all pizzerias frequented by at least one person under the age of 18.
2. Find the names of all females who eat either mushroom or pepperoni
pizza (or both).
3. Find the names of all females who eat both mushroom and pepperoni
pizza.
4. Find all pizzerias that serve at least one pizza that Amy eats for less than
$10.00.
5. Find all pizzerias that are frequented by only females or only males.
6. For each person, find all pizzas the person eats that are not served by any
pizzeria the person frequents. Return all such person (name) / pizza pairs.
7. Find the names of all people who frequent only pizzerias serving at least
one pizza they eat.
8. Find the names of all people who frequent every pizzeria serving at least
one pizza they eat.
9. Find the pizzeria serving the cheapest pepperoni pizza. In the case of ties,
9/24/2021 return all of the cheapest-pepperoni pizzerias. 13
DATABASE MANAGEMENT SYSTEM
Exercise
Draw the Query tree for the queries given in the previous slides
9/24/2021 14
DATABASE MANAGEMENT SYSTEM
Summary
Relational Algebra
9/24/2021 15
THANK YOU
S Nagasundari
Department of Computer Science and Engineering
[email protected]