Download RelationalDataModel - Institute for Web Science and Technologies

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Corecursion wikipedia , lookup

Transcript
Web Science & Technologies
University of Koblenz ▪ Landau, Germany
Relational Data Model
Overview





Relational data model;
Tuples and relations;
Schemas and instances;
Named vs. unnamed perspective;
Relational algebra;
WeST
Steffen Staab
[email protected]
Advanced Data Modeling
2 of 48
Table
Player
Birth Year
Andy
1980
Wim
1975
Liam
1985
Mike
1988
Bert
1971
WeST
Steffen Staab
[email protected]
Advanced Data Modeling
3 of 48
Observations
 The rows of the table contain pairs occurring in the relation
player.
 There are two columns, labeled respectively by “name”
and “birth year”.
 The values in each column belong to different domains of
possible values.
WeST
Steffen Staab
[email protected]
Advanced Data Modeling
4 of 48
How to specify a relation
1. specifying the names of the columns (also called fields or
attributes);
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.
WeST
Steffen Staab
[email protected]
Advanced Data Modeling
5 of 48
Domains and attributes
 A set of domains
D1 ; D 2
(sets of values);
 A set of corresponding domain names d1, d2, …
 A set of attributes a1, a2, …
WeST
Steffen Staab
[email protected]
Advanced Data Modeling
6 of 48
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 distinct
attributes, d1, …, dn are domain names.
Relation instance:
finite set of tuples (v1, …, vn) of arity n such that vi 2 Di for
all i.
WeST
Steffen Staab
[email protected]
Advanced Data Modeling
7 of 48
Observation
1. The attributes in each column must be unique.
2. A relation is a set. Therefore, when we represent a relation
by 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.
WeST
Steffen Staab
[email protected]
Advanced Data Modeling
8 of 48
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
D1 ; : : : ; D n
be the corresponding domains.
 The tuple conforms to a relation schema
r(a1:d1, …, an:dn) if vi 2 Di for all i.
WeST
Steffen Staab
[email protected]
Advanced Data Modeling
9 of 48
Relational data are structured
Note that in the relational data model tuples stored in a table
are 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 the
second condition can be dropped.
WeST
Steffen Staab
[email protected]
Advanced Data Modeling
10 of 48
Typed or untyped?
Consider the relation admire:
WeST
Steffen Staab
[email protected]
admirer
admired
wim
andy
mike
wim
liam
andy
liam
arsenal
Advanced Data Modeling
11 of 48
Database schema and instance
 Relational database schema: a collection of relation
schemas with distinct relation names.
 Relational database instance conforming to a relational
database schema S:
 a mapping I from the relation names of S to relation
instances such that
• for every relation schema r(a1:d1, … , an:dn) in S
the relation instance I(r) conforms to this relation schema.
WeST
Steffen Staab
[email protected]
Advanced Data Modeling
12 of 48
Unnamed perspective
 No attributes
 a tuple is simply a sequence (v_1, … , v_n) of values.
 The components of tuples can therefore be identified by
their position in the tuple.
WeST
Steffen Staab
[email protected]
Advanced Data Modeling
13 of 48
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) with
r(#1:d1, … ,#n:dn).
WeST
Steffen Staab
[email protected]
Advanced Data Modeling
14 of 48
Relational Algebra and SQL
1. Can define new relations from existing ones;
2. Uses a collection of operations on relations to do so.
WeST
Steffen Staab
[email protected]
Advanced Data Modeling
15 of 48
Constant
{ (v11, … , v1n),
…..
(vk1, … , vkn) }
WeST
Steffen Staab
[email protected]
Advanced Data Modeling
16 of 48
Union
R1 [ R2 =
{(c1, … , ck) |
WeST
Steffen Staab
[email protected]
(c1, … , ck) 2 R1 or
(c1, … , ck) 2 R2 }
Advanced Data Modeling
17 of 48
Set difference
R_1 - R2 =
{(c1, … , ck) |
WeST
Steffen Staab
[email protected]
(c1, … , ck) 2 R1 and
(c1, … , ck) 2 R2 }
Advanced Data Modeling
18 of 48
Cartesian product
R1 £ R2 =
{(c1, … , ck, d1, … , dm) |
(c1, … , ck) 2 R1 and
(d1, … , dm) 2 R2 }.
WeST
Steffen Staab
[email protected]
Advanced Data Modeling
19 of 48
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) 2 R }.
We say that ¼i1, … ,im(R) is obtained from R
by projection (on arguments i1, … , im).
WeST
Steffen Staab
[email protected]
Advanced Data Modeling
20 of 48
Selection
Assume formulas on domains with “variables” #1, #2, ….
For example, #1 = #2.
¾F (R) = {(c1, …, ck) |
(c1, …, ck) 2 R and
F holds on (c1, …, ck)}.
WeST
Steffen Staab
[email protected]
Advanced Data Modeling
21 of 48
Overview




Relational algebra, named perspective
SQL
Integrity constraints
(Aggregates and grouping)
WeST
Steffen Staab
[email protected]
Advanced Data Modeling
22 of 48
Constant
{ { a1 = v11, … , an = v1n },
…
…
…
{ a1 = vk1, … , an = vkn } }
WeST
Steffen Staab
[email protected]
Advanced Data Modeling
23 of 48
Union
Let R1, R2 be relations with the same attributes.
R1 [ R2 = { t | t 2 R1 or t2 R2 }
WeST
Steffen Staab
[email protected]
Advanced Data Modeling
24 of 48
Union, example
R1
R1 [ R2
R2
A
B
A
B
®
1
®
2
®
1
®
2
¯
3
®
2
¯
1
¯
1
¯
3
WeST
Steffen Staab
[email protected]
A
Advanced Data Modeling
25 of 48
B
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} 2 R}
WeST
Steffen Staab
[email protected]
Advanced Data Modeling
26 of 48
SQL
SQL is based on set and relational operations with certain
modifications and enhancements
A typical SQL query has the form:
select a1, … , an
from R1, … , Rm
where P
This query is equivalent to relational algebra expression:
¼a1,…,an (¾P (R1£ … £ Rm))
The result of an SQL query is a relation.
Exceptions?
WeST
Steffen Staab
[email protected]
Advanced Data Modeling
27 of 48
Integrity constraints
 Domain constraints.
 Key constraints.
 Foreign key constraints.
 More general, defined constraints.
 How to translate them?
WeST
Steffen Staab
[email protected]
Advanced Data Modeling
28 of 48
Query language
Allow one to define:
 Relation and database schemas;
 Relations through our relations;
 Integrity constraints;
 Updates.
WeST
Steffen Staab
[email protected]
Advanced Data Modeling
29 of 48