Documente Academic
Documente Profesional
Documente Cultură
1 / 104 2 / 104
Relational Model
I by Dr. E. F. Codd, 1970
Introduction
I data is modelled as relations:
Keys
Referential Integrity α ⊆ A × B × C × ...
TutorialD I relations are assigned to relation variables (relvar)
I each element of a relation is a tuple
SQL I each piece of data of an element is an attribute
Data Types
Data Definition I relations can be represented using tables
Data Manipulation I relation → table, tuple → row, attribute → column
Referential Integrity
3 / 104 4 / 104
Relation Example Relation Structure
MOVIE
TITLE YEAR DIRECTOR SCORE VOTES
The Usual Suspects 1995 Bryan Singer 8.7 3502 I relation header: set of attributes of the relation
Suspiria 1977 Dario Argento 7.1 1004 I affected by data definition language statements
Being John Malkovich 1999 Spike Jonze 8.3 13809
... ... ... ... ... I relation body: set of tuples in the relation
I affected by data manipulation language statements
I relation variable: MOVIE
I tuple: (Suspiria, 1977, Dario Argento, 7.1, 1004)
I attribute: YEAR
5 / 104 6 / 104
I relation predicate: “meaning” of the relation I each tuple is either True or False according to the predicate
7 / 104 8 / 104
Tuple Order Attribute Order
example example
I these relations are equivalent: I these relations are equivalent:
9 / 104 10 / 104
11 / 104 12 / 104
Domain Example Attribute Values
I TITLE from the titles domain, YEAR from the years domain, I attribute values must be scalar
DIRECTOR from the directors domain, . . . I no arrays, lists, records, . . .
I if data types are used:
TITLE string, YEAR integer, DIRECTOR string, . . . example: multiple directors
13 / 104 14 / 104
15 / 104 16 / 104
Keys Candidate Key Examples
17 / 104 18 / 104
19 / 104 20 / 104
Primary Key Primary Key Example
I if more than one candidate key, one is selected as the primary key
I others are alternate keys MOVIE# TITLE YEAR DIRECTOR SCORE VOTES
I names of attributes in the primary key are underlined ... ... ... ... ... ...
6 The Usual Suspects 1995 Bryan Singer ... ...
I any attribute that is part of the primary key 1512 Suspiria 1977 Dario Argento ... ...
can not be empty in any tuple 70 Being John Malkovich 1999 Spike Jonze ... ...
... ... ... ... ... ...
I every relation must have a primary key
21 / 104 22 / 104
23 / 104 24 / 104
Scalarity Example Scalarity Example
MOVIE
MOVIE MOVIE# TITLE ... DIRECTOR#
MOVIE# TITLE ... 6 The Usual Suspects ... 639
1512 Suspiria ... 2259
6 The Usual Suspects ...
70 Being John Malkovich ... 1485
1512 Suspiria ...
... ... ... ...
70 Being John Malkovich ...
... ... ... PERSON CASTING
PERSON# NAME MOVIE# ACTOR# ORD
ACTOR CASTING 308 Gabriel Byrne 6 308 2
ACTOR# NAME MOVIE# ACTOR# ORD 1485 Spike Jonze 70 282 2
639 Bryan Singer 70 503 14
308 Gabriel Byrne 6 308 2
282 Cameron Diaz ... ... ...
282 Cameron Diaz 70 282 2
2259 Dario Argento
503 John Malkovich 70 503 14
503 John Malkovich
... ... ... ... ...
... ...
25 / 104 26 / 104
-
MOVIE PERSON
DIRECTOR# NAME
639
I foreign key: an attribute of a relation MOVIE# TITLE ... PERSON#
2259
6 The Usual Suspects ... 308 Gabriel Byrne
that is a candidate key of another relation 1512 Suspiria ... PP 1485 Spike Jonze
q
70 Being John Malkovich ... 1485 PP 639 Bryan Singer
... ... ... ... P P 282 Cameron Diaz
2259 Dario Argento
503 John Malkovich
... ...
27 / 104 28 / 104
Foreign Key Examples Referential Integrity
I referential integrity:
MOVIE# TITLE ... DIRECTOR# NAME
MOVIE PERSON
The Usual Suspects ... - Gabriel Byrne
PERSON# all values of a foreign key attribute must be present
among the values of the referenced candidate key attribute
1512 Z}Suspiria
6 639 308
Z ... 2259 7
1485 Spike Jonze
... ...
Z ... ... ... ...
Z I if a request would break referential integrity:
Z
Z
Z CASTING I don’t allow
MOVIE# ORD
Z
ACTOR# I reflect the change to affected tuples
6 308 2
70 282 2 I assign null value
... ... ...
I assign default value
29 / 104 30 / 104
MOVIE PERSON
MOVIE# TITLE ... DIRECTOR# PERSON# NAME I INTEGER
... ... ... ... ... ... I RATIONAL
1512 Suspiria ... 2259 2259 Dario Argento I BOOL
... ... ... ... ... ...
I CHAR
31 / 104 32 / 104
Type Definition Type Definition Examples
33 / 104 34 / 104
I generating a value for a type: I getting the value of a field: THE_ operators
type_name(base_value [, ...]) THE_field_name(variable_name)
example example
I generating a SCORE value: I getting the VALUE field of a SCORE variable:
SCORE(8.7) THE_VALUE(SCORE)
35 / 104 36 / 104
Type Operations Relation Definition
CAST_AS_RATIONAL(VOTES)
37 / 104 38 / 104
39 / 104 40 / 104
Relation Variable Examples Relation Variable Examples
41 / 104 42 / 104
TUPLE
{ MOVIE# MOVIE#(6),
I
TITLE "The Usual Suspects",
generating a tuple:
YEAR YEAR(1995),
TUPLE DIRECTOR# PERSON#(639),
{ attribute_name attribute_value SCORE SCORE(8.7),
[, ...] } VOTES 35027 }
TUPLE
{ PERSON# PERSON#(639),
NAME "Bryan Singer" }
43 / 104 44 / 104
Relation Generation Relation Assignment Example
45 / 104 46 / 104
47 / 104 48 / 104
Tuple Deletion Tuple Deletion Example
I deleting tuples: I delete movies with scores less than 3.0 and votes more than 4
DELETE relvar_name DELETE MOVIE
[ WHERE condition ]; WHERE ((SCORE < SCORE(3.0))
AND (VOTES > 4));
I if no condition is specified, all tuples will be deleted
49 / 104 50 / 104
51 / 104 52 / 104
Attribute Renaming Foreign Key Definition
53 / 104 54 / 104
CONSTRAINT CASTING_FKEY_MOVIE
CONSTRAINT MOVIE_FKEY_DIRECTOR CASTING { MOVIE# } <= MOVIE { MOVIE# };
MOVIE { DIRECTOR# }
RENAME { DIRECTOR# AS PERSON# } CONSTRAINT CASTING_FKEY_ACTOR
<= PERSON { PERSON# }; CASTING { ACTOR# }
RENAME { ACTOR# AS PERSON# }
<= PERSON { PERSON# };
55 / 104 56 / 104
Data Types String Data Types
I INTEGER
I CHARACTER [ VARYING ] (n)
I NUMERIC (precision, scale)
I precision: total number of digits
I CHARACTER (n): if the string is shorter than n characters
I scale: number of digits after the decimal point it will be padded with spaces
I same as: DECIMAL (precision, scale)
I CHAR (n) instead of CHARACTER (n)
I FLOAT I VARCHAR (n) instead of CHARACTER VARYING (n)
I BOOLEAN
57 / 104 58 / 104
I DATE
I value example: 2005-09-26
I arbitrary length objects
59 / 104 60 / 104
Domain Creation Domain Example
I creating a domain:
CREATE DOMAIN domain_name [ AS ] base_type I a domain for valid SCORE values:
[ DEFAULT default_value ]
CREATE DOMAIN SCORES AS FLOAT
[ { CHECK ( condition ) } [, ...] ]
CHECK ((VALUE >= 1.0) AND (VALUE <= 10.0))
I deleting domains:
DROP DOMAIN domain_name [, ...]
61 / 104 62 / 104
I using a domain:
I creating a table:
CREATE TABLE table_name ( CREATE TABLE MOVIE ( CREATE TABLE MOVIE (
{ column_name data_type } ID INTEGER, ID INTEGER,
[, ... ] TITLE VARCHAR(80), TITLE VARCHAR(80),
) YR NUMERIC(4), YR NUMERIC(4),
DIRECTORID INTEGER, DIRECTORID INTEGER,
I deleting tables: SCORE FLOAT, SCORE SCORES,
DROP TABLE table_name [, ... ] VOTES INTEGER VOTES INTEGER
) )
63 / 104 64 / 104
Null and Default Values Table Creation Example
65 / 104 66 / 104
I defining constraints on values: I SCORE values must be between 1.0 and 10.0
67 / 104 68 / 104
Primary Keys Primary Key Example
69 / 104 70 / 104
71 / 104 72 / 104
Uniqueness Uniqueness Example
73 / 104 74 / 104
Uniqueness Indexes
I speeds up queries
example: person names are unique
I slows down inserts and updates
CREATE TABLE PERSON (
ID SERIAL PRIMARY KEY, example: create a year index on movies
NAME VARCHAR(40) UNIQUE NOT NULL
) CREATE INDEX MOVIE_YEAR ON MOVIE (YR)
75 / 104 76 / 104
Renaming Tables Adding Columns
example
ALTER TABLE MOVIE
example
RENAME TO FILM ALTER TABLE MOVIE
ADD COLUMN RUNTIME INTEGER
77 / 104 78 / 104
example example
ALTER TABLE MOVIE ALTER TABLE MOVIE
DROP COLUMN RUNTIME RENAME COLUMN TITLE TO NAME
79 / 104 80 / 104
Column Defaults Adding Constraints
I removing the default value from a column: I removing a constraint from a table:
ALTER TABLE table_name ALTER TABLE table_name
ALTER [ COLUMN ] column_name DROP [ CONSTRAINT ] constraint_name
DROP DEFAULT
I when adding constraints, what happens with existing tuples?
81 / 104 82 / 104
83 / 104 84 / 104
Row Insertion Example Row Insertion Example
85 / 104 86 / 104
I deleting rows: I delete movies with scores less than 3.0 and votes more than 4:
DELETE FROM table_name
DELETE FROM MOVIE
[ WHERE condition ]
WHERE ((SCORE < 3.0) AND (VOTES > 4))
I if no condition is specified, all rows will be deleted
87 / 104 88 / 104
Row Update Row Update Example
I updating rows: I register a new vote (9) for the movie ”Suspiria”
UPDATE table_name UPDATE MOVIE
SET { column_name = column_value } [, ...] SET SCORE = (SCORE * VOTES + 9)
[ WHERE condition ] / (VOTES + 1),
VOTES = VOTES + 1
I if no condition is specified, all rows will be updated
WHERE (TITLE = ’Suspiria’)
I order of column assignments is insignificant
89 / 104 90 / 104
91 / 104 92 / 104
Foreign Keys Foreign Keys
93 / 104 94 / 104
I
CREATE TABLE table_name (
what to do if referential integrity will be broken?
...
I don’t allow if used: RESTRICT, NO_ACTION [ { FOREIGN KEY ( column_name [, ...] )
I reflect the change to affected tuples: CASCADE REFERENCES table_name
[ ( column_name [, ...] ) ]
I assign null value: SET NULL
[ ON DELETE option ]
I assign default value: SET DEFAULT [ ON UPDATE option ] } [, ...] ]
...
)
95 / 104 96 / 104
Foreign Key Example Referential Integrity Example
97 / 104 98 / 104