0% found this document useful (0 votes)
6 views41 pages

1 RelationalModel

Uploaded by

erterek23
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views41 pages

1 RelationalModel

Uploaded by

erterek23
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 41

Relational Model

Chapter 3

CENG 351 File Structures and Data Management 1


Relational Database: Definitions
Relational database: a set of relations

Relation: made up of 2 parts:


– Schema : specifies name of relation, plus name
and type of each column.
• e.g. Student(sid: string, name: string, login: string,
age: integer, gpa: real).
– Instance : a table, with rows and columns.
#Rows = cardinality, #fields = degree (arity).

CENG 351 File Structures and Data Management 2


Relational Database: Definitions
We can think of a relation as a set of rows or
tuples (i.e., all rows are distinct).

In relational model, a field can only contain


atomic values, i.e., lists or sets are disallowed!

CENG 351 File Structures and Data Management 3


Example Instance of Student Relation

sid name login age gpa


53666 Jones jones@cs 18 3.4
53688 Smith smith@eecs 18 3.2
53650 Smith smith@math 19 3.8

• Cardinality = 3, degree = 5, all rows distinct


• Do all columns in a relation instance have to
be distinct?
• While depicting a relation instance, order of
rows doesn’t matter!
CENG 351 File Structures and Data Management 4
Alternative Terminology for Relational Model

Formal Terms Alternative 1 Alternative 2


Relation Table File
Tuple Row Record
Attribute Column Field

CENG 351 File Structures and Data Management 5


A Relational Database Schema for a
University

• Student(sid: string, name: string, login:


string, age: integer, gpa:real)
• Course(cid: string, cname:string,
credits:integer)
• Enrolled(sid:string, cid:string, grade:string)

• This is a conceptual schema.


Query Languages
A major strength of DBMSs:
– Data Definition Language (DDL) is used to
set up schema
– Data Manipulation Language (DML) is used
to modify and query the data
• SQL:
– Standard language for creating, manipulating,
and querying data in a relational DBMS

CENG 351 File Structures and Data Management 7


Relational Query Languages
A major strength of the relational model:
supports simple, powerful querying of data.
Queries can be written intuitively, and the
DBMS is responsible for efficient
evaluation.
– Precise semantics for relational queries.
– Allows the optimizer to extensively re-order
operations, and still ensure that the answer does
not change.

CENG 351 File Structures and Data Management 8


The SQL Query Language
Developed by IBM (system R) in the 1970s
Need for a standard since it is used by many
vendors
Standards:
– SQL-86
– SQL-89 (minor revision)
– SQL-92 (major revision, current standard)
– SQL-99 (major extensions)
– SQL-2003, SQL-2006 (extensions on XML)
– SQL- 2008
– SQL-2011
CENG 351 File Structures and Data Management 9
Creating (Declaring) a Relation
• Simplest form is:
CREATE TABLE <name> (
<list of elements>
);
• To delete a relation:
DROP TABLE <name>;

CENG 351 File Structures and Data Management 10


Elements of Table Declarations
• The most common types are:
– INT or INTEGER (synonyms).
– REAL or FLOAT (synonyms).
– CHAR(n ) = fixed-length string of n
characters.
– VARCHAR(n ) = variable-length string of up
to n characters.
– DATE (e.g.:’2007-09-30’)
– TIME (e.g.:’15:30:02.5’ )
– Any value can be NULL
CENG 351 File Structures and Data Management 11
Creating Relations in SQL
 Creates the Student CREATE TABLE Student
relation. Observe that the (sid CHAR(20),
type (domain) of each field name CHAR(20),
is specified, and enforced by login CHAR(10),
the DBMS whenever tuples age INTEGER,
are added or modified. gpa REAL)

 As another example, the CREATE TABLE Enrolled


Enrolled table holds (sid CHAR(20),
information about courses cid CHAR(20),
that students take. grade CHAR(2))

CENG 351 File Structures and Data Management 12


Adding and Deleting Tuples
 Can insert a single tuple using:
INSERT INTO Student (sid, name, login, age, gpa)
VALUES (300, ‘Smith’, ‘smith@ee’, 18, 3.4)

sid name
name login
login age
age gpa
300 Smith smith@ee 18 3.4

* Powerful variants of these commands are available.


CENG 351 File Structures and Data Management 13
Adding and Deleting Tuples
 Can delete all tuples
DELETE
FROM Student S

sid
sid name login
name login age gpa
age gpa
300 Smith smith@ee 18 3.4
100 John john@ceng 19 1.7
200 Fatma fatma@ee 18 3.6

* Powerful variants of these commands are available.


CENG 351 File Structures and Data Management 14
Adding and Deleting Tuples
 Can delete all tuples satisfying some
condition (e.g., name = John):
DELETE
FROM Student S
WHERE S.name = ‘John’
sid name login age gpa
300 Smith smith@ee 18 3.4
200
100 Fatma
John fatma@ee 19
john@ceng 18 3.6
1.7
200 Fatma fatma@ee 18 3.6

* Powerful variants of these commands are available.


CENG 351 File Structures and Data Management 15
Modifying Tuples
 Can modify the column values in an existing row
using:
UPDATE Student S
SET S.age = S.age + 1

sid name login age gpa


300 Smith smith@ee 18
19 3.2
100 John john@ceng 19
20 1.7
200 Fatma fatma@ee 18
19 3.6

CENG 351 File Structures and Data Management 16


Modifying Tuples
 Can modify the column values in an existing row
using:
UPDATE Student S
SET S.age = S.age + 1, S.gpa = S.gpa –1
WHERE S.sid = 200

sid name login age gpa


300 Smith smith@ee 18 3.4
100 John john@ceng 19 1.7
200 Fatma fatma@ee 18
19 3.6
2.6

CENG 351 File Structures and Data Management 17


Modifying Tuples
 Can modify the column values in an existing row
using:
UPDATE Student S
SET S.gpa = S.gpa – 0.1
WHERE S.gpa >= 3.3
sid name login age gpa
300 Smith smith@ee 18 3.4
3.3
100 John john@ceng 19 1.7
200 Fatma fatma@ee 18 3.6
3.5

Remark: In the examples of DELETE and UPDATE, the


WHERE clause is applied first to determine the rows to be
deleted or updated. CENG 351 File Structures and Data Management 18
Integrity Constraints (ICs)
 IC: a condition specified on a DB schema &
restricts the data that can be stored in an instance
 condition that must be true for any instance of the
database; e.g., domain constraints.
– ICs are specified when schema is defined.
– ICs are checked when relations are modified.
 A legal instance of a relation is one that satisfies all
specified ICs.
– DBMS should not allow illegal instances.
 If the DBMS checks ICs, stored data is more
faithful to real-world meaning.
– Avoids data entry errors, too!
CENG 351 File Structures and Data Management 19
Key Constraints
 A set of fields is a (candidate) key for a relation if :
1. uniqueness: Two distinct tuples cannot have identical
values in all the fields of a key, and
2. minimality: No subset of the set of fields in a key is a
unique identifier for a tuple
– If part 2 false, then it is a superkey.
– If there is more than one (candidate) key for a relation, one
of the keys is chosen (by DBA) to be the primary key.

The DBMS optimizes several tasks for the use of PK (eg., while
referring to a tuple from another relation or while accessing the table
via an index, etc.)
CENG 351 File Structures and Data Management 20
Key Constraints: Example
• Student(sid: string, name: string, login:
string, age: integer, gpa:real)
• Based on the application requriements of a university DB:
– What can be the candidate keys for Student? sid or: login
– What can be the superkeys? sid,name
– Which one would you choose as sid,gpa PK?
sid,name,gpa
sid sid,name,gpa,login,age
Remark: ICs are based upon the semanticslogin,name
of the real-world enterprise that is being login,name,gpa
described in the database relations. sid,login

CENG 351 File Structures and Data Management 21
Primary and Candidate Keys in SQL
 Possibly many candidate keys, one of which is chosen as
the primary key.
CREATE TABLE Student
(sid CHAR(20),
name CHAR(20),
login CHAR(10),
age INTEGER,
gpa REAL,
PRIMARY KEY (sid),
UNIQUE (login))
 For domain, PK and unique constraints: If a DB modification
