0% found this document useful (0 votes)
17 views

Dbms Join

DBMS

Uploaded by

veronica
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Dbms Join

DBMS

Uploaded by

veronica
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 96

UML class diagrams

• Represent classes (similar to entity types) as large rounded boxes with


three sections:
• Top section includes entity type (class) name
• Second section includes attributes
• Third section includes class operations (operations are not in basic ER model)
• Relationships (called associations) represented as lines connecting the
classes
• Other UML terminology also differs from ER terminology
• Used in database design and object-oriented software design

Slide 3- 14
UML class diagrams

• Represent classes (similar to entity types) as large rounded boxes with


three sections:
• Top section includes entity type (class) name
• Second section includes attributes
• Third section includes class operations (operations are not in basic ER model)
• Relationships (called associations) represented as lines connecting the
classes
• Other UML terminology also differs from ER terminology
• Used in database design and object-oriented software design

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

SMITH ← σFname=‘John’ AND Lname=‘Smith’(EMPLOYEE)


SMITH_PNOS ← πPno(WORKS_ON X Essn= Ssn SMITH)
SSN_PNOS ← πEssn, Pno(WORKS_ON)
SSNS(Ssn) ← SSN_PNOS ÷ SMITH_PNOS
DIVISION T<---- R÷ S
SSNS(Ssn) ← SSN_PNOS ÷ SMITH_PNOS
Practice/assignment

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))

SUPERVISION(Ssn1, Ssn2) ← πSsn,Super_ssn(EMPLOYEE)


Retrieve the supervisees of ‘James Borg’ at all levels

RESULT1(Ssn) ← πSsn1(SUPERVISION X Ssn2=Ssn BORG_SSN)


RESULT2(Ssn) ← πSsn1(SUPERVISION X Ssn2=Ssn RESULT1)
RESULT ← RESULT2 ∪ RESULT1
Additional Relational Operations:
The OUTER JOIN
• In NATURAL JOIN and EQUIJOIN, tuples without a matching(or
related) tuple are eliminated from the join result

• Tuples with null in the join attributes are also eliminated


• This amounts to loss of information
• To keep all tuples from both relation,outer join is used.
The OUTER JOIN Operation
The OUTER JOIN Operation
TEMP ← (EMPLOYEE X Ssn=Mgr_ssn DEPARTMENT)

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 ?

• How do we create tables using the relations and the relationship


constraints e etc..
Mapping
• ER-to-Relational Mapping Algorithm
• Step 1: Mapping of Regular Entity Types
• Step 2: Mapping of Weak Entity Types
• Step 3: Mapping of Binary 1:1 Relation Types
• Step 4: Mapping of Binary 1:N Relationship Types.
• Step 5: Mapping of Binary M:N Relationship Types.
• Step 6: Mapping of Multivalued attributes.
• Step 7: Mapping of N-ary Relationship Types.
Step 1: Mapping of Regular Entity Types

• For each regular (strong) entity type E in the ER


diagram, create a relation R that includes all the
simple attributes of E.
• Choose one of the key attribute as primary key

Employee

Fname Minit Lname SSN Bdate Address Sex Salary


Step 2: Mapping of Weak Entity Types

• 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 as foreign key attributes of R the primary key attribute(s) of the


relation(s) that correspond to the owner entity type(s).

• 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

The primary key of the DEPENDENT relation is the


combination {ESSN, DEPENDENT_NAME}

SSN: Primary key of Owner entity EMPLOYEE


Dependent_name: Partial key of weak entity
DEPENDENT
Step 3: Mapping of Binary 1:1
Relation Types
• There are three possible approaches:
• 1.Foreign Key approach: Identify the relations T and
S. Include a foreign key in S the primary key of T.
• Choose an entity type with total participation in R in
the role of S.
• Example: 1:1 relation MANAGES is mapped by choosing
the participating entity type DEPARTMENT because its
participation in the MANAGES relationship type is total.
Step 3: Mapping of Binary 1:1
Relation 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.

3.Cross-reference or relationship relation option: Set up a third


relation R for the purpose of cross-referencing the primary keys of the
two relations S and T representing the entity types
Step 4: Mapping of Binary 1:N
Relationship Types
• 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

• Include as foreign key in S the primary key of the


relation T that represents the other entity type
participating in R
• Include any simple attributes of the 1:N relation type
as attributes of S
Step 4: Mapping of Binary 1: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
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.

• 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 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.

• For each multivalued attribute A, create a new relation R.

• This relation R will include an attribute corresponding to A, plus the


primary key attribute K-as a foreign key in R-of the relation

• The primary key of R is the combination of A and K. If the multivalued


attribute is composite, we include its simple components.
Step 6: Mapping of Multivalued attributes.

• Example: The relation DEPT_LOCATIONS is created.


• The primary key of R is the combination of
{DNUMBER, DLOCATION}.

Department
Dname Dnumber Mgr_ssn Mgr_start_date

Dept_locations
Dnumber Dlocation
Final Mapping
Step 7: Mapping of N-ary Relationship Types.

• For each n-ary relationship type R, where n>2, create a new


relationship S to represent 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 (or
simple components of composite attributes) as attributes of S.
Step 7: Mapping of N-ary Relationship Types.

• Example: The relationship type SUPPY

• This can be mapped to the relation SUPPLY shown in the relational


schema, whose primary key is the combination of the three foreign
keys {SNAME, PARTNO, PROJNAME}
The SUPPLY relationship
Mapping the n-ary relationship type
SUPPLY
Summary of Mapping constructs
and constraints Table
END OF UNIT 3
Test1
1.Explain the advantages of DBMS over Traditional File system

2.Discuss about different DBMS users.

3.Explain three schema architecture with proper diagram

4.What is data model? Explain the difference between logical


data and physical data independence.

5.With a proper diagram explain component module of DBMS

6.Discuss different DBMS languages.


More Examples
SQL INNER JOIN Keyword
• The INNER JOIN keyword return rows when there is at least one
match in both tables
SELECT column_name(s)
FROM table_name1 INNER JOIN table_name2 ON
table_name1.column_name = table_name2.column_name
SQL INNER JOIN Example
SQL LEFT JOIN (LEFT OUTER JOIN)
SQL LEFT JOIN contd…
SQL RIGHT JOIN (RIGHT OUTER
JOIN )
SQL RIGHT JOIN (RIGHT OUTER
JOIN ) contd…
SQL FULL JOIN
SQL FULL JOIN
SQL FULL JOIN contd
Important Questions
1.Discuss the characteristics of a relation.
2.Explain entity integrity constraint.
3.Explain How referential integrity constraint can be implemented?
4.What is foreign key? Can a foreign key exist for a single table?
5.Explain briefly SELECT,PROJECT and RENAME operation.
6.Explain briefly UNION,INTERSECTION and SET DIFERRENCE
with example.
7.Discuss with example ER to relational mapping transformation.
8.Discuss all possible options that can be specified when
referential integrity constraint is violated with proper example.
9. How assertion and trigger is defined in SQL. Give some example.
Important Questions
10.How a view is defined? What are the problems associated with
updating a view?
11.Explain embedded SQL ,Dynamic SQL and SQL/PSM with proper
example
12. Discuss about stored procedure with example
13.How will you connect to a database to fetch records and display.
14.Why Cursor is required?
15. What is SQL CODE and SQL STATE?
16. What is impedance mismatch problem?
17.What are the different DB programming approach?
In-Class Exercise

You might also like