Download presentation
Presentation is loading. Please wait.
1
Computer Science 317 Database Management
The Relational Algebra (database assembly language)
2
Relational Algebra - Introduction
Just as numerical operations (+,-,*, etc.) operate on numbers and produce numbers, the relational operators act on relations (tables) and produce new relations. With number systems, we build an algebra using the operators, constants, variables, parentheses. So with the relational algebra.
3
The SELECT Operator Unary - operates on a single table
Result table has same attributes as operand Predicate - A function that returns true or false. If F is a predicate on tuples of type R and r is instance of R, then F(r) = {t r | F(t) is true} or <sel-cond>(<relation_name>)
4
SELECT (cont.) Legal selection conditions are formed by combining comparison clauses using AND, OR, NOT. Comparison operators: =, <, , >, , Comparison clauses: <attr name> <comp op> <attr name> or <attr name> <comp op> <constant>
5
SELECT (cont.) Examples: Note: The operator “selects” rows from table.
CLASS = ‘02’(SMAJORS) (PREREQ=‘210’) OR (PREREQ=‘211’)(COURSES) Note: The operator “selects” rows from table.
6
The PROJECT Operator Unary: result attributes are subset of those of the operand. Note: Selects columns. If we have schemas R(A1,…,An) and B={Ai1,…, Aik} with {Ai1,…,Aik}{A1,…,An } then B (r) = b where b is instance of B(Ai1,…, Aik) and tb if and only if there is ur such that t[Aij ]= u[Aij ] for j=1,…,k
7
PROJECT (cont.) <attr list> (<rel name>)
Note: must eliminate duplicates Example r A B C D a x s a y s c x t d x u a y t A(A=‘a’ or D=‘u’(r)) A a d A=‘a’ or D=‘u’(r) A B C D a x s a y s d x u a y t A,D(r) A D a s c t d u a t
8
Set Theoretic Operations
UNION (Binary on relations of same type): r s INTERSECTION: r s DIFFERENCE: r - s Convention: Result uses attribute names of first operand.
9
Set Theoretic (cont.) CARTESIAN PRODUCT: (Binary) r A B a 1 b 0 a 2 s
C D a c a 2 r x s A B C D a a c a a b a c b a a a c a a 2
10
The JOIN Operator JOIN: r <condition> s = <condition>(r x s) EQUIJOIN: condition uses only ‘=‘ operations NATURAL JOIN: eliminate duplicate columns in EQUIJOIN - denoted by * Actually, the NATURAL JOIN requires that the relations have common attributes and then selects tuples where the common attributes are the same.
11
The RENAME Operator Sometimes it is convenient to consider a relation, s, which is the same as relation r except that the attributes have been renamed: B1,…,Bn(r) where B1,…,Bn are the new attribute names.
12
Natural Join Example Faculty Lname SSN Office Adams 111 304
Smith Wilson SMajors Lname AdvSSN Class ID Jones Baker Miller Dodson Cardwell Faculty * SLName,SSN,Class,Id(SMajors) Lname SSN Office SLName Class ID Adams Jones Adams Dodson Adams Cardwell Smith Baker Wilson Miller
13
The DIVISION Operator If we have r of type R(A1,…,An,B1,…,Bm) and s of type S(B1,…,Bm), then rs = t of type T(A1,…,An) where u t if and only if for every tuple v s, we have uv r.
14
DIVISION - Example H = HasTaught FacSSN SLName ID 111 Miller 22
Smith Miller Wilson 44 Jones Jones Wilson 44 P = PhiBK SLname ID Jones Miller H P FacSSN 111 222 F = FullProf FacSSN 111 333 H F SLname ID Miller
15
A Complete Set of Relational Operators
Factoid: {, , , -, x} form a complete set; i.e., all other relational algebra operations can be derived from these. Example: Derivation of division operator r2 ((r1 x s) - r r3 A1,…,An (r2) : all A parts from r that do not match all s parts. rs r1- r3 r1 A1,…,An (r) : all A parts from r
16
s = P = PhiBK SLname ID Jones Miller r1 = A1,…,An (r) FacSSN 111 222 333 r = H = HasTaught FacSSN SLName ID Miller Miller Smith Miller Wilson 44 Jones Jones Wilson 44 r1 x s FacSSN SLName ID Jones Miller Jones Miller Jones Miller r3 = A1,…,An (r2) FacSSN 333 H P = r1 – r3 FacSSN 111 222 r2 = (r1 x s) - r FacSSN SLName ID Jones
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.