Entity-Relationship Model Cont….. CST203-2 Database Management Systems Lecture 5
There are 2 formal languages for relational model Relational algebra Relational calculus
Relational Algebra
What is relational algebra? The result? Sequence of relational algebra Divided into 2 groups
Special operations Set operations Selection Projection Join Rename Union Intersection Set difference
Similar to normal algebra Operation My HTML Symbol Projection PROJECT Selection SELECT Renaming RENAME Union UNION Intersection INTERSECTION
σ σ<Selection Condition> (R) SELECT operation σ σ<Selection Condition> (R) σ – SELECT operator Selection Condition : Boolean expression R : relation If more selection conditions, Use ‘OR’, ‘AND’ and ‘NOT’
Select the students who has the GPA greater than 3.5 Horizontal partition Select the students who has the GPA greater than 3.5 σ σGPA > 3.5 (STUDENT) Student Table NID Name StudentId ExamId GPA
σsalary < 200(Employee) σsalary < 200 and nr >= 7(Employee) +++++++++ id name salary 1 John 100 5 Sarah 300 7 Tom SQL Result Relational algebra select * from E where salary < 200 σsalary < 200(Employee) select * from E where salary < 200 and nr >= 7 σsalary < 200 and nr >= 7(Employee) id name salary 1 John 100 7 Tom id name salary 7 Tom 100
σ<Condition1>(σ<Condition2>(R)) = σ<Condition2>(σ<Condition1>(R)) σ<Cond1>(σ<Cond2>(σ<Cond3>(R))) = σ<Cond1> AND <Cond2> AND <Cond3>(R)
Assignment Name ENo DOB Address Sex Salary DNo Write the relational algebra for selecting all details whose department is 4 and whose salary is greater than 30,000 Write it in another way
PROJECT operation If want to choose a subset of the columns in a relation and discards the rest, Use Π Π Name, GPA (Student) Π <attribute list>(R)) Π<list1>(Π<list2>(R))) = Π<list>(R))
id name salary 1 John 100 5 Sarah 300 7 Tom SQL Result Relational algebra select salary from E PROJECTsalary(E) select nr, salary from E PROJECTnr, salary(E) salary 100 300 nr salary 1 100 5 300 7
Assignment Name ENo DOB Address Sex Salary DNo Write the relational algebra for selecting Name, Eno, and Address
Sequence of operations STU_1stCLASS Πname(σGPA > 3.5 (STUDENT))
Assignment Name ENo DOB Address Sex Salary DNo Write the relational algebra for selecting Name, Eno, and Address of all male employees whose salary is greater than 20,000
CARTESIAN PRODUCT eid ename dept 1 Bill A 2 Sarah C 3 John dnr dname A Marketing B Sales C Legal SQL Result Relational algebra select * from Employee, Department E X D enr ename dept dnr dname 1 Bill A Marketing B Sales C Legal 2 Sarah 3 John
INNER JOIN eid ename dept 1 Bill A 2 Sarah C 3 John dnr dname A Marketing B Sales C Legal SQL Result Relational algebra select * from E, D where dept = dnr SELECTdept = dnr (E X D) or, using the equivalent join operation E JOINdept = dnr D eid name dept dnr dname 1 Bill A Marketing 2 Sarah C Legal 3 John
UNION operation Result Result1 υ Result 2 Result 1 Πname(σGPA > 3.5 (STUDENT)) Result 2 Πname(σ(GPA > 2.5 AND GPA < 3.5) (STUDENT)) Result Result1 υ Result 2
Result 1 Result 2 Result Name Amal Sunil Name Kamal Name Amal Sunil
INTERSECTION operation Result 1 Πname(σGPA > 3.5 (STUDENT)) Result 2 Πname(σ(GPA > 2.5) (STUDENT)) Result Result1 Result 2 υ
Result 1 Result 2 Result Name Amal Sunil Name Amal Name Amal
SET DIFFERENCE Also called as MINUS Result Result1 – Result2
Name Amal Sunil Result 1 Result 2 Result Name Amal Name Sunil