0% found this document useful (0 votes)
7 views

Relational Model

Uploaded by

Aung Khant Hein
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)
7 views

Relational Model

Uploaded by

Aung Khant Hein
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/ 29

3.

The Relational Model

3.1) Introduction to Relational Model

3.2) Integrity Constraints over Relations

3.3 ) Logical Database Design and View

Faculty of Information Science


The Relational Model

Objectives

1 To construct the relational model using SQL

2 To determine the keys on relations

3 To recognize integrity constraints ensure that changes


made to the database by authorized users do not
result in a loss of data consistency.

4 To convert the conceptual data model (ER schema) into a


relational database design.

5 To recognize the concepts of view


2

Faculty of Information Science


Introduction to Relational Model

Creating and Modifying Relations By Using SQL


 Relation

 data in the relational model

 consists of a relation schema and a relation instance. Table = relation


Row = tuples
 Relation instance
Column = attribute
 is a set of tuples ,also called records

Faculty of Information Science


Introduction to Relational Model
 Relational database

 a collection of relations with distinct relation names.


 Relation schema

 describes the column heads for the table

 Specifies the relation’s name, field‘s name and


the domain of each field.

 Must have a unique name.

 Every attribute value must be atomic .

 Every row must be unique

 The order of the columns must be irrelevant.

 The order of the rows must be irrelevant


4

Faculty of Information Science


Introduction to Relational Model

E.g;

 To create the Students relation,


CREATE TABLE Students ( sid CHAR(20),
name CHAR(30),
login CHAR(20),
age INTEGER,
gpa REAL);
 insert a single tuple into the Students table :
INSERT INTO Students (sid, name, login, age, gpa)
VALUES ( 53688, ‘Smith’, ‘smith@ee’, 18, 3.2);

Faculty of Information Science


Introduction to Relational Model

 Delete tuples :
DELETE
FROM Student
WHERE name=‘Smith’;
 Modify the column values in an existing row
UPDATE Students S
SET S.age=S.age +1, S.gpa=S.gpa-1
WHERE S.sid= 53688;

Faculty of Information Science


Introduction to Relational Model

Keys On Relations
 Avoids data entry errors

 one or more attributes that determine


other attributes.

 Each row in a table must be uniquely identifiable.


(1) Candidate Key
(2) Primary Key
(3) Foreign Key
(4) Alternate Key
(5) Composite Key
(6) Super Key
(7) Secondary Key

Faculty of Information Science


Introduction to Relational Model
Example

 Employee(EmployeeID, FullName, DatofBirth,SSN,


DeptNo)
(1) candidate key:(EmployeeID, SSN)
(2) Primary Key:(EmployeeID)
/* Employee ID is more perfectable choice.*/
(3) Foreign Key:(DeptNo)
(4) Alternate Key:(SSN)
(5)Composite Key :(FullName, Dateof Birth)
(6) Super Key:
EmployeeID (/* Minium Superkey*/)
(EmployeeID, FullName)
(EmployeeID, FullName, DatofBirth)
(EmployeeID, FullName, DatofBirth,SSN)
(EmployeeID, FullName, DatofBirth,SSN,DeptNo)
(7)Secondary Key:
(FullName, DatofBirth,SSN,DeptNo)
8

Faculty of Information Science


Introduction to Relational Model

Super Key  Any key that uniquely identifies each row

Candidate
 A super key without unnecessary attributes
Key

 A candidate key selected to uniquely


Primary identify all other attribute values in any
Key given row.
 Cannot contain null entries.
9

Faculty of Information Science


Introduction to Relational Model

Composite  Composed of more than one attribute


Key

Foreign key An attribute whose values match primary key


values in the related table

Secondary key Key used strictly for data retrieval purposes

10

Faculty of Information Science


3. The Relational Model

3.1) Introduction to Relational Model

3.2) Integrity Constraints over Relations

3.3) Logical Database Design and View

Faculty of Information Science


Integrity Constraints over Relations

Integrity Constraints

 Avoids data entry errors

Primary Key  minimal subset of the fields of a


Constraints relation is a unique identifier for a tuple.
 Cannot contain null entries.

CREATE TABLE Student


( sid CHAR(20)
name CHAR(30),
login CHAR(20),
age INTEGER,
gpa REAL,
12
PRIMARY KEY (sid))

Faculty of Information Science


Integrity Constraints over Relations

Candidate  Possibly many candidate keys


Key (specified using UNIQUE), one of
Constraints
which is chosen as the primary key.

CREATE TABLE Student


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

13

Faculty of Information Science


Integrity Constraints over Relations

Foreign Keys, Referential


Integrity Constraints

CREATE TABLE Enrolled


(sid CHAR(20),
cid CHAR(20),
grade CHAR(2),
PRIMARY KEY (sid,cid),
FOREIGN KEY (sid) REFERENCES Students
FOREIGN KEY (cid) REFERENCES Courses)

14

Faculty of Information Science


Integrity Constraints over Relations

The impact of foreign key constraints is more complex

when an insert, delete or update command causes a violation


to values that match primary key values in the related tables.
several alternative ways to handle foreign key violations.

4 options on deletes and updates.

1. Default is NO ACTION
 delete/update is rejected
2. CASCADE
 also delete all tuples that refer to deleted tuple
3. SET NULL
 sets foreign key value of referencing tuple

15
4. SET DEFAULT
 sets foreign key value of referencing tuple
Faculty of Information Science
Integrity Constraints over Relations

CREATE TABLE Enrolled


