M2 Notes
M2 Notes
MODULE 2
22CML42
Relational Model:
Relational Model Concepts
• The relational model represents the database as a collection of relations.
• Informally, each relation resembles a table of values or, to some extent, a flat file of records
• A relation is thought of as a table of values, each row in the table represents a collection of related data
values.
• A row represents a fact that typically corresponds to a real-world entity or relationship. The table name
and column names are used to help to interpret the meaning of the values in each row.
• In the formal relational model terminology,
o a row →a tuple, a column header →an attribute, and the table →a relation. The data type describing
the types of values that can appear in each column is represented by a domain of possible values.
• A domain D is a set of atomic values. By atomic means each value in the domain is indivisible in
formal relational model. A common method of specifying a domain is to specify a data type from which
the data values forming the domain are drawn.
• Some examples of domains follow:
USA_phone_number: string of digits of length ten SSN: string of digits of length nine
Name: string of characters beginning with an upper case letter GPA: a real number between 0.0 and 4.0
Gender: a member of the set { female, male }
Dept_Code: a member of the set { CMPS, MATH, ENGL, PHYS, PSYC, ... }
✓ A 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.
✓ Attribute: Ai is the 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).
✓ Tuple: A tuple is a mapping from attributes to values drawn from the respective domains of those
attributes. A tuple is intended to describe some entity (or relationship between entities) in the miniworld.
✓ R is called the name of this relation.
✓ The degree (or arity) of a relation is the number of attributes n of its relation schema.
✓ A relation of degree seven, which stores information about university students, would contain seven
attributes describing each student as follows:
STUDENT(Name, Ssn, Home_phone, Address, Office_phone, Age, Gpa)
✓ Relational Database: A collection of relations, each one consistent with its specified relational schema.
✓ A relation (or relation state) r of the relation schema R(A1, A2, … , An), also denoted by r(R), is a set
of n-tuples r = {t1, t2, … , tm}. Each n-tuple t is an ordered list of n values t =<v1,v2…,vn>
Characteristics of Relations
The schema-based constraints include domain constraints, key constraints, constraints on NULLs, entity
integrity constraints, and referential integrity constraints.
1. Domain Constraints
✓ Domain constraints specify that within each tuple, the value of each attribute A must be an atomic value
from the domain dom(A).
✓ The data types associated with domains typically include standard numeric data types for integers and real
numbers. Characters, Booleans, fixed-length strings, and variable-length strings are also available, as are
date, time, timestamp, and other special data types.
SuperKey
• A superkey SK specifies a uniqueness constraint that no two distinct tuples in any state r of R can have
the same value for SK.
• A key k of a relation schema R is a superkey of R with the additional property that removing any
attribute A from K leaves a set of attributes K′ that is not a superkey of R any more.
Hence, a key satisfies two properties:
1. Two distinct tuples in any state of the relation cannot have identical values for (all) the attributes in the
key. This uniqueness property also applies to a superkey.
2. It is a minimal superkey—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.
Candidate key
A relation schema may have more than one key. In this case, each of the keys is called a
candidate key. For example, the CAR relation has two candidate keys: License_number and
Engine_serial_number
Primary key
It is common to designate one of the candidate keys as the primary key of the relation. This
is the candidate key whose values are used to identify tuples in the relation. We use the
convention that the attributes that form the primary key of a relation schema are underlined.
Other candidate keys are designated as unique keys and are not underlined
• The entity integrity constraint states that no primary key value can be NULL. This is because the
primary key value is used to identify individual tuples in a relation.
• Key constraints and entity integrity constraints are specified on individual relations.
Referential Integrity Constraint
• The referential integrity constraint is specified between two relations and is used to maintain the
consistency among tuples in the two relations.
• Informally, the referential integrity constraint states that a tuple in one relation that refers to
another relation must refer to an existing tuple in that relation.
• For example, 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.
Foreign key
• The conditions for a foreign key, given below, specify a referential integrity constraint between
the two relation schemas R1 and R2.
A set of attributes FK in relation schema R1 is a foreign key of R1 that references relation R2 if it
satisfies the following rules:
1. The attributes in FK 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.
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 is NULL. In the former case, we have t1[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. If these two
conditions hold, a referential integrity constraint from R1 to R2 is said to hold.
In the EMPLOYEE relation, the attribute Dno refers to the department for which an employee works;
hence, it is designated Dno to be a foreign key of EMPLOYEE referencing the DEPARTMENT relation.
This means that a value of Dno in any tuple t1 of the EMPLOYEE relation must match a value of
Constraints the primary key of DEPARTMENT—the Dnumber attribute—in some tuple t2 of the
DEPARTMENT relation, or the value of Dno can be NULL if the employee does not belong to a department
or will be assigned to a department later.
RELATIONAL ALGEBRA
Unary and Binary relational operations
SELECT and PROJECT
The SELECT Operation
The SELECT operation is used to choose a subset of the tuples from a relation that satisfies a selection
condition.
It restricts the tuples in a relation to only those tuples that satisfy the condition.
It can also be visualized as a horizontal partition of the relation into two sets of tuples—those tuples that
satisfy the condition and are selected, and those tuples that do not satisfy the condition and are discarded.
For example, to select the EMPLOYEE tuples whose department is 4, or those whose salary is greater
than $30,000
σDno=4(EMPLOYEE)
σSalary>30000(EMPLOYEE)
where the symbol σ (sigma) is used to denote the SELECT operator and the selection condition is a
Boolean expression (condition) specified on the attributes of relation R.
The Boolean expression specified in is made up of a number of clauses of the form :
<attribute name><comparison op><constant value>
Or
<attribute name><comparison op><attribute name>
Clauses can be connected by the standard Boolean operators and, or, and not to form a general
selection condition.
For example, to select the tuples for all employees who either work in department 4 and
make over
$25,000 per year, or work in department 5 and make over $30,000:
• The Boolean conditions AND, OR, and NOT have their normal interpretation, as follows:
• (cond1 AND cond2) is TRUE if both (cond1) and (cond2) are TRUE; otherwise,it is FALSE.
• (cond1 OR cond2) is TRUE if either (cond1) or (cond2) or both are TRUE; otherwise, it is FALSE.
• (NOT cond) is TRUE if cond is FALSE; otherwise, it is FALSE.
• The SELECT operator is unary; that is, it is applied to a single relation. Hence, selection
conditions cannot involve more than one tuple.
• The degree of the relation resulting from a SELECT operation—its number of attributes—is the
same as the degree of R.
• The SELECT operation is commutative; that is,
σ (cond1)(σ(cond2)(R)) = σ(cond2)(σ(cond1)(R))
The PROJECT Operation
• The PROJECT operation, selects certain columns from the table and discards the other columns.
• The result of the PROJECT operation can be visualized as a vertical partition of the relation into two
relations: one has the needed columns (attributes) and contains the result of the operation, and the
other contains the discarded columns.
• For example, to list each employee’s first and last name and salary, we can use the PROJECT
operation as follows:
πLname, Fname, Salary(EMPLOYEE)
The general form of the PROJECT operation is
π<attribute list>(R)
o where (pi) is the symbol used to represent the PROJECT operation, and is the desired sublist of
attributes from the attributes of relation R.
o The result of the PROJECT operation has only the attributes specified in in the same order as they
appear in the list. Hence, its degree is equal to the number of attributes in <attribute list>.
o The PROJECT operation removes any duplicate tuples, so the result of the PROJECT operation is
a set of distinct tuples, and hence a valid relation. This is known as duplicate elimination.
Sequences of Operations and the RENAME Operation
• The relations shown above depict operation results do not have any names.
• Either we can write the operations as a single relational algebra expression by nesting the operations, or
we can apply one operation at a time and create intermediate result relations.
• In the latter case, we must give names to the relations that hold the intermediate results.
• For example, to retrieve the first name, last name, and salary of all employees who work in department
number 5, apply a SELECT and a PROJECT operation.
•
πFname, Lname, Salary(σDno=5(EMPLOYEE))
• Alternatively, we can explicitly show the sequence of operations, giving a name to each intermediate
relation, and using the assignment operation, denoted by ← (left arrow), as follows:
DEP5_EMPS ← σDno=5(EMPLOYEE)
RESULT ← πFname, Lname, Salary(DEP5_EMPS)
✓ Both UNION and INTERSECTION can be treated as n-ary operations applicable to any number of
relations because both are also associative operations; that is,
R ∪ (S ∪ T ) = (R ∪ S) ∪ T and (R ∩ S) ∩ T = R ∩ (S ∩ T)
✓ The MINUS operation is not commutative; that is, in general, R − S ≠ S − R
✓ The INTERSECTION can be expressed in terms of union and set difference as follows:
R ∩ S = ((R ∪ S) − (R − S)) − (S − R)
✓ The general form of a JOIN operation on two relations R(A1, A2, … , An) and S(B1, B2, … , Bm) is
R <join condition>S
✓ The result of the JOIN is a relation Q with n + m attributes Q(A1, A2, … , An, B1, B2, … , Bm) in that
order; Q has one tuple for each combination of tuples—one from R and one from S—whenever the
combination satisfies the join condition.
✓ The main difference between CARTESIAN PRODUCT and JOIN are ,In JOIN, only combinations of
tuples satisfying the join condition appear in the result, whereas in the CARTESIAN PRODUCT all
combinations of tuples are included in the result.
✓ The join condition is specified on attributes from the two relations R and S and is evaluated for each
combination of tuples. Each tuple combination for which the join condition evaluates to TRUE is included
in the resulting relation Q as a single combined tuple.
✓ A general join condition is of the form
<condition>AND<condition> AND … AND<condition>
where each <condition> is of the form Ai θ Bj , Ai is an attribute of R, Bj is an attribute of S, Ai
and Bj have the same domain, and θ (theta) is one of the comparison operators {=, <,>,< , ≥, ≠}.
A JOIN operation with such a general join condition is called a THETA JOIN.
Variations of JOIN:
The EQUIJOIN and NATURAL JOIN
✓ The most common use of JOIN involves join conditions with equality comparisons only. Such a JOIN,
where the only comparison operator used is =, is called an EQUIJOIN.
✓ Notice that in the result of an EQUIJOIN we always have one or more pairs of attributes that haveidentical
values in every tuple.
✓ For example, the values of the attributes Mgr_ssn and Ssn are identical in every tuple of DEPT_MGR (the
EQUIJOIN result) because the equality join condition specified on these two attributes requires the values
to be identical in every tuple in the result.
✓ Because one of each pair of attributes with identical values is superfluous, a new operation called
NATURAL JOIN—denoted by * was created to get rid of the second (superfluous) attribute in an
EQUIJOIN condition.
✓ The standard definition of NATURAL JOIN requires that the two join attributes (or each pair of join
attributes) have the same name in both relations. If this is not the case, a renaming operation is applied first.
✓ Suppose we want to combine each PROJECT tuple with the DEPARTMENT tuple that controls the
project. In the following example, first we rename the Dnumber attribute of DEPARTMENT to Dnum—
so that it has the same name as the Dnum attribute in PROJECT—and then we apply NATURAL JOIN:
PROJ_DEPT ← PROJECT * ρ(Dname, Dnum, Mgr_ssn, Mgr_start_date)(DEPARTMENT)
✓ The same query can be done in two steps by creating an intermediate table DEPT as follows:
DEPT ← ρ(Dname, Dnum, Mgr_ssn, Mgr_start_date)(DEPARTMENT)
PROJ_DEPT ← PROJECT * DEPT
✓ The attribute Dnum is called the join attribute for the NATURAL JOIN operation, because it is the only
attribute with the same name in both relations.
✓ In general, the join condition for NATURAL JOIN is constructed by equating each pair of join attributes
that have the same name in the two relations and combining these conditions with AND.
✓ A single JOIN operation is used to combine data from two relations so that related information can be
presented in a single table. These operations are also known as inner joins.
✓ A more general, but nonstandard definition for NATURAL JOIN is Q ← R *(list1),(list2)S
✓ In this case,<list1> specifies a list of i attributes from R, and <list2>specifies a list of i attributes from S.
✓ The NATURAL JOIN or EQUIJOIN operation can also be specified among multiple tables, leading to
an n-way join. For example, consider the following three-way join:
((PROJECT Dnum=DnumberDEPARTMENT) Mgr_ssn=SsnEMPLOYEE)
πPnumber,Dnum,Lname,Address,Bdate(((σPlocation=‘Stafford’(PROJECT)) Dnum=Dnumber(DEPARTMENT))
Mgr_ssn=Ssn(EMPLOYEE))
Dept. of CSE(AIML), GAT 14
22CML42
✓ Query tree for the abobe query.In this, the three leaf nodes P, D, and E represent the three relations
PROJECT, DEPARTMENT, and EMPLOYEE.
✓ In order to execute query , the node marked (1) in Figure must begin execution before node(2) because
some resulting tuples of operation (1) must be available before we can begin to execute operation (2).
Similarly, node (2) must begin to execute and produce results before node (3) can start execution, and so
on.
✓ In general, a query tree gives a good visual representation and understanding of the query in terms of the
relational operations it uses and is recommended as an additional means for expressing queries in relational
algebra.
✓ Mathematical aggregate functions on collections of values from the database. Examples of such functions
include retrieving the average or total salary of all employees or the total number of employee tuples.
✓ Common functions applied to collections of numeric values include SUM, AVERAGE, MAXIMUM,
and MINIMUM.
The COUNT function is used for counting tuples or values.
✓ AGGREGATE FUNCTION operation defined, using the symbol ℑ , to specify these types of requests
as follows:
<grouping attribute>ℑ<function list> (R)
Where<grouping attribute> is a list of attributes of the relation specified in R, and <function list>is a list
of (<function><attribute> ) pairs.
✓ In each such pair, is one of the allowed functions—such as SUM, AVERAGE, MAXIMUM,
MINIMUM, COUNT—and is an attribute of the relation specified by R.
✓ The resulting relation has the grouping attributes plus one attribute for each element in the function list.
✓ For example, to retrieve each department number, the number of employees in the department, and their
average salary, while renaming the resulting attributes as indicated below, we write:
ρR(Dno, No_of_employees, Average_sal) (Dno ℑ COUNT Ssn, AVERAGE Salary (EMPLOYEE))
✓ If we do not want to rename the attributes then the above query we can write it as,
Dno ℑ COUNT Ssn, AVERAGE Salary(EMPLOYEE)
✓ Note: If no grouping attributes are specified, the functions are applied to all the tuples in the relation, so
the resulting relation has a single tuple only.
NULL value. We can apply an operation LEFT OUTER JOIN, denoted by , to retrieve the result
as follows:
TEMP ← (EMPLOYEE Ssn=Mgr_ssnDEPARTMENT)
RESULT ← πFname, Minit, Lname, Dname(TEMP)
✓ 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 with NULL values.
✓ A similar operation, RIGHT OUTER JOIN, denoted by , keeps every tuple in the second, or right,
Query 1. Retrieve the name and address of all employees who work for the
Query 3. Find the names of employees who work on all the projects controlled by
department number 5.
Dept. of CSE(AIML), GAT 17
22CML42
Query 4. Make a list of project numbers for projects that involve an employee whose last name is
that controls the project.
Query 5. List the names of all employees with two or more dependents.
Query 7. List the names of managers who have at least one dependent.