Date post: | 03-Jan-2016 |
Category: |
Documents |
Upload: | sloane-cannon |
View: | 18 times |
Download: | 0 times |
Entity-Relationship ModelCont…..
CST203-2 Database Management SystemsLecture 5
There are 2 formal languages for relational model
Relational algebraRelational calculus
Relational Algebra
What is relational algebra?
The result?
Sequence of relational algebra
Divided into 2 groups
Special operationsSelection Projection Join Rename
Set operationsUnionIntersectionSet difference
Similar to normal algebra
Operation My HTML Symbol
Projection PROJECT
Selection SELECT
Renaming RENAME
Union UNION
Intersection INTERSECTION
SELECT operation
σ σ<Selection Condition> (R)
σ – SELECT operator Selection Condition : Boolean expression R : relation
If more selection conditions,Use ‘OR’, ‘AND’ and ‘NOT’
Horizontal partition
Select the students who has the GPA greater than 3.5
σ σGPA > 3.5 (STUDENT)Student TableNID Name StudentId ExamId GPA
+++++++++
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
5 Sarah 300
7 Tom 100
idname
salary
1 John 100
7 Tom 100
idname
salary
7 Tom 100
σ<Condition1>(σ<Condition2>(R)) =
σ<Condition2>(σ<Condition1>(R))
σ<Cond1>(σ<Cond2>(σ<Cond3>(R))) =
σ<Cond1> AND <Cond2> AND <Cond3>(R)
Assignment
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
Name
ENo DOB Address
Sex Salary
DNo
PROJECT operationIf 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 100
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 100
Assignment
Write the relational algebra for selecting Name, Eno, and Address
Name
ENo DOB Address
Sex Salary
DNo
Sequence of operations STU_1stCLASS Πname(σGPA > 3.5
(STUDENT))
Assignment
Write the relational algebra for selecting Name, Eno, and Address of all male employees whose salary is greater than 20,000
Name
ENo DOB Address
Sex Salary
DNo
CARTESIAN PRODUCTeid
ename dept
1 Bill A
2 Sarah C
3 John A
dnr dname
A Marketing
B Sales
C LegalSQL Result Relational algebra
select * from Employee, Department
E X D
enr
ename dept dnr dname
1 Bill A A Marketing
1 Bill A B Sales
1 Bill A C Legal
2 Sarah C A Marketing
2 Sarah C B Sales
2 Sarah C C Legal
3 John A A Marketing
3 John A B Sales
3 John A C Legal
INNER JOIN
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 A Marketing
2 Sarah C C Legal
3 John A A Marketing
eidename dept
1 Bill A
2 Sarah C
3 John A
dnr dname
A Marketing
B Sales
C Legal
UNION operationResult 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
Kamal
INTERSECTION operationResult 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 DIFFERENCEAlso called as MINUS
Result Result1 – Result2
Result 1
Result 2
Result
Name
Amal
Sunil
Name
Amal
Name
Sunil