Algebra

16
Introduction to Database Systems David Maier / Judy Cushing 1 Relational Algebra, Calculus & SQL Domains, Relations & Base RelVars (Ch. 5 Overview) Relational Algebra & Calculus,, & SQL (Ch. 6,7) Preview – Design Issues & Integrity Constraints (Ch.8) A Design Review Ch. 5? Each attribute implies a domain (data type, set of values) Types do not imply physical representation (encap…) • But at least one possible physical representation is suggested Specific data types are orthogonal to the relational model. Scalar types have visible components (non-scalars don’t). The relational model is strongly typed . Some operators ( selector = < > := CAST AS )

Transcript of Algebra

Page 1: Algebra

Introduction to Database Systems

David Maier / Judy Cushing 1Relational Algebra, Calculus & SQL

Domains, Relations & Base RelVars (Ch. 5 Overview)Relational Algebra & Calculus,, & SQL (Ch. 6,7)Preview – Design Issues & Integrity Constraints

(Ch.8)A Design Review

• Ch. 5?– Each attribute implies a domain (data type, set of values)– Types do not imply physical representation (encap…)

• But at least one possible physical representation is suggested

– Specific data types are orthogonal to the relational model.– Scalar types have visible components (non-scalars don’t).– The relational model is strongly typed.– Some operators ( selector = < > := CAST AS )

Page 2: Algebra

Introduction to Database Systems

David Maier / Judy Cushing 2Relational Algebra, Calculus & SQL

Relations & RelVars• Relations (relation values)

A table is a concrete representation of a relation.– Tuple – a set of ordered pairs, no duplicates, unordered.– Relations have cardinality & degree.

• RelVars & Views define relations– The system catalog maintains the relvars.

• SQL is used to create – RelVars – CREATE DOMAIN, CREATE TABLE– Relations – INSERT, DELETE, UPDATE (tuples)

Page 3: Algebra

Introduction to Database Systems

David Maier / Judy Cushing 3Relational Algebra, Calculus & SQL

Type Checking

• J.CITY = P.CITY• JNAME | | PNAME• QTY * 100• QTY + 100• STATUS = 5• J.CITY < S.CITY• COLOR = P.CITY• J.CITY = P.CITY | | ‘burg’

Page 4: Algebra

Introduction to Database Systems

David Maier / Judy Cushing 4Relational Algebra, Calculus & SQL

Relational Algebra

Operates on relations, gives relations as results

Restrict (Select) -- subset of tuplesProject -- subset of columnsProduct -- all possible combinations of two tuples

AB

(Natural) Join: connecting tuples based on common value in same-named columns

XY

A XA YB XB Y

A1 B1A2 B1A3 B2

B1 C1B2 C2B3 C3

A1 B1 C1A2 B1 C1A3 B2 C2

Page 5: Algebra

Introduction to Database Systems

David Maier / Judy Cushing 5Relational Algebra, Calculus & SQL

Other Joins

• Theta Join -- join two relations but not on the equality operation

((S RENAME CITY AS SCITY) TIMES (P RENAME CITY AS PCITY) )

WHERE SCITY > PCITY

• Semi Join -- (natural) join two relations, but return only attributes of the first

