Download as pdf or txt
Download as pdf or txt
You are on page 1of 33

The Relational Model

„ How do we represent data in the relational model?


„ How are data created and modified?
„ How do we express integrity constraints?
„ How can we perform the operations above using SQL?
„ How do we translate an ER diagram to a relational DB
schema?
„ What are views and how are they used?

Intro. to Dababase Management Systems 3.1 Lecture notes by Nikos Mamoulis


Why Learn the Relational Model

„ By far the most dominant database model


„ Used in leading DBMSs:
¾ IBM’s DB2
¾ Microsoft’s Access and SQL Server
¾ Oracle
¾ Sybase
¾ Informix
¾…
„ Very simple and elegant
¾ A database is a collection of one or more relations
¾ A relation is a table with rows and columns
„ Very powerful theory for creating databases
„ Powerful query languages

Intro. to Dababase Management Systems 3.2 Lecture notes by Nikos Mamoulis


Basic Structure

„ The relation is the main construct of the relational DB model.


„ A relation consists of a relation schema and a relation instance
„ relation schema:
¾ The name of the relation (e.g., Employee)
¾ A set of fields (attributes) and the domain (set of possible values) of
each field
ƒ Example: ssn: integer, name: char[20], lot: integer
„ relation instance:
¾ Formally: a subset of D1 x D2 x … x Dn, where, D1, D2, …. Dn are the
domains of the attributes A1, A2, …. An of the relation attributes
¾ Informally: a table having as columns the attributes and as rows tuples
of values for these attributes
ƒ constraint: there cannot be two rows with exactly the same values
– Stems from the fact that relation is a set
– Q: what is a set, a list, a bag?

Intro. to Dababase Management Systems 3.3 Lecture notes by Nikos Mamoulis


Example

„ relation schema:
¾ Students(sid: string, name: string, login: string, age: integer, gpa: real)
„ relation instance:

sid name login age gpa


53821 Jones jones@math 18 1.8
53832 Smith smith@math 19 3.2
53927 Parker parker@cs 21 2.5
tuples
(records, 53111 Smith smith@eee 20 2.8
rows)
53267 Black black@me 19 3.1
53542 Dave dave@phy 18 3.6

Intro. to Dababase Management Systems 3.4 Lecture notes by Nikos Mamoulis


Attribute Types

„ Each attribute of a relation has a name


„ The number of attributes in a relation schema is called degree or
arity. The number of tuples in a relation instance is called its
cardinality.
„ The set of allowed values for each attribute is called the domain
of the attribute
¾ We may also have domain constraints (e.g., sid cannot be
negative)
„ Attribute values are (normally) required to be atomic, that is,
indivisible
„ The special value null is a member of every domain
¾ That is a tuple may not have a value in some attribute
„ The null value causes complications in the definition of many
operations
¾ we shall ignore the effect of null values in our main presentation
and consider their effect later

Intro. to Dababase Management Systems 3.5 Lecture notes by Nikos Mamoulis


Database

„ A database consists of multiple relations


„ Information about an enterprise is broken up into parts, with each
relation storing one part of the information
„ Example:
¾ employees : stores information about employees
¾ departments : stores information about departments
¾ works_in : stores information about which employee works in which
department
„ The relational database schema is the collection of the
schemas of all relations in the database
„ A relational database instance is a collection of relation
instances; one instance for each relation in the database schema
¾ each relation instance must satisfy its domain constraints

Intro. to Dababase Management Systems 3.6 Lecture notes by Nikos Mamoulis


Example of a Database Schema & Instance

„ A database schema :
¾ Employees(ssn: char(11), name: char(30), lot: integer)
¾ Departments(did: integer, dname: char(20), budget: real)
¾ Works_in(ssn: char(11), did: integer, since: date)
„ A database instance :

Employees Departments Works_in

ssn name lot did dname budget ssn did since


13-324 Jones 22 34 Toys 122000 13-324 34 1/1/01
13-322 Smith 45 12 Tools 239000 13-322 34 1/4/02
12-824 Parker 125 76 Food 100000 13-322 12 2/2/05
21-397 Smith 12 12-824 76 1/1/03
21-397 76 1/1/05

