Operations in the Relational Model COP 4720 Lecture 8 Lecture Notes.

Post on 17-Jan-2016

220 views 0 download

Tags:

Transcript of Operations in the Relational Model COP 4720 Lecture 8 Lecture Notes.

Operations in the Relational Model

COP 4720Lecture 8

Lecture Notes

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

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

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

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

Lecture 8COP 4720 6

Sample Relational Schema

Movies(title,year,length,filmType)

MovieStars(name,address,gender,birthdate)

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)

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

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

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)

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)

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

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

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

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

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

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

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

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

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

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

Lecture 8COP 4720 22

Sample Schema for Exercises

Student(ID, name, address, GPA, SAT)

Campus(location, enrollment, rank)

Apply(ID, location, date, major, decision)

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