((S SEMIJOIN (SP WHERE P# = # (‘P2’))

Page 6: Algebra

Introduction to Database Systems

David Maier / Judy Cushing 6Relational Algebra, Calculus & SQL

Standard Set Operations

Union, Intersection, Difference treat relations as sets of tuples

old_ext(inv# ext)

13 111813 111914 344316 3439

rings_at(inv# ext)

12 111813 111813 112014 340014 344315 344316 3439(inv# ext)

(inv# ext)

(inv#ext)

Page 7: Algebra

Introduction to Database Systems

David Maier / Judy Cushing 7Relational Algebra, Calculus & SQL

Division

Given 2 unary and 1 binary relation -- find tuples in the (first) unary relation

matched in the binary relation --w/ tuples in the other unary relation

ABC

A XA YA ZB XC Y

XZ

A

Page 8: Algebra

Introduction to Database Systems

David Maier / Judy Cushing 8Relational Algebra, Calculus & SQL

Division

phone_bk(person ext)

Randall 2116Ross 2116Ricard 2116Randall 2218Ross 2218Rivers 2218Ross 3972Ricard 3972Rivers 3972

r(ext)

2116

2118

= (person)

RandallRoss

s(person)

RossRicard

= (ext)

21163972

Analogya b is the largest q such that b * q a

Page 9: Algebra

Introduction to Database Systems

David Maier / Judy Cushing 9Relational Algebra, Calculus & SQL

Properties & Examples• Associative & Commutative --

UNION, INTERSECTION, TIMES, JOIN

• ((SP JOIN S) WHERE P# = P# (‘P2’)) {SNAME}• (((P WHERE COLOR = COLOR (‘Red’))

JOIN SP) {S#} JOIN S) {SNAME}• ((S {S#} DIVIDEBY P {P#} PER SP {S#, P#})

JOIN S {SNAME}• S{S#} DIVIDEBY (SP WHERE S# = S# (‘S2’)) {P#}

PER SP {S#,P#}• (((S RENAME S# AS SA) {SA, CITY} JOIN

(S RENAME S# AS SB) {SB, CITY} ) WHERE SA < SB) {SA,SB}

Page 10: Algebra

Introduction to Database Systems

David Maier / Judy Cushing 10Relational Algebra, Calculus & SQL

the Algebra enables writing relational expressions

• Retrieval• Update• Integrity constraints• Derived relvars (views)• Stability requirements (concurrency control)• Security constraints (scope of authorization)• Transformation rules -> OPTIMIZATION

((SP JOIN S) WHERE P# = P# (‘P2’)) {SNAME}((SP WHERE P# = P# (‘P2’)) JOIN S) {SNAME}

Page 11: Algebra

Introduction to Database Systems

David Maier / Judy Cushing 11Relational Algebra, Calculus & SQL

Miscellaneous

• Aggregates -- COUNT, SUM, AVG, MAX, MIN, ALL, ANY• SUMMARIZE SP PER SP {P#} ADD SUM (QTY) AS TOTQTY

SUMMARIZE (P JOIN SP) PER P {CITY} ADD COUNT AS NSP

• Relational Comparisons= equals/= not equals<= subset of (IN)< proper subset of>= superset> proper superset

• IS_EMPTY• GROUP, UNGROUP

Page 12: Algebra

Introduction to Database Systems

David Maier / Judy Cushing 12Relational Algebra, Calculus & SQL

Relational CalculusThe algebra - a language to describe

how to construct a new relation.The calculus - a language to write a

definition of that new relation. FORALL PX (PX.COLOR = COLOR (‘Red’))

EXISTS SPX (SX.S# = SX.S# AND SPX.P# = P# (‘P2’)

Procedural vs. Non-proceduralRelational Completeness

Page 13: Algebra

Introduction to Database Systems

David Maier / Judy Cushing 13Relational Algebra, Calculus & SQL

Query Exercises

• 6.13 Get full details of all projects.– J– JX– SELECT * FROM J

• 6.15 Get supplier numbers for suppliers who supply project J1.– ( SPJ WHERE J# = J# (‘J1’) ) {S#}– SPJX.S# WHERE SPJX.J# = J# (‘J1’) – SELECT DISTINCT SPJ.S#

FROM SPJ WHERE SPJ.J# = ‘J1’

• 6.18 Get supplier-number/part-number/project-number triples such that supplier part and project are all colocated).– (S JOIN P JOIN J) {S#, P#, J#}– (SX.S#, PX.P#, JX.J# ) WHERE SX.CITY = PX.CITY AND PX.CITY = JX.CITY AND

JX.CITY = SX.CITY

– SELECT S.S#, P.P#, J.J# FROM S,P,J WHERE S.CITY = P.CITY AND P.CITY = J.CITY.

Page 14: Algebra

Introduction to Database Systems

David Maier / Judy Cushing 14Relational Algebra, Calculus & SQL

Integrity Preview!

• An attribute value must match its type.• A primary key must be unique.• A tuple’s foreign key must be of the same type as its

“matching” primary key.• To create a record with a given foreign key, a record in the

corresponding table must have that value as its primary key.

• Many to many relationships cannot be modeled (directly). Find examples to support these in SPJ!

Page 15: Algebra

Introduction to Database Systems

David Maier / Judy Cushing 15Relational Algebra, Calculus & SQL

Study Questions

Which of the relational algebra, tuple calculus and domain calculus is SQL based on?

Can any relational query be expressed in a single SQL statement?

To what extent do these query languages go beyond the relational model?

Why might QueryByExample languages be easier to use than SQL for someone unfamiliar with the database scheme of a database?

Page 16: Algebra

Introduction to Database Systems

David Maier / Judy Cushing 16Relational Algebra, Calculus & SQL

Lab Assignment for Next Week

With your partner, finish creating and populating the SPJ database.

Run the assigned queries from Chapter 6.

SQL BNFhttp://cuiwww.unige.ch/~falquet/sdbd/langage/SQL92/BNFindex.html