Relational Model
SQL
Relations
- Relation Schema
- the column heads for the table which contains name of each field and the domain of each field
- e.g. Students( sid:
string, name:string, login:string, age:integer, gpa:real)
| sid | name | login | age | gpa |
| :—-: | :—-: | :—-: | :—-: | :—-: |
- e.g. Students( sid:
- the column heads for the table which contains name of each field and the domain of each field
Relation Instance
a table with rows and columns
- e.g.
| sid | name | login | age | gpa |
| :—-: | :—-: | :—-: | :—-: | :—-: |
| 50000 | Dave | dave@cs | 19 | 3.3 |
| 53666 | Jones | jones@cs | 18 | 3.4 |
| 53688 | Smith | smith#@ee | 18 | 3.2 |
| 53650 | Smith | smith@math | 19 | 3.8 |
| 53831 | Madayan | madayan@music | 11 | 1.8 |
| 53832 | Guldu | guldu@music | 20 | 2.0 |
- e.g.
Reminder:
- no two rows are identical
- the order in the rows and fields are listed is not important
- Definitions:
- degree: the number of fields
- cardinality: the number of tuples (records)
- relational database: a collection of relations with distinct relation names
- relational database schema: the collection of schemas for the relations in the database
- instance of relational database: a collection of relation instances
Data Definition Language
- Create
- create the Student relation
1
2
3
4
5
6
7Create table Students(
sid CHAR(20),
name CHAR(30),
login CHAR(20),
age INTEGER,
gpa REAL
)
- create the Student relation
- Insert
- insert a single tuple
1
2
3Insert
into Students (sid, name, login, age, gpa)
Values (53688, 'Smith', 'smith@ee', 18, 3.2) - Noted: it is not necessary to list out the name of fields, but make sure to match the order
- insert a single tuple
- Delete
- delete tuples
1
2
3DELETE
From Students S
WHERE S.name = 'Smith'
- delete tuples
- Update
- modify the column values in an existing row
1
2
3UPDATE Students S
SET S.age = S.age + 1, S.gpa = S.gpa - 1
WHERE S.sid = 53688 - Noted: it would identify all satisfied records then update them
- e.g. | sid | name | login | age | gpa |
1
2
3UPDATE Students S
SET S.gpa = S.gpa - 0.1
WHERE S.gpa >= 3.3
| :—-: | :—-: | :—-: | :—-: | :—-: |
| 50000 | Dave | dave@cs | 19 | 3.2 |
| 53666 | Jones | jones@cs | 18 | 3.3 |
| 53688 | Smith | smith#@ee | 18 | 3.2 |
| 53650 | Smith | smith@math | 19 | 3.7 |
| 53831 | Madayan | madayan@music | 11 | 1.8 |
| 53832 | Guldu | guldu@music | 20 | 2.0 |
- modify the column values in an existing row
Key Constraint
- certain minimal subset of the fields, which is a unique identifier for a tuple, declare by
UNIQUEcommand - designer can also identify a primary key by
PRIMARY KEY1
2
3
4
5
6
7
8
9Create table Students(
sid CHAR(20),
name CHAR(30),
login CHAR(20),
age INTEGER,
gpa REAL,
UNIQUE(name, age), -- a key: not allow two records have the same name and age
CONSTRAINT StudentsKey PRIMARY KEY (sid) -- a primary key
) -- the constraint name StudentsKey is not necessary to be set
Foreign Key
a set of fields in one relation r that is used to refer to a tuple in another relation s
e.g.
| cid | grade | sid |
| :—-: | :—-: | :—-: |
| CSC101 | D | 53831 |
| ERG202 | B | 53832 |
| IEE321 | A | 53650 |
| PSY203 | B | 53666 |In this case, sid would be the foreign key
1
2
3
4
5
6
7CREATE TABLE Enrolled{
sid CHAR(20),
cid CHAR(20),
grade CHAR(10),
PRIMARY KEY (sid, cid),
FOREIGN KEY (sid) REFERENCES Students
}- Noted:
- Cannot insert
<55555, ART104, A>because there is no tuple inStudentswithsid = 55555. - Cannot delete
<53666, Jones, ...>because there is a tuple fromEnrolledwithsid = 53666.
- Cannot insert
Foreign key violations
Insert:
- insert a row to
Enrolledwith a sid value that does not appear inStudents - The
INSERTis reject
- insert a row to
Delete & Update:
- Delete or update a
Studentsrow, the row inEnrolledrefer to the target can have following operations:- Delete all rows in
Enrolledthat refer to the deleteStudentsrow - Disallow the deletion
- Set the sid column in
Enrolledrow that refers to it todefault - Set the sid column in
Enrolledrow that refers to it tonull
- Delete all rows in
- Delete or update a
Operations:
NO ACTION(default)- The action (
DELETEorUPDATE) is to be rejected
- The action (
CASCADE- If
Studentsrow is deleted, allEnrolledrows that refer to it are deleted
- If
SET DEFAULT- Switch the
Enrolledrow to aDEFAULTstudent
- Switch the
SET NULL- Allow the use of
NULLas theDEFAULTvalue
1
2
3
4
5
6
7
8
9CREATE TABLE Enrolled{
sid CHAR(20),
cid CHAR(20),
grade CHAR(10),
PRIMARY KEY (sid, cid),
FOREIGN KEY (sid) REFERENCES Students
ON DELETE CASCADE
ON UPDATE NO ACTION
}- Allow the use of
Logical Database Design
Entity Sets

Attributes:
- all attributes of that entity
Keys:
- the primary key and condidate keys (if exist)
Entity Set:
Employees: id, name, lot
1
2
3
4
5
6CREATE TABLE Employees(
id CHAR(11),
name CHAR(30),
lot INTEGER,
PRIMARY KEY(id) -- primary key {id}
)
Relationship Sets

Attributes:
- the primary key attributes of each participating entity set
- its own attribute
- the descriptive attribute
Keys:
- primary key: the group of primary keys of each participating entity
- the foreign keys
Entity Table:
- Employees: id, name, lot
- Departments: did, dname, budget
- Locations: address, capacity
Relationship Table:
Work_In2: id, did, address, since (primary key:
{id, did, address})1
2
3
4
5
6
7
8
9
10
11
12
13
14CREATE TABLE Work_In2(
-- primary key of each participating entity set
id CHAR(11),
did INTEGER,
address CHAR(20),
-- its own attribute
since DATE,
-- primary key (cannot be null)
PRIMARY KEY (id, did, address), -- the group of primary keys of each entity
-- foreign keys
FOREIGN KEY (id) REFERENCES Employees,
FOREIGN KEY (did) REFERENCES Departments,
FOREIGN KEY (address) REFERENCES Locations
)
demonstrating on descriptive attributes

Entity Table:
- Employees: id, name, lot
Relationship Table:
Reoports_To: supervisor_id, subordinate_id (primary key:
{supervisor_id, subordinate_id})1
2
3
4
5
6
7
8
9
10CREATE TABLE Reoports_To(
-- the descriptive attributes
supervisor_id CHAR(11),
subordinate_id CHAR(11),
-- primary key
PRIMARY KEY (supervisor_id, subordinate_id), -- the group of attributes
-- foreign keys (relate the descriptive attributes to the associated attribute)
FOREIGN KEY (supervisor_id) REFERENCES Employees(id),
FOREIGN KEY (subordinate_id) REFERENCES Employees(id),
)
Relationship Sets (Key Constraints)

- each department has at most one manager (i.e., no two tuples can have same
didbut can have sameidinManagerstable) didis the primary / candidate key but{id, did}is not as it is not minimal
Attributes:
- all attributes of the entity set with the candidate key
- the primary key attributes of each participating entity set
- its own attributes
Keys:
- primary key: the only required primary key
- the foreign keys
Entity Table:
- Employees: id, name, lot
- Departments: did, dname, budget
Relationship Table:
- Managers: did, dname, budget, id, since (primary key:
{did})
- Managers: did, dname, budget, id, since (primary key:
Cons: if a lot of departments have no manager, it is wasting space
1
2
3
4
5
6
7
8
9
10
11
12
13CREATE TABLE Managers(
-- all attributes of the entity set with the candidate key
did INTEGER,
dname CHAR(20),
budget REAL,
-- primary key of each entity set
id CHAR(11),
-- its own attributes
since DATE,
-- primary key (not the set of primary keys in entity sets)
PRIMARY KEY (did),
FOREIGN KEY (id) REFERENCES Employees
)
Relationship Sets (Participation Constraints)

each department has exactly one one manager (i.e.,
idcannot set to beDELETE)1
2
3
4
5
6
7
8
9
10CREATE TABLE Managers(
did INTEGER,
dname CHAR(20),
budget REAL,
id CHAR(11),
since DATE,
PRIMARY KEY (did),
-- must have one manager (turn on cannot DELETE)
FOREIGN KEY (id) REFERENCES Employees ON DELETE NO ACTION
)
Week Entity Sets

The weak entity set always
- participates in a one-to-many binary relationship
- has a key constraint
- has total participation
Primary key:
{id, pname}asDependentsis a weak entity
Attributes:
- all attributes of the entity set with the candidate key
- the primary key attributes of each participating entity set
- its own attributes
Keys:
- primary key: the group of primary key in participating entity
- the foreign keys
Entity Table:
- Employees: id, name, lot
- Departments: did, dname, budget
Relationship Table:
Managers: did, dname, budget, id, since (primary key:
{id, pname})1
2
3
4
5
6
7
8
9
10
11
12
13CREATE TABLE Policy(
-- all attributes of the entity set
pname CHAR(20),
age INTEGER,
-- the primary key of each participating entity
id CHAR(11),
-- its own attributes
cost REAL,
-- primary key
PRIMARY KEY (pname,id),
-- ensure that information about employee's policy and dependents is deleted
FOREIGN KEY (id) REFERENCES Employees ON DELETE CASCADE
)
Class Hierarchies

- Entity Table:
- Employees: id, name, lot
- Hourly-Emps: id, hour_worked, hour_wages
- Contract-Emps: id, contract_id
Aggregation

Entity Table:
- Employees: id, name, lot
- Departments: did, dname, budget
- Projects: pid, started-on, pbudget
Relationship Table:
- Monitors: id, pid, did, until (primary key:
{id, pid, did}) - Sponsors: pid, did, since (primary key:
{pid, did})
- Monitors: id, pid, did, until (primary key:


