CH03-The Relational Model
CH03-The Relational Model
Chapter 03:
The Relational Model
Outline
2
Introduction
3
Relational Database: Definitions
• Relational database: a set of relations
• Relation: made up of 2 parts:
– Instance: a table, with rows and columns.
#Rows = cardinality
#Fields = degree
– Schema : specifies name of relation, plus name and
type of each column.
• Example: Students(sid: string, name: string, login:
string, age: integer, gpa: real)
• Can think of a relation as a set of rows or tuples
4
Example Instance of Students Relation
5
Relational Query Languages
• A major strength of the relational model: supports
simple, powerful querying of data.
6
Attribute Types / Domain
• Each column of a relation has a name
• Set of allowed values for each column is called
domain of column
– Domain specifies that values of the column must be
drawn from the domain associated with the column –
domain constraint
7
Relations are Unordered
• Order of tuples is irrelevant (tuples may be stored in an
arbitrary order)
• E.g., Account relation with unordered tuples
8
Database
• A database consists of multiple relations.
• Information about an enterprise is broken up into
parts, each relation storing one part of the
information
• E.g.:
– account : stores information about account.
– depositor : stores information about which customer
owns which account
– customer : stores information about customers
9
The SQL Query Language
• To find all 18 year old students, we can write:
SELECT *
FROM Students S
WHERE S.age=18
10
Querying Multiple Relations
What does the following query compute?
SELECT S.name, E.cid
FROM Students S, Enrolled E
WHERE S.sid=E.sid AND E.grade=“A”
we get:
11
Create Table
Syntax
Create table tablename(
Attribute1 attrubitetype Primary Key,
Attrbuite2 attrbuitetype,
.
.
.
)
12
Creating Relations in SQL
• Creates the Students
CREATE TABLE Students
relation. Observe that the (sid CHAR(20),
type (domain) of each name CHAR(20),
field is specified, and login CHAR(10),
enforced by the DBMS age INTEGER,
whenever tuples are gpa REAL)
added or modified.
13
Creating Relations in SQL
14
Destroying and Altering Relations
15
Adding and Deleting Tuples
16
Integrity Constraints (ICs)
• IC: 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 check ICs, stored data is more close to
real-world meaning.
– Avoids data entry errors, too!
17
Integrity Constraints (ICs) “Enhancements”
18
Candidate Key
• A candidate key is a column, or set of columns, in a
table that can uniquely identify any database record
• Example:
• {customer-name} is a candidate key for Customer.
• If there’s more than one key for a relation, one of the keys
is chosen (by DBA) to be the primary key.
19
Primary Key Constraints
• If there are more than one candidate key for a
relation, one of the keys is chosen (by DBA) to be the
primary key.
20
Primary and Candidate Keys in SQL
• Possibly many candidate keys (specified using UNIQUE),
one of which is chosen as the primary key.
CREATE TABLE Enrolled
• “For a given student and course, (sid CHAR(20)
there is a single grade.” vs. cid CHAR(20),
“Students can take only one grade CHAR(2),
course, and receive a single grade PRIMARY KEY (sid,cid) )
for that course; further, no two
students in a course receive the
CREATE TABLE Enrolled
same grade.”
(sid CHAR(20)
• Used carelessly, an IC can prevent cid CHAR(20),
the storage of database instances grade CHAR(2),
that arise in practice! PRIMARY KEY (sid),
UNIQUE (cid, grade) )
21
Key Constraints
22
Foreign Keys, Referential Integrity
• Foreign key : Set of fields in one relation that is used
to refer to a tuple, only one, in another relation.
– Almost correspond to primary key of the second
relation “Like a logical pointer”.
• E.g. sid is a foreign key referring to Students:
– Enrolled(sid: string, cid: string, grade: string)
– If all foreign key constraints are enforced, referential
integrity is achieved, i.e., no dangling references.
23
Foreign Keys in SQL
• Only students listed in the Students 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 Students )
Enrolled
Students
24
Referential action
• A foreign key is a column or set of columns that
links each row in the child table contains the
foreign key to the row of the parent table contains
the marching primary key.
• A referential integrity means that if the foreign key
contains a value must refers to an existing value in
the parent table
25
Enforcing Referential Integrity
• Consider Students and Enrolled; sid in Enrolled is a
foreign key that references Students.
– What should be done if an Enrolled tuple with a non-
existent student id is inserted? (Reject it!)
– What should be done if a Students tuple is deleted?
• Also delete all Enrolled tuples that refer to it.
• Disallow deletion of a Students 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’.)
– Similar if primary key of Students tuple is updated.
26
Referential action
When the user want to delete row from a parent
table, and there are some matching rows in the child
table, SQL supports four options regarding the
referential action:
Cascade: Delete the row from the parent table automatically
deleting the making rows in the child table.
Set NULL: Delete the row from the parent table, set foreign key in
the child table to NULL.
Set Default: Delete the row from the parent table, set foreign key in
the child table to default value.
No Action: Reject the deletion in parent table if it has rows in the
child table. This action is the default.
27
Referential Integrity in SQL
CREATE TABLE Enrolled
(sid CHAR(20),
cid CHAR(20),
grade CHAR(2),
PRIMARY KEY (sid,cid),
FOREIGN KEY (sid)
REFERENCES Students
ON DELETE CASCADE
ON UPDATE SET DEFAULT
)
28
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 NEVER 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.
29
Not Null
NULL values
– Each column supports NULL value by default, however, if you want the value
to not be empty any time, you can put NOT NULL in front of the attribute.
– The NOT NULL constraint enforces a field to always contain a value
CREATE TABLE Persons
(
P_Id number primary key,
LastName varchar(25) NOT NULL,
FirstName varchar(25),
Address varchar(25),
City varchar(25)
)
– primary key means automatically not null.
30
Default value
The DEFAULT constraint is used to insert a default value into
a column.
Status Char default (‘T’);
• The value of the status will be T incase it is not given.
CREATE TABLE Persons
(
P_Id number primary key,
LastName varchar(25) NOT NULL,
FirstName varchar(25),
Address varchar(25),
City varchar(25) Default ‘Palestine’
)
31
Auto increment
• By default, the starting value for IDENTITY is 1, and it will increment by 1 for
each new record.
• To specify that the "P_Id" column should start at value 10 and increment by
5, change the identity to IDENTITY(10,5).
32
Domain constraint
33
Domain constraint
34
Logical DB Design: ER to Relational
• Entity sets to tables:
35
Relationship Sets to Tables
• In translating a relationship
set to a relation, attributes
CREATE TABLE Works_In(
of the relation must include:
ssn CHAR(11),
– Keys for each did INTEGER,
participating entity set since DATE,
(as foreign keys). PRIMARY KEY (ssn, did),
– All descriptive attributes. FOREIGN KEY (ssn)
REFERENCES Employees,
FOREIGN KEY (did)
REFERENCES Departments)
36
Review: Key Constraints
since
• Each dept has at most name dname
one manager, ssn lot did budget
according to the key
constraint on Employees Manages Departments
Manages.
Translation to
relational model?
Hourly_Emps Contract_Emps
Review: ISA Hierarchies
General approach:
3 relations: Employees, Hourly_Emps and Contract_Emps.
•Hourly_Emps: Every employee is recorded in Employees. For
hourly emps, extra info recorded in Hourly_Emps
(hourly_wages, hours_worked, ssn); must delete Hourly_Emps
tuple if referenced Employees tuple is deleted).
•Queries involving all employees easy, those involving just
Hourly_Emps require a join to get some attributes.
Alternative: Just Hourly_Emps and Contract_Emps.
• Hourly_Emps: ssn, name, lot, hourly_wages, hours_worked.
• Each employee must be in one of these two subclasses
Review: Binary vs. Ternary
Relationships (Contd.)
Views
46
Views
this.
47
Views and Security
• Views can be used to present necessary information
(or a summary), while hiding details in underlying
relation(s).
– Given YoungStudents, but not Students or Enrolled, we
can find students s who have are enrolled, but not the
cid’s of the courses they are enrolled in.
48
Create Table Syntax
51
ALTER TABLE Syntax
53
Relational Model: Summary
• A tabular representation of data.
• Simple and intuitive, currently the most widely used.
• Integrity constraints can be specified by the DBA,
based on application semantics. DBMS checks for
violations.
– Two important ICs: primary and foreign keys
– In addition, we always have domain constraints.
• Powerful and natural query languages exist.
• Rules to translate ER to relational model
54