Dbms Join
Dbms Join
Slide 3- 14
UML class diagrams
Slide 3- 15
Some properties of JOIN
Some properties of JOIN
EQUIJOIN
NATURAL JOIN Operation
NATURAL JOIN Operation
Objective: We intend to join DEPARTMENT and
DEPT_LOCATIONS
For natural join to be done between two tables, what’s the first and
foremost condition?
Ans: Attribute (column) with the same name
Natural Join between DEPARTMENT
and DEPT_LOCATIONS
DIVISION
Retrieve the SSN of employees who work on all project that john smith works
DIVISION
1.Find the SSN of employees who work on all the project controlled
by Department No 5
2. List the name of all employees with two or more dependents.
3. List the name of employees who has atleast one dependent
4. Retrieve the name of employee who has no dependent
Query Tree Notation
• For every project located in ‘Stafford’, retrieve the
project number, the controlling department number,
and the department manager’s last name, address,
and birth date
Additional Relational Operations:
Aggregate Functions and
Grouping
Aggregate Function Operation
Grouping with Aggregation
Examples
Aggregate functions and grouping
SELECT
Additional Relational Operations
Recursive Closure
• Recursive closure
• This operation is applied to a recursive relationship
• Example of a recursive operation
• retrieve all SUPERVISEES of an EMPLOYEE e at all levels
Recursive Closure
• Borg supervises - Wong , Wallace
• Wong supervises –Smith ,Narayan and English
• Wallace supervises –Zelaya and Jabbar
Retrieve the supervisees of ‘James Borg’ at all levels
Retrieve the supervisees of ‘James Borg’ at all levels
σ
BORG_SSN ← πSsn( Fname=‘James’ AND Lname=‘Borg’(EMPLOYEE))
Loss of Data
The LEFT OUTER JOIN Operation
OUTER UNION Operations
OUTER UNION Operations
OUTER UNION Example
• STUDENT(Name, SSN, Department, Advisor)
• INSTRUCTOR(Name, SSN, Department, Rank).
STUDENT INSTRUCTOR
Name SSN Depart Advisor Name SSN Depart Rank
ment ment
OUTER UNION Example
• The result relation STUDENT_OR_INSTRUCTOR will have the following
attributes:
• STUDENT_OR_INSTRUCTOR
Name SSN Department Advisor Rank
OUTER UNION Example
Examples of Queries in Relational
Algebra : Procedural Form
Examples of Queries in Relational
Algebra –Single expressions
ER-to-Relational Mapping Algorithm
• How is it useful in creating the actual tables and build the database ?
Employee
• 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.
• The primary key of R is the combination of the primary key(s) of the owner(s)
and the partial key of the weak entity type W.
Step 2: Mapping of Weak Entity Types
Employee
Fname Minit Lname SSN Bdate Address Sex Salary
Department
Dname Dnumber Mgr_ssn Mgr_start_date
Step 3: Mapping of Binary 1:1
Relation Types
2.Merged relation option: Merge the two entity types and the
relationship into a single relation. when both participations are total.
Employee
Fname Minit Lname SSN Bdate Addres Sex Salary Super_ Dno
s ssn
Department
Dname Dnumber Mgr_ssn Mgr_start_date
Project
Pname Pnumber Plocation Dnum
Step 5: Mapping of Binary M:N Relationship Types
• For each regular binary M:N relationship type R, create a new relation
S to represent R.
• Also include any simple attributes of the M:N relationship type (or
simple components of composite attributes) as attributes of S.
Step 5: Mapping of Binary M:N
Relation Types
Employee
Fname Minit Lname SSN Bdate Addres Sex Salary Super_ Dno
s ssn
Department
Dname Dnumber Mgr_ssn Mgr_start_date
Project
Pname Pnumber Plocation Dnum
Works_on
ESSN Pno Hours
Step 6: Mapping of Multivalued attributes.
Department
Dname Dnumber Mgr_ssn Mgr_start_date
Dept_locations
Dnumber Dlocation
Final Mapping
Step 7: Mapping of N-ary Relationship Types.
• Also include any simple attributes of the n-ary relationship type (or
simple components of composite attributes) as attributes of S.
Step 7: Mapping of N-ary Relationship Types.