relational data model - west.uni-koblenz.de · steffen staab [email protected] advanced data...
TRANSCRIPT
Steffen [email protected]
Advanced Data Modeling2 of 48
WeST
Overview
Relational data model;Tuples and relations;Schemas and instances;Named vs. unnamed perspective;Relational algebra;
Steffen [email protected]
Advanced Data Modeling3 of 48
WeST
Table
1971Bert
1988Mike
1985Liam
1975Wim
1980Andy
Birth YearPlayer
Steffen [email protected]
Advanced Data Modeling4 of 48
WeST
Observations
The rows of the table contain pairs occurring in the relationplayer.There are two columns, labeled respectively by “name”and “birth year”.The values in each column belong to different domains of possible values.
Steffen [email protected]
Advanced Data Modeling5 of 48
WeST
How to specify a relation
1. specifying the names of the columns (also called fields orattributes);
2. specifying a domain of possible values for each column;
3. enumerate all tuples in the relation.
(1)–(2) refer to the schema of this relation, while (3) to an instance.
Steffen [email protected]
Advanced Data Modeling6 of 48
WeST
Domains and attributes
A set of domains (sets of values);
A set of corresponding domain names d1, d2, …
A set of attributes a1, a2, …
D1,D2
Steffen [email protected]
Advanced Data Modeling7 of 48
WeST
Relation Schema and Instances
Tuple: any finite sequence (v1, … , vn).n is the arity of this tuple.
Relation schema: r(a1:d1, …, an:dn)
where n ≥ 0, r is a relation name, a1, …, an are distinctattributes, d1, …, dn are domain names.
Relation instance:finite set of tuples (v1, …, vn) of arity n such that vi ∈ Di forall i.
Steffen [email protected]
Advanced Data Modeling8 of 48
WeST
Observation
1. The attributes in each column must be unique.
2. A relation is a set. Therefore, when we represent a relationby a table, the order of rows in the table does not matter.
Let us add to this:3. The order of attributes does not matter.
Steffen [email protected]
Advanced Data Modeling9 of 48
WeST
New notation for tuples
A tuple is a set of pairs { (a1, v1), …, (an, vn) } denoted by{ a1=v1, …, an=vn } ,
Let d1, …, dn be domain names and be the corresponding domains.
The tuple conforms to a relation schemar(a1:d1, …, an:dn) if vi ∈ Di for all i.
D1, . . . ,Dn
Steffen [email protected]
Advanced Data Modeling10 of 48
WeST
Relational data are structured
Note that in the relational data model tuples stored in a tableare structured:all tuples conform to the same relation schema;the values in the same column belong to the same domain.
Untyped perspective: there is a single domain, so thesecond condition can be dropped.
Steffen [email protected]
Advanced Data Modeling11 of 48
WeST
Typed or untyped?
Consider the relation admire:
arsenalliam
andyliam
wimmike
andywim
admiredadmirer
Steffen [email protected]
Advanced Data Modeling12 of 48
WeST
Database schema and instance
Relational database schema: a collection of relationschemas with distinct relation names.
Relational database instance conforming to a relational database schema S:
a mapping I from the relation names of S to relationinstances such that
• for every relation schema r(a1:d1, … , an:dn) in S the relation instance I(r) conforms to this relation schema.
Steffen [email protected]
Advanced Data Modeling13 of 48
WeST
Unnamed perspective
No attributes
a tuple is simply a sequence (v_1, … , v_n) of values.
The components of tuples can therefore be identified bytheir position in the tuple.
Steffen [email protected]
Advanced Data Modeling14 of 48
WeST
From Unnamed to Named Perspective
Introduce a collection of attributes #1,#2, …,
identify tuple (v1, … , vn) with the tuple{ #1 = v1, … ,#n = vn }.
Likewise, identify relation schema r(d1, … , dn) withr(#1:d1, … ,#n:dn).
Steffen [email protected]
Advanced Data Modeling15 of 48
WeST
Relational Algebra and SQL
1. Can define new relations from existing ones;2. Uses a collection of operations on relations to do so.
Steffen [email protected]
Advanced Data Modeling16 of 48
WeST
Constant
{ (v11, … , v1n),…..(vk1, … , vkn) }
Steffen [email protected]
Advanced Data Modeling17 of 48
WeST
Union
R1 ∪ R2 = {(c1, … , ck) | (c1, … , ck) ∈ R1 or
(c1, … , ck) ∈ R2 }
Steffen [email protected]
Advanced Data Modeling18 of 48
WeST
Set difference
R_1 - R2 = {(c1, … , ck) | (c1, … , ck) ∈ R1 and
(c1, … , ck) ∈ R2 }
Steffen [email protected]
Advanced Data Modeling19 of 48
WeST
Cartesian product
R1 × R2 = {(c1, … , ck, d1, … , dm) |
(c1, … , ck) ∈ R1 and (d1, … , dm) ∈ R2 }.
Steffen [email protected]
Advanced Data Modeling20 of 48
WeST
Projection
Let now R be a relation of arity k and i1, … , im be numbers in {1, … , k}.
πi1, … ,im (R) = {(ci1
, … , cim) | (c1, … , ck) ∈ R }.
We say that πi1, … ,im(R) is obtained from R by projection (on arguments i1, … , im).
Steffen [email protected]
Advanced Data Modeling21 of 48
WeST
Selection
Assume formulas on domains with “variables” #1, #2, ….
For example, #1 = #2.
σF (R) = {(c1, …, ck) | (c1, …, ck) ∈ R andF holds on (c1, …, ck)}.
Steffen [email protected]
Advanced Data Modeling22 of 48
WeST
Overview
Relational algebra, named perspectiveSQLIntegrity constraints(Aggregates and grouping)
Steffen [email protected]
Advanced Data Modeling23 of 48
WeST
Constant
{ { a1 = v11, … , an = v1n },… … …
{ a1 = vk1, … , an = vkn } }
Steffen [email protected]
Advanced Data Modeling24 of 48
WeST
Union
Let R1, R2 be relations with the same attributes.
R1 ∪ R2 = { t | t ∈ R1 or t∈ R2 }
Steffen [email protected]
Advanced Data Modeling25 of 48
WeST
Union, example
1β
2α
1α
BA
3β
1β
2α
1α
BA
3β
2α
BA
R1 R2 R1 ∪ R2
Steffen [email protected]
Advanced Data Modeling26 of 48
WeST
Renaming
Let R be a relation whose set of attributes is a1,…,an, c1,…,cm
Let b1, … , bn be distinct attributes such that{b1, … , bn} Å { c1, … , cm} = ∅
Thenρa1→b1, … , an→bn
(R) ={{b1 = v1, … , bn=vn , c1=w1 , … , cm=wm} |
{a1 = v1, … , an=vn , c1=w1 , … , cm=wm} ∈ R}
Steffen [email protected]
Advanced Data Modeling27 of 48
WeST
SQL
SQL is based on set and relational operations with certainmodifications and enhancements
A typical SQL query has the form:select a1, … , an
from R1, … , Rm
where PThis query is equivalent to relational algebra expression:
πa1,…,an(σP (R1× … × Rm))
The result of an SQL query is a relation.
Exceptions?
Steffen [email protected]
Advanced Data Modeling28 of 48
WeST
Integrity constraints
Domain constraints.Key constraints.Foreign key constraints.
More general, defined constraints.
How to translate them?
Steffen [email protected]
Advanced Data Modeling29 of 48
WeST
Query language
Allow one to define:Relation and database schemas;Relations through our relations;Integrity constraints;Updates.