lt11-relalg

30

Transcript of lt11-relalg

Page 1: lt11-relalg

7/29/2019 lt11-relalg

http://slidepdf.com/reader/full/lt11-relalg 1/30

Page 2: lt11-relalg

7/29/2019 lt11-relalg

http://slidepdf.com/reader/full/lt11-relalg 2/30

Database Systems &Applications

Lec 11Relational Algebra 

Page 3: lt11-relalg

7/29/2019 lt11-relalg

http://slidepdf.com/reader/full/lt11-relalg 3/30

3

Algebra

• Algebra is Mathematical system consisting of:

– Operands --- variables or values from which new

values can be constructed.

– Operators --- symbols denoting procedures that

construct new values from given values.

Page 4: lt11-relalg

7/29/2019 lt11-relalg

http://slidepdf.com/reader/full/lt11-relalg 4/30

4

Algebra

• Operands are relations or variables that represent

relations.

• Operators are designed to do the most common things

that we need to do with relations in a database.

– The result is an algebra that can be used as a query 

language for relations.

Page 5: lt11-relalg

7/29/2019 lt11-relalg

http://slidepdf.com/reader/full/lt11-relalg 5/30

5

Relational Algebra

• There is a core relational algebra that has traditionally

been thought of as the relational algebra.

•• But there are several other operators we shall add to the

core in order to model better the SQL language --- the

principal language used in relational database systems.

Page 6: lt11-relalg

7/29/2019 lt11-relalg

http://slidepdf.com/reader/full/lt11-relalg 6/30

6

Core Relational Algebra

• Union, Intersection, and Difference.

– Usual set operations, but require both operands have

the same relation schema.

• Selection: picking certain rows.

• Projection: picking certain columns.

• Products and  joins: compositions of relations.

• Renaming of relations and attributes.

Page 7: lt11-relalg

7/29/2019 lt11-relalg

http://slidepdf.com/reader/full/lt11-relalg 7/30

7

Set Operations

•  Note: R and S same relation schema. (i.e.,

the structure of R and S is same)• Union: R S . The union of R and S, is the set of

elements that are in R or S or both.

Note: an element appears only once in the union even

it is present in both R and S.• Intersection: R S . The intersection of R and S, is the

set of elements that both in R and S.

• Difference: R – S . The difference of R and S, is the set

of elements that are in R but not in S.

  Note: R – S is different from S – R. S – R is the set of

elements that are in S but not in R.

Page 8: lt11-relalg

7/29/2019 lt11-relalg

http://slidepdf.com/reader/full/lt11-relalg 8/30

scope of set operationslimited to union compatibleunion compatible

relationsrelations

Page 9: lt11-relalg

7/29/2019 lt11-relalg

http://slidepdf.com/reader/full/lt11-relalg 9/30

9

Union Compatible Relations

• Two relations are union compatibleunion compatible if

–Both have same number of columns

– Names of attributes are the same in both

– Attributes with the same name in both

relations have the same domain.

Page 10: lt11-relalg

7/29/2019 lt11-relalg

http://slidepdf.com/reader/full/lt11-relalg 10/30

10

Example

Tables:  PersonPerson (P-ID, Name, Address, Hobby )

  ProfessorProfessor (Id, Name, Office, Phone)

are not union compatible. 

But

  π Name

 (PersonPerson)  and π Name

 (ProfessorProfessor)

are union compatible so

  π Name

 (PersonPerson) -  π Name

 (ProfessorProfessor)

makes sense.

Page 11: lt11-relalg

7/29/2019 lt11-relalg

http://slidepdf.com/reader/full/lt11-relalg 11/30

Example

• Student(ID,Name,DOB,Address)

• Faculty (psrn,Name,Courses)

π Name

(Student) U π Name

(Faculty) ?

π Name

(Student) π Name

(Faculty) ?

π Name

(Student) – π Name

(Faculty) ?

π Name

(Faculty) – π Name

(Student ) ?

Page 12: lt11-relalg

7/29/2019 lt11-relalg

http://slidepdf.com/reader/full/lt11-relalg 12/30

12

Selection

• R1 := σ C (R2)

– C  is a condition (as in “if” statements) that

refers to attributes of R2.

– R1 is all those tuples of R2 that satisfy C .

Page 13: lt11-relalg

7/29/2019 lt11-relalg

http://slidepdf.com/reader/full/lt11-relalg 13/30

13

Example 1• Relation Registers

• 2004P7PS001Menu := σ st_id=“2004P7PS001” (Registers):

st_id c_no sem year

2009P7PS001 CSGC352 2 2009-10

2009P7PS020 CSGC352 2 2009-10

2009P7PS001 CSGC342 2 2009-10

