PowerPoint 97
Download
Report
Transcript PowerPoint 97
Introduction to Database Systems
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 )
Relational Algebra, Calculus & SQL
David Maier / Judy Cushing
1
Introduction to Database Systems
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)
Relational Algebra, Calculus & SQL
David Maier / Judy Cushing
2
Introduction to Database Systems
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’
Relational Algebra, Calculus & SQL
David Maier / Judy Cushing
3
Introduction to Database Systems
Relational Algebra
Operates on relations, gives relations as results
Restrict (Select) -- subset of tuples
Project -- subset of columns
Product -- all possible combinations of two tuples
A
B
AX
AY
BX
BY
X
Y
(Natural) Join: connecting tuples
based on common value in same-named columns
A1 B1
A2 B1
A3 B2
Relational Algebra, Calculus & SQL
B1 C1
B2 C2
B3 C3
A1 B1 C1
A2 B1 C1
A3 B2 C2
David Maier / Judy Cushing
4
Introduction to Database Systems
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’))
Relational Algebra, Calculus & SQL
David Maier / Judy Cushing
5
Introduction to Database Systems
Standard Set Operations
Union, Intersection, Difference
treat relations as sets of tuples
old_ext(inv#
ext)
13
13
14
16
(inv#
rings_at(inv#
1118
1119
3443
3439
ext)
(inv#
12
13
13
14
14
15
16
ext)
1118
1118
1120
3400
3443
3443
3439
ext)
(inv#
Relational Algebra, Calculus & SQL
ext)
David Maier / Judy Cushing
6
Introduction to Database Systems
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
A
B
C
A
A
A
B
C
X
Y
Z
X
Y
Relational Algebra, Calculus & SQL
X
Z
A
David Maier / Judy Cushing
7
Introduction to Database Systems
Division
phone_bk(person
ext)
Randall
Ross
Ricard
Randall
Ross
Rivers
Ross
Ricard
Rivers
2116
2116
2116
2218
2218
2218
3972
3972
3972
=
r(ext)
Randall
Ross
2116
2118
=
s(person)
(ext)
2116
3972
Ross
Ricard
Analogy
a b is the largest q such that b
Relational Algebra, Calculus & SQL
(person)
* q
a
David Maier / Judy Cushing
8
Introduction to Database Systems
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}
Relational Algebra, Calculus & SQL
David Maier / Judy Cushing
9
Introduction to Database Systems
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}
Relational Algebra, Calculus & SQL
David Maier / Judy Cushing
10
Introduction to Database Systems
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
Relational Algebra, Calculus & SQL
David Maier / Judy Cushing
11
Introduction to Database Systems
Relational Calculus
The 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-procedural
Relational Completeness
Relational Algebra, Calculus & SQL
David Maier / Judy Cushing
12
Introduction to Database Systems
• 6.13 Get full details of all projects.
– J
– JX
– SELECT * FROM J
Query Exercises
• 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.
Relational Algebra, Calculus & SQL
David Maier / Judy Cushing
13
Introduction to Database Systems
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!
Relational Algebra, Calculus & SQL
David Maier / Judy Cushing
14
Introduction to Database Systems
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?
Relational Algebra, Calculus & SQL
David Maier / Judy Cushing
15
Introduction to Database Systems
Lab Assignment for Next Week
With your partner, finish creating and populating the SPJ
database.
Run the assigned queries from Chapter 6.
SQL BNF
http://
cuiwww.unige.ch/~falquet/sdbd/langage/SQL92/BNFindex.html
Relational Algebra, Calculus & SQL
David Maier / Judy Cushing
16