Upload
mitchell-long
View
219
Download
0
Embed Size (px)
Citation preview
Module Title?
DBMS
Relational Algebra
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).
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.
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
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
Module Title?
DBMS
Select Operation()
Syntax for the Select operator is
(relation)
predicate
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.
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
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
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 ).
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
Module Title?
DBMS
Selection Condition
Operators: <, , , >, =,
Simple selection condition:
<attribute> operator <constant>
<attribute> operator <attribute>
<condition> AND <condition>
<condition> OR <condition>
NOT <condition>
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
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
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.
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
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:
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
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.
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):
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)
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
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
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
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.
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
Module Title?
DBMS
S# SNAME STATUS CITY
S1 Smith 20 London
S4 Clark 20 London
A B
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
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.
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
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 :
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.
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)
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
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
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.
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.
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
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.