LCC 6317/4720 Interactive Narrative Lecture 4 January 25, 2005.
Operations in the Relational Model COP 4720 Lecture 8 Lecture Notes.
-
Upload
theresa-oneal -
Category
Documents
-
view
220 -
download
0
Transcript of Operations in the Relational Model COP 4720 Lecture 8 Lecture Notes.
![Page 1: Operations in the Relational Model COP 4720 Lecture 8 Lecture Notes.](https://reader036.fdocuments.us/reader036/viewer/2022081513/5697bf821a28abf838c85b9a/html5/thumbnails/1.jpg)
Operations in the Relational Model
COP 4720Lecture 8
Lecture Notes
![Page 2: Operations in the Relational Model COP 4720 Lecture 8 Lecture Notes.](https://reader036.fdocuments.us/reader036/viewer/2022081513/5697bf821a28abf838c85b9a/html5/thumbnails/2.jpg)
Lecture 8COP 4720 2
Outline of Lecture
• Relational Algebra– Set Operations– Projection– Selection– Cartesian Product – Join– Combining Operations
• Completeness of Relational Algebra Operations
• Sec. 3.2
![Page 3: Operations in the Relational Model COP 4720 Lecture 8 Lecture Notes.](https://reader036.fdocuments.us/reader036/viewer/2022081513/5697bf821a28abf838c85b9a/html5/thumbnails/3.jpg)
Lecture 8COP 4720 3
Database Query Languages
• Given a database, ask questions, get data as answers– Ex: Get all students with GPA > 3.7 who applied to
Berkeley and Stanford and nowhere else
– Ex: Get all humanities departments at campuses in Florida with < 1000 applicants
– Ex: Get the campus with highest average accept rate over the last five years
• Some questions are easy to pose, some are not
• Some questions are easy for DBMS to answer, some are not.
• "Query language" also used to update the database
![Page 4: Operations in the Relational Model COP 4720 Lecture 8 Lecture Notes.](https://reader036.fdocuments.us/reader036/viewer/2022081513/5697bf821a28abf838c85b9a/html5/thumbnails/4.jpg)
Lecture 8COP 4720 4
Relational Query Languages
• Formal(pure): relational algebra, relational calculus
• Actual: SQL
• In all languages, a query is executed over a set of relations, get a relation as the result
![Page 5: Operations in the Relational Model COP 4720 Lecture 8 Lecture Notes.](https://reader036.fdocuments.us/reader036/viewer/2022081513/5697bf821a28abf838c85b9a/html5/thumbnails/5.jpg)
Lecture 8COP 4720 5
Relational Algebra• Construct new relations from old ones
– Set of operators– Relations are operands
• Build progressively more complex expressions by applying operators to relations or to rel. algebra expressions (which are relations as well)
• Query is a relational algebra expression– First concrete example of a query language
• Four broad classes of operations– Set operations, selection operations,
operations that combine data from two relations, rename operation
![Page 6: Operations in the Relational Model COP 4720 Lecture 8 Lecture Notes.](https://reader036.fdocuments.us/reader036/viewer/2022081513/5697bf821a28abf838c85b9a/html5/thumbnails/6.jpg)
Lecture 8COP 4720 6
Sample Relational Schema
Movies(title,year,length,filmType)
MovieStars(name,address,gender,birthdate)
![Page 7: Operations in the Relational Model COP 4720 Lecture 8 Lecture Notes.](https://reader036.fdocuments.us/reader036/viewer/2022081513/5697bf821a28abf838c85b9a/html5/thumbnails/7.jpg)
Lecture 8COP 4720 7
Relational Operators: Select
Select or Restrict (unary) <predicate> (r) = {t | t r and predicate(t)}
• <predicate> is a conditional expression of the type that we are familiar with from conventional programming languages– <attribute> <op> <attribute>– <attribute> <op> <constant>– attribute in R– op {=,,<,>,, …, AND, OR}
• Ex: length100 (Movies)
![Page 8: Operations in the Relational Model COP 4720 Lecture 8 Lecture Notes.](https://reader036.fdocuments.us/reader036/viewer/2022081513/5697bf821a28abf838c85b9a/html5/thumbnails/8.jpg)
Lecture 8COP 4720 8
Pictorially
A1 A2 A3 … An
...
i
A1 A2 A3 … An
...
j, i j
title year length filmTypeStar Wars
Mighty Ducks
Wayne’sWorld
1977
1991
1992
124
104
95
color
color
color
Movies
result set
Comparison with null values return a false condition
![Page 9: Operations in the Relational Model COP 4720 Lecture 8 Lecture Notes.](https://reader036.fdocuments.us/reader036/viewer/2022081513/5697bf821a28abf838c85b9a/html5/thumbnails/9.jpg)
Lecture 8COP 4720 9
Relational Operators: Project
Project (unary) <attr list> (r)
• <attr list> is a list of attributes (columns) from R only
• Ex: title, year, length (Movies) A1 A2 A3 … An
...
i
A1 A2… Ak
...
i
n k, k<=n
![Page 10: Operations in the Relational Model COP 4720 Lecture 8 Lecture Notes.](https://reader036.fdocuments.us/reader036/viewer/2022081513/5697bf821a28abf838c85b9a/html5/thumbnails/10.jpg)
Lecture 8COP 4720 10
Relational Operators: Rename
Rename (unary) New (Old)
• New is the name and schema of the relation to be renamed (Old)
• Ex: MyMovies (Movies)
• Ex: MyMovies(title,year,length,type) (Movies)
![Page 11: Operations in the Relational Model COP 4720 Lecture 8 Lecture Notes.](https://reader036.fdocuments.us/reader036/viewer/2022081513/5697bf821a28abf838c85b9a/html5/thumbnails/11.jpg)
Lecture 8COP 4720 11
Set OperationsUnion (binary, commutative, associative)• R SIntersection (binary, commutative, associative)• R SSet Difference (binary)• R - S
– Set of elements in R but not in S– R-S S-R !!
• R(A1,A2,…,An), S(B1,B2,…,Bn) must be union compatible– R and S are of the same degree
– for each i, dom(Ai) = dom(Bi)
![Page 12: Operations in the Relational Model COP 4720 Lecture 8 Lecture Notes.](https://reader036.fdocuments.us/reader036/viewer/2022081513/5697bf821a28abf838c85b9a/html5/thumbnails/12.jpg)
Lecture 8COP 4720 12
Example
name address gender birthdateCarrie Fisher
Mark Hamil
123 Maple St., Hollywood
456 Oak Rd., Brentwood
F
M
9/9/99
8/8/88
R
name address gender birthdateCarrie Fisher
Harrison Ford
123 Maple St., Hollywood
789 Palm Dr., Beverly Hills
F
M
9/9/99
7/7/77
S
![Page 13: Operations in the Relational Model COP 4720 Lecture 8 Lecture Notes.](https://reader036.fdocuments.us/reader036/viewer/2022081513/5697bf821a28abf838c85b9a/html5/thumbnails/13.jpg)
Lecture 8COP 4720 13
Sample Operations
name address gender birthdateCarrie Fisher 123 Maple St., Hollywood F 9/9/99
R S
name address gender birthdateCarrie Fisher
Harrison Ford
123 Maple St., Hollywood
789 Palm Dr., Beverly Hills
F
M
9/9/99
7/7/77
R S
Mark Hamil 456 Oak Rd., Brentwood M 8/8/88
name address gender birthdate
R - S
Mark Hamil 456 Oak Rd., Brentwood M 8/8/88
![Page 14: Operations in the Relational Model COP 4720 Lecture 8 Lecture Notes.](https://reader036.fdocuments.us/reader036/viewer/2022081513/5697bf821a28abf838c85b9a/html5/thumbnails/14.jpg)
Lecture 8COP 4720 14
Cartesian Product
Cartesian Product (binary, commutative, associative)
• R x S• Sets of all pairs that can be formed by
choosing the first element of the pair to be any element of R, the second any element of S
• Resulting schema may be ambiguous– Use R.A or S.A to remove ambiguity for an
attribute that occurs in both schemas– Alternatively, use Rename operator and rename
![Page 15: Operations in the Relational Model COP 4720 Lecture 8 Lecture Notes.](https://reader036.fdocuments.us/reader036/viewer/2022081513/5697bf821a28abf838c85b9a/html5/thumbnails/15.jpg)
Lecture 8COP 4720 15
Example
A B
1 2
3 4
B C
2 5
4 7
D
6
8
9 10 11
x
A R.BS.B C D
R S
1 2 2 5 6
1 2 4 7 8
1 2 9 10 11
3 4
3 4
3 4
2 5 6
4 7 8
9 10 11
![Page 16: Operations in the Relational Model COP 4720 Lecture 8 Lecture Notes.](https://reader036.fdocuments.us/reader036/viewer/2022081513/5697bf821a28abf838c85b9a/html5/thumbnails/16.jpg)
Lecture 8COP 4720 16
Join Operations
Natural Join (binary)• R join S R S • Match only those tuples from R and S
that agree in whatever attributes are common to the schemas of R and S– If r and s from r(R) and s(S) are
successfully paired, result is called a joined tuple
![Page 17: Operations in the Relational Model COP 4720 Lecture 8 Lecture Notes.](https://reader036.fdocuments.us/reader036/viewer/2022081513/5697bf821a28abf838c85b9a/html5/thumbnails/17.jpg)
Lecture 8COP 4720 17
Example
A B
1 2
3 4
B C
2 5
4 7
D
6
8
9 10 11
join
• Resulting schema has attributes from R, either R or S (i.e., joining attribute(s)), and S
• Tuples that fail to pair with any tuple of the other relation are called dangling tuples
A B C D
R S
1 2 5 6
3 4 7 8
![Page 18: Operations in the Relational Model COP 4720 Lecture 8 Lecture Notes.](https://reader036.fdocuments.us/reader036/viewer/2022081513/5697bf821a28abf838c85b9a/html5/thumbnails/18.jpg)
Lecture 8COP 4720 18
Join Operations
Theta Join (binary)
• R joinC S, where C is an arbitrary join condition
– R S is an arbitrary join condition
• Step 1: take the product of R and S• Step 2: Select from the product only those tuples
that satisfy condition C• As with the product operation, the schema for
the result is the union of the schemas of R and S natural join - join based on common attributes
R S = R x S
![Page 19: Operations in the Relational Model COP 4720 Lecture 8 Lecture Notes.](https://reader036.fdocuments.us/reader036/viewer/2022081513/5697bf821a28abf838c85b9a/html5/thumbnails/19.jpg)
Lecture 8COP 4720 19
Example
B C
2 3
2 3
D
4
5
7 8 10
joinA<D AND U.BV.B
A B
1 2
6 7
C
3
8
9 7 8
U V
V.BV.C DA U.BU.C
1 2 3 7 8 10
![Page 20: Operations in the Relational Model COP 4720 Lecture 8 Lecture Notes.](https://reader036.fdocuments.us/reader036/viewer/2022081513/5697bf821a28abf838c85b9a/html5/thumbnails/20.jpg)
Lecture 8COP 4720 20
Combining Operations to Form Queries
• Form expressions of arbitrary complexity by applying operators either to – given relations or – to relations that are the result of applying one
or more relational operators to relations
• Single relational expression, using parenthesis as necessary
• Apply only one operator at a time and create intermediate result relations
![Page 21: Operations in the Relational Model COP 4720 Lecture 8 Lecture Notes.](https://reader036.fdocuments.us/reader036/viewer/2022081513/5697bf821a28abf838c85b9a/html5/thumbnails/21.jpg)
Lecture 8COP 4720 21
Complete Set of Relational Algebra Expressions
select
project
union
set difference
cartesian product
• Any of the other operators can be expressed as a sequence of operations from this set
![Page 22: Operations in the Relational Model COP 4720 Lecture 8 Lecture Notes.](https://reader036.fdocuments.us/reader036/viewer/2022081513/5697bf821a28abf838c85b9a/html5/thumbnails/22.jpg)
Lecture 8COP 4720 22
Sample Schema for Exercises
Student(ID, name, address, GPA, SAT)
Campus(location, enrollment, rank)
Apply(ID, location, date, major, decision)
![Page 23: Operations in the Relational Model COP 4720 Lecture 8 Lecture Notes.](https://reader036.fdocuments.us/reader036/viewer/2022081513/5697bf821a28abf838c85b9a/html5/thumbnails/23.jpg)
Lecture 8COP 4720 23
Sample Queries
PROJECT_{date,decision} (Apply)
Campus X Apply
Find Names and addresses of all students with GPA > 3.7 who applied to CS major and were rejected
List name and address of all students with GPA > 3.7