„ Logical design problem:


¾ Given the specifications for a database design its relational schema
ƒ Solution: first design the ER diagram, then translate it to a relational
schema

Intro. to Dababase Management Systems 3.7 Lecture notes by Nikos Mamoulis


Creating and Modifying Relations in a DBMS

„ Databases are created and updated by the DBA with the use of a
Data Definition Language (DDL)
¾ SQL (pronounced Se-Qu-El or Es-Qu-El) is the dominant DDL used
by all commercial DBMSs
¾ SQL is also a DML (data manipulation language)
ƒ More on DML features of SQL next week
„ How to create a table in SQL:
¾ CREATE TABLE Employees
(ssn CHAR(11), An empty table with no tuples is the
name CHAR(30), instance of relation Employees
lot INTEGER)
„ How to destroy a table: The schema and the instance of the relation
¾ DROP TABLE Employees are deleted from the DBMS
„ How to alter the schema of a table:
¾ ALTER TABLE Employees For each tuple in the instance of
ADD COLUMN address CHAR(40) Employees a null value is added
for the new attribute

Intro. to Dababase Management Systems 3.8 Lecture notes by Nikos Mamoulis


Creating and Modifying Relations in a DBMS

„ How to add tuples in a table:


¾ INSERT INTO Employees (ssn, name, lot)
VALUES (‘13-324’, ‘Jones’, 22)
ssn name lot
ssn name lot
13-322 Smith 45
13-322 Smith 45
12-824 Parker 125
12-824 Parker 125
21-397 Smith 12
21-397 Smith 12
13-324 Jones 22

„ How to delete tuples in a table:


¾ DELETE FROM Employees Tuples that satisfy this condition are deleted
WHERE name = ‘Smith’

ssn name lot


13-322 Smith 45 ssn name lot

12-824 Parker 125 12-824 Parker 125

21-397 Smith 12 13-324 Jones 22

13-324 Jones 22

Intro. to Dababase Management Systems 3.9 Lecture notes by Nikos Mamoulis


Integrity Constraints

„ Conditions that must be true for any instance of the


database
„ Specified by the DBA when defining the schema of
the DB
„ After ICs have been imposed, only legal instances of
the DB are allowed
¾ ICs cannot be violated
„ The DBMS checks for violations of ICs after every
change in the DB
¾ Any changes to the DB that violate some IC are rejected by
the DBMS, and may trigger changes as necessary in order to
bring the DB in a consistent state

Intro. to Dababase Management Systems 3.10 Lecture notes by Nikos Mamoulis


Primary Key Constraints

„ A set of fields s is a key if


1. No two distinct tuples can have the same values in all key
fields, and
2. Condition 1 does not hold for any subset of s
¾ If Condition 2 is false, then s is a superkey
¾ If there are more than one keys in a relation, the DBA
chooses one of them to be the primary key
¾ Example: ssn is the primary key in Employees. How about
{ssn, name}?
¾ How can we be sure that there exists a primary key for a
relation?

Intro. to Dababase Management Systems 3.11 Lecture notes by Nikos Mamoulis


Why Primary Key?

„ The primary key plays the role of an identity for each


tuple
„ Tuples from other tables may be associated with a
tuple through the primary key
¾ For example, tuples in the Works_in relation refer to
employees in the Employees table via ssn (primary key for
Employees).
„ The DBMS often creates indexes on primary keys,
which are data structures for efficient lookup of key
values.

Intro. to Dababase Management Systems 3.12 Lecture notes by Nikos Mamoulis


Specifying Key Constraints in SQL

„ Recall our Students entity set from the last exercise in


the previous chapter
„ We can model the entity set by a relation:
¾ CREATE TABLE Students
(name CHAR(30),
university_number INTEGER,
HKID CHAR(7),
address CHAR(50),
PRIMARY KEY(university_number),
UNIQUE (HKID))
¾ PRIMARY KEY defines the primary key of the relation
¾ UNIQUE is used to specify constraints for other candidate keys (two
students cannot have the same HKID)

Intro. to Dababase Management Systems 3.13 Lecture notes by Nikos Mamoulis


