Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 54

Database Management Systems

Chapter 03:
The Relational Model
Outline

• How is data represented in the relational model?


• What integrity constraints can be expressed?
• How can data be created and modified?
• How can data be manipulated and queried?
• How can we create, modify, and query tables using
SQL?
• How do we obtain a relational database design from
an ER diagram?
• What are views and why are they used?

2
Introduction

• Today, the relational model is by far the central data


model and the foundation for the leading DBMS
products.
• The major advantages of the relational model are its
simple data representation and the ease with which
even complex queries can be expressed.
• This chapter introduce the Data Definition Language
(DDL)

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

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 are distinct

5
Relational Query Languages
• A major strength of the relational model: supports
simple, powerful querying of data.

• Queries can be written naturally, and the DBMS is


responsible for efficient evaluation.
– The key of that is the precise semantics for relational
queries.
– Allows the optimizer to extensively re-order
operations, and still ensure that the answer does not
change.

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

• To find just names and logins, replace the first line:


SELECT S.name, S.login

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”

Given the following instances of Enrolled and Students:

we get:
11
Create Table

The CREATE TABLE statement is used to create a table in a database.

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

Create table Employee(


Employee_No INTEGER primary key,
Name varchar2(40),
Position varchar2(20),
Salary number,
Project_No number foreign key reference project)

14
Destroying and Altering Relations

DROP TABLE Students


• Destroys the relation Students. The schema
information and the tuples are deleted.

ALTER TABLE Students


ADD COLUMN firstYear: integer
• The schema of Students is altered by adding a new
field; every tuple in the current instance is extended
with a null value in the new field

15
Adding and Deleting Tuples

• Can insert a single tuple using:


INSERT INTO Students (sid, name, login, age, gpa)
VALUES (53688, ‘Smith’, ‘smith@ee’, 18, 3.2)
• Can delete all tuples satisfying some condition (e.g.,
name = Smith):
DELETE
FROM Students S
WHERE S.name = ‘Smith’
* Powerful variants of these commands are available;
more later

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”

• Integrity control consists of constraints that we


wish to impose in order to protect the database
from being inconsistent.
– Type of integrity constraint
• Required data;
– Some columns must contain a valid data (NOT NULL)
• Domain constraint;
• Referential integrity

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.

– E.g., sid is a key for Students.


• What about name?

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

CREATE TABLE Students (


sid CHAR(20) ,
name CHAR (30) ,
login CHAR(20) ,
age INTEGER,
gpa REAL,
UNIQUE (name, age),
CONSTRAINT StudentsKey PRIMARY KEY (sid) )

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

• Auto-increment allows a unique number to be generated when a new record


is inserted into a table.
• Very often we would like the value of the primary key field to be created
automatically every time a new record is inserted.

CREATE TABLE Persons


