+ All Categories
Home > Documents > Entity-Relationship Model Cont…..

Entity-Relationship Model Cont…..

Date post: 03-Jan-2016
Category:
Upload: sloane-cannon
View: 18 times
Download: 0 times
Share this document with a friend
Description:
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. - PowerPoint PPT Presentation
27
Entity-Relationship Model Cont….. CST203-2 Database Management Systems Lecture 5
Transcript
Page 1: Entity-Relationship Model Cont…..

Entity-Relationship ModelCont…..

CST203-2 Database Management SystemsLecture 5

Page 2: Entity-Relationship Model Cont…..

There are 2 formal languages for relational model

Relational algebraRelational calculus

Page 3: Entity-Relationship Model Cont…..

Relational Algebra

Page 4: Entity-Relationship Model Cont…..

What is relational algebra?

The result?

Sequence of relational algebra

Divided into 2 groups

Page 5: Entity-Relationship Model Cont…..

Special operationsSelection Projection Join Rename

Set operationsUnionIntersectionSet difference

Page 6: Entity-Relationship Model Cont…..

Similar to normal algebra

Operation My HTML Symbol

Projection PROJECT

Selection SELECT

Renaming RENAME

Union UNION

Intersection INTERSECTION

Page 7: Entity-Relationship Model Cont…..

SELECT operation

σ σ<Selection Condition> (R)

σ – SELECT operator Selection Condition : Boolean expression R : relation

If more selection conditions,Use ‘OR’, ‘AND’ and ‘NOT’

Page 8: Entity-Relationship Model Cont…..

Horizontal partition

Select the students who has the GPA greater than 3.5

σ σGPA > 3.5 (STUDENT)Student TableNID Name StudentId ExamId GPA

Page 9: Entity-Relationship Model Cont…..

+++++++++

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

Page 10: Entity-Relationship Model Cont…..

σ<Condition1>(σ<Condition2>(R)) =

σ<Condition2>(σ<Condition1>(R))

σ<Cond1>(σ<Cond2>(σ<Cond3>(R))) =

σ<Cond1> AND <Cond2> AND <Cond3>(R)

Page 11: Entity-Relationship Model Cont…..

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

Page 12: Entity-Relationship Model Cont…..

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

Page 13: Entity-Relationship Model Cont…..

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

Page 14: Entity-Relationship Model Cont…..

Assignment

Write the relational algebra for selecting Name, Eno, and Address

Name

ENo DOB Address

Sex Salary

DNo

Page 15: Entity-Relationship Model Cont…..

Sequence of operations STU_1stCLASS Πname(σGPA > 3.5

(STUDENT))

Page 16: Entity-Relationship Model Cont…..

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

Page 17: Entity-Relationship Model Cont…..

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

Page 18: Entity-Relationship Model Cont…..

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

Page 19: Entity-Relationship Model Cont…..

UNION operationResult 1 Πname(σGPA > 3.5 (STUDENT))

Result 2 Πname(σ(GPA > 2.5 AND GPA < 3.5)

(STUDENT))

Result Result1 υ Result 2

Page 20: Entity-Relationship Model Cont…..

Result 1

Result 2

Result

Name

Amal

Sunil

Name

Kamal

Name

Amal

Sunil

Kamal

Page 21: Entity-Relationship Model Cont…..
Page 22: Entity-Relationship Model Cont…..

INTERSECTION operationResult 1 Πname(σGPA > 3.5 (STUDENT))

Result 2 Πname(σ(GPA > 2.5) (STUDENT))

Result Result1 Result 2

υ

Page 23: Entity-Relationship Model Cont…..

Result 1

Result 2

Result

Name

Amal

Sunil

Name

Amal

Name

Amal

Page 24: Entity-Relationship Model Cont…..
Page 25: Entity-Relationship Model Cont…..

SET DIFFERENCEAlso called as MINUS

Result Result1 – Result2

Page 26: Entity-Relationship Model Cont…..

Result 1

Result 2

Result

Name

Amal

Sunil

Name

Amal

Name

Sunil

Page 27: Entity-Relationship Model Cont…..

Recommended