Unit 3 Relational Data Model
Unit 3 Relational Data Model
Unit 3 Relational Data Model
The SELECT operation is used to filter and choose a subset of tuples from a
relation that satisfies a specific selection condition.
It acts as a filter, retaining only those tuples that meet the condition and
excluding those that do not.
Visualization:
Example:
σDno=4(EMPLOYEE)
σSalary>30000(EMPLOYEE)
Selection Condition:
Properties:
Commutativity: The order of SELECT operations does not affect the result:
σ<cond1>(σ<cond2>(R)) = σ<cond2>(σ<cond1>(R))
Example:
σDno=4 AND Salary>25000(EMPLOYEE)
SQL equivalent:
SELECT * FROM EMPLOYEE WHERE Dno=4 AND Salary>25000
Figures Explanation:
Boolean Operators: AND, OR, NOT are used to form complex conditions.
This detailed breakdown covers the essential aspects of the SELECT operation
in relational algebra, its syntax, usage, and how it translates to SQL queries.
Visualized as a vertical partition of the relation into two: one with needed
columns (result of the operation) and the other with discarded columns.
Example:
πLname, Fname, Salary(EMPLOYEE)
This lists each employee’s last name, first name, and salary.
Resulting Relation:
Duplicate Elimination:
The PROJECT operation removes any duplicate tuples, ensuring the result
is a set of distinct tuples.
Example:
πSex, Salary(EMPLOYEE)
Properties:
The number of tuples in the resulting relation is always less than or equal to
the number of tuples in R.
If the projection list is a superkey of R, the resulting relation has the same
number of tuples as R.
SQL Correspondence:
SQL equivalent:
SELECT DISTINCT Sex, Salary FROM EMPLOYEE
Example of Sequence:
To retrieve the first name, last name, and salary of all employees in
department 5:
The RENAME operation can rename the relation name, attribute names, or
both.
Denoted by:
ρS(B1, B2, ... , Bn)(R)
ρS(R)
ρ(B1, B2, ... , Bn)(R)
SQL Correspondence:
Example:
SELECT E.Fname AS First_name, E.Lname AS Last_name, E.Salary AS
Salary
FROM EMPLOYEE AS E
WHERE E.Dno=5
These notes cover the essential aspects of the PROJECT operation, its syntax,
usage, the concept of sequences of operations, and the RENAME operation in
relational algebra, including how they translate to SQL queries.
∪
1. UNION (R S): This operation combines all tuples from both relations,
eliminating duplicates. The result is a relation containing all unique tuples
that are in either relation or both.
4. Select tuples where the employee's Ssn matches the dependent's Essn:
ACTUAL_DEPENDENTS ← σSsn = Essn(EMP_DEPENDENTS)
Summary
These set theory operations (UNION, INTERSECTION, and MINUS) and the
CARTESIAN PRODUCT are fundamental
Referential Integrity:
Mgr_ssn is a foreign key in the DEPARTMENT relation referencing Ssn in the
EMPLOYEE relation, ensuring matching tuples in the EMPLOYEE relation.
Example sequence:
Replaced by:
R <join condition> S
Ai is an attribute of R.
Bj is an attribute of S.
Result Inclusion:
Tuples are included in the resulting relation Q if the join condition evaluates to
TRUE. Does not necessarily preserve all information from the participating
relations.
Example Result:
Table: DEPT_MGR
Columns: Dname, Dnumber, Mgr_ssn, Fname, Minit, Lname, Ssn
Example rows:
NATURAL JOIN:
Denoted by *. Removes superfluous attributes from EQUIJOIN. Requires join
attributes to have the same name in both relations.
Example:
Combine PROJECT and DEPARTMENT by renaming Dnumber to Dnum:
Resulting relation:
Each tuple combines PROJECT tuple with DEPARTMENT tuple controlling the
project. Only one join attribute value is kept.
No Renaming Needed:
If join attributes already have the same names:
Example: DEPARTMENT and DEPT_LOCATIONS on Dnumber:
Join Condition:
Constructed by equating pairs of join attributes with the same name in both
relations.
Example result:
Empty Result:
If no combination satisfies the join condition, the result is an empty relation.
Result size ranges from zero to nR * nS, where nR and nS are the number of
tuples in R and S.
CROSS JOIN:
If no join condition, all combinations of tuples qualify. Degenerates into a
CARTESIAN PRODUCT or CROSS PRODUCT.
SQL Implementation:
JOIN can be realized in several ways in SQL:
INTERSECTION:
R ∩ S ≡ (R ∪ S) – ((R – S) ∪ (S – R))
JOIN:
R <condition> S ≡ σ<condition>(R × S)
Convenience Operations:
Operations like JOIN are important for convenience and common usage. Not
strictly necessary for the expressive power of relational algebra. Other
operations, such as NATURAL JOIN, are also included for convenience.
Grouping:
Grouping involves grouping tuples by the value of some attributes and applying
an aggregate function independently to each group.
Example:
To retrieve department numbers, the number of employees, and their average
salary:
ρ(Dno, No_of_employees, Average_sal) (Dno ℑ COUNT Ssn, AVERAGE Salary
(EMPLOYEE))
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.
Query 3:
Find the names of employees who work on all the projects controlled by
department number 5.
Query 4:
Make a list of project numbers for projects that involve an employee whose last
name is 'Smith', either as a worker or as a manager of the department that
controls the project.
Query 5:
List the names of all employees with two or more dependents.
Query 6:
Retrieve the names of employees who have no dependents.
Query 7:
List the names of managers who have at least one dependent.