Chapter 3 The Relational Model
Chapter 3 The Relational Model
Model
Introduction to Relational Model
based on older data models (the and the 1161455 Noor COMP
3
Keys
• A super key of an entity set is a set of one or more attributes whose
values uniquely determine each entity.
• A candidate key of an entity set is a minimal super key
• ID is candidate key of instructor
• course_id is candidate key of course
• Although several candidate keys may exist, one of the candidate keys is
selected to be the primary key.
• A DBMS permits the use of SQL to query, and manipulate data and
relations in a database.
SQL
• Composed of
• DDL
• DML
6
Main Constructs
• SHOW DATABASES;
• SHOW DATABASES;
• USE university;
• Query:
SELECT *
FROM student;
• INSERT INTO STUDENT VALUES (1051122, 'Ahmad', '1980-01-20', 99);
• SELECT * FROM student;
• INSERT INTO STUDENT (sid, sname) VALUES (1061122, 'Sireen’);
• DELETE FROM student WHERE sid>=1060000 AND sid<=1069999;
• Query:
SELECT sid, sname
FROM student
WHERE sname = ‘Ahmad’;
MySQL Basics – Auto increment
• Composite key
• Composed of more than one attribute
• Key attribute
• Any attribute that is part of a key
• Superkey
• Any key that uniquely identifies each row
• Candidate key
• A superkey without redundancies
42
Keys (continued)
• Nulls:
• No data entry
• Not permitted in primary key
• Should be avoided in other attributes
• Can represent
• An unknown attribute value
• A known, but missing, attribute value
• A “not applicable” condition
• Can create problems when functions such as COUNT,
AVERAGE, and SUM are used
• Can create logical problems when relational tables
are linked
43
SQL for Data Definition: CREATE with
CONSTRAINT
44
SQL for Data Definition: CREATE with
CONSTRAINT
45
Keys for Relationship Sets
• The combination of primary keys of the participating entity sets forms a
super key of a relationship set.
• (s_id, i_id) is the super key of advisor
• NOTE: this means a pair of entity sets can have at most one relationship in a particular
relationship set.
• Example: if we wish to track multiple meeting dates between a student and her advisor, we cannot assume a
relationship for each meeting. We can use a multivalued attribute though
Department Employee
DeptName DeptID
Foreign Key
Location EmpName
49
Referential Integrity
50
SQL for Data Definition: CREATE with
CONSTRAINT
• Creating database tables using PRIMARY KEY and FOREIGN KEY
constraints
• The SQL CREATE TABLE statement
• The SQL CONSTRAINT keyword
51
SQL for Data Definition: CREATE with CONSTRAINT
• Creating database tables using PRIMARY KEY and FOREIGN KEY
constraints
• The SQL CREATE TABLE statement
• The SQL CONSTRAINT keyword
• ON UPDATE CASCADE and ON DELETE CASCADE
CREATE TABLE Emp_Skill (
EmpID Integer Not Null,
SkillID Integer Not Null,
SkillLevel Integer,
PRIMARY KEY (EmpID, SkillID),
FOREIGN KEY (EmpID)
REFERENCES Employee (EmpID)
ON DELETE CASCADE,
FOREIGN KEY (SkillID)
REFERENCES Skill (SkillID)
ON UPDATE CASCADE
);
When the row of EmpID (primary key) in Employee TABLE is deleted, the
EmpFK (foreign key) is deleted also.
52
Deleting Database Objects: DROP
53
Enforcing Integrity Constraints
INSERT INTO Enrolled (sid, cid, grade) VALUES (51111, ‘Hindi101’, ‘B’);
Ways to handle foreign key violations
• Option 3: Set the sid of Enrolled to some existing (default) sid value
in Students for every involved Enrolled row (SET NULL / SET
DEFAULT ). Both are affected.
Referential Integrity in SQL
57
Relationship Types to Relational Model
1 1
Owner ow Car
ns
N 1
Owner ow Car
ns
N M
Owner ow Car
ns
58
ER to Relational Model - Entities
• Entity sets to tables:
• Attributes to columns
nam
ss e lo
n t
Employee
• CREATE TABLE Employees
s
(ssn int,
name CHAR(20),
lot INTEGER,
PRIMARY KEY (ssn))
ER to Relational Model - Relationships
CREATE TABLE EMP ….
• Relationship Sets to Tables CREATE TABLE DEPT …
• Attributes to columns
• In translating a relationship set to a CREATE TABLE works_in (
since date,
relation, attributes of the relation ssn_emp int,
must include: did_dept int
• Keys for each participating entity set (as primary key (ssn_emp, did_dept),
foreign keys). Foreign key (ssn_emp) references emp(ssn),
• This set of attributes forms a Foreign key (did_dept) references dept(Did))
key for the relation.
Dept_locations
( did int,
Location int,
Primary key( did, location),
Foreign key (did) references dept(did));
Musicians Example
Create Table Address ( Phone int primary key, City
varchar(16), Street varchar(32));
CREATE TABLE Musicians (Ssn int primary key,
Name char(32), Phone int,
Foreign key(phone) references Address(phone));
Create table Songs (song_id int primary key, title varchar(32), author varchar(32),
Alb_id int, foreign key (alb_id) references albums(alb_id));
Create table Mus2Songs(ssn int, song_id int, primary key (ssn, song_id),
Foreign key (ssn) References Musicians(ssn),
Foreign key (song_id) references Songs(song_id));
University Example
Create Table Prof(ssn int primary key, age int,
rank int);
CREATE TABLE run_Dept (did int primary key,
office varchar(32),
dname varchar(32), mgr_ssn int,
Foreign key(mgr_ssn) references Prof(ssn));
Create table Proj(pnum int primary key, sponser varchar(32), sdate date,
Edate date, budget real, mgr_ssn int,
Foreign key (mgr_ssn) references Prof(ssn));
Contract_Emp
• General approach: Hourly_Emp
s
s
• 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.
Relational Model for Recursive Relationships
manage
1 Ahmad -1
2 Dania 1