Date post: | 28-Mar-2015 |
Category: |
Documents |
Upload: | riley-nolan |
View: | 216 times |
Download: | 0 times |
Relational data objects
1
Relational data objects
Lecture 6
Relational data objects
2
Answer to last lecture’s activity
Relational data objects
3
Example used throughout the course
Suppliers• s_id (string); s_name (string); status (integer); city
(string)
Parts• p_id (string); p_name (string); colour (string);
weight (real); city (string)
Contracted• s_id (string); p_id (string); qty (integer)
Relational data objects
4
Diagrammatic representation
Suppliers Parts
Contracted
s_name status city p_name colour
s_id p_id
qty
Relational data objects
5
Terminology
S_id S_name Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens
Relation
Attributes
Degree
Tuples
Cardinality
Relational data objects
6
Domains
Relational data objects
7
Why domains?
comparisons price with price OK weight with weight OK weight with price !!!
– POSSIBLE (they are both numbers)
– BUT WRONG, i.e. MEANINGLESS
integrity of the DB
Relational data objects
8
Why domains? - example
SELECT P_name, QtyFROM Parts, ContractedWHERE Parts.P_id = Contracted.P_id
SELECT P_name, QtyFROM Parts, ContractedWHERE Parts.P_id = Contracted.S_id
Suppose the P_id and S_id fields are numeric in all tables
meaningless
Relational data objects
9
Domains
domains are not explicitly stored in the DB are specified as part of the DB definition (where?) each attribute - defined on a certain domain
Relational data objects
10
Scalars
the smallest semantic unit of data atomic values - no internal structure with
respect to the relational model NOTE: they may have internal structure
comparison with a basic data type (Pascal) non-deterministic definition (“slippery”)
Relational data objects
11
Domain = data + operators
integer: +, -, *, integer-division real char: concatenate, find string, … date: more complex data types spatial operators: all regions crossed by a line images
Relational data objects
12
Domains and data types
domain (in the relational model) is the same with data type in a programming language
analyse the extent to which domain / data type definition can be supported
primitive support for domains in relational databases (INTEGER, CHAR(n), ...)
Relational data objects
13
Data definition
domains are not explicitly stored in the DB are specified as part of the DB definition (where?) each attribute - defined on a certain domain
Relational data objects
14
Conventions
domains unique in the DB
named relations unique in the DB
attributes unique within a relation
Relational data objects
15
Domain based queries
if domains exist which relations contain information related to… a
certain domain (e.g. related to suppliers - S_id)
if domains don’t exist naming convention + attribute interrogation
Relational data objects
16
Domains - advantages
domain constrained operators increased representational power
• spatial primitives for GIS; image processing features …
domain based queries
Relational data objects
17
Relations
Relational data objects
18
Variable vs value
variable - named object whose values can change value - element of a type
DECLARE VARIABLE example OF TYPE INTEGER// ...example := 1; // variable has certain value//...example := example * 2; // variable has certain value//...
Relational data objects
19
Relation variable vs relation value
relation variable named object whose value at a given time is a relation value its values change with time
relation value
CREATE BASE RELATION example //...// insert operationsSELECT //...FROM example // the variable has a certain valueWHERE //...
Relational data objects
20
Relation value
a relation value on a collection of domains (not necessarily distinct) consists of a heading and a body
Relational data objects
21
Relation heading
a fixed set of attribute_name:domain_name pairs { <A1:D1>, <A2:D2>, ..., <An:Dn>}
each Ak corresponds to only one Dk
A1, ..., An are all distinct
degree (arity) - n
Relational data objects
22
Relation body
a set of (n-)tuples n is the same for all the tuples in the set
n-tuple a set of attribute_name:attribute_value pairs { <A1:vi1>, <A2:vi2>, ..., <An:vin> } (tuple i)
vik must be from Dk
one and only one pair for each attribute
cardinality - no of tuples
Relational data objects
23
Example - the relation Suppliers
{<s_id:STRING>, <S_name:STRING>, <Status: INT>, <City:STRING>}
heading
{ {<s_id:’s1’>, <s_name:’Smith’>, <status:20>, <city:’London’>}, {<s_id:’s2’>, <s_name:’John’>, <status:30>, <city:’Leeds’>}, {<s_id:’s3’>, <s_name:’Stella’>, <status:70>, <city:’Paris’>} }
body
Relational data objects
24
Generate some incorrect examples
Relational data objects
25
Tables as relations
relation table a table can be interpreted as a relation
there are some underlying domains each column corresponds to a certain domain the table’s heading represents the relation’s heading each row represents a tuple, etc.
Relational data objects
26
Properties of relations
no duplicate tuples• SQL!!!• consequence - primary key
tuples are un-ordered attributes are un-ordered all attributes are atomic
• 1NF; why do we need 1NF?• unnormalised relation - example
Relational data objects
27
Kinds of relations
named relationbase relationderived relationexpressible relationquery resultintermediate resultview (virtual)snapshot (real)stored relation
Relational data objects
28
Relational database
“a database that is perceived by the user as a collection of normalised relations of assorted degrees”
(Codd, 1983)