(cid VARCHAR(20),
grade VARCHAR(2),
sid VARCHAR(10),
PRIMARY KEY (sid,cid),
FOREIGN KEY (sid) REFERENCES Students(sid),
FOREIGN KEY (cid) REFERENCES Courses(cid)
ON DELETE CASCADE
ON UPDATE CASCADE);

16

Faculty of Information Science


Integrity Constraints over Relations

General Constraints
 very useful in preventing data entry errors.
E.g student ages be within a certain range of
values; given such an IC specification,
 DBMS rejects inserts and updates that violate
the constraint.

illegal instance
legal instance

17

Faculty of Information Science


Integrity Constraints over Relations
Transactions and Constraints
 A transaction is any one execution of a user
program in a DBMS.

 can contain queries ,inserts ,updates ,etc.

 A constraint is checked at the end of every SQL


statement that could lead to a violation, the
statement is rejected.

E.g CREATE TABLE Student


( sid CHAR(20)
name CHAR(30),
login CHAR(20),
age INTEGER,
honors CHAR(10)
18 gpa REAL,
PRIMARY KEY (sid),
FOREIGN KEY (honors) REFERENCES Courses(cid))
Faculty of Information Science
3. The Relational Model

3.1) Introduction to Relational Model

3.2) Integrity Constraints over Relations


3.3) Logical Database Design and View

Faculty of Information Science


Logical Database Design and View

Entity Sets to Tables


 An entity set is mapped to a relation
 Attribute of entity set becomes an attribute of table.
 Example- Employee entity set with attributes
ssn, name and lot

E.g;

CREATE TABLE Employee


(ssn CHAR(11),
name CHAR(30),
20 lot INTEGER,
PRIMARY KEY(ssn));
Faculty of Information Science
Logical Database Design and View

Relationship Sets to Tables


 A relationship set is mapped to a relation.
 Attribute of relation include:
 The primary key attributes of each participating
entity set, as foreign key fields
 The descriptive attributes of the relationship set
E.g;

CREATE TABLE Works_In


(ssn CHAR(11),
did INTEGER,
since DATE,
21
PRIMARY KEY (ssn, did),
FORENGN KEY (ssn) REFERENCES Employees
FORENGN KEY (did) REFERENCES Departments); Faculty of Information Science
Logical Database Design and View
Translating Relationship Sets with Key Constraints:
E.g;

CREATE TABLE Manages


(ssn CHAR (11) ,
did INTEGER,
since DATE,
PRIMARY KEY (did),
FOREIGN KEY (ssn) REFERENCES Employees,
FOREIGN KEY (did)REFERENCES Departments));

 each department has at most one manager,


 no two tuples can have the same did value but
22 differ on the ssn value.
 did is itself a key for Manages.
Faculty of Information Science
Logical Database Design and View

Introduction to View
 A view is a relation whose rows are not explicitly
stored in the database but are computed as needed
from a view definition
 A view is a virtual table based on a SELECT query.
 The tables on which the view is based are called
base tables.
 Syntax: CREATE VIEW View-name
AS <SELECT Query Expression>

23

Faculty of Information Science


Logical Database Design and View

E.g; University Database


• Students (sid: string, name: string, login: string,
age: integer, gpa: float)
•Courses(cid: string, cname: string, credits: integer)
•Enrolled (studid: string, cid: string, grade: string)
 Finding the names and student identifiers of students
who got a grade of B in some course, together with the
course identifier.
CREATE VIEW B-Students (name, sid, courseid) AS
SELECT S.name, S.sid, E.cid
FROM Students S, Enrolled E

24
WHERE S.sid = E.studid AND E.grade = ‘B’;

Faculty of Information Science


Logical Database Design and View

Update On View

 Updatable view is a view that can be used to update


attributes in the base tables.

 Not all views are updatable

 GROUP BY expressions or aggregate functions


cannot be used

 Cannot use set operators

 Most restrictions based on use of JOINs

25

Faculty of Information Science


Logical Database Design and View
E.g;
 Consider instances of

B-Students (name, sid, courseid) view

 To insert (John, 55000, Reggae203) into B-Students view


may require the following actions
 insert (55000, John, null, null, null) into Students table .
 insert (55000 ,Reggae203, B) into Enrolled table

 Inserting/ updating data to view may violate null values,

duplications of primary key.

 To delete a tuple from the view B-Students, we can delete

the tuple from Enrolled.


26

Faculty of Information Science


Logical Database Design and View
E.g;
 Consider the following view:

CREATE VIEW GoodStudents (sid, gpa) AS

SELECT S.sid, S.gpa

FROM Students S

WHERE S.gpa > 3.0

 could update/delete the gpa of a GoodStudents row by modifying


the corresponding row in Students table.

 can insert a GoodStudents row by inserting a row into Students,


using null values in columns of Students that do not appear in

GoodStudents (sname, login, age).

27
 No violation happened because the view include a key value for

the underlying table


Faculty of Information Science
Logical Database Design and View
E.g;
 Consider the following view:

CREATE VIEW GoodStudents (sname, gpa) AS

SELECT S.sname, S.gpa

FROM Students S WHERE S.gpa > 3.0

 view did not include a key for the underlying table,

several rows in the table

 could not insert rows into Students through insertions


to GoodStudents.

 the insertions will be rejected because primary key columns


are not allowed to contain null values

28

Faculty of Information Science


The Relational model

Let's summarize what we learned today

Relational model using SQL.


the keys on relations
integrity constraints
conceptual data model (ER schema) into a
relational database design.
The concepts of view

29

Faculty of Information Science

You might also like