0% found this document useful (0 votes)
177 views72 pages

02 Ch2 3slot

The document provides an introduction to the relational data model. It discusses key concepts such as the relational model representing data in tables with rows and columns, attributes defining the column headers, and tuples representing individual records in each row. The relational model uses relations or tables to store data and relational algebra to perform operations on the data, with constraints ensuring data integrity.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
177 views72 pages

02 Ch2 3slot

The document provides an introduction to the relational data model. It discusses key concepts such as the relational model representing data in tables with rows and columns, attributes defining the column headers, and tuples representing individual records in each row. The relational model uses relations or tables to store data and relational algebra to perform operations on the data, with constraints ensuring data integrity.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 72

INTRODUCTION

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

Instructor: Nguyễn Trọng Tài, MS Computer Science


Objectives

 Understand concepts of:


I2DB: The Relational Data Model

 Data model

 Relational data model

 Relational algebra

 Constraints in relational data model


2.1 An Overview of Data Models

 Data model, a notation for describing data or


I2DB: The Relational Data Model

information
 The description generally consists of three parts:
 Structure of data

 Operations on data

 Constraints
Important Data Models

 The relational model, including object-relational


I2DB: The Relational Data Model

extensions
 The semi-structured data model, including XML
and related standards
The Relational Model in Brief

 The relational model is based on tables


 Relation’s name
I2DB: The Relational Data Model

 Attributes (column headers)

 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

 Semi-structured data resembles trees or graphs rather


than tables or arrays
I2DB: The Relational Data Model

 XML, a way to represent data by hierarchically nested


tagged elements
 Operations involve following paths in tree from an element
to one or more of its nested sub elements, and so on
 Constraints involve the data type of values associated
with a nested tag
I2DB: The Relational Data Model

XML file example


2.2 Basics of the Relational Model

 The relational model represents data as a


I2DB: The Relational Data Model

2-dimensional table called a relation


 Suppose relation named as Movies

 Each row represents a movie

 Each column represents a property of movie

title Year length genre


Gone With the Wind 1939 231 Drama
Star Wars 1977 124 Scifi
Wayne’s World 1992 95 Comedy
Figure 2.3: The relation Movies
2.2 Basics of Relational Model

 Attributes
I2DB: The Relational Data Model

 a columns of a relation

 appears at the top of the columns

title year length genre


Gone With the Wind 1939 231 Drama
Star Wars 1977 124 Scifi
Wayne’s World 1992 95 Comedy

Figure 2.3: The relation Movies


2.2 Basics of Relational Model

 Relation schema
I2DB: The Relational Data Model

 Relation’s name

 Set of attributes for this relation

 I.e. Movies (title, year, length, genre)

 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

 A tuple has one component for each attribute of the relation

title year length genre


Gone With the Wind 1939 231 Drama
Star Wars 1977 124 Scifi
Wayne’s World 1992 95 Comedy

 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

 Equivalent Representation of a Relation


I2DB: The Relational Data Model

 Relations are sets of tuples, not lists of tuples

 The order in which the tuples of a relation are presented is not

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

Figure 2.4: Another presentation of Movies


2.2 Basics of Relational Model

 Relation Instances
 A relation Movies is changing over time
I2DB: The Relational Data Model

▪ Insert tuples for new movie as these appear


▪ Edit existing tuples if there are some modifications
▪ Delete a tuple from the database

 Relation schema can be changed, but expensive and not often


▪ Add new attributes to relation schema
▪ Alter existing attributes of relation schema
▪ Drop attributes of relation schema

 A set of (current) tuples for a given relation is an (current) instance of

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)

title year length genre


Gone With the Wind 1939 231 Drama
Star Wars 1977 124 Scifi
Wayne’s World 1992 95 Comedy
2.2 Basics of Relational Model

 An example of Database Schema


I2DB: The Relational Data Model

Movies( StarsIn( Studio(


title: string, movieTitle: string, name: string,
year: integer, movieYear: integer, address: string,
length: integer, starName: string presC#: integer
genre: string, ) )
studioName: string,
producerC#: integer
)
MovieStar( MovieExec(
name: string, name: string,
address: string, address: string,
gender: char, cert#: integer,
birthdate: date netWorth: integer
) )
Summary 1: Relational Model

 Relation schema: relation_name(<list of attributes>)


I2DB: The Relational Data Model

(relation name + attribute names + attribute types)


 Relation instance: current set of rows for a relation
schema. Each row is also called a tuple
 Database schema: collection of relation schemas
 Database instance: all relation instances for every
relation in the database schema.
Summary 2: Why relations?

 Very simple model


I2DB: The Relational Data Model

 Often a good match for the way we think about


our data
 Abstract model that underlies SQL, the most
important language in DBMS today
Exercises

 Exercise 2.2.1 (page 28)


I2DB: The Relational Data Model

 Exercise 2.2.3 (page 29)


 Propose a suitable database schema to manage
the information of supermarket invoice
2.3 Defining a Relation Schema in
SQL

 SQL is the principle language used to describe


I2DB: The Relational Data Model

and manipulate relation databases


 Data Definition Language: declare database schemas

 Data Manipulation Language: query databases and

modify the database


2.3 Defining a Relation Schema in
SQL

 Relations in SQL
I2DB: The Relational Data Model

 Stored relations (called tables) – a relation that exists in the

database and that can be modified by changing its tuples


 Views – relations defined by a computation – these are not

stored, but are constructed, in whole or in part


 Temporary tables – these are constructed by SQL when it

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

 Data Types, supported by SQL systems


 Character strings of fixed or varying length CHAR(n), VARCHAR(n)
I2DB: The Relational Data Model

 Bit strings of fixed or varying length BIT(n), BIT VARYING(n)

 Boolean type, BOOLEAN , the possible values are TRUE, FALSE,

and UNKNOWN
 INT or INTEGER

 Floating point numbers FLOAT, REAL, DOUBLE, and

DECIMAL(n,d)
 DATE, TIME
2.3 Defining a Relation Schema in
SQL

 Simple Table Declarations


I2DB: The Relational Data Model

CREATE TABLE Movies (


title CHAR(100),
year INT,
length INT,
genre CHAR(10),
studioName CHAR(30),
producerC#INT
);

Figure 2.7: SQL declaration of Movies


2.3 Defining a Relation Schema in
SQL

 Modifying Relation Schemas


I2DB: The Relational Data Model

 Delete a relation R
▪ DROP TABLE R;

 Modify a relation schema by keyword ALTER TABLE


▪ ALTER TABLE MovieStar ADD phone CHAR(15);
▪ ALTER TABLE MovieStar DROP phone;
▪ ALTER TABLE MovieStar ALTER COLUMN name CHAR(50);
2.3 Defining a Relation Schema in
SQL

 Default Values
I2DB: The Relational Data Model

 We can define default value when declare attribute and its

data type by keyword DEFAULT and an appropriate value


 Example
▪ gender CHAR(1) DEFAULT ‘?’

▪ birthdate DATE DEFAULT DATE ‘0000-00-00’

 ALTER TABLE MovieStar ADD phone CHAR(16)

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

▪ After finish a list of attributes, declare a set of attributes as a key

 If the key consists of more than one attribute, we must use method (2)

 If the key is a single attribute, either method may be used

 Two declarations are used to indicate key


▪ PRIMARY KEY

▪ 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

CREATE TABLE MovieStar ( CREATE TABLE Movies (


name CHAR(30) PRIMARY KEY, title CHAR(100),
address VARCHAR(255), year INT,
gender CHAR(1), length INT,
birthdate DATETIME genre CHAR(10),
) studioName CHAR(30),
producerC# INT,
CREATE TABLE MovieStar ( PRIMARY KEY (title,year)
name CHAR(30), )
address VARCHAR(255),
gender CHAR(1),
birthdate DATETIME,
PRIMARY KEY (name)
)
2.4 An Algebraic Query Language

 Relational Algebra
I2DB: The Relational Data Model

 An algebra consists of operators and atomic operands

 Relational algebra is an example of an algebra, its atomic

operands are
▪ Variables that stand for relations

▪ Constants, which are finite relations

 Relational algebra is a set of operations on relations

 Operations operate on one or more relations to create new

relation
An Algebraic Query Language

 Relational algebra fall into four classes


I2DB: The Relational Data Model

 Set operations – union, intersection, difference

 Selection and projection

 Cartesian product and joins

 Rename
Why we need …

 … set operations on relations?


I2DB: The Relational Data Model
Set Operations on Relations

 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}

 Intersection can be expressed in terms of set difference


 R  S = R \ (R \ S)
Set Operations on Relations

 R and S must be ‘type compatible’


I2DB: The Relational Data Model

 The same number of attributes

 The domain of corresponding attributes must be

compatible
Set Operations on Relations

 Example
name address gender birthdate
I2DB: The Relational Data Model

Carrie Fisher 123 Maple St., Holywood F 9/9/99


Mark Hamill 456 Oak Rd., Brentwood M 8/8/88

Relation R

name address gender birthdate


Carrie Fisher 123 Maple St., Holywood F 9/9/99
Harrison Ford 789 Palm Dr., Beverly Hills M 8/8/88

Relation S
Set Operations on Relations

 Example (cont.)
RS name address gender birthdate
I2DB: The Relational Data Model

Carrie Fisher 123 Maple St., Holywood F 9/9/99


Mark Hamill 456 Oak Rd., Brentwood M 8/8/88
Harrison Ford 789 Palm Dr., Beverly Hills M 8/8/88

RS name address gender birthdate


Carrie Fisher 123 Maple St., Holywood F 9/9/99

R\S name address gender birthdate


Mark Hamill 456 Oak Rd., Brentwood M 8/8/88
I2DB: The Relational Data Model


… selection?
Why we need …
Selection

 R1 := σC (R2)
I2DB: The Relational Data Model

 C is a condition (as in “if” statements) that refers to

attributes of R2
 R1 is all those tuples of R2 that satisfy C

 R1 has the same schema as R2

 The number of tuples of R1 is always less or equal to

the number of tuples of R2


Selection

 Example
Movies
I2DB: The Relational Data Model

title year length genre


Gone With the Wind 1939 231 Drama
Star Wars 1977 124 Scifi
Wayne’s World 1992 95 Comedy

σlength100(Movies)
title year length genre
Gone With the Wind 1939 231 Drama
Star Wars 1977 124 Scifi
Selection

 The Selection is commutative


 <condition1>( < condition2> ( R)) =  <condition2> ( < condition1> ( R))
I2DB: The Relational Data Model

 The cascaded Selection may be applied in any


order
 <condition1>( < condition2> ( <condition3> ( R))
=  <condition2> ( < condition3> ( < condition1> ( R)))

 Selection may be replaced by a single selection


with a conjunction of all the conditions
 <condition1>( < condition2> ( <condition3> ( R)) )
=  <condition1> AND < condition2> AND < condition3> ( R)
I2DB: The Relational Data Model


… projection?
Why we need …
Projection

 The projection () is used to produce from a relation


I2DB: The Relational Data Model

R a new relation S that has only some of R’s


columns
 S := πA1,A2,…,An (R)
 A1,A2,…,An are attributes of R

 S relation schema S(A1,A2,…,An)

 The projection eliminates duplicated tuples, if any


Projection

 Example
Movies
I2DB: The Relational Data Model

title year length genre


Star Wars 1977 124 Scifi
Galaxy Quest 1999 104 Comedy
Wayne’s World 1992 95 Comedy

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

 S := πA1,A2,…,An (R) or πL (R), with L is A1,A2,…,An


I2DB: The Relational Data Model

 The number of tuples of S is always less or equal to the


number of tuples of R
 If the list of attributes L (A1,A2,…,An) includes the key of R, then
the number of tuples of S is equal to the number of tuples of R
  <list1> ( <list2> ( R) ) =  <list1> ( R) as long as <list2>
contains the attributes in <list1>
Why we need …

 … Cartesian product?
I2DB: The Relational Data Model
Cartesian Product

 R3 := R1 Χ R2
I2DB: The Relational Data Model

 Pair each tuple t1 of R1 with each tuple t2 of R2

 Concatenation t1t2 is a tuple of R3

 Schema of R3 is the attributes of R1 and then R2, in order

 But beware attribute A of the same name in R1 and R2: use

R1.A and R2.A


 Suppose R1 has n1 attributes and tt1 tuples, R2 has n2

attributes and tt2 tuples,


then R3 has (n1+n2) attributes, and (tt1*tt2) tuples
Cartesian Product

 Example
I2DB: The Relational Data Model

Relation R Relation S Cartesian Product R X S


A B B C D A R.B S.B 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
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

 Schema of R3 is the attributes of R1 and then R2, in order

 But beware attribute A of the same name in R1 and R2: use R1.A and R2.A

 The result is constructed as follows


 Take the product of R1 and R2

 Select from the product only those tuples that satisfy the <join condition>

 R1 ⋈<join condition> R2 =  <join condition> (R1 x R2)


Theta Join

 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

Figure 2.17: Result of U ⋈ A<D V

A U.B U.C V.B V.C D


1 2 3 7 8 10
Result of U ⋈ A<D AND U.BV.B V
I2DB: The Relational Data Model


… natural join?
Why we need …
Natural Join

 R3 := R1 ⋈ R2
I2DB: The Relational Data Model

 Pair only those tuples from R1 and R2 that agree in

whatever attributes are common to the schema of R1


and R2
 The result R3 keeps one component for each of the

attributes in the union of the schemas of R1 and R2


Natural Join

 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

 Relational algebra allows us to form expressions


I2DB: The Relational Data Model

 Relational expression is constructed by applying


operations to the result of other operations
 Expressions can be presented as expression tree
Relational Expression

 Example: What are the titles and years of movies


I2DB: The Relational Data Model

made by Fox that are at least 100 minutes long?


 (1) Select those Movies tuples that have length  100

 (2) Select those Movies tuples that have

studioName=‘Fox’
 (3) Compute the intersection of (1) and (2)

 (4) Project the relation from (3) onto attributes title and

year
Relational Expression

 Example (con.) title,year


I2DB: The Relational Data Model

length>=100 studioName=‘Fox’

Movies Movies

Figure 2.18: Expression tree for a relational algebra expression

title,year(length100 (Movies)  studioName=‘Fox’ (Movies))


title,year(length100 AND studioName=‘Fox’ (Movies))
I2DB: The Relational Data Model


… renaming?
Why we need …
Naming and Renaming

 The  operation gives a new schema to a relation


I2DB: The Relational Data Model

 ρS(A1,…,An)(R) makes S be a relation with attributes

A1,…,An and the same tuples as R


 Simplified notation: S:=R (A1,A2,…,An)
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

 There are many kinds of constraints can be


I2DB: The Relational Data Model

expressed in relational algebra


 Key constraints

 Referential integrity constraints

 Multi-columns one relation

 Multi-columns multi-relations
Relational Algebra as a Constraint
Language

 Two ways we can use expressions of relational


I2DB: The Relational Data Model

algebra to express constraints


 If R is an expression of relational algebra, then R= is a

constraint – the value of R must be empty, there are no


tuples in the result of R
 If R and S are expressions of relational algebra, then

R  S is a constraint
 every tuple in the result of R must also be in the result of
S
Why we need …

 … a referential integrity constraint?


I2DB: The Relational Data Model
Referential Integrity Constraints

 A value appearing in one context also appears in another,


related context
I2DB: The Relational Data Model

 Any value v as the component in attribute A of some tuples


in relation R, v will appear in a component for attribute B of
some tuples in relation S
 We say, the attribute A of relation R refers to the attribute
B of relation S
 A (R)  B(S)

 A (R) - B(S) = 
Referential Integrity Constraints

 Example
I2DB: The Relational Data Model

 Movies(title, year, length, genre, studioName, producerC#)

 MovieExec(name, address, cert#, netWorth)

 The producer of every movies must appear in the


MovieExec relation
 We say, producerC# of Movies refers to cert# of
MovieExec, they make a referential integrity constraint
 producerC#(Movies) cert#(MovieExec)
Why we need …

 … key constraints?
I2DB: The Relational Data Model
Key constraints

 No two tuples agree on the key component, and


I2DB: The Relational Data Model

don’t agree on the non-key component,


 A is a key component of R relation

 B is a non-key component of R relation

 R1,R2 is two instances of R relation

 R1.A=R2.A AND R1.BR2.B(R1xR2)=


Key constraints

 Example
I2DB: The Relational Data Model

 MovieStar(name, address, gender, birthdate)

 Suppose that name is a key, and address is one non-

key attributes, and MS1, MS2 are two instances of


MovieStar, then
▪ MS1.name=MS2.name AND MS1.addressMS2.address(MS1xMS2)=, where

▪ MS1(name,address,gender,birthdate (MovieStar), and

▪ MS2(name,address,gender,birthdate (MovieStar)
Additional Constraint Examples

 Suppose that the only values for the gender


I2DB: The Relational Data Model

attribute of MovieStar are ‘F’ and ‘M’, then


 gender ‘F’ AND gender  ‘M’(MovieStar) = 

 Suppose that one must have a net worth of at least


$10,000,000 to be the president of a movie studio
 netWorth<10,000,000 (Studio ⋈pres#C=cert# MovieExec )=

 presC#(Studio)  cert#(netWorth≥10,000,000 (MovieExec))


Basics of Relational Model

 An example of Database Schema


I2DB: The Relational Data Model

Movies( StarsIn( Studio(


title: string, movieTitle: string, name: string,
year: integer, movieYear: integer, address: string,
length: integer, starName: string presC#: integer
genre: string, ) )
studioName: string,
producerC#: integer
)
MovieStar( MovieExec(
name: string, name: string,
address: string, address: string,
gender: char, cert#: integer,
birthdate: date netWorth: integer
) )
Summary of chapter 2

 Relational Model
I2DB: The Relational Data Model

 Semi-structured Data Model


 Basic Concepts on Relational Model
 Relational Algebra
 Constraints in Relational Algebra
What are in the next chapter?

 Functional dependencies
I2DB: The Relational Data Model

 Rules for functional dependencies

 Closure of attributes

 Projecting functional dependencies

 Normalization
 BCNF

 Decomposition
 To BCNF relations
Exercises

 Exercise 2.3.1 (a->f; page 36)


I2DB: The Relational Data Model

 Exercise 2.4.1 (a->g; page 52)


 Exercise 2.5.1 (a,b,c; page 62)
The Role of Relational Algebra
in a DBMS

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy