b
b
Today’s agenda
ERD to Relational Schema
Arithmetic operators
• AND, OR, NOT,
• <, <=, =, <>,>
Expression
• +,-,*,/
• LIKE _,%
SET operators
• UNION, INTERSECT, EXCEPT
• UNION ALL, INTERSEC ALL, EXCEPT ALL
• IN, NOT IN
• EXISTS, NOT EXISTS
• ANY, ALL
RMIT Classification: Trusted
Entities to Relations
Entities to Relations
Relationships to Relations
Entity, Many-to-Many relationship
Employee(ssn,address,name)
Department(did,dname,budget) Relational Schema
works_in(Employee.ssn ssn, Department.did did,since)
Relationships to Relations | One-to-Many
13
Aggregations to Relations
Project(pID,pName,budget)
Employee(ssn,name,lot,since,Project.pID pID)
Machinery(mID,info,Project.pID pID)
Uses(Employee.ssn ssn,Machinery.mID mID)
14
RMIT Classification: Trusted
Exercise
15
Exercise
Write the relational schema to implement the below Entity-Relationship diagram.
14
Exercise
14
Documenting Entity-Relationship
Entities
Entity & Description Attribute & Description Data type and example value Nul Primary key
Ex.
Entity & Description Attribute & Description Data type and example value Null Primary key
Ex.
Relationship & Description Attribute & Description Entities Multiplicity Note
Notes
SQL Statement
DBMS way
Step 1. Checks if the query is according to the (correct) syntax
Step 2. Checks if all the tables and columns exist
Step 3. Executes the query in the most efficient way possible
CREATE TABLE Sailors(sid INT, sname VARCHAR(20), rating INT, age FLOAT, PRIMARY KEY (sid));
CREATE TABLE Boats(bid INT, bname VARCHAR(20), color VARCHAR(10), PRIMARY KEY (bid));
CREATE TABLE Reserves(sid INT, bid INT, day VARCHAR(30), PRIMARY KEY (sid, bid), FOREIGN KEY
(sid) REFERENCES Sailors(sid), FOREIGN KEY (bid) REFERENCES Boats(bid));
RMIT Classification: Trusted
This query shows all columns of Sailors. Instead of writing all columns
in the SELECT clause, we can write SELECT *
RMIT Classification: Trusted
SELECT S.sname
FROM Sailors S, Reserves R
WHERE S.sid = R.sid AND R.bid = 103
table-join
row filter condition
condition
FROM Sailors S, Reserves R WHERE S.sid = R.sid R.bid = 103
RMIT Classification: Trusted
Example
Question:
Who reserved a red boat?
Query:
Get the names of Sailors who
reseved a red boat.
RMIT Classification: Trusted
Example
Query: Get the names of Sailors who reseved a red boat.
SELECT S.sname
FROM Sailors S, Reserves R, Boats B
WHERE S.sid = R.sid AND R.bid = B.bid AND
B.color = 'red'
Ex. Calculate the rating increments (+1) of the sailors who reserved two boats in the same day
Set operators
* All duplicated rows are removed by default. To include duplicates, use UNION ALL, INTERSECT ALL,
EXCEPT ALL
RMIT Classification: Trusted
UNION Example
Names of the sailors who reserve green OR red boats
Alternative
with UNION
SELECT DISTINCT S.sname
FROM Sailors S, Reserves R, Boats B WHERE S.sid = R.sid AND
R.bid = B.bid AND B.color = 'green'
UNION
SELECT DISTINCT S.sname
FROM Sailors S, Reserves R, Boats B WHERE S.sid = R.sid AND
R.bid = B.bid AND B.color = 'red'
RMIT Classification: Trusted
Summary
Practice translating ERD to Relational Schema
Arithmetic operators
AND, OR, NOT,
<, <=, =, <>,>
Expression
+,-,*,/
LIKE _,%
SET operators
UNION, INTERSECT, EXCEPT
UNION ALL, INTERSEC ALL, EXCEPT ALL
Further reading
Chapter 17. Methodology – Logical Database Design for the Relational Model