Foreign Keys and 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’
„ Works_in.ssn is a foreign key to Employees table and
Works_in.did is a foreign key to Departments

Works_in
Employees Departments
ssn did since

ssn name lot 13-324 34 1/1/01


did dname budget
13-324 Jones 22 13-322 34 1/4/02
34 Toys 122000
13-322 Smith 45 13-322 12 2/2/05
12 Tools 239000
12-824 Parker 125 12-824 76 1/1/03
76 Food 100000
21-397 Smith 12 21-397 76 1/1/05

Intro. to Dababase Management Systems 3.14 Lecture notes by Nikos Mamoulis


Foreign Keys and Referential Integrity

„ The referential integrity constraint does not allow us


to insert tuples in the referring table, if the foreign key
value does not appear in the referred table
„ Ex: What happens if we try to insert the tuple
<12-824, 15, 1/2/04> to the Works_in table?

Works_in
Employees Departments
ssn did since

ssn name lot 13-324 34 1/1/01


did dname budget
13-324 Jones 22 13-322 34 1/4/02
34 Toys 122000
13-322 Smith 45 13-322 12 2/2/05
12 Tools 239000
12-824 Parker 125 12-824 76 1/1/03
76 Food 100000
21-397 Smith 12 21-397 76 1/1/05
?
OK! 12-824 15 1/2/04
Referential constraint
violation!

Intro. to Dababase Management Systems 3.15 Lecture notes by Nikos Mamoulis


Expressing Foreign Key Constraints

„ CREATE TABLE Works_in


(ssn CHAR(11),
did INTEGER,
since DATE,
PRIMARY KEY(ssn,did),
FOREIGN KEY(ssn) REFERENCES Employees,
FOREIGN KEY(did) REFERENCES Departments)
„ Foreign key may refer to the same table:
CREATE TABLE Employees
(ssn CHAR(11),
name CHAR(30),
lot INTEGER,
manager_ssn CHAR(30),
PRIMARY KEY(ssn),
FOREIGN KEY(manager_ssn) REFERENCES Employees)
„ Q: What happens if manager_ssn not yet known, or is not
inserted yet to the DB?
¾ null value can be used for foreign keys (but not for primary)
Intro. to Dababase Management Systems 3.16 Lecture notes by Nikos Mamoulis
Enforcing Integrity

„ What happens if a DB operation leads to a violation of an IC?


„ A tuple is inserted into Works_in with ssn that does not exist in
Employees
¾ The insertion is rejected
„ A tuple is deleted from Employees and its ssn appears in the
Works_in table
¾ Delete Works_in all tuples with that ssn or
¾ Disallow the deletion or
¾ Set the ssn value of those tuples in Works_in to a default value or
to null (assuming that ssn is not part of its primary key)
„ A tuple in Employees is updated and its ssn appears in the
Works_in table
¾ Treat similarly to deletion

Intro. to Dababase Management Systems 3.17 Lecture notes by Nikos Mamoulis


Enforcing Integrity
Action:
„ CREATE TABLE Works_in
if ssn is deleted
(ssn CHAR(11),
from Employees
did INTEGER, delete referring
since DATE, tuples from
PRIMARY KEY(ssn,did), Works_in
FOREIGN KEY(ssn) REFERENCES Employees
ON DELETE CASCADE
ON UPDATE NO ACTION, Default action:
FOREIGN KEY(did) REFERENCES Departments) reject change

„ Q1. What will happen if an Employees tuple is deleted and its ssn
appears in some tuples of Works_in?
„ Q2. What will happen if an Employees tuple is updated and its previous
ssn value appears in some tuples of Works_in?
„ Q3. What will happen if a Departments tuple is deleted and its did
appears in some tuples of Works_in?
„ Other:
¾ ON DELETE SET DEFAULT
¾ ON DELETE SET NULL
Intro. to Dababase Management Systems 3.18 Lecture notes by Nikos Mamoulis
More Constraints

„ More constraint types exist


