module title? dbms relational algebra. module title? dbms what do we mean by an "algebra"?...

39
Module Title? DBMS Relational Algebra

Upload: mitchell-long

Post on 21-Jan-2016

218 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Module Title? DBMS Relational Algebra. Module Title? DBMS What do we mean by an "algebra"?  "Ordinary" algebra works on numbers, and contains operators

Module Title?

DBMS

Relational Algebra

Page 2: Module Title? DBMS Relational Algebra. Module Title? DBMS What do we mean by an "algebra"?  "Ordinary" algebra works on numbers, and contains operators

Module Title?

DBMS

What do we mean by an "algebra"?

"Ordinary" algebra works on numbers, and contains operators that work on numbers: 2+4, 6-3, 95/5, 8*7.

Relational Algebra works on relations, and has operators which work on relations (tables).

Page 3: Module Title? DBMS Relational Algebra. Module Title? DBMS What do we mean by an "algebra"?  "Ordinary" algebra works on numbers, and contains operators

Module Title?

DBMS

Relational Algebra

Algebra is a formal structure consisting of sets and operations

on those sets.

Relational algebra is a formal system for manipulating

relations.

Page 4: Module Title? DBMS Relational Algebra. Module Title? DBMS What do we mean by an "algebra"?  "Ordinary" algebra works on numbers, and contains operators

Module Title?

DBMS

Relational Algebra

A language based on operators and a domain of values

Operators map values taken from the domain into other domain

values

Hence, an expression involving operators and arguments

produces a value in the domain

When the domain is a set of all relations (and the operators are

as described later), we get the relational algebra

We refer to the expression as a query and the value produced

as the query result

Page 5: Module Title? DBMS Relational Algebra. Module Title? DBMS What do we mean by an "algebra"?  "Ordinary" algebra works on numbers, and contains operators

Module Title?

DBMS

Relational Algebra-Terminology

Domain: set of relations

Basic operators: select, project, union, set difference,

Cartesian product and rename

Derived operators: set intersection, division, join

Procedural: Relational expression specifies query by

describing an algorithm (the sequence in which operators are

applied) for determining the result of an expression

Page 6: Module Title? DBMS Relational Algebra. Module Title? DBMS What do we mean by an "algebra"?  "Ordinary" algebra works on numbers, and contains operators

Module Title?

DBMS

Select Operation()

Syntax for the Select operator is

(relation)

predicate

Page 7: Module Title? DBMS Relational Algebra. Module Title? DBMS What do we mean by an "algebra"?  "Ordinary" algebra works on numbers, and contains operators

Module Title?

DBMS

What does the Select operator do?

The Select operator, s , selects tuples (rows) from the relation which

satisfy (make TRUE) the specified predicate. So, we start with a given

relation, and produce a new relation which is a (horizontal) subset of

the old one, depending upon which rows are extracted from the old

relation.

Page 8: Module Title? DBMS Relational Algebra. Module Title? DBMS What do we mean by an "algebra"?  "Ordinary" algebra works on numbers, and contains operators

Module Title?

DBMS

STUID STUNAME MAJOR CREDITS

151234 Jones,Ted MGMT 47

241504 Smith, Jane CS 101310927 Kool, Bill MATH 68

428541 Lan, Jackie IS 93

529624 Witt, Stu COE 75

629632 Kahn, Fran CS 65

The query (STUDENT) STUID = 241504

STUID STUNAME MAJOR CREDITS241504 Smith, Jane CS 101

The STUDENT Relation

Page 9: Module Title? DBMS Relational Algebra. Module Title? DBMS What do we mean by an "algebra"?  "Ordinary" algebra works on numbers, and contains operators

Module Title?

DBMS

STUID STUNAME MAJOR CREDITS151234 Jones,Ted MGMT 47241504 Smith, Jane CS 101310927 Kool, Bill MATH 68428541 Lan, Jackie IS 93529624 Witt, Stu COE 75629632 Kahn, Fran CS 65