2009P7PS005 CSGC352 2 2009-10

st_id c_no sem year2009P7PS001 CSGC352 2 2009-10

2009P7PS001 CSGC342 2 2009-10

Page 14: lt11-relalg

7/29/2019 lt11-relalg

http://slidepdf.com/reader/full/lt11-relalg 14/30

14

Example 2

•Produce table containing subset of rows of argument table satisfying condition

σcondition (relation)

Example:

PersonPerson σHobby =‘stamps’(PersonPerson)

 

1123 John 123 VSP stamps1123 John 123 VSP coins

5556 Mary 7 VJY reading

9876 Samu 5 HYD stamps

1123 John 123 VSP stamps9876 Samu 5 HYD stamps

  Id Name Address Hobby  Id Name Address Hobby

Page 15: lt11-relalg

7/29/2019 lt11-relalg

http://slidepdf.com/reader/full/lt11-relalg 15/30

15

Selection Condition

• Operators: <, ≤ , ≥ , >, =, ≠

• Simple selection condition:

– <attribute> operator <constant >

– <attribute> operator <attribute>

• <condition> AND <condition>

• <condition> OR <condition>

• NOT <condition>

Page 16: lt11-relalg

7/29/2019 lt11-relalg

http://slidepdf.com/reader/full/lt11-relalg 16/30

16

Selection Condition - Examples

∀σ   Id>3000  OR Hobby=‘reading’ (PersonPerson)

∀σ   Id>3000 AND Hobby=‘stamps’  (PersonPerson)

∀σ   NOT(Hobby=‘reading’) (PersonPerson)

∀σ   Hobby ≠  ‘reading’ (PersonPerson)

Page 17: lt11-relalg

7/29/2019 lt11-relalg

http://slidepdf.com/reader/full/lt11-relalg 17/30

17

Projection

• R1 := π L (R2)

– L is a list of attributes from the schema of R2.

– R1 is constructed by looking at each tuple of R2,

extracting the attributes on list L, in the order

specified, and creating from those components a

tuple for R1.

– Eliminate duplicate tuples, if any.

Page 18: lt11-relalg

7/29/2019 lt11-relalg

http://slidepdf.com/reader/full/lt11-relalg 18/30

18

Example

• Relation Registers

• courses := π c_no,sem,year (Registers):

st_id c_no sem year

2009P7PS001 CSGC352 2 2009-10

2009P7PS020 CSGC352 2 2009-10

2009P7PS001 CSGC342 2 2009-10

2009P7PS005 CSGC352 2 2009-10

c_no sem yearCSGC352 2 2009-10

CSGC342 2 2009-10

Page 19: lt11-relalg

7/29/2019 lt11-relalg

http://slidepdf.com/reader/full/lt11-relalg 19/30

19

Project Operator

•Produces table containing subset of columns of argument table

πattribute list (relation)

•Example

•PersonPerson πName,Hobby (PersonPerson)

1123 John 123 VSP stamps1123 John 123 VSP coins

5556 Mary 7 VJY reading

9876 Samu 5 HYD stamps

 John stamps John coinsMary readingSamu stamps

  Id   Name Address  Hobby   Name  Hobby 

Page 20: lt11-relalg

7/29/2019 lt11-relalg

http://slidepdf.com/reader/full/lt11-relalg 20/30

20

Project Operator

1123 John 123 VSP stamps

1123 John 123 VSP coins5556 Mary 7 VJY reading9876 Samu 5 HYD stamps

 John 123 VSPMary 7 VJYSamu 5 HYD

Result is a table (no duplicates); can have fewer tuples thanthe original

  Id Name Address Hobby  Name Address

• Example:

  PersonPerson πName,Address(PersonPerson)

Page 21: lt11-relalg

7/29/2019 lt11-relalg

http://slidepdf.com/reader/full/lt11-relalg 21/30

21

Expressions

1123 John 123 VSP stamps1123 John 123 VSP coins5556 Mary 7 VJY reading9876 Samu 5 HYD stamps

1123 John5556 Mary

Id Name Address Hobby Id Name

PersonPerson

ResultResult

π Id, Name

 (σ   Hobby=’reading’  OR Hobby=’coins’

 (PersonPerson) ) 

Page 22: lt11-relalg

7/29/2019 lt11-relalg

http://slidepdf.com/reader/full/lt11-relalg 22/30

22

Product

• R3 := R1 × R2

– Pair each tuple t1 of R1 with each tuple t2 of R2.

– Concatenation t1t2 is a tuple of R3.

– Schema of R3 is the attributes of R1 and then R2, in

order.

– But beware attribute A of the same name in R1 and

R2: use R1. A and R2. A.

Page 23: lt11-relalg

7/29/2019 lt11-relalg