¾ Domain constraints: restrict the domain of an attribute to
some value range
ƒ E.g., Employees.lot can take values between 1 and 250
ƒ Use table constraints to enforce them
ƒ E.g. CHECK (lot >=1 AND lot <= 250)
¾ Complex constraints: involve computations from different
tables
ƒ E.g., do not allow a student to take a course, unless
he/she has passed all prerequisites
ƒ Can be enforced by assertions or triggers
ƒ …more in chapter 5

Intro. to Dababase Management Systems 3.19 Lecture notes by Nikos Mamoulis


Logical DB design:
From ER diagram to relational schema
„ How do we determine the relations and their schema,
when designing a database?
„ Solution:
¾ First design the ER diagram, then convert it to a relational
DB schema, following some simple rules

Intro. to Dababase Management Systems 3.20 Lecture notes by Nikos Mamoulis


Entity Sets to Relations
„ CREATE TABLE Employees
(ssn CHAR(11),
name CHAR(30),
lot INTEGER,
PRIMARY KEY(ssn))

ssn name lot

Employees

Intro. to Dababase Management Systems 3.21 Lecture notes by Nikos Mamoulis


Relationship Sets (without constraints)
to Relations
„ CREATE TABLE Works_in
(ssn CHAR(11),
did INTEGER,
since DATE,
PRIMARY KEY(ssn,did),
FOREIGN KEY(ssn) REFERENCES Employees,
FOREIGN KEY(did) REFERENCES Departments)
„ Primary key is the combination of keys in participating entities
¾ These are also foreign keys to relations that model the entity sets
„ Add descriptive attributes

ssn name lot since did dname budget

Employees Works_in Departments

Intro. to Dababase Management Systems 3.22 Lecture notes by Nikos Mamoulis


Why do we use many tables?
„ What’s wrong with the following design? Redundancy!
„ CREATE TABLE Emp_Works_in_Dept
(ssn CHAR(11),
name CHAR(30), Emp_Works_in_Dept
lot INTEGER, ssn name lot did dname budget since
did INTEGER, 13-324 Jones 22 34 Toys 122000 1/1/01
dname CHAR(20), 13-322 Smith 45 34 Toys 122000 1/4/02

budget REAL, 13-322 Smith 45 12 Tools 239000 2/2/05


12-824 Parker 125 76 Food 100000 1/1/03
since DATE,
21-397 Smith 12 76 Food 100000 1/1/05
PRIMARY KEY(ssn,did))
21-323 Bones 34 null null null null

ssn name lot since did dname budget

Employees Works_in Departments

Intro. to Dababase Management Systems 3.23 Lecture notes by Nikos Mamoulis


Relationship Sets with key constraints
to Relations (1)
„ CREATE TABLE Manages
(ssn CHAR(11),
did INTEGER,
since DATE,
PRIMARY KEY(did),
FOREIGN KEY(ssn) REFERENCES Employees,
FOREIGN KEY(did) REFERENCES Departments)
„ Primary key is did (why?)
„ Also define tables for Employees and Departments

ssn name lot since did dname budget

Employees Manages Departments

Intro. to Dababase Management Systems 3.24 Lecture notes by Nikos Mamoulis


Relationship Sets with key constraints
to Relations (2)
„ CREATE TABLE Dept_Mgr
(did INTEGER,
dname CHAR(20),
budget REAL,
mgr_ssn CHAR(11),
since DATE,
PRIMARY KEY(did),
FOREIGN KEY(mgr_ssn) REFERENCES Employees)
„ DO NOT create separate table for Manages, inlcude manages
information in table Employees
„ If no manager for a dept, put null in mgr_ssn field
„ When is this better than approach (1)?
ssn name lot since did dname budget

Employees Manages Departments

Intro. to Dababase Management Systems 3.25 Lecture notes by Nikos Mamoulis


Relationship Sets with Participation+Key
Constraints to Relations
„ CREATE TABLE Dept_Mgr
(did INTEGER,
dname CHAR(20), implies total participation
budget REAL,
mgr_ssn CHAR(11) NOT NULL, makes little sense to delete
a dept if its manager is deleted!
since DATE,
PRIMARY KEY(did),
FOREIGN KEY(mgr_ssn) REFERENCES Employees
ON DELETE NO ACTION)
„ Must use approach (2) – why?