(Student)MAJOR = 'CS'

STUID STUNAME MAJOR CREDITS241504 Smith, Jane CS 101629632 Kahn, Fran CS 65

Page 10: Module Title? DBMS Relational Algebra. Module Title? DBMS What do we mean by an "algebra"?  "Ordinary" algebra works on numbers, and contains operators

Module Title?

DBMS

The 'Usual' Comparisons are Allowed in the

Predicate

We may include the "usual" comparison operators ( < , > , =, etc.) in the predicate.

We can form "compound" predicates with an AND ( ) and the OR ( V ).

Page 11: Module Title? DBMS Relational Algebra. Module Title? DBMS What do we mean by an "algebra"?  "Ordinary" algebra works on numbers, and contains operators

Module Title?

DBMSSelection ( )

( employee) - it will select rows having salary > 500

sal > 500

eno ename sal desig

IT1 ALI 500 TUTOR

BUS2 AHMED 1000 HEAD

IT2 SABA 400 CLERK

IT3 SALEH 500 TUTOR

BUS1 BADER 650 TUTOR

eno ename sal desig

BUS2 AHMED 1000 HEAD

BUS1 BADER 650 TUTOR

Page 12: Module Title? DBMS Relational Algebra. Module Title? DBMS What do we mean by an "algebra"?  "Ordinary" algebra works on numbers, and contains operators

Module Title?

DBMS

Selection Condition

Operators: <, , , >, =,

Simple selection condition:

<attribute> operator <constant>

<attribute> operator <attribute>

<condition> AND <condition>

<condition> OR <condition>

NOT <condition>

Page 13: Module Title? DBMS Relational Algebra. Module Title? DBMS What do we mean by an "algebra"?  "Ordinary" algebra works on numbers, and contains operators

Module Title?

DBMSSTUID STUNAME MAJOR CREDITS

151234 Jones,Ted MGMT 47241504 Smith, Jane CS 101310927 Kool, Bill MATH 68428541 Lan, Jackie IS 93529624 Witt, Stu COE 75629632 Kahn, Fran CS 65

(Student)((MAJOR = 'CS' ) (CREDITS < 100))V(MAJOR='IS')

STUID STUNAME MAJOR CREDITS

428541 Lan, Jackie IS 93

629632 Kahn, Fran CS 65

Page 14: Module Title? DBMS Relational Algebra. Module Title? DBMS What do we mean by an "algebra"?  "Ordinary" algebra works on numbers, and contains operators

Module Title?

DBMS

The Project Operator

a unary operator (works on a single relation)

produces a vertical subset of a relation, extracting the values of the attributes we specify, eliminating duplicates, and placing the values in a new relation

Page 15: Module Title? DBMS Relational Algebra. Module Title? DBMS What do we mean by an "algebra"?  "Ordinary" algebra works on numbers, and contains operators

Module Title?

DBMSThe Project Operator,

(relation)attribute(1),attribute(2),...attribute(n)

This will produce a list of the attribute values given, for all entities in the specified relation.

Page 16: Module Title? DBMS Relational Algebra. Module Title? DBMS What do we mean by an "algebra"?  "Ordinary" algebra works on numbers, and contains operators

Module Title?

DBMS(Student)STUNAME, MAJOR

Query states : What are the names and majors of all students?

STUNAME MAJORJones,Ted MGMT

Smith, Jane CSKool, Bill MATH

Lan, Jackie ISWitt, Stu COE

Kahn, Fran CS

Page 17: Module Title? DBMS Relational Algebra. Module Title? DBMS What do we mean by an "algebra"?  "Ordinary" algebra works on numbers, and contains operators

Module Title?

DBMSUse the Relational Algebra to write a query which lists the

names and id's of all students who are IS majors

( (student)) STUNAME, STUID MAJOR='IS'

STUNAME STUIDLan, Jackie 428541

The innermost (select expression) produces: STUID STUNAME MAJOR CREDITS

428541 Lan, Jackie IS 93

The "project" is applied to this relation, yielding:

Page 18: Module Title? DBMS Relational Algebra. Module Title? DBMS What do we mean by an "algebra"?  "Ordinary" algebra works on numbers, and contains operators

Module Title?

DBMS

Set Operators

Relation is a set of tuples => set operations should apply

Result of combining two relations with a set operator is a

relation => all its elements must be tuples having same

structure

Hence, scope of set operations limited to union compatible

relations

Page 19: Module Title? DBMS Relational Algebra. Module Title? DBMS What do we mean by an "algebra"?  "Ordinary" algebra works on numbers, and contains operators

Module Title?

DBMS

Union Compatible Relations

Two relations are union compatible if

Both have same number of columns

Type of attributes are the same in both

Attributes with the same name in both relations have the same

domain

Union compatible relations can be combined using union,

intersection, and set difference.

Page 20: Module Title? DBMS Relational Algebra. Module Title? DBMS What do we mean by an "algebra"?  "Ordinary" algebra works on numbers, and contains operators

Module Title?

DBMS

Examples of Union Compatibility and Non-Compatibility

INVENTORY (Inumber, Description, SalesPrice)ORDER (Partnumber, Description, Price)

And

EMPLOYEE (Enumber, Name, Dept)VACATION_SCHEDULE(Number, Name, Dept-To-Be-Billed)

INVENTORY (Pnum, Desc, Quantity-In-Stock)ORDER (Pnum, Desc, Supplier)

And

INVENTORY (Part, Description)ORDER( Pnum, Desc, Supplier)

Non-Compatible:

Compatible (assuming expected domains):

Page 21: Module Title? DBMS Relational Algebra. Module Title? DBMS What do we mean by an "algebra"?  "Ordinary" algebra works on numbers, and contains operators

Module Title?

DBMS

Union

Union Operator ... U

a binary operator, having usual definition for Union: A U B

consists of tuples (rows) in either A or B or both (duplicate rows

eliminated)

Page 22: Module Title? DBMS Relational Algebra. Module Title? DBMS What do we mean by an "algebra"?  "Ordinary" algebra works on numbers, and contains operators

Module Title?

DBMS

Union ( )

Union compatible rule :- 1.Two relations should have same degree 2. Two relation corresponding same type of column domain.

IT-Student (Degree = 4 Cardinality = 3)

Eno Ename addr gsm

i1 ali ruwi 9456799

i25 bushra nizwa 9545456

I34 saleh seeb 9967883