(
P_Id number NOT NULL IDENTITY,

• 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

• Every column has a domain “set of legal values”.


• Two mechanism for specifying domains:
– CHECK: which allow a constraints to be defined on a
column.
– CHECK constraint is used to limit the value range that can
be placed in a column
• Syntax: CHECK (searchCondition)
• Examples:
Status varchar2(5) check (Status in (‘true’,’false’));
Eyear number(4) check( Eyear > 2004)
Does not allow the year to be less than or equal 2004

33
Domain constraint

– Create domain: “to create user defined data type”


Create domain StatusType AS CHAR
Default ‘T’
Check (Value IN (‘T’,”F’));

Now StatusType can be used as a data type:


Status StatusType NOT NULL;

34
Logical DB Design: ER to Relational
• Entity sets to tables:

CREATE TABLE Employees


name (ssn CHAR(11),
ssn lot
name CHAR(20),
lot INTEGER,
Employees PRIMARY KEY (ssn))

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?

1-to-1 1-to Many Many-to-1 Many-to-Many


Translating ER Diagrams with Key Constraints
CREATE TABLE Manages(
• Map relationship to a ssn CHAR(11),
table: did INTEGER,
– Note that did is the since DATE,
PRIMARY KEY (did),
key now! FOREIGN KEY (ssn) REFERENCES
– Separate tables for Employees,
Employees and FOREIGN KEY (did) REFERENCES
Departments. Departments)
• Since each CREATE TABLE Dept_Mgr(
department has a did INTEGER,
unique manager, we dname CHAR(20),
could instead combine budget REAL,
Manages and ssn CHAR(11),
since DATE,
Departments.
PRIMARY KEY (did),
FOREIGN KEY (ssn) REFERENCES
Employees)
Review: Participation Constraints

 Does every department have a manager?


• If so, this is a participation constraint: the participation of
Departments in Manages is said to be total (vs. partial).
• Every did value in Departments table must appear in a row
of the Manages table (with a non-null ssn valu
Review: Participation Constraints

We can capture participation constraints involving one entity set


in a binary relationship, but little else (without resorting to
CHECK constraints).
Review: Weak Entities

A weak entity can be identified uniquely only by considering the


primary key of nother (owner) entity.
Owner entity set and weak entity set must participate in a one-to-
many relationship set (1 owner, many weak entities).
Weak entity set must have total participation in this identifying
relationship set
Review: Weak Entities

• Weak entity set and identifying relationship set are


translated into a single table.
• When the owner entity is deleted, all owned weak
entities must also be deleted
Review: ISA Hierarchies
name
 As in C++, or other PLs, ssn lot

attributes are inherited.


 If we declare A ISA B, every Employees

A entity is also considered hourly_wages hours_worked


to be a B entity. ISA
contractid

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

• A view is a table whose rows are not explicitly stored


in the database but are computed as needed from a
view definition.
• Consider the Students and Enrolled relations.
Suppose we are often interested in finding the names
and student identifiers of students who got a grade of
B in some course, together with the course identifier

46
Views

• A view is just a relation, but we store a definition,


rather than a set of tuples.
CREATE VIEW YoungActiveStudents (name, grade)
AS SELECT S.name, E.grade
FROM Students S, Enrolled E
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.
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

CREATE TABLE tablename(


col1name DATATYPE [CONSTRAINT ... , DEFAULT ...],
col2name DATATYPE [CONSTRAINT ... , DEFAULT ...],
...
colxname DATATYPE [CONSTRAINT ... , DEFAULT ...], [CONSTRAINT ... , DEFAULT ...]
); Optional use

CREATE TABLE tablename(


col1name DATATYPE [DEFAULT ...],
col2name DATATYPE [DEFAULT ...],
...
colxname DATATYPE [ DEFAULT ...],
[CONSTRAINT constraintname CONSTRAINTTYPE(colname)…]
);

Main Constraints: PRIMARY KEY,


FOREIGN KEY “REFERENCES”
UNIQUE
NOT NULL 49
CREATE TABLE department(
deptno NUMBER(2) PRIMARY KEY,
dname VARCHAR2(25) UNIQUE NOT NULL,
location VARCHAR2(25),
mngrno NUMBER(3)
); CREATE TABLE department(
deptno NUMBER(2),
dname VARCHAR2(25) UNIQUE NOT NULL,
location VARCHAR2(25),
mngrno NUMBER(3),
PRIMARY KEY(deptno),
UNIQUE(dname)
);
CREATE TABLE department(
deptno NUMBER(2),
dname VARCHAR2(25) NOT NULL,
location VARCHAR2(25),
mngrno NUMBER(3),
CONSTRAINT dept_pk PRIMARY KEY(deptno),
CONSTRAINT dname_un UNIQUE(dname)
); 50
ALTER TABLE Syntax

ALTER TABLE tablename


ADD COLUMN colname DATATYPE [CONSTRAINT…, DEFAULT…];

ALTER TABLE tablename


MODIFY colname NEWDATATYPE [CONSTRAINT…, DEFAULT…];

ALTER TABLE tablename


ADD CONSTRAINT constraintname TYPE(colname);

ALTER TABLE tablename


DROP CONSTRAINT constraintname;

51
ALTER TABLE Syntax

ALTER TABLE deparment


ADD COLUMN empcount NUMBER(2) DEFAULT 0;

ALTER TABLE deparment


ADD CONSTRAINT dept_pk PRIMARY KEY(deptno);

ALTER TABLE deparment


ADD CONSTRAINT dept_mngr_fk FOREIGN KEY(mngrno)
REFERENCES employee(empno);

ALTER TABLE deparment


MODIFY COLUMN empcount NUMBER(3) DEFAULT 1 NOT NULL;
52
Delete table

• DROP TABLE Students

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

You might also like