What is Relational model
Characteristics
Relational constraints
Representation of schemas
characteristics and Constraints of Relational model with proper examples.
Updates and dealing with constraint violations in Relational model
2. Session objective
Quick look of previous Session
Mapping Class hierarchies
Relational algebraic expressions
RA operations
Introduction to TRC
3. Textbook Reference Book(s) & other resources
T1 Ramez Elmasri & Shamkant B. Navathe, Fundamentals of
Database Systems, Pearson Education, 5th Edition, 2007
Reference Book(s) & other resources
R1 Abraham Silberschatz, Henry F Korth and S Sudarshan,
Database System Concepts, McGraw Hill, 6th Ed., 2013
Text Book
4. How does a SQL engine work ?
SQL query relational algebra plan
Relational algebra plan Optimized plan
Execute each operator of the plan
DBMS Architecture
5. Formalism for creating new relations from existing
ones
Its place in the big picture:
Declarative
query
language
Algebra Implementation
SQL,
relational calculus
Relational algebra
Relational bag algebra
Relational Algebra
6. Query languages: Allow manipulation and retrieval of
data from a database.
Relational model supports simple, powerful QLs:
Strong formal foundation based on logic.
Allows for much optimization.
Query Languages != programming languages!
QLs not expected to be “Turing complete”.
QLs not intended to be used for complex calculations.
QLs support easy, efficient access to large data sets.
Relational Query Languages
8. Core of Relational query language
Provides framework query implementation and
optimization
RA operations
Operations
Set operations
• Union
• Intersection,
• set,
• diff,
• Cartesian product
Op dedicated to RA
9. 1. Union and 2. Difference
R1 R2
Example:
ActiveEmployees RetiredEmployees
R1 – R2
Example:
AllEmployees -- RetiredEmployees
RA operations
10. SAME NUMBER OF ATTRIBUTES
Same domain as ith attribute of R2
r1Ur2=t| t Є r1 OR t Є r2
R1=STUDENT R2=PLAYERS
Union
ID NAME
1 A
2 B
3 C
4 D
ID NAME
1 D
2 A
3 C
4 B
12. It is a derived operator
SAME NUMBER OF ATTRIBUTES
Same domain as ith attribute of R2
r1Ur2=t| t Є r1 AND t Є r2
Eg. All customer has both account and loan
R1=DEPOSITOR R2=BORROWER
Intersection
ID NAME
1 A
2 B
3 C
9 z
ID NAME
2 B
3 A
5 D
10 y
13. 3. Selection
Returns all tuples which satisfy a condition
Notation: sc(R)
Examples
sSalary > 40000 (Employee)
sname = “Smith” (Employee)
The condition c can be =, <, , >, , <>
RA operations
14. R= sSalary > 40000 (Employee)
SSN Name Salary
1234545 John 200000
5423341 Smith 600000
4352342 Fred 500000
SSN Name Salary
5423341 Smith 600000
4352342 Fred 500000
15. 4. Projection
Eliminates columns, then removes duplicates
Notation: P A1,…,An (R)
Example: project social-security number and
names:
P SSN, Name (Employee)
Output schema: Answer(SSN, Name)
It returns a set . But it will not repeat a element.
RA operations
Select :
It gives complete records.
It is unary operations.
16. P Name,Salary (Employee)
SSN Name Salary
1234545 John 200000
5423341 John 600000
4352342 John 200000
Name Salary
John 20000
John 60000
17. 4. Projection
Suppose R1( A ,B) and R2( C ,D ) are two
relations.b is foreign key refer to C in R2.R1 and R2
satisfy referential integrity constraint . Which is
true:
A) P B ( r1)- P C ( r2)=Ø
B) P C( r2)- PB ( r1)=Ø
C) P B ( r1)= P C ( r2)
D) P B ( r1)- P C ( r2)!=Ø
RA operations
18. 5. Cartesian Product/cross product
Each tuple in R1 with each tuple in R2
Notation: R1 R2
Example:
Employee Dependents
Very rare in practice; mainly used to express joins
Costly operation
Generally select operation
RA operations
19. Cartesian Product Example
Employee
Name SSN
John 999999999
Tony 777777777
Dependents
EmployeeSSN Dname
999999999 Emily
777777777 Joe
Employee x Dependents
Name SSN EmployeeSSN Dname
John 999999999 999999999 Emily
John 999999999 777777777 Joe
Tony 777777777 999999999 Emily
Tony 777777777 777777777 Joe
20. 5. Cartesian Product/cross product
RA operations
NO RELATION THE TABLES
P Name [sG=A (STUDENT X GRADE)]
R1=STUDENT R2=GRADE
ID NAME
1 AA
2 BB
3 CC
ID GRADE
2 A
3 B
4 A
21. 6. Rename
Its indicated by ρ
NO RELATION THE TABLES
P Name [sG=A (STUDENT X GRADE)]
The resultant relation after cross product it can be given a
name that can be treated as a new table or relation
ρStudentGrade=[sG=A (STUDENT X GRADE)]
Table/Relation name can be renamed
ρEmployeeTable(Employee)
Attribute can also be renamed
ρID/EmpId(EmployeeTable)
RA operations
22. Joins :
Joins
Inner Join: o/p or result
contains matching tuples
Types:=
1. Theta Join
2.Equi-Join
3. Natural Join
Outer join : All the rows from
both or any relations
Types :=
1. Left outer
2.Right outer
3. Full outer
23. Natural Join
Given the schemas R(A, B, C, D), S(A, C, E), what is
the schema of R S ?
Given R(A, B, C), S(D, E), what is R || S ?
Given R(A, B), S(A, B), what is R || S ?
RA operations
24. Theta Join
A join that involves a predicate
R1 || q R2 = s q (R1 R2)
Here q can be any condition
RA operations
25. Eq-join
A theta join where q is an equality
R1 || A=B R2 = s A=B (R1 R2)
Example:
Employee || SSN=SSN Dependents
Most useful join in practice
RA operations
26. Semijoin
R | S = P A1,…,An (R || S)
Where A1, …, An are the attributes in R
Example:
Employee | Dependents
RA operations
27. Semijoins in Distributed Databases
Semijoins are used in distributed databases
SSN Name
. . . . . .
SSN Dname Age
. . . . . .
Employee
Dependents
network
Employee | ssn=ssn (s age>71 (Dependents))
T = P SSN s age>71 (Dependents)
R = Employee | T
Answer = R || Dependents
RA operations