http://slidepdf.com/reader/full/lt11-relalg 23/30

23

ExampleRegisters:(R) Students: (S)

st_id c_no sem year st_id st_name

  2009P7PS001 CSGC342 2 2009-10 2009P7PS001 Amit

2009P7PS001 CSGC352 2 2009-10 2009P7PS020 Piyush

2009P7PS020 CSGC342 2 2009-10 2009P7PS050 Sunil

2009P7PS005 CSGC352 2 2009-10

Page 24: lt11-relalg

7/29/2019 lt11-relalg

http://slidepdf.com/reader/full/lt11-relalg 24/30

24

Example (cont.) T := R × S

R.st_id c_no sem year S.st_id st_name

2009P7PS001 CSGC342 2 2009-10 2009P7PS001 Amit2009P7PS001 CSGC342 2 2009-10 2009P7PS020 Piyush

2009P7PS001 CSGC342 2 2009-10 2009P7PS050 Sunil

2009P7PS001 CSGC352 2 2009-10 2009P7PS001 Amit

2009P7PS001 CSGC352 2 2009-10 2009P7PS020 Piyush2009P7PS001 CSGC352 2 2009-10 2009P7PS050 Sunil

2009P7PS020 CSGC342 2 2009-10 2009P7PS001 Amit

2009P7PS020 CSGC342 2 2009-10 2009P7PS020 Piyush

2009P7PS020 CSGC342 2 2009-10 2009P7PS050 Sunil

2009P7PS005 CSGC352 2 2009-10 2009P7PS001 Amit2009P7PS005 CSGC352 2 2009-10 2009P7PS020 Piyush

2009P7PS005 CSGC352 2 2009-10 2009P7PS050 Sunil

Page 25: lt11-relalg

7/29/2019 lt11-relalg

http://slidepdf.com/reader/full/lt11-relalg 25/30

25

Theta-Join

• R3 := R1  c R2

– Take the product R1 × R2.

– Then apply σ   C  to the result.

Page 26: lt11-relalg

7/29/2019 lt11-relalg

http://slidepdf.com/reader/full/lt11-relalg 26/30

26

ExampleRegisters:(R) Students: (S)

st_id c_no sem year st_id st_name

 2009P7PS001 CSGC342 2 2009-10 2009P7PS001 Amit

2009P7PS001 CSGC352 2 2009-10 2009P7PS020 Piyush

2009P7PS020 CSGC342 2 2009-10 2009P7PS050 Sunil

2009P7PS005 CSGC352 2 2009-10

StudentsInfo := Registers JOIN Registers.st_id = Student.st_id Students

StudentsInfo:

R.st_id c_no sem year S.st_id st_name2009P7PS001 CSGC342 2 2009-10 2009P7PS001 Amit

2009P7PS001 CSGC352 2 2009-10 2009P7PS001 Amit

2009P7PS020 CSGC342 2 2009-10 2009P7PS020 Piyush

Page 27: lt11-relalg

7/29/2019 lt11-relalg

http://slidepdf.com/reader/full/lt11-relalg 27/30

Page 28: lt11-relalg

7/29/2019 lt11-relalg

http://slidepdf.com/reader/full/lt11-relalg 28/30

28

ExampleRegisters:(R) Students: (S)

st_id c_no sem year st_id st_name

  2009P7PS001 CSGC342 2 2009-10 2004P7PS001 Amit

2009P7PS001 CSGC352 2 2009-10 2004P7PS020 Piyush

2009P7PS020 CSGC342 2 2009-10 2004P7PS050 Sunil

2009P7PS005 CSGC352 2 2009-10

StudentsInfo := Registers JOIN StudentsStudentsInfo:

  st_id c_no sem year st_name2009P7PS001 CSGC342 2 2009-10 Amit

2009P7PS001 CSGC352 2 2009-10 Amit

2009P7PS020 CSGC342 2 2009-10 Piyush

Page 29: lt11-relalg

7/29/2019 lt11-relalg

http://slidepdf.com/reader/full/lt11-relalg 29/30

29

Renaming

• The RENAME operator gives a new schema to a

relation.

• R1 := RENAMER1(A1,…,An)(R2) makes R1 be a relation with attributes A1,…,An and the same tuples as R2.

• Simplified notation: R1(A1,…,An) := R2.

Page 30: lt11-relalg

7/29/2019 lt11-relalg

http://slidepdf.com/reader/full/lt11-relalg 30/30

30

Example

• Courses:

• R(c_code, c_title, credits) := Courses

• R:

  c_no c_name units

CSGC342 Operating Systems 3

CSGC352 Database Systems 3

  c_code c_title creditsCSGC342 Operating Systems 3

CSGC352 Database Systems 3