02 Ch2 3slot
02 Ch2 3slot
TO
DATABASE
L E A R N B Y E X A M P L E S
R E L AT I O N A L D ATA M O D E L
Data model
Relational algebra
information
The description generally consists of three parts:
Structure of data
Operations on data
Constraints
Important Data Models
extensions
The semi-structured data model, including XML
and related standards
The Relational Model in Brief
Tuples (rows)
The operations associated with the relational model form the relational
algebra
The constraints on relational model define limitations on what the data can be
on tables
The Semi-structured Model in
Brief
Attributes
I2DB: The Relational Data Model
a columns of a relation
Relation schema
I2DB: The Relational Data Model
Relation’s name
Database schema
Set of schemas for the relations of a database
2.2 Basics of Relational Model
Tuples
The rows of a relation, excepted the header row
I2DB: The Relational Data Model
We shall use the order in which the attributes were listed in relation
schema
Ex: (Gone With the Wind,1939,231,Drama)
2.2 Basics of Relational Model
Domains
Each component of each tuple must be elementary type such as
I2DB: The Relational Data Model
INTEGER or STRING
It is not permitted for a value to be a record structure, set, list, array,
or any type that can have its values broken into smaller components
Domain is a particular elementary type of attribute
The components of any tuple must have a value that belongs to the
domain of the corresponding column
Example in figure 2.3
Movies(title:string,year:integer,length:integer,genre:string)
2.2 Basics of Relational Model
important
Reorder the attributes reorder the columns reorder the
components of tuples
year genre title length
1939 Drama Star Wars 124
1977 Scifi Wayne’s World 95
1992 Comedy Gone With the Wind 231
Relation Instances
A relation Movies is changing over time
I2DB: The Relational Data Model
that relation
2.2 Basics of Relational Model
Keys of Relations
I2DB: The Relational Data Model
A set of attributes forms a key for a relation if we don’t allow two tuples in
all relation instance to have the same values in all attributes of the key
Example: in Figure 2.3, we consider that there could not ever be two
movies that had both the same title and the same year. So, we choose a
set of title, and year as a key
▪ Movies(title, year, length, genre)
Relations in SQL
I2DB: The Relational Data Model
performs its job, and are thrown away and not stored
SQL command for creating tables
CREATE TABLE statement
2.3 Defining a Relation Schema in
SQL
and UNKNOWN
INT or INTEGER
DECIMAL(n,d)
DATE, TIME
2.3 Defining a Relation Schema in
SQL
Delete a relation R
▪ DROP TABLE R;
Default Values
I2DB: The Relational Data Model
DEFAULT ‘unlisted’;
2.3 Defining a Relation Schema in
SQL
Declaring Keys
Two ways to declare keys in the CREATE TABLE statement
I2DB: The Relational Data Model
▪ Declare one attribute to be a key when that attribute is listed in the relation schema
If the key consists of more than one attribute, we must use method (2)
▪ UNIQUE
NULL values are available for UNIQUE, but not for PRIMARY KEY
2.3 Defining a Relation Schema in
SQL
Declaring keys
I2DB: The Relational Data Model
Relational Algebra
I2DB: The Relational Data Model
operands are
▪ Variables that stand for relations
relation
An Algebraic Query Language
Rename
Why we need …
Union
R S = { t | t R t S}
I2DB: The Relational Data Model
Intersection
R S = { t | t R t S}
Difference
R \ S = { t | t R t S}
compatible
Set Operations on Relations
Example
name address gender birthdate
I2DB: The Relational Data Model
Relation R
Relation S
Set Operations on Relations
Example (cont.)
RS name address gender birthdate
I2DB: The Relational Data Model
… selection?
Why we need …
Selection
R1 := σC (R2)
I2DB: The Relational Data Model
attributes of R2
R1 is all those tuples of R2 that satisfy C
Example
Movies
I2DB: The Relational Data Model
σlength100(Movies)
title year length genre
Gone With the Wind 1939 231 Drama
Star Wars 1977 124 Scifi
Selection
… projection?
Why we need …
Projection
Example
Movies
I2DB: The Relational Data Model
title,year,length(Movies) genre(Movies)
title year length genre
Star Wars 1977 124 Scifi
Galaxy Quest 1999 104 Comedy
Wayne’s World 1992 95
Projection
… Cartesian product?
I2DB: The Relational Data Model
Cartesian Product
R3 := R1 Χ R2
I2DB: The Relational Data Model
Example
I2DB: The Relational Data Model
… theta joins?
Why we need …
Theta Join
R3 := R1 ⋈<join condition> R2
Each tuple t1 of R1 connects with all those tuple t2 of R2 that satisfy <join
I2DB: The Relational Data Model
condition>
<join condition> refers to attributes of R1 and R2
But beware attribute A of the same name in R1 and R2: use R1.A and R2.A
Select from the product only those tuples that satisfy the <join condition>
Example
A B C B C D A U.B U.C V.B V.C D
I2DB: The Relational Data Model
1 2 3 2 3 4 1 2 3 2 3 4
6 7 8 2 3 5 1 2 3 2 3 5
9 7 8 7 8 10 1 2 3 7 8 10
6 7 8 7 8 10
Relation U Relation V
9 7 8 7 8 10
… natural join?
Why we need …
Natural Join
R3 := R1 ⋈ R2
I2DB: The Relational Data Model
Example
Natural Join R ⋈ S
I2DB: The Relational Data Model
Relation R Relation S
A B B C D A B C D
1 2 2 5 6 1 2 5 6
3 4 4 7 8 3 4 7 8
9 10 11
How we need …
… relational expression?
I2DB: The Relational Data Model
Relational Expression
studioName=‘Fox’
(3) Compute the intersection of (1) and (2)
(4) Project the relation from (3) onto attributes title and
year
Relational Expression
length>=100 studioName=‘Fox’
Movies Movies
… renaming?
Why we need …
Naming and Renaming
Example
S(X,C,D) (S)
I2DB: The Relational Data Model
Relation R Relation S RX
A B B C D A B X C D
1 2 2 5 6 1 2 2 5 6
3 4 4 7 8 1 2 4 7 8
9 10 11 1 2 9 10 11
3 4 2 5 6
3 4 4 7 8
3 4 9 10 11
2.5 Constraints on Relations
Multi-columns multi-relations
Relational Algebra as a Constraint
Language
R S is a constraint
every tuple in the result of R must also be in the result of
S
Why we need …
A (R) - B(S) =
Referential Integrity Constraints
Example
I2DB: The Relational Data Model
… key constraints?
I2DB: The Relational Data Model
Key constraints
Example
I2DB: The Relational Data Model
▪ MS2(name,address,gender,birthdate (MovieStar)
Additional Constraint Examples
Relational Model
I2DB: The Relational Data Model
Functional dependencies
I2DB: The Relational Data Model
Closure of attributes
Normalization
BCNF
Decomposition
To BCNF relations
Exercises