operation (add/del/update) violates them , operation is rejected
22
Primary and Candidate Keys in SQL
 “For a given student and course, there is a single grade.”

sid cid grade CREATE TABLE Enrolled


(sid CHAR(20)
53666 Ceng351 A cid CHAR(20),
53666 Ceng351 B- grade CHAR(2),
53666 Ceng331 A PRIMARY KEY (sid,cid))

Good design if Enrolled is


intended to keep the
current semester’s data!

An IC can prevent storage of instances that arise in practice! So, DBA


must set ICs carefully depending on the nature of the application
domain!
CENG 351 File Structures and Data Management 23
Primary and Candidate Keys in SQL
CREATE TABLE Enrolled
(sid CHAR(20)  Implication: “Students can take
cid CHAR(20), only one course, and receive a
grade CHAR(2), single grade for that course;
PRIMARY KEY (sid), further, no two students in a
UNIQUE (cid, grade) ) course receive the same grade.”

“sid cid grade An IC can prevent


storage of instances that
53666 Ceng351 A arise in practice!
53666 Ceng331 B-
53444 Ceng351 A X Bad design (or your
university is really weird!!!)

CENG 351 File Structures and Data Management 24


PRIMARY KEY vs. UNIQUE

1. There can be only one PRIMARY KEY


for a relation, but several UNIQUE
attributes.
2. No attribute of a PRIMARY KEY can
ever be NULL in any tuple. But attributes
declared UNIQUE may have NULL’s,
and there may be several tuples with
NULL.
For unknown/missing/inapplicable values
CENG 351 File Structures and Data Management 25
Foreign Keys, Referential Integrity

Foreign key : Set of fields in one relation that is


used to `refer’ to a tuple in another relation.
(Must correspond to primary key of the second
relation.) Like a `logical pointer’.
e.g. sid is a foreign key referring to Student:
– Enrolled(sid: string, cid: string, grade: string)
– If all foreign key constraints are enforced,
referential integrity is achieved, i.e., no dangling
references.
CENG 351 File Structures and Data Management 26
Foreign keys…
Enrolled(sid: string, cid: string, …)
Student(sid:string, …) Course(cid:string, …)
Primary key Primary key
Foreign Keys in SQL
Only students listed in the Student relation
should be allowed to enroll for courses.
CREATE TABLE Enrolled
(sid CHAR(20), cid CHAR(20), grade CHAR(2),
PRIMARY KEY (sid,cid),
FOREIGN KEY (sid) REFERENCES Student )
Enrolled
sid cid grade Student
sid name login age gpa
53666 Carnatic101 C
53666 Reggae203 B 53666 Jones jones@cs 18 3.4
53650 Topology112 A 53688 Smith smith@eecs 18 3.2
53666 History105 B 53650 Smith smith@math 19 3.8

CENG 351 File Structures and Data Management 28


Enforcing Referential Integrity
 Consider Student and Enrolled; sid in Enrolled is a foreign
key that references Student.
 What should be done if an Enrolled tuple with a non-
existent student id is inserted? (Reject it!)
 What should be done if a Student tuple is deleted?
– Disallow deletion of a Student tuple that is referred to.
– Set sid in Enrolled tuples that refer to it to a default sid.
– (In SQL, also: Set sid in Enrolled tuples that refer to it to a special
value null, denoting `unknown’ or `inapplicable’.)
– Also delete all Enrolled tuples that refer to it.
 Similar if primary key of Student tuple is updated.
CENG 351 File Structures and Data Management 29
Handling Foreign Key Violations

• Deletion from Student:


– NO ACTION: Reject if row(s) in Enrolled
references row to be deleted (default response)

Enrolled
Student
x ?
x

Request to
delete row

rejected 30
Handling Foreign Key Violations (cont’d)
• Deletion from Student (cont’d):
– SET NULL: Set value of foreign key in
referencing row(s) in A to null

Enrolled Student

x
null
x

Row
deleted

31
Handling Foreign Key Violations (cont’d)
• Deletion from Student (cont’d):
– SET DEFAULT: Set value of foreign key in
referencing row(s) in Enrolled to default value
(y) which must exist in Student
Default
Enrolled value

y Student
x
x

Row
deleted