ssn name lot since did dname budget

Employees Manages Departments

Intro. to Dababase Management Systems 3.26 Lecture notes by Nikos Mamoulis


Relationship Sets with Participation
Constraints to Relations
„ Unfortunately, not easy to model the total participation
constraints for Works_in in SQL
„ Must define an assertion or a CHECK constraint that checks that
every employee or dept participates in the Works_in relationship
„ …more on this in chapter 5.7

ssn name lot since did dname budget

Employees Manages Departments

Works_in
since
Intro. to Dababase Management Systems 3.27 Lecture notes by Nikos Mamoulis
Translating Weak Entities
„ Similar to total participation + key constraint case, however, we also
need to consider the partial key and the weak entity dependence
„ CREATE TABLE Dep_Policy
Include the primary key of
(pname CHAR(20), Employees as attribute
age INTEGER,
Use primary key of
cost REAL, Employees + partial key as
ssn CHAR(11), the primary key of relation
PRIMARY KEY(pname,ssn),
FOREIGN KEY(ssn) REFERENCES Employees
propagate the deletion of
ON DELETE CASCADE) an employee to the
deletion of its dependents

ssn name lot cost pname age

Employees Policy Dependents

Intro. to Dababase Management Systems 3.28 Lecture notes by Nikos Mamoulis


Translating Class Hierarchies
„ Approach 1:
¾ Define table for Employees ssn name lot
¾ Define tables for Hourly_Emps and
Contract_Emps:
ƒ CREATE TABLE Hourly_Emps Employees
(hours_worked INTEGER,
hour_wages REAL,
ssn CHAR(11),
PRIMARY KEY(ssn), ISA
FOREIGN KEY(ssn) REFERENCES Employees
ON DELETE CASCADE)
„ Approach 2
Hourly_Emps Contract_Emps
¾ Define only two tables for Hourly_Emps
and Contract_Emps that also include
generic attributes (name, lot) hours_worked contractid
¾ Appropriate if disjoint sets which cover
the whole Employees set. hour_wages

„ Overlap/covering constraints can only be checked with assertions


Intro. to Dababase Management Systems 3.29 Lecture notes by Nikos Mamoulis
Translating Aggregation
„ Similar to translating relationship sets
¾ Monitors has
ƒ primary key (ssn, pid, did)
ƒ foreign keys ssn ref. Employees, (pid, did) ref. Sponsors
ƒ descriptive attribute until

ssn name lot

Employees Monitors until

pid started_on pbudget since did dname budget

Projects Sponsors Departments

Intro. to Dababase Management Systems 3.30 Lecture notes by Nikos Mamoulis


Exercises: Translate to Relational Schema
ssn name lot pname age

Employees Dependants
Purchaser beneficiary

„ ER diagram A policyid cost


Policies

ssn name lot

Employees

supervisor sub-ordinate
„ ER diagram B
Reports_to

Intro. to Dababase Management Systems 3.31 Lecture notes by Nikos Mamoulis


Views in Databases

„ Views are used to define an external schema for the


database
¾ How some applications view the contents of the database
„ Views are implemented as virtual tables
¾ They have a logical schema, but the data are derived from
physical tables of the database
¾ Views support logical data independence in the database
ƒ If the schema of a stored database changes, views can
still operate on the old schema
¾ Views support security
ƒ Define views that give users restricted access to
information (they cannot access the actual DB schema)
ƒ Ex: students can see other student’s name, but not GPA

Intro. to Dababase Management Systems 3.32 Lecture notes by Nikos Mamoulis


Views in Databases

„ A view is just a relation, but we store a definition, rather than a


set of tuples.
¾ CREATE VIEW YoungActiveStudents (name, grade) Query that
AS SELECT S.name, E.grade defines the
FROM Students S, Enrolled E view contents
WHERE S.sid = E.sid and S.age<21
„ Views can be dropped using the DROP VIEW command.
„ How to handle DROP TABLE if there’s a view on the table?
¾ DROP TABLE command has options to let the user specify this.

Intro. to Dababase Management Systems 3.33 Lecture notes by Nikos Mamoulis

You might also like