0% found this document useful (0 votes)
16 views27 pages

Chapter 2 - Relational Model

Relational Model
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)
16 views27 pages

Chapter 2 - Relational Model

Relational Model
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/ 27

In the Lecture Series Introduction to Database Systems

Relational Model

Introduction to Database Systems


Data Models

• Hierarchical Model 1965 (IMS)


• Network Model 1965 (DBTG)
• Relational Model (1NF) 1970s
(E.F. Codd “A Relational Model for Large Shared Data
Banks” Communication of the ACM, Vol 13, #6)
• Nested Relational Model 1970s
• Complex Object 1980s
• Object Model 1980 (OQL)
• Object Relational Model 1990s (SQL)
• XML (DTD), XML Schema 1990s (Xpath, Xquery)
• NoSQL Databases (MongoDB)
https://www.youtube.com/watch?v=Q45sr5p_NmQ

Introduction to Database Systems


Designing Database Applications

• Real World

• Logical Model (Relational Model)


(Logical Data Independence: ``Future users of large data banks
must be protected from having to know how the data is organized
in the machine’’ E.F Codd)

(We need a model for design and implementation)

• Physical Model
(need to be understood for tuning – cs3223, cs4221)

Introduction to Database Systems


Idea

• Use mathematics to describe and represent


records and collections of records: the relation
• can be understood formally
• leads to formal query languages
• properties can be explained and proven
• Use a simple data structure: the Table
• simple to understand
• useful data structure (capture many situations)
• leads to useful yet not too complex query languages

(SQL was invented by D. Chamberlain and R. Boyce in 1974 at IBM for the
first relational database management system System R. SQL is an ANSI
standard since1986. SQL is an ISO standard since 1987. We refer to
SQL-92 (or SQL2))

Introduction to Database Systems


Relation Instance

relation name column


number of columns: degree or arity
book attribute name:
domain
table
(or type)
title:VARCHAR(128) authors:VARCHAR(128) publisher:VARCHAR(32) ISBN13:CHAR(14)
relation schema
The Future of Cathy N. Davidson, The MIT Press 978-0262513593
Learning Institutions David Theo Goldberg
row t-uple
in a Digital Age
Introduction to Thomas H. Cormen, The MIT Press 978-0262033848
Algorithms Charles E. Leiserson,
Ronald L. Rivest,
Clifford Stein
The Shallows: What Nicholas Carr W. W. Norton & 978-0393072228
the Internet Is Doing Company number
to Our Brains of rows:
The Digital Scott Kelby Peachpit Press 978-0321474049
cardinality
Photography Book
Computer David A. Patterson,relation instance
Morgan Kaufmann 978-0123744937
Organization and John L. Hennessy
Design
Introduction to Thomas H. Cormen, The MIT Press 978-0262033848
Algorithms Charles E. Leiserson,
Ronald L. Rivest,
Clifford Stein

Introduction to Database Systems


Integrity Constraints in SQL

Introduction to Database Systems


SQL Integrity Constraints

• PRIMARY KEY
• NOT NULL
• UNIQUE
• FOREIGN KEY
• CHECK

Introduction to Database Systems


Structural Constraints

• The choice of the number of columns and


their domains imposes structural
constraints

CREATE TABLE registration(


Student VARCHAR(10);
Module VARCHAR(6));

• No student without a module and no


module without a student, unless we use
NULL values
Introduction to Database Systems
Integrity Constraint: What Do They Do?

• Integrity constraints are checked by the


DBMS before a transaction
(BEGIN...END) modifying the data is
committed;
• If an integrity constraint is violated, the
transaction is aborted and rolled back,
the changes are not reflected;
• Otherwise the transaction is committed
and the changes are effective.
Note: In SQL integrity constraints can be immediate or deferred. You should always use deferred
constraints.

Introduction to Database Systems


Integrity Constraints – Primary Key

• A Primary Key is a set of attributes that


identifies uniquely a t-uple:
• People
• national identification number
• email address
• first name and last name
• Flights
• Airline name and flight number
• Books
• ISBN
• You cannot have two t-uples with the
same Primary Key in the same table
Introduction to Database Systems
Column (Value) Constraint – PRIMARY KEY

CREATE TABLE book (


title VARCHAR(256),
authors VARCHAR(256),
publisher VARCHAR(64),
ISBN10 CHAR(10),
ISBN13 CHAR(14) PRIMARY KEY
)

book(title VARCHAR(128), authors VARCHAR(128), publisher VARCHAR(32), ISBN10


CHAR(10), ISBN13 CHAR(14) )

book(title , authors, publisher, ISBN10, ISBN13 CHAR(14) )

Introduction to Database Systems


Table Constraint – PRIMARY KEY

CREATE TABLE copy (


owner VARCHAR(256),
book CHAR(14),
copy INT,
PRIMARY KEY (owner, book, copy))

Introduction to Database Systems


NULL Values

• Every domain (type) has an additional


value: the NULL value (read
Ramakrishnan)
• The semantics of NULL is ambiguous:
• Unknown
• Does not exists
• Unknown or does not exists

Introduction to Database Systems


NULL Values Logic

P Q P AND Q P OR Q NOT P
True True True True False
False True False True True
Unknown True Unknown True Unknown
True False False True False
False False False False True
Unknown False False Unknown Unknown
True Unknown Unknown True False
False Unknown False Unknown True
Unknown Unknown Unknown Unknown Unknown

Introduction to Database Systems


NULL Values Arithmetic

• Something = NULL is unknown


• Something < NULL is unknown
• Something > NULL is unknown
• 10 + NULL is unknown
• 10 * NULL is unknown
• COUNT(*) count NULL values
• COUNT, AVG, MAX, MIN eliminate NULL
values

Introduction to Database Systems


Column Constraint – NOT NULL

CREATE TABLE book (


title VARCHAR(256),
authors VARCHAR(256),
publisher VARCHAR(64),
ISBN13 CHAR(14) PRIMARY KEY
ISBN10 CHAR(10) NOT NULL)

Introduction to Database Systems


Column Constraint - UNIQUE

CREATE TABLE book (


title VARCHAR(256),
authors VARCHAR(256),
publisher VARCHAR(64),
ISBN13 CHAR(14) PRIMARY KEY
ISBN10 CHAR(10) NOT NULL UNIQUE)

Introduction to Database Systems


Table Constraint - UNIQUE

CREATE TABLE student (


first_name VARCHAR(32)
last_name VARCHAR(32),
UNIQUE (first_name, last_name))

• The combination of the two attributes must be


unique

Introduction to Database Systems


Column Constraint – FOREIGN KEY (referential
integrity)

CREATE TABLE copy (


owner VARCHAR(256) REFERENCES student(email),
book CHAR(14) REFERENCES book(ISBN13),
copy INT,
PRIMARY KEY (owner, book, copy))

email is an attribute of the relation student


email must be the primary key the relation student

Introduction to Database Systems


Column Constraint - CHECK

CREATE TABLE copy (


owner VARCHAR(256) REFERENCES student(email),
book CHAR(14) REFERENCES book(ISBN13),
copy INT CHECK(copy > 0),
PRIMARY KEY (owner, book, copy))

See also CREATE DOMAIN and CREATE TYPE

Introduction to Database Systems


Column Constraint - CHECK

CREATE TABLE copy (


owner VARCHAR(256) REFERENCES student(email),
book CHAR(14) REFERENCES book(ISBN13),
copy INT CONSTRAINT non_zero CHECK(copy > 0),
PRIMARY KEY (owner, book, copy))

Introduction to Database Systems


Table Constraint - CHECK

CREATE TABLE loan (


borrower VARCHAR(256) REFERENCES student(email),
owner VARCHAR(256),
book CHAR(14),
Copy INT,
borrowed DATE NOT NULL ,
return DATE,
FOREIGN KEY (owner, book, copy) REFERENCES
copy(owner, book, copy),
PRIMARY KEY (borrower, owner, book, copy),
CHECK(return >= borrowed OR return IS NULL))

Introduction to Database Systems


Table Constraint? – CHECK (Doesn’t work! )

CHECK(NOT EXISTS
(SELECT *
FROM loan l1, loan l2
WHERE l1.owner=l2.owner AND l1.book=l2.book AND
l1.copy=l2.copy AND l1.borrowed <= l2.borrowed AND
(l2.borrowed <= l1.return OR l1.return IS NULL))

``A copy cannot be borrowed until it is returned’’

Introduction to Database Systems


Enforcing Integrity Constraints

CREATE TABLE copy (


owner VARCHAR(256) REFERENCES student(email),
book CHAR(14) REFERENCES book(ISBN13),
copy INT,
PRIMARY KEY (owner, book, copy))

Introduction to Database Systems


Enforcing Integrity Constraints

Updates and deletions that violates foreign key constraints are rejected.

copy Could they be compensated?

copy book email


1 978-0596101992 [email protected]
1 978-0596520830 [email protected]
2 978-0596520830 [email protected]

student
email name year
[email protected] Jong-jin Lee 2009

[email protected] Thomas Lee 2008

[email protected] Helen Dewi Gema 2009

Introduction to Database Systems


Enforcing Integrity Constraints

CREATE TABLE copy (


owner VARCHAR(256) REFERENCES
student(email)
ON UPDATE CASCADE
ON DELETE CASCADE,
book CHAR(14) REFERENCES
book(ISBN13)
ON UPDATE CASCADE
ON DELETE CASCADE,
copy INT,
PRIMARY KEY (owner, book, copy))

Introduction to Database Systems


Enforcing Integrity Constraints

ON UPDATE/DELETE
 CASCADE
 NO ACTION
 SET DEFAULT
 SET NULL

Introduction to Database Systems

You might also like