32
Handling Foreign Key Violations (cont’d)
• Deletion from Student (cont’d):
– SET DEFAULT: Set value of foreign key in
referencing row(s) in Enrolled to default value
(y) which must exist in Student
Default
Enrolled value

y Student
y

Row
deleted

33
Handling Foreign Key Violations
(cont’d)

• Deletion from Student (cont’d):


– CASCADE: Delete referencing row(s) in Enrolled as well

Enrolled
Student

x x

34
Referential Integrity in SQL/92

 SQL/92 supports all 4 options on deletes and updates.


– Default is NO ACTION (delete/update is rejected)
– SET NULL / SET DEFAULT (sets foreign key value of
referencing tuple)
– CASCADE (also delete all tuples that refer to deleted
tuple)

CENG 351 File Structures and Data Management 35


Which of these make sense for Enrolled?
 SQL/92 supports all 4 options on deletes and updates:
NO ACTION / SET NULL / SET DEFAULT / CASCADE

CREATE TABLE Enrolled


(sid CHAR(20) DEFAULT ‘9999’,
cid CHAR(20),
grade CHAR(2),
PRIMARY KEY (sid,cid),
FOREIGN KEY (sid) REFERENCES Student ON DELETE NO ACTION
ON UPDATE CASCADE
FOREIGN KEY (cid) REFERENCES Course ON DELETE NO ACTION
ON UPDATE CASCADE )

CENG 351 File Structures and Data Management 36


Foreign Keys: Remarks
• NULL can appear in a FK (unless FK is also the part of
the PK in the child table, of course)
• Assume in Student relation, we also keep student’s advisor
id, which refers to Instructor relation (with PK field iid):
Student
sid name login age gpa iid
53666 Jones jones@cs 18 3.4 1 Instructor
53688 Smith Smith@eecs 18 3.2 Null iid name salary
53650 Smith Smith@math 19 3.8 1 1 Altingovde
2 Karagoz
3 Chair
CENG 351 File Structures and Data Management 37
CREATE TABLE Student
(sid CHAR(20),
name CHAR(20),
Foreign Keys: Remarks
login CHAR(10),
age INTEGER,
• NULL
gpa REAL,can appear in a FK (unless FK is also the part of the
iid CHAR(20) DEFAULT ‘3’,
PK in the child table, of course)
PRIMARY KEY (sid),
• Assume
FOREIGN KEYin(iid)
Student relation, we
REFERENCES also keepON
Instructor student’s
DELETEadvisor id,
SET DEFAULT
which refers to Instructor relation (with ON
PK field iid):CASCADE )
UPDATE
Student
sid name login age gpa iid
53666 Jones jones@cs 18 3.4 1
53688 Smith Smith@eecs 18 3.2 Null iid name salary
53650 Smith Smith@math 19 3.8 1 1 Altingovde
2 Karagoz
3 Chair
CENG 351 File Structures and Data Management 38
Foreign Keys: Remarks
• NULL can appear in a FK (unless FK is also the part of the
PK in the child table, of course)
• The field names in the FK may be different than that of
the referred PK, but the number of fields should be same
and field types should be compatible
CREATE TABLE Enrolled
(student-id CHAR(20) DEFAULT ‘9999’,
cid CHAR(20),
grade CHAR(2),
PRIMARY KEY (student-id,cid),
FOREIGN KEY (student-id) REFERENCES Student(sid)
ON DELETE NO ACTION
ON UPDATE CASCADE
CENG 351 File Structures and Data Management 39
Foreign Keys: Remarks
• A FK can refer to the same relation
– In Student relation, there could be the partner field that is
intended to store the sid of another student…
– More examples will be discussed in the ER week…

CENG 351 File Structures and Data Management 40


Where do ICs Come From?
 ICs are based upon the semantics of the real-world
enterprise that is being described in the database
relations.
 We can check a database instance to see if an IC is
violated, but we can not infer that an IC is true by
looking at an instance.
– An IC is a statement about all possible instances!
– From example, we know name is not a key, but the
assertion that sid is a key is given to us.
 Key and foreign key ICs are the most common;
more general ICs supported too.
CENG 351 File Structures and Data Management 41

You might also like