Bus-Student (Degree = 4 Cardinality = 3

Eno Ename addr gsm

B45 bilal wadi 9456839

B2 saleh seeb 9655456

B45 aliya jebroo 9236883

Page 23: Module Title? DBMS Relational Algebra. Module Title? DBMS What do we mean by an "algebra"?  "Ordinary" algebra works on numbers, and contains operators

Module Title?

DBMS

IT-Student U Bus-Student (Degree = 4 Cardinality =6)

Union ( )

Union returns those tuples that are either or both of two relations.

Eno Ename addr gsm

i1 ali ruwi 9456799

i25 bushra nizwa 9545456

I34 aliya seeb 9967883

B45 bilal wadi 9456839

B2 saleh seeb 9655456

B45 aliya jebroo 9236883

Page 24: Module Title? DBMS Relational Algebra. Module Title? DBMS What do we mean by an "algebra"?  "Ordinary" algebra works on numbers, and contains operators

Module Title?

DBMS

Example of Union Operator U

Consider the following relations and instances:M: MANUFACTURERS (Name, PeopleCount, Revenue)C: COMPANY (Name, NumberEmployees, Sales)

IBM 50,000 9,000,000,000

Univac 25,000 500,000,000

Microsoft 35,000 1,500,000,000

IBM 50,000 9,000,000,000

Univac 25,000 500,000,000

M:

C:

M U C : IBM 50,000 9,000,000,000

Univac 25,000 500,000,000

Microsoft 35,000 1,500,000,000

Page 25: Module Title? DBMS Relational Algebra. Module Title? DBMS What do we mean by an "algebra"?  "Ordinary" algebra works on numbers, and contains operators

Module Title?

DBMS

Intersection

Represented by R1 R2

Produces a relation that includes all the tuples in both R1 and

R2; R1 and R2 must be union compatible.

Page 26: Module Title? DBMS Relational Algebra. Module Title? DBMS What do we mean by an "algebra"?  "Ordinary" algebra works on numbers, and contains operators

Module Title?

DBMS

S# SNAME STATUS CITY

S1 Smith 20 London

S4 Clark 20 London

S# SNAME STATUS CITY

S1 Smith 20 London

S4 Clark 20 London

A

B

Page 27: Module Title? DBMS Relational Algebra. Module Title? DBMS What do we mean by an "algebra"?  "Ordinary" algebra works on numbers, and contains operators

Module Title?

DBMS

S# SNAME STATUS CITY

S1 Smith 20 London

S4 Clark 20 London

A B

Page 28: Module Title? DBMS Relational Algebra. Module Title? DBMS What do we mean by an "algebra"?  "Ordinary" algebra works on numbers, and contains operators

Module Title?

DBMS

Example of Intersection Operator Consider the following relations and instances:

M: MANUFACTURERS (Name, PeopleCount, Revenue)C: COMPANY (Name, NumberEmployees, Sales)

IBM 50,000 9,000,000,000

Univac 25,000 500,000,000

Microsoft 35,000 1,500,000,000

IBM 50,000 9,000,000,000

Univac 25,000 500,000,000

M:

C:

M C

IBM 50,000 9,000,000,000

Univac 25,000 500,000,000

Page 29: Module Title? DBMS Relational Algebra. Module Title? DBMS What do we mean by an "algebra"?  "Ordinary" algebra works on numbers, and contains operators

Module Title?

DBMS

Difference

Represented by R1 - R2:

Produces a relation that includes all the tuples in R1 that are

not in R2; R1 and R2 must be union compatible.

Page 30: Module Title? DBMS Relational Algebra. Module Title? DBMS What do we mean by an "algebra"?  "Ordinary" algebra works on numbers, and contains operators

Module Title?

DBMS

Set-difference (–)

The minus operator returns only records which are only in the first relation.

rollno name

22045 saleh

22056 arif

22345 ahmed

23356 majid

23456 anwar

rollno name

22045 saleh

22056 arif

23456 anwar

rollno name

22345 ahmed

23356 majid

students Sports students

students-sports students

Page 31: Module Title? DBMS Relational Algebra. Module Title? DBMS What do we mean by an "algebra"?  "Ordinary" algebra works on numbers, and contains operators

Module Title?

DBMS

Example of Difference Operator - Consider the following relations and instances:

M: MANUFACTURERS (Name, PeopleCount, Revenue)C: COMPANY (Name, NumberEmployees, Sales)

IBM 50,000 9,000,000,000

Univac 25,000 500,000,000

Microsoft 35,000 1,500,000,000

IBM 50,000 9,000,000,000

Univac 25,000 500,000,000

M:

C:

Microsoft 35,000 1,500,000,000

M – C :

Page 32: Module Title? DBMS Relational Algebra. Module Title? DBMS What do we mean by an "algebra"?  "Ordinary" algebra works on numbers, and contains operators

Module Title?

DBMS

Cartesian product

Represented by R1 X R2:

Produces a relation that has the attributes of R1 and R2 and

includes as tuples all possible combinations of tuples from R1

and R2.

Build a relation from two specified relations consisting of all

possible combinations of tuples, one from each of the two

relations.

Page 33: Module Title? DBMS Relational Algebra. Module Title? DBMS What do we mean by an "algebra"?  "Ordinary" algebra works on numbers, and contains operators

Module Title?

DBMS

Dno Dname Loc

10 Business B2

20 Information Tech.

B1

30 Foundation C1

Eno Ename sal Desig Dno

E1 ali 500 tutor 10

E2 ahmed 1000 head 10

E3 saleh 700 tutor 20

E4 sami 400 clerk 30

CROSS PRODUCT (X) or CARTESIAN Product

It takes 2 relations and it returns new relation where every record of first relation is combined with every record of second relation. 

EMP 

(Degree = 5 Cardinality =4) DEPT

 (Degree = 3 Cardinality = 3)

Page 34: Module Title? DBMS Relational Algebra. Module Title? DBMS What do we mean by an "algebra"?  "Ordinary" algebra works on numbers, and contains operators

Module Title?

DBMS

EMPDEPT Degree = 5+3 = 8 Cardinality = 4*3 = 12 )

Eno Ename sal Desig Dno Dno Dname Loc

E1 ali 500 tutor 10 10 Business B2

E1 ali 500 tutor 10 20 Information Tech.

B1

E1 ali 500 tutor 10 30 Foundation C1

E2 ahmed 1000 head 10 10 Business B2

E2 ahmed 1000 head 10 20 Information Tech.

B1

E2 ahmed 1000 head 10 30 Foundation C1

E3 saleh 700 tutor 20 10 Business B2

E3 saleh 700 tutor 20 20 Information Tech.

B1

E3 saleh 700 tutor 20 30 Foundation C1

E4 sami 400 clerk 30 10 Business B2

E4 sami 400 clerk 30 20 Information Tech.

B1

E4 sami 400 clerk 30 30 Foundation C1

Page 35: Module Title? DBMS Relational Algebra. Module Title? DBMS What do we mean by an "algebra"?  "Ordinary" algebra works on numbers, and contains operators

Module Title?

DBMS

The Natural JOIN Operator in The

Relational Algebra

"the heart of the relational algebra"

a binary operator

we "join" together two tables, based on a common attribute (the Join

attribute)

the new relation will contain the columns of both tables which have

been joined, and its rows will be the concatenation of a row from the

first table and a row from the second table which matches the Join

attribute

Page 36: Module Title? DBMS Relational Algebra. Module Title? DBMS What do we mean by an "algebra"?  "Ordinary" algebra works on numbers, and contains operators

Module Title?

DBMS

The Natural Join Symbol:

A B means the Natural Join of relations A and B

Note that if there is a tuple in relation A in which its attribute to be joined does not match any value of the attribute to be joined in relation B, it will not appear in the joined relation.

Page 37: Module Title? DBMS Relational Algebra. Module Title? DBMS What do we mean by an "algebra"?  "Ordinary" algebra works on numbers, and contains operators

Module Title?

DBMSTAGNUM COMPID EMPNUM

32808 M759 61137691 B121 12457772 C007 56759836 B221 12477740 M759 56780269 C007 852

PC EMPLOYEEEMPNUM EMPNAME

124 Alvarez, R.

258 Lopez, M.

567 Feinstein, B.

611 Dinh, M.

PC EMPLOYEE would then be:TAGNUM COMPID EMPNUM EMPNAME

32808 M759 611 Dinh. M.37691 B121 124 Alvarez, R.

57772 C007 567 Feinstein, B.59836 B221 124 Alvarez, R.77740 M759 567 Feinstein, B.

Page 38: Module Title? DBMS Relational Algebra. Module Title? DBMS What do we mean by an "algebra"?  "Ordinary" algebra works on numbers, and contains operators

Module Title?

DBMS

List the tag number and computer ID together with the name of the

employee to whom the PC is assigned.

( PC

EMPLOYEE ) TAGNUM, COMPID, EMPNAME

Page 39: Module Title? DBMS Relational Algebra. Module Title? DBMS What do we mean by an "algebra"?  "Ordinary" algebra works on numbers, and contains operators

Module Title?

DBMS

Answer to the Query

TAGNUM COMPID EMPNAME32808 M759 Dinh. M.37691 B121 Alvarez, R.57772 C007 Feinstein, B.59836 B221 Alvarez, R.77740 M759 Feinstein, B.