Database Systems, Edition 2.0 (Jukic et. al.
) Instructors Manual – Chapter 05
CHAPTER 5 – SQL
KEY CONCEPTS
The goal of this chapter is to provide comprehensive coverage of SQL.
The following topics related to SQL are covered:
- SQL Commands Categories
o Data Definition Language (DDL)
o Data Manipulation Language (DML)
o Data Control Language (DCL)
o Transaction Control Language (TCL)
- SQL Commands and Keywords
o CREATE TABLE
o DROP TABLE
o INSERT INTO
o SELECT
o WHERE
o DISTINCT
o ORDER BY
o LIKE
o Aggregate Functions (COUNT, SUM, AVG, MIN and MAX)
o GROUP BY
o HAVING
o IN
o ALTER TABLE
o UPDATE
o DELETE
o CREATE VIEW
o DROP VIEW
o IS NULL
o EXISTS
o NOT
- Nested Queries
- JOIN
o Joining Multiple Relations
o JOIN of a Relation with Itself (Self-JOIN)
o Outer JOIN
o JOIN without using a Primary Key/Foreign Key Combination
- Set Operators
o UNION
o INTERSECT
o EXCEPT (MINUS)
- Constraint Management
- Alias
Copyright (c) 2020 Nenad Jukic and Prospect Press IM Chapter 05 - Page 1
Database Systems, Edition 2.0 (Jukic et. al.) Instructors Manual – Chapter 05
- Inserting from a Query
- Inappropriate Use of Observed Values in SQL
- SQL Standard
- SQL Syntax Differences
Copyright (c) 2020 Nenad Jukic and Prospect Press IM Chapter 05 - Page 2
Database Systems, Edition 2.0 (Jukic et. al.) Instructors Manual – Chapter 05
Chapter 5 - ANSWERS (Review Questions)
Q5.1 What is the purpose of DDL SQL statements?
DDL statements are used to create and modify the structure of the database.
Q5.2 What is the purpose of DML SQL statements?
DML statements are used to manipulate the data within the database. DML statements include
the commands for inserting, modifying, deleting, and retrieving the data in the database.
Q5.3 What is the purpose of the CREATE TABLE command?
The CREATE TABLE command is used for creating and connecting relational tables.
Q5.4 What is the purpose of the DROP TABLE command?
The DROP TABLE command is used to remove a table from the database.
Q5.5 What is the purpose of the INSERT INTO command?
The INSERT INTO command is used to populate the created relations with data.
Q5.6 What is the purpose of the SELECT command?
The SELECT command is used for the retrieval of data from the database relations.
Q5.7 What is the purpose of the WHERE condition?
The WHERE condition determines which rows should be retrieved and consequently which rows
should not be retrieved in a query.
Q5.8 What is the purpose of the DISTINCT keyword?
The DISTINCT keyword is used to eliminate duplicate values from a query result
Q5.9 What is the purpose of the ORDER BY clause?
The ORDER BY clause is used to sort the results of the query by one or more columns.
Q5.10 What is the purpose of the LIKE keyword?
The LIKE keyword is used to retrieve the records whose values partially match a certain criteria.
Copyright (c) 2020 Nenad Jukic and Prospect Press IM Chapter 05 - Page 3
Database Systems, Edition 2.0 (Jukic et. al.) Instructors Manual – Chapter 05
Q5.11 What aggregate functions are provided by SQL?
COUNT, SUM, AVG, MIN and MAX.
Q5.12 What is the purpose of the GROUP BY clause?
The GROUP BY clause enables summarizations across the groups of related data within tables.
Q5.13 What is the purpose of the HAVING clause?
The HAVING clause determines which groups will be displayed in the result of a query and
consequently which groups will not be displayed in the result of the query.
Q5.14 What is a nested query?
A query that is used within another query
Q5.15 What is the purpose of the IN keyword?
The IN keyword is used to compare a value with a set of values.
Q5.16 What is the purpose of the JOIN condition?
JOIN condition is used to facilitate the querying of multiple tables.
Q5.17 What is the purpose of an alias?
An alias is an alternative and usually shorter name that can be used anywhere within a query
instead of the full relation name.
Q5.18 What is the purpose of the ALTER TABLE command?
The ALTER TABLE command is used to change the structure of the relation, once the relation is
already created.
Q5.19 What is the purpose of the UPDATE command?
The UPDATE command is used for modifying the data stored in database tables.
Q5.20 What is the purpose of the DELETE command?
The DELETE command is used to delete the data stored in database relations.
Copyright (c) 2020 Nenad Jukic and Prospect Press IM Chapter 05 - Page 4
Database Systems, Edition 2.0 (Jukic et. al.) Instructors Manual – Chapter 05
Q5.21 What is the purpose of the CREATE VIEW and DROP VIEW commands?
These commands are used to create and remove views.
Q5.22 What set operators are available in SQL?
UNION, INTERSECT, EXCEPT (MINUS)
Q5.23 What is the purpose of OUTER JOIN?
The purpose of OUTER JOIN is to supplement the JOIN query results with the records from one
of the relations that have no match in the other relation
Q5.24 What is the purpose of the IS NULL keyword?
The IS NULL keyword is used for comparison with an empty value in a column of a record.
Q5.25 What is the purpose of the EXISTS keyword?
The EXISTS keyword is used to check if the result of the inner query is empty.
Q5.26 What is the purpose of the NOT keyword?
The NOT keyword is used in conjunction with the condition comparison statements, returning
the Boolean values TRUE when the specified condition is FALSE, and returning the Boolean
values FALSE when the specified condition is TRUE.
Copyright (c) 2020 Nenad Jukic and Prospect Press IM Chapter 05 - Page 5
Database Systems, Edition 2.0 (Jukic et. al.) Instructors Manual – Chapter 05
Chapter 5 - ANSWERS (Exercises)
E5.1 Write the SQL queries that accomplish the following tasks in the ZAGI Retail Company
Sales Department Database:
E5.1.1 Display all the records in the table REGION.
select *
from region;
REGIONID REGIONNAME
C Chicagoland
T Tristate
E5.1.2 Display the StoreID and StoreZip for all stores.
select storeid, storezip
from store;
STOREID STOREZIP
S1 60600
S2 60605
S3 35400
E5.1.3 Display the CustomerName and CustomerZip for all customers, sorted alphabetically by
CustomerName.
select customername, customerzip
from customer
order by customername;
CUSTOMERNAME CUSTOMERZIP
Pam 35401
Tina 60137
Tony 60611
E5.1.4 Display the RegionIDs of regions where we have stores (use only table STORE and do
not display the same information more than once).
select distinct regionid
from store;
REGIONID
C
T
Copyright (c) 2020 Nenad Jukic and Prospect Press IM Chapter 05 - Page 6
Database Systems, Edition 2.0 (Jukic et. al.) Instructors Manual – Chapter 05
E5.1.5 Display all the information for all stores whose RegionID value is C.
select *
from store
where regionid = 'C';
STOREID STOREZIP REGIONID
S1 60600 C
S2 60605 C
E5.1.6 Display CustomerID and CustomerName for all customers whose CustomerName begins
with a letter T.
select customerid, customername
from customer
where customername like 'T%';
CUSTOMERID CUSTOMERNAME
1-2-333 Tina
2-3-444 Tony
E5.1.7 Display the ProductID, ProductName, and ProductPrice for products with a Product
Price of $100 or higher.
SELECT productid, productname, productprice
FROM product
WHERE productprice >= 100;
PRODUCTID PRODUCTNAME PRODUCTPRICE
1X1 Zzz Bag 100
5X5 Tiny Tent 150
6X6 Biggy Tent 250
E5.1.8 Display the ProductID, ProductName, ProductPrice, and VendorName for all
products. Sort the results by ProductID.
select p.productid, p.productname, p.productprice, v.vendorname
from product p, vendor v
where p.vendorid = v.vendorid
order by p.productid;
PRODUCTID PRODUCTNAME PRODUCTPRICE VENDORNAME
1X1 Zzz Bag 100 Pacifica Gear
2X2 Easy Boot 70 Mountain King
3X3 Cosy Sock 15 Mountain King
4X4 Dura Boot 90 Pacifica Gear
5X5 Tiny Tent 150 Mountain King
6X6 Biggy Tent 250 Mountain King
Copyright (c) 2020 Nenad Jukic and Prospect Press IM Chapter 05 - Page 7
Database Systems, Edition 2.0 (Jukic et. al.) Instructors Manual – Chapter 05
E5.1.9 Display the ProductID, ProductName, ProductPrice, VendorName, and CategoryName
for all products. Sort the results by ProductID.
select p.productid, p.productname, p.productprice, v.vendorname,
c.categoryname
from product p, vendor v, category c
where p.categoryid = c.categoryid and
p.vendorid = v.vendorid
order by p.productid;
PRODUCTID PRODUCTNAME PRODUCTPRICE VENDORNAME CATEGORYNAME
1X1 Zzz Bag 100 Pacifica Gear Camping
2X2 Easy Boot 70 Mountain King Footwear
3X3 Cosy Sock 15 Mountain King Footwear
4X4 Dura Boot 90 Pacifica Gear Footwear
5X5 Tiny Tent 150 Mountain King Camping
6X6 Biggy Tent 250 Mountain King Camping
E5.1.10 Display the ProductID, ProductName, and ProductPrice for products in the category
whose CategoryName value is Camping. Sort the results by ProductID.
select p.productid, p.productname, p.productprice
from product p, category c
where p.categoryid = c.categoryid and
c.categoryname = 'Camping'
order by p.productid;
PRODUCTID PRODUCTNAME PRODUCTPRICE
1X1 Zzz Bag 100
5X5 Tiny Tent 150
6X6 Biggy Tent 250
E.5.1.11 Display the ProductID, ProductName, and ProductPrice for products that were sold in
the zip code 60600. Sort the results by ProductID.
select p.productid, p.productname, p.productprice
from product p, includes i, salestransaction t, store s
where p.productid = i.productid and
t.tid = i.tid and
s.storeid = t.storeid and
s.storezip = '60600'
order by p.productid;
PRODUCTID PRODUCTNAME PRODUCTPRICE
1X1 Zzz Bag 100
Copyright (c) 2020 Nenad Jukic and Prospect Press IM Chapter 05 - Page 8
Database Systems, Edition 2.0 (Jukic et. al.) Instructors Manual – Chapter 05
E.5.1.12 Display the ProductID, ProductName, and ProductPrice for Products whose
VendorName is Pacifica Gear that were sold in the region whose RegionName is Tristate. Do
not display the same information more than once (i.e., do not show the same product more than
once). Sort the results by ProductID.
select distinct p.productid, p.productname, p.productprice
from product p, vendor v, includes i, salestransaction t, store s,
region r
where p.vendorid = v.vendorid and
p.productid = i.productid and
t.tid = i.tid and
s.storeid = t.storeid and
s.regionid = r.regionid and
r.regionname = 'Tristate' and
v.vendorname = 'Pacifica Gear'
order by p.productid;
PRODUCTID PRODUCTNAME PRODUCTPRICE
1X1 Zzz Bag 100
4X4 Dura Boot 90
E5.1.13 Display the TID, CustomerName, and TDate for sales transactions involving a
customer buying a product whose ProductName is Easy Boot.
select t.tid, c.customername, t.tdate
from salestransaction t, customer c, product p, includes i
where t.tid = i.tid
and p.productid = i.productid
and c.customerid = t.customerid
and p.productname = 'Easy Boot';
TID CUSTOMERNAME TDATE
T222 Tony 01-JAN-20
T444 Pam 02-JAN-20
E5.1.14 Display the RegionID, RegionName, and number of stores in the region for all regions.
select r.regionid, r.regionname, count(*)
from region r, store s
where r.regionid = s.regionid
group by r.regionid, r.regionname;
REGIONID REGIONNAME COUNT(*)
C Chicagoland 2
T Tristate 1
Copyright (c) 2020 Nenad Jukic and Prospect Press IM Chapter 05 - Page 9
Database Systems, Edition 2.0 (Jukic et. al.) Instructors Manual – Chapter 05
E5.1.15 For each product category, display the CategoryID, CategoryName, and average price
of a product in the category.
select c.categoryid, c.categoryname, avg(p.productprice)
from product p, category c
where p.categoryid = c.categoryid
group by c.categoryid, c.categoryname;
CATEGORYID CATEGORYNAME AVG(P.PRODUCTPRICE)
CP Camping 166.666667
FW Footwear 58.3333333
E5.1.16 For each product category, display the CategoryID and the total number of items
purchased in the category.
select p.categoryid, sum(i.quantity)
from includes i, product p
where i.productid = p.productid
group by p.categoryid;
CATEGORYID SUM(NOOFITEMS)
CP 5
FW 13
E5.1.17 Display the RegionID, RegionName, and the total amount of sales (in dollars) in the
region for all regions. Display the total amount of sales as AmountSpent.
select r.regionid, r.regionname,
sum(i.quantity*p.productprice) as amountspent
from includes i, product p, salestransaction t, store s, region r
where i.productid = p.productid
and t.tid = i.tid
and t.storeid = s.storeid
and s.regionid = r.regionid
group by r.regionid, r.regionname;
REGIONID REGIONNAME AMOUNTSPENT
C Chicagoland 170.00
T Tristate 1315.00
Copyright (c) 2020 Nenad Jukic and Prospect Press IM Chapter 05 - Page 10
Database Systems, Edition 2.0 (Jukic et. al.) Instructors Manual – Chapter 05
E5.1.18 Display the TID and the total number of items (of all products) sold within the
transaction for all sales transactions whose total number of items (of all products) sold
within the transaction is greater than five.
select tid, sum(quantity)
from includes
group by tid
having sum(quantity) > 5;
TID SUM(QUANTITY)
T333 6
T555 7
E5.1.19 For each vendor whose product sales exceed $700, display the VendorID, VendorName,
and total amount of sales in dollars. Display the total amount of sales as TotalSales.
select v.vendorid, v.vendorname,
sum(i.quantity*p.productprice) as totalsales
from includes i, product p, vendor v
where i.productid = p.productid and
p.vendorid = v.vendorid
group by v.vendorid, v.vendorname
having sum(i.quantity*p.productprice) > 700;
VENDORID VENDORNAME TOTALSALES
MK Mountain King 835.00
E5.1.20 Display the ProductID, ProductName, and ProductPrice of the cheapest product.
select productid, productname, productprice
from product
where productprice = (select min(productprice)
from product);
PRODUCTID PRODUCTNAME PRODUCTPRICE
3X3 Cosy Sock 15.00
Copyright (c) 2020 Nenad Jukic and Prospect Press IM Chapter 05 - Page 11
Database Systems, Edition 2.0 (Jukic et. al.) Instructors Manual – Chapter 05
E5.1.21 Display the ProductID, ProductName, and VendorName for products whose price is
below the average price of all products.
select p.productid, p.productname, v.vendorname
from product p, vendor v
where p.vendorid = v.vendorid
and productprice < (select avg(productprice) from product);
PRODUCTID PRODUCTNAME VENDORNAME
1X1 Zzz Bag Pacifica Gear
2X2 Easy Boot Mountain King
3X3 Cosy Sock Mountain King
4X4 Dura Boot Pacifica Gear
E5.1.22 Display the ProductID and ProductName of the product for the products whose total
quantity sold in all transactions is greater than 2. Sort the results by ProductID.
select p.productid, p.productname
from product p, includes i
where i.productid = p.productid
group by p.productid, p.productname
having sum(i.quantity) > 2
order by productid;
PRODUCTID PRODUCTNAME
2X2 Easy Boot
3X3 Cosy Sock
4X4 Dura Boot
E5.1.23 Display the ProductID for the product that has been sold the most within all
transactions (i.e., that has the highest total quantity sold summarized across all transactions).
select productid
from includes
group by productid
having sum(quantity) = (select max(A)
from (select sum(quantity) as A from includes
group by productid) as B);
/* In Postgres, nested queries used in the “from” part need aliases, hence in the version above we use the letter B as alias for the nested query at
the end. That same alias would not be necessary in Oracle for example, as shown below */
select productid
from includes
group by productid
having sum(quantity) = (select max(A)
from (select sum(quantity) as A from includes
group by productid));
PRODUCTID
3X3
4X4
Copyright (c) 2020 Nenad Jukic and Prospect Press IM Chapter 05 - Page 12
Database Systems, Edition 2.0 (Jukic et. al.) Instructors Manual – Chapter 05
E5.1.24 Rewrite Query 30 using a join statement (no nested queries).
select p.productid, p.productname, p.productprice
from product p, includes s
where p.productid = s.productid
group by p.productid, p.productname, p.productprice
having sum(s.quantity) > 3;
PRODUCTID PRODUCTNAME PRODUCTPRICE
4X4 Dura Boot 90
3X3 Cosy Sock 15
E5.1.25 Rewrite Query 31 using a join statement (no nested queries).
select p.productid, p.productname, p.productprice
from product p, includes s
where p.productid = s.productid
group by p.productid, p.productname, p.productprice
having count(s.tid) > 1;
PRODUCTID PRODUCTNAME PRODUCTPRICE
1X1 Zzz Bag 100
2X2 Easy Boot 70
4X4 Dura Boot 90
Copyright (c) 2020 Nenad Jukic and Prospect Press IM Chapter 05 - Page 13
Database Systems, Edition 2.0 (Jukic et. al.) Instructors Manual – Chapter 05
E5.2 Write the SQL queries that accomplish the following tasks in the HAFH Realty Company
Property Management Database:
E5.2.1 Display all the records in the table STAFFMEMBER.
select *
from staffmember;
SMEMBERID SMEMBERNAME
5432 Brian
9876 Boris
7652 Caroline
E5.2.2 Display the BuildingID and number of floors in the building for all buildings.
select buildingid, bnooffloors
from building;
BUILDINGID BNOOFFLOORS
B1 5
B2 6
B3 4
B4 4
E5.2.3 Display the CCID, CCName, and CCIndustry for all corporate clients, sorted
alphabetically by CCName.
select ccid, ccname, ccindustry
from corpclient
order by ccname;
CCID CCNAME CCINDUSTRY
C111 BlingNotes Music
C222 SkyJet Airline
C888 SouthAlps Sports
C777 WindyCT Music
E5.2.4 Display all the records in the table STAFF MEMBER for staff members whose
SMemberName begins with a letter B.
select *
from staffmember
where smembername like 'B%';
SMEMBERID SMEMBERNAME
5432 Brian
9876 Boris
Copyright (c) 2020 Nenad Jukic and Prospect Press IM Chapter 05 - Page 14
Database Systems, Edition 2.0 (Jukic et. al.) Instructors Manual – Chapter 05
E5.2.5 Display the BuildingID, AptNo, and ANoOfBedrooms for all apartments that have more
than one bedroom.
select buildingid, aptno, anoofbedrooms
from apartment
where anoofbedrooms > 1;
BUILDINGID APTNO ANOOFBEDROOMS
B2 11 2
B2 31 2
B3 11 2
B4 11 2
E5.2.6 Display the answer to the following question: How many HAFH buildings have exactly 4
floors?
select count(*)
from building
where bnooffloors = 4;
COUNT
2
E5.2.7 Display the total amount HAFH spends on manager salaries (as TotalSalary) and the
total amount HAFH spends on manager bonuses (as TotalBonus).
select sum(msalary) as totalsalary, sum(mbonus) as totalbonus
from manager;
TOTALSALARY TOTALBONUS
162000.00 7000.00
E5.2.8 Display the ManagerID, MFName, MLName, MSalary, and MBonus, for all managers
with a salary greater than $50,000 and a bonus greater than $1,000.
select managerid, mfname, mlname, msalary, mbonus
from manager
where msalary > 50000 and mbonus > 1000;
MANAGERID MFNAME MLNAME MSALARY MBONUS
M34 George Sherman 52000.00 2000.00
Copyright (c) 2020 Nenad Jukic and Prospect Press IM Chapter 05 - Page 15
Database Systems, Edition 2.0 (Jukic et. al.) Instructors Manual – Chapter 05
E5.2.9 Display the BuildingID, BNoOfFloors, and the manager’s MFName and MLName for all
buildings.
select b.buildingid, b.bnooffloors, m.mfname, m.mlname
from building b, manager m
where b.bmanagerid = m.managerid;
BUILDINGID BNOOFFLOORS MFNAME MLNAME
B1 5 Boris Grant
B2 6 Austin Lee
B3 4 Austin Lee
B4 4 George Sherman
E5.2.10 Display the MFName, MLName, MSalary, MBDate, and number of buildings that the
manager manages for all managers with a salary less than $55,000.
select m.mfname, m.mlname, m.msalary, m.mbdate, count(*)
from building b, manager m
where b.bmanagerid = m.managerid AND
m.msalary < 55000
group by m.mfname, m.mlname, m.msalary, m.mbdate;
MFNAME MLNAME MSALARY MBDATE COUNT(*)
Austin Lee 50000 30-OCT-75 2
George Sherman 52000 11-JAN-76 1
E5.2.11 Display the BuildingID and AptNo, for all apartments leased by the corporate client
WindyCT.
select a.buildingid, a.aptno
from apartment a, corpclient c
where a.ccid = c.ccid AND
c.ccname = 'WindyCT';
BUILDINGID APTNO
B3 11
B4 11
E5.2.12 Display the InsID and InsName for all inspectors whose next inspection is scheduled
after 1-APR-2020. Do not display the same information more than once.
select distinct ir.insid, ir.insname
from inspector ir, inspecting ig
where ir.insid = ig.insid AND
ig.datenext > '04-01-2020';
INSID INSNAME
I11 Jane
I33 Mick
Copyright (c) 2020 Nenad Jukic and Prospect Press IM Chapter 05 - Page 16
Database Systems, Edition 2.0 (Jukic et. al.) Instructors Manual – Chapter 05
E5.2.13 Display the ManagerID, MFName, MLName, and MSalary, for all managers with a
salary greater than the average manager’s salary.
select managerid, mfname, mlname, msalary
from manager
where msalary > (select avg(msalary)
from manager);
MANAGERID MFNAME MLNAME MSALARY
M12 Boris Grant 60000
E5.2.14 Display the ManagerID, MFName, MLName, and the number of buildings managed, for
all managers.
select m.managerid, m.mfname, m.mlname, count(*)
from manager m, building b
where b.bmanagerid = m.managerid
group by m.managerid, m.mfname, m.mlname;
MANAGERID MFNAME MLNAME COUNT
M12 Boris Grant 1
M23 Austin Lee 2
M34 George Sherman 1
E5.2.15 Display the ManagerID, MFName, MLName, and number of buildings managed, for all
managers that manage more than one building.
select m.managerid, m.mfname, m.mlname, count(*)
from manager m, building b
where b.bmanagerid = m.managerid
group by m.managerid, m.mfname, m.mlname
having count(*) > 1;
MANAGERID MFNAME MLNAME COUNT
M23 Austin Lee 2
E5.2.16 Display the SMemberID, SMemberName, and the number of apartments that the staff
members cleans, for all staff members.
select s.smemberid, s.smembername, count(*)
from staffmember s, cleaning c
where s.smemberid = c.smemberid
group by s.smemberid, s.smembername;
SMEMBERID SMEMBERNAME COUNT
5432 Brian 3
9876 Boris 2
7652 Caroline 1
Copyright (c) 2020 Nenad Jukic and Prospect Press IM Chapter 05 - Page 17
Database Systems, Edition 2.0 (Jukic et. al.) Instructors Manual – Chapter 05
E5.2.17 Display the SMemberID and SMemberName of staff members cleaning apartments
rented by corporate clients whose corporate location is Chicago. Do not display the same
information more than once.
select distinct s.smemberid, s.smembername
from staffmember s, cleaning cl, apartment a, corpclient cc
where s.smemberid = cl.smemberid and
a.aptno = cl.aptno and
a.buildingid = cl.buildingid and
a.ccid = cc.ccid and
cc.cclocation = 'Chicago';
SMEMBERID SMEMBERNAME
7652 Caroline
5432 Brian
E5.2.18 Display the CCName of the client and the CCName of the client who referred it, for
every client referred by a client in the music industry.
select c1.ccname client, c2.ccname referredby
from corpclient c1, corpclient c2
where c2.ccid = c1.ccidreferredby and
c2.ccindustry = 'Music';
CLIENT REFERREDBY
SkyJet BlingNotes
SouthAlps WindyCT
E5.2.19 Display the BuildingID, AptNo, and ANoOfBedrooms for all apartments that are not
leased.
select buildingid, aptno, anoofbedrooms
from apartment
where ccid is null;
BUILDINGID APTNO ANOOFBEDROOMS
B1 41 1
B2 31 2
E5.2.20 Display the ManagerID, MFName, MLName, MSalary, and MBonus, for the manager
with the lowest total compensation (total compensation is defined as the salary plus bonus).
NOTE: At first glance this code appears to be correct and it produces the correct solution, as
shown
select managerid, mfname, mlname, msalary, mbonus
from manager
where msalary+mbonus = (select min(msalary+mbonus)
from manager);
Copyright (c) 2020 Nenad Jukic and Prospect Press IM Chapter 05 - Page 18
Database Systems, Edition 2.0 (Jukic et. al.) Instructors Manual – Chapter 05
MANAGERID MFNAME MLNAME MSALARY MBONUS
M34 George Sherman 52000 2000
However, we have to take into account that mbonus is an optional column that may contain
NULL values. NULL values cannot be used in arithmetical operations (such as
msalary+mbonus). Therefore, we have to use a function that converts coverts NULL values to 0
(zero) and leaves the non-NULL values as is. COALESCE(field,0) is an SQL function (that
works in PostgreSQL, Oracle, MySQL, etc.) used for that purpose, and here is the correct code
using that function:
select managerid, mfname, mlname, msalary, COALESCE(mbonus,0) as mgrbonus
from manager
where msalary+COALESCE(mbonus,0) = (select min(msalary+COALESCE(mbonus,0))
from manager);
MANAGERID MFNAME MLNAME MSALARY MBONUS
M34 George Sherman 52000 2000
Copyright (c) 2020 Nenad Jukic and Prospect Press IM Chapter 05 - Page 19
Database Systems, Edition 2.0 (Jukic et. al.) Instructors Manual – Chapter 05
Chapter 5 - ANSWERS (Exercises) for exercises performed on extended data sets ZAGIMORE
and HAFHMORE (available at https://www.prospectpressvt.com/textbooks/jukic-database-systems-2-
0/instructor-resources-jukic/ under SQL and Data Sets Supplements) - these solutions are marked with
the prefix Ext.
ExtE.1 Write the SQL queries that accomplish the following tasks in the ZAGIMORE database:
ExtE5.1.1 Display all the records in the table REGION.
select *
from region;
REGIONID REGIONNAME
C Chicagoland
T Tristate
I Indiana
N North
ExtE5.1.2 Display the StoreID and StoreZip for all stores.
select storeid, storezip
from store;
STOREID STOREZIP
S1 60600
S2 60605
S3 35400
S4 60640
S5 46307
S6 47374
S7 47401
S8 55401
S9 54937
S10 60602
S11 46201
S12 55701
S13 60085
S14 53140
ExtE5.1.3 Display the CustomerName and CustomerZip for all customers, sorted alphabetically
by CustomerName.
select customername, customerzip
from customer
order by customername;
CUSTOMERNAME CUSTOMERZIP
Dan 55499
Copyright (c) 2020 Nenad Jukic and Prospect Press IM Chapter 05 - Page 20
Database Systems, Edition 2.0 (Jukic et. al.) Instructors Manual – Chapter 05
Elly 47374
Maggie 47401
Miles 60602
Neil 55403
Nora 60640
Pam 35401
Ryan 46202
Tina 60137
Tony 60611
ExtE5.1.4 Display the RegionIDs of regions where we have stores (use only table STORE and do
not display the same information more than once).
select distinct regionid
from store;
REGIONID
N
C
T
I
ExtE5.1.5 Display all the information for all stores whose RegionID value is C.
select *
from store
where regionid = 'C';
STOREID STOREZIP REGIONID
S1 60600 C
S2 60605 C
S4 60640 C
S10 60602 C
ExtE5.1.6 Display CustomerID and CustomerName for all customers whose CustomerName
begins with a letter T.
select customerid, customername
from customer
where customername like 'T%';
CUSTOMERID CUSTOMERNAME
1-2-333 Tina
2-3-444 Tony
ExtE5.1.7 Display the ProductID, ProductName, and ProductPrice for products with a Product
Price of $100 or higher.
Copyright (c) 2020 Nenad Jukic and Prospect Press IM Chapter 05 - Page 21
Database Systems, Edition 2.0 (Jukic et. al.) Instructors Manual – Chapter 05
SELECT productid, productname, productprice
FROM product
WHERE productprice >= 100;
PRODUCTID PRODUCTNAME PRODUCTPRICE
1X1 Zzz Bag 100.00
5X5 Tiny Tent 150.00
6X6 Biggy Tent 250.00
7X7 Hi-Tec GPS 300.00
1X2 Comfy Harness 150.00
1X3 Sunny Charger 125.00
4X3 Mega Camera 275.00
5X3 Luxo Tent 500.00
ExtE5.1.8 Display the ProductID, ProductName, ProductPrice, and VendorName for all
products. Sort the results by ProductID.
select p.productid, p.productname, p.productprice, v.vendorname
from product p, vendor v
where p.vendorid = v.vendorid
order by productid;
PRODUCTID PRODUCTNAME PRODUCTPRICE VENDORNAME
1X1 Zzz Bag 100.00 Pacifica Gear
1X2 Comfy Harness 150.00 Mountain King
1X3 Sunny Charger 125.00 Outdoor Adventures
1X4 Safe-T Helmet 40.00 Pacifica Gear
2X1 Mmm Stove 80.00 Wilderness Limited
2X2 Easy Boot 70.00 Mountain King
2X3 Reflect-o Jacket 35.00 Pacifica Gear
2X4 Strongster Carribeaner 20.00 Mountain King
3X1 Sleepy Pad 25.00 Wilderness Limited
3X2 Bucky Knife 60.00 Wilderness Limited
3X3 Cosy Sock 15.00 Mountain King
3X4 Treado Tire 30.00 Outdoor Adventures
4X1 Slicky Tire 25.00 Outdoor Adventures
4X2 Electra Compass 45.00 Mountain King
4X3 Mega Camera 275.00 Wilderness Limited
4X4 Dura Boot 90.00 Pacifica Gear
5X1 Simple Sandal 50.00 Pacifica Gear
5X2 Action Sandal 70.00 Pacifica Gear
5X3 Luxo Tent 500.00 Outdoor Adventures
5X5 Tiny Tent 150.00 Mountain King
6X6 Biggy Tent 250.00 Mountain King
7X7 Hi-Tec GPS 300.00 Outdoor Adventures
8X8 Power Pedals 20.00 Mountain King
9X9 Trusty Rope 30.00 Wilderness Limited
Copyright (c) 2020 Nenad Jukic and Prospect Press IM Chapter 05 - Page 22
Database Systems, Edition 2.0 (Jukic et. al.) Instructors Manual – Chapter 05
ExtE5.1.9 Display the ProductID, ProductName, ProductPrice, VendorName, and
CategoryName for all products. Sort the results by ProductID.
select p.productid, p.productname, p.productprice, v.vendorname,
c.categoryname
from product p, vendor v, category c
where p.categoryid = c.categoryid and
p.vendorid = v.vendorid
order by p.productid;
PRODUCTID PRODUCTNAME PRODUCTPRICE VENDORNAME CATEGORYNAME
1X1 Zzz Bag 100.00 Pacifica Gear Camping
1X2 Comfy Harness 150.00 Mountain King Climbing
1X3 Sunny Charger 125.00 Outdoor Adventures Electronics
1X4 Safe-T Helmet 40.00 Pacifica Gear Cycling
2X1 Mmm Stove 80.00 Wilderness Limited Camping
2X2 Easy Boot 70.00 Mountain King Footwear
2X3 Reflect-o Jacket 35.00 Pacifica Gear Cycling
2X4 Strongster Carribeaner 20.00 Mountain King Climbing
3X1 Sleepy Pad 25.00 Wilderness Limited Camping
3X2 Bucky Knife 60.00 Wilderness Limited Camping
3X3 Cosy Sock 15.00 Mountain King Footwear
3X4 Treado Tire 30.00 Outdoor Adventures Cycling
4X1 Slicky Tire 25.00 Outdoor Adventures Cycling
4X2 Electra Compass 45.00 Mountain King Electronics
4X3 Mega Camera 275.00 Wilderness Limited Electronics
4X4 Dura Boot 90.00 Pacifica Gear Footwear
5X1 Simple Sandal 50.00 Pacifica Gear Footwear
5X2 Action Sandal 70.00 Pacifica Gear Footwear
5X3 Luxo Tent 500.00 Outdoor Adventures Camping
5X5 Tiny Tent 150.00 Mountain King Camping
6X6 Biggy Tent 250.00 Mountain King Camping
7X7 Hi-Tec GPS 300.00 Outdoor Adventures Electronics
8X8 Power Pedals 20.00 Mountain King Cycling
9X9 Trusty Rope 30.00 Wilderness Limited Climbing
ExtE5.1.10 Display the ProductID, ProductName, and ProductPrice for products in the
category whose CategoryName value is Camping. Sort the results by ProductID.
select p.productid, p.productname, p.productprice
from product p, category c
where p.categoryid = c.categoryid and
c.categoryname = 'Camping'
order by p.productid;
PRODUCTID PRODUCTNAME PRODUCTPRICE
1X1 Zzz Bag 100.00
2X1 Mmm Stove 80.00
Copyright (c) 2020 Nenad Jukic and Prospect Press IM Chapter 05 - Page 23
Database Systems, Edition 2.0 (Jukic et. al.) Instructors Manual – Chapter 05
3X1 Sleepy Pad 25.00
3X2 Bucky Knife 60.00
5X3 Luxo Tent 500.00
5X5 Tiny Tent 150.00
6X6 Biggy Tent 250.00
E.5.1.11 Display the ProductID, ProductName, and ProductPrice for products that were sold in
the zip code 60600. Sort the results by ProductID.
select p.productid, p.productname, p.productprice
from product p, includes i, salestransaction t, store s
where p.productid = i.productid and
t.tid = i.tid and
s.storeid = t.storeid and
s.storezip = '60600'
order by p.productid;
PRODUCTID PRODUCTNAME PRODUCTPRICE
1X1 Zzz Bag 100
E.5.1.12 Display the ProductID, ProductName, and ProductPrice for Products whose
VendorName is Pacifica Gear that were sold in the region whose RegionName is Tristate. Do
not display the same information more than once (i.e., do not show the same product more than
once). Sort the results by ProductID.
select distinct p.productid, p.productname, p.productprice
from product p, vendor v, includes i, salestransaction t, store s,
region r
where p.vendorid = v.vendorid and
p.productid = i.productid and
t.tid = i.tid and
s.storeid = t.storeid and
s.regionid = r.regionid and
r.regionname = 'Tristate' and
v.vendorname = 'Pacifica Gear'
order by p.productid;
PRODUCTID PRODUCTNAME PRODUCTPRICE
1X1 Zzz Bag 100.00
4X4 Dura Boot 90.00
5X1 Simple Sandal 50.00
ExtE5.1.13 Display the TID, CustomerName, and TDate for sales transactions involving a
customer buying a product whose ProductName is Easy Boot.
select t.tid, c.customername, t.tdate
from salestransaction t, customer c, product p, includes i
where t.tid = i.tid
and p.productid = i.productid
Copyright (c) 2020 Nenad Jukic and Prospect Press IM Chapter 05 - Page 24
Database Systems, Edition 2.0 (Jukic et. al.) Instructors Manual – Chapter 05
and c.customerid = t.customerid
and p.productname = 'Easy Boot';
TID CUSTOMERNAME TDATE
T222 Tony 01-JAN-20
T444 Pam 02-JAN-20
T505 Miles 05-JAN-20
T606 Dan 06-JAN-20
T808 Neil 06-JAN-20
T022 Ryan 07-JAN-20
ExtE5.1.14 Display the RegionID, RegionName, and number of stores in the region for all
regions.
select r.regionid, r.regionname, count(*)
from region r, store s
where r.regionid = s.regionid
group by r.regionid, r.regionname;
REGIONID REGIONNAME COUNT(*)
N North 3
I Indiana 3
C Chicagoland 4
T Tristate 4
ExtE5.1.15 For each product category, display the CategoryID, CategoryName, and average
price of a product in the category.
select c.categoryid, c.categoryname, avg(p.productprice)
from product p, category c
where p.categoryid = c.categoryid
group by c.categoryid, c.categoryname;
CATEGORYID CATEGORYNAME AVG(P.PRODUCTPRICE)
CY Cycling 30
CP Camping 166.4285714285714286
EL Electronics 186.25
CL Climbing 66.6666666666666667
FW Footwear 59
ExtE5.1.16 For each product category, display the CategoryID and the total number of items
purchased in the category.
select p.categoryid, sum(i.quantity)
from includes i, product p
where i.productid = p.productid
group by p.categoryid;
Copyright (c) 2020 Nenad Jukic and Prospect Press IM Chapter 05 - Page 25
Database Systems, Edition 2.0 (Jukic et. al.) Instructors Manual – Chapter 05
CATEGORYID SUM(NOOFITEMS)
CP 21
EL 12
CL 25
FW 34
CY 24
ExtE5.1.17 Display the RegionID, RegionName, and the total amount of sales (in dollars) in the
region for all regions. Display the total amount of sales as AmountSpent.
select r.regionid, r.regionname,
sum(i.quantity*p.productprice) as amountspent
from includes i, product p, salestransaction t, store s, region r
where i.productid = p.productid
and t.tid = i.tid
and t.storeid = s.storeid
and s.regionid = r.regionid
group by r.regionid, r.regionname;
REGIONID REGIONNAME AMOUNTSPENT
N North 1520.00
C Chicagoland 1630.00
I Indiana 2170.00
T Tristate 3060.00
ExtE5.1.18 Display the TID and the total number of items (of all products) sold within the
transaction for all sales transactions whose total number of items (of all products) sold
within the transaction is greater than five.
select tid, sum(quantity)
from includes
group by tid
having sum(quantity) > 5;
TID SUM(QUANTITY)
T808 8
T888 7
T333 6
T022 8
T606 18
T555 7
T707 7
T303 9
T505 8
T999 10
Copyright (c) 2020 Nenad Jukic and Prospect Press IM Chapter 05 - Page 26
Database Systems, Edition 2.0 (Jukic et. al.) Instructors Manual – Chapter 05
ExtE5.1.19 For each vendor whose product sales exceed $700, display the VendorID,
VendorName, and total amount of sales in dollars. Display the total amount of sales as
TotalSales.
select v.vendorid, v.vendorname,
sum(i.quantity*p.productprice) as totalsales
from includes i, product p, vendor v
where i.productid = p.productid and
p.vendorid = v.vendorid
group by v.vendorid, v.vendorname
having sum(i.quantity*p.productprice) > 700;
VENDORID VENDORNAME TOTALSALES
PG Pacifica Gear 1385.00
MK Mountain King 3590.00
OA Outdoor Adventures 1405.00
WL Wilderness Limited 2000.00
ExtE5.1.20 Display the ProductID, ProductName, and ProductPrice of the cheapest product.
select productid, productname, productprice
from product
where productprice = (select min(productprice)
from product);
PRODUCTID PRODUCTNAME PRODUCTPRICE
3X3 Cosy Sock 15.00
ExtE5.1.21 Display the ProductID, ProductName, and VendorName for products whose price is
below the average price of all products.
select p.productid, p.productname, v.vendorname
from product p, vendor v
where p.vendorid = v.vendorid
and productprice < (select avg(productprice) from product);
PRODUCTID PRODUCTNAME VENDORNAME
5X2 Action Sandal Pacifica Gear
5X1 Simple Sandal Pacifica Gear
2X3 Reflect-o Jacket Pacifica Gear
1X4 Safe-T Helmet Pacifica Gear
4X4 Dura Boot Pacifica Gear
1X1 Zzz Bag Pacifica Gear
4X2 Electra Compass Mountain King
2X4 Strongster Carribeaner Mountain King
8X8 Power Pedals Mountain King
3X3 Cosy Sock Mountain King
2X2 Easy Boot Mountain King
4X1 Slicky Tire Outdoor Adventures
3X4 Treado Tire Outdoor Adventures
Copyright (c) 2020 Nenad Jukic and Prospect Press IM Chapter 05 - Page 27
Database Systems, Edition 2.0 (Jukic et. al.) Instructors Manual – Chapter 05
3X2 Bucky Knife Wilderness Limited
3X1 Sleepy Pad Wilderness Limited
2X1 Mmm Stove Wilderness Limited
9X9 Trusty Rope Wilderness Limited
ExtE5.1.22 Display the ProductID and ProductName of the product for the products whose total
quantity sold in all transactions is greater than 2. Sort the results by ProductID.
select p.productid, p.productname
from product p, includes i
where i.productid = p.productid
group by p.productid, p.productname
having sum(i.quantity) > 2
order by productid;
PRODUCTID PRODUCTNAME
1X2 Comfy Harness
1X3 Sunny Charger
1X4 Safe-T Helmet
2X1 Mmm Stove
2X2 Easy Boot
2X3 Reflect-o Jacket
2X4 Strongster Carribeaner
3X1 Sleepy Pad
3X3 Cosy Sock
3X4 Treado Tire
4X2 Electra Compass
4X3 Mega Camera
4X4 Dura Boot
8X8 Power Pedals
9X9 Trusty Rope
ExtE5.1.23 Display the ProductID for the product that has been sold the most within all
transactions (i.e., that has the highest total quantity sold summarized across all transactions).
select productid
from includes
group by productid
having sum(quantity) = (select max(A)
from (select sum(quantity) as A from includes
group by productid) as B);
/* In Postgres, nested queries used in the “from” part need aliases, hence in the version above we use the letter B as alias for the nested query at
the end. That same alias would not be necessary in Oracle for example, as shown below */
select productid
from includes
group by productid
having sum(quantity) = (select max(A)
Copyright (c) 2020 Nenad Jukic and Prospect Press IM Chapter 05 - Page 28
Database Systems, Edition 2.0 (Jukic et. al.) Instructors Manual – Chapter 05
from (select sum(quantity) as A from includes
group by productid));
PRODUCTID
2X2
ExtE5.1.24 Rewrite Query 30 using a join statement (no nested queries).
select p.productid, p.productname, p.productprice
from product p, includes s
where p.productid = s.productid
group by p.productid, p.productname, p.productprice
having sum(s.quantity) > 3;
PRODUCTID PRODUCTNAME PRODUCTPRICE
4X3 Mega Camera 275.00
2X2 Easy Boot 70.00
9X9 Trusty Rope 30.00
4X4 Dura Boot 90.00
2X1 Mmm Stove 80.00
2X3 Reflect-o Jacket 35.00
3X4 Treado Tire 30.00
3X3 Cosy Sock 15.00
1X4 Safe-T Helmet 40.00
8X8 Power Pedals 20.00
3X1 Sleepy Pad 25.00
2X4 Strongster Carribeaner 20.00
4X2 Electra Compass 45.00
1X2 Comfy Harness 150.00
ExtE5.1.25 Rewrite Query 31 using a join statement (no nested queries).
select p.productid, p.productname, p.productprice
from product p, includes s
where p.productid = s.productid
group by p.productid, p.productname, p.productprice
having count(s.tid) > 1;
PRODUCTID PRODUCTNAME PRODUCTPRICE
4X3 Mega Camera 275.00
2X2 Easy Boot 70.00
9X9 Trusty Rope 30.00
4X4 Dura Boot 90.00
3X2 Bucky Knife 60.00
2X1 Mmm Stove 80.00
2X3 Reflect-o Jacket 35.00
3X4 Treado Tire 30.00
1X3 Sunny Charger 125.00
3X3 Cosy Sock 15.00
Copyright (c) 2020 Nenad Jukic and Prospect Press IM Chapter 05 - Page 29
Database Systems, Edition 2.0 (Jukic et. al.) Instructors Manual – Chapter 05
6X6 Biggy Tent 250.00
1X4 Safe-T Helmet 40.00
8X8 Power Pedals 20.00
3X1 Sleepy Pad 25.00
2X4 Strongster Carribeaner 20.00
4X2 Electra Compass 45.00
1X1 Zzz Bag 100.00
1X2 Comfy Harness 150.00
ExtE5.2.1 Display all the records in the table STAFFMEMBER.
select *
from staffmember;
SMEMBERID SMEMBERNAME
5432 Brian
9876 Boris
7652 Caroline
2537 Howard
3984 Luis
4196 Arthur
8467 Mariana
1028 Franz
ExtE5.2.2 Display the BuildingID and number of floors in the building for all buildings.
select buildingid, bnooffloors
from building;
BUILDINGID BNOOFFLOORS
B1 5
B2 6
B3 4
B4 4
B5 3
B6 3
B7 2
B8 4
B9 3
ExtE5.2.3 Display the CCID, CCName, and CCIndustry for all corporate clients, sorted
alphabetically by CCName.
select ccid, ccname, ccindustry
from corpclient
order by ccname;
CCID CCNAME CCINDUSTRY
Copyright (c) 2020 Nenad Jukic and Prospect Press IM Chapter 05 - Page 30
Database Systems, Edition 2.0 (Jukic et. al.) Instructors Manual – Chapter 05
C111 BlingNotes Music
C999 CommuteAir Airline
C666 DelishInc Food Service
C555 EntertainUs Broadcasting
C444 NanoCorp Broadcasting
C222 SkyJet Airline
C888 SouthAlps Sports
C777 WindyCT Music
C333 Xilerate Sports
ExtE5.2.4 Display all the records in the table STAFF MEMBER for staff members whose
SMemberName begins with a letter B.
select *
from staffmember
where smembername like 'B%';
SMEMBERID SMEMBERNAME
5432 Brian
9876 Boris
ExtE5.2.5 Display the BuildingID, AptNo, and ANoOfBedrooms for all apartments that have
more than one bedroom.
select buildingid, aptno, anoofbedrooms
from apartment
where anoofbedrooms > 1;
BUILDINGID APTNO ANOOFBEDROOMS
B2 11 2
B2 21 2
B2 31 2
B2 41 2
B2 51 2
B2 61 2
B3 11 2
B3 21 2
B3 31 2
B3 41 2
B4 11 2
B4 21 2
B4 31 2
B4 41 2
B5 11 3
B5 21 3
B5 31 3
B7 11 3
Copyright (c) 2020 Nenad Jukic and Prospect Press IM Chapter 05 - Page 31
Database Systems, Edition 2.0 (Jukic et. al.) Instructors Manual – Chapter 05
B7 12 3
B7 13 3
B7 21 3
B7 22 3
B7 23 3
B8 11 2
B8 12 2
B8 21 2
B8 22 2
B8 31 2
B8 32 2
B8 41 2
B8 42 2
B9 11 2
B9 21 2
B9 31 2
ExtE5.2.6 Display the answer to the following question: How many HAFH buildings have
exactly 4 floors?
select count(*)
from building
where bnooffloors = 4;
COUNT
3
ExtE5.2.7 Display the total amount HAFH spends on manager salaries (as TotalSalary) and the
total amount HAFH spends on manager bonuses (as TotalBonus).
select sum(msalary) as totalsalary, sum(mbonus) as totalbonus
from manager;
TOTALSALARY TOTALBONUS
335000.00 12000.00
ExtE5.2.8 Display the ManagerID, MFName, MLName, MSalary, and MBonus, for all
managers with a salary greater than $50,000 and a bonus greater than $1,000.
select managerid, mfname, mlname, msalary, mbonus
from manager
where msalary > 50000 and mbonus > 1000;
MANAGERID MFNAME MLNAME MSALARY MBONUS
M34 George Sherman 52000.00 2000.00
M56 Fiona Keane 57000.00 2000.00
M67 Alexander Sanborn 62000.00 3000.00
Copyright (c) 2020 Nenad Jukic and Prospect Press IM Chapter 05 - Page 32
Database Systems, Edition 2.0 (Jukic et. al.) Instructors Manual – Chapter 05
ExtE5.2.9 Display the BuildingID, BNoOfFloors, and the manager’s MFName and MLName for
all buildings.
select b.buildingid, b.bnooffloors, m.mfname, m.mlname
from building b, manager m
where b.bmanagerid = m.managerid;
BUILDINGID BNOOFFLOORS MFNAME MLNAME
B1 5 Boris Grant
B2 6 Austin Lee
B3 4 Austin Lee
B4 4 George Sherman
B5 3 Mariana Gonzalez
B6 3 Mariana Gonzalez
B7 2 Fiona Keane
B8 4 Alexander Sanborn
B9 3 Alexander Sanborn
ExtE5.2.10 Display the MFName, MLName, MSalary, MBDate, and number of buildings that
the manager manages for all managers with a salary less than $55,000.
select m.mfname, m.mlname, m.msalary, m.mbdate, count(*)
from building b, manager m
where b.bmanagerid = m.managerid AND
m.msalary < 55000
group by m.mfname, m.mlname, m.msalary, m.mbdate;
MFNAME MLNAME MSALARY MBDATE COUNT(*)
George Sherman 52000.00 11-JAN-1976 1
Mariana Gonzalez 54000.00 27-DEC-1980 2
Austin Lee 50000.00 30-OCT-1975 2
ExtE5.2.11 Display the BuildingID and AptNo, for all apartments leased by the corporate client
WindyCT.
select a.buildingid, a.aptno
from apartment a, corpclient c
where a.ccid = c.ccid AND
c.ccname = 'WindyCT';
BUILDINGID APTNO
B3 11
B3 21
B4 11
B4 21
B8 11
B8 12
Copyright (c) 2020 Nenad Jukic and Prospect Press IM Chapter 05 - Page 33
Database Systems, Edition 2.0 (Jukic et. al.) Instructors Manual – Chapter 05
ExtE5.2.12 Display the InsID and InsName for all inspectors whose next inspection is scheduled
after 1-APR-2020. Do not display the same information more than once.
select distinct ir.insid, ir.insname
from inspector ir, inspecting ig
where ir.insid = ig.insid AND
ig.datenext > '04-01-2020';
INSID INSNAME
I11 Jane
I33 Mick
I44 Bianca
I55 Sergei
ExtE5.2.13 Display the ManagerID, MFName, MLName, and MSalary, for all managers with a
salary greater than the average manager’s salary.
select managerid, mfname, mlname, msalary
from manager
where msalary > (select avg(msalary)
from manager);
MANAGERID MFNAME MLNAME MSALARY
M12 Boris Grant 60000.00
M56 Fiona Keane 57000.00
M67 Alexander Sanborn 62000.00
ExtE5.2.14 Display the ManagerID, MFName, MLName, and the number of buildings managed,
for all managers.
select m.managerid, m.mfname, m.mlname, count(*)
from manager m, building b
where b.bmanagerid = m.managerid
group by m.managerid, m.mfname, m.mlname;
MANAGERID MFNAME MLNAME COUNT
M23 Austin Lee 2
M12 Boris Grant 1
M34 George Sherman 1
M56 Fiona Keane 1
M45 Mariana Gonzalez 2
M67 Alexander Sanborn 2
ExtE5.2.15 Display the ManagerID, MFName, MLName, and number of buildings managed, for
all managers that manage more than one building.
Copyright (c) 2020 Nenad Jukic and Prospect Press IM Chapter 05 - Page 34
Database Systems, Edition 2.0 (Jukic et. al.) Instructors Manual – Chapter 05
select m.managerid, m.mfname, m.mlname, count(*)
from manager m, building b
where b.bmanagerid = m.managerid
group by m.managerid, m.mfname, m.mlname
having count(*) > 1;
MANAGERID MFNAME MLNAME COUNT
M23 Austin Lee 2
M45 Mariana Gonzalez 2
M67 Alexander Sanborn 2
ExtE5.2.16 Display the SMemberID, SMemberName, and the number of apartments that the
staff members cleans, for all staff members.
select s.smemberid, s.smembername, count(*)
from staffmember s, cleaning c
where s.smemberid = c.smemberid
group by s.smemberid, s.smembername;
SMEMBERID SMEMBERNAME COUNT
7652 Caroline 6
4196 Arthur 6
2537 Howard 4
1028 Franz 8
8467 Mariana 5
5432 Brian 7
3984 Luis 3
9876 Boris 6
ExtE5.2.17 Display the SMemberID and SMemberName of staff members cleaning apartments
rented by corporate clients whose corporate location is Chicago. Do not display the same
information more than once.
select distinct s.smemberid, s.smembername
from staffmember s, cleaning cl, apartment a, corpclient cc
where s.smemberid = cl.smemberid and
a.aptno = cl.aptno and
a.buildingid = cl.buildingid and
a.ccid = cc.ccid and
cc.cclocation = 'Chicago';
SMEMBERID SMEMBERNAME
1028 Franz
2537 Howard
3984 Luis
4196 Arthur
5432 Brian
7652 Caroline
8467 Mariana
Copyright (c) 2020 Nenad Jukic and Prospect Press IM Chapter 05 - Page 35
Database Systems, Edition 2.0 (Jukic et. al.) Instructors Manual – Chapter 05
ExtE5.2.18 Display the CCName of the client and the CCName of the client who referred it, for
every client referred by a client in the music industry.
select c1.ccname client, c2.ccname referredby
from corpclient c1, corpclient c2
where c2.ccid = c1.ccidreferredby and
c2.ccindustry = 'Music';
CLIENT REFERREDBY
SkyJet BlingNotes
NanoCorp BlingNotes
SouthAlps WindyCT
CommuteAir BlingNotes
ExtE5.2.19 Display the BuildingID, AptNo, and ANoOfBedrooms for all apartments that are not
leased.
select buildingid, aptno, anoofbedrooms
from apartment
where ccid is null;
BUILDINGID APTNO ANOOFBEDROOMS
B1 41 1
B1 51 1
B2 31 2
B2 41 2
B5 21 3
B7 21 3
B8 31 2
B8 32 2
ExtE5.2.20 Display the ManagerID, MFName, MLName, MSalary, and MBonus, for the
manager with the lowest total compensation (total compensation is defined as the salary plus
bonus).
select managerid, mfname, mlname, msalary, COALESCE(mbonus,0) as mgrbonus
from manager
where msalary+COALESCE(mbonus,0) = (select min(msalary+COALESCE(mbonus,0))
from manager);
MANAGERID MFNAME MLNAME MSALARY MGRBONUS
M34 George Sherman 52000 2000
M45 Mariana Gonzalez 54000 0
Copyright (c) 2020 Nenad Jukic and Prospect Press IM Chapter 05 - Page 36
Database Systems, Edition 2.0 (Jukic et. al.) Instructors Manual – Chapter 05
Chapter 5 - ANSWERS (Mini Cases)
MC1 Advance Science Incorporated
ER Diagram
Relational Schema
CREATE TABLE statements
CREATE TABLE employee
(
eid CHAR(3) NOT NULL,
ename VARCHAR NOT NULL,
PRIMARY KEY (eid)
);
CREATE TABLE facility
(
facilityid CHAR(3) NOT NULL,
facilitytype VARCHAR NOT NULL,
PRIMARY KEY (facilityid)
Copyright (c) 2020 Nenad Jukic and Prospect Press IM Chapter 05 - Page 37
Database Systems, Edition 2.0 (Jukic et. al.) Instructors Manual – Chapter 05
);
CREATE TABLE device
(
deviceid CHAR(3) NOT NULL,
devicetype VARCHAR NOT NULL,
eid CHAR(3) NOT NULL,
PRIMARY KEY (deviceid),
FOREIGN KEY (eid) REFERENCES employee(eid)
);
CREATE TABLE hasaccessto
(
eid CHAR(3) NOT NULL,
facilityid CHAR(3) NOT NULL,
PRIMARY KEY (eid, facilityid),
FOREIGN KEY (eid) REFERENCES employee(eid),
FOREIGN KEY (facilityid) REFERENCES facility(facilityid)
);
INSERT INTO statements
INSERT INTO employee VALUES ('E11','Chris');
INSERT INTO employee VALUES ('E22','Lauren');
INSERT INTO employee VALUES ('E33','Moe');
INSERT INTO facility VALUES ('F11','Laboratory');
INSERT INTO facility VALUES ('F22','Storage');
INSERT INTO facility VALUES ('F33','Manufacturing');
INSERT INTO device VALUES ('D01','Company Phone','E33');
INSERT INTO device VALUES ('D02','Centrifuge','E22');
INSERT INTO device VALUES ('D03','Calorimeter','E22');
INSERT INTO hasaccessto VALUES ('E11','F22');
INSERT INTO hasaccessto VALUES ('E11','F33');
INSERT INTO hasaccessto VALUES ('E22','F11');
INSERT INTO hasaccessto VALUES ('E33','F11');
INSERT INTO hasaccessto VALUES ('E33','F22');
Copyright (c) 2020 Nenad Jukic and Prospect Press IM Chapter 05 - Page 38
Database Systems, Edition 2.0 (Jukic et. al.) Instructors Manual – Chapter 05
MC2 Investco Scout
ER Diagram
Relational Schema
CREATE TABLE statements
CREATE TABLE investmentcompany
(
icid CHAR(4) NOT NULL,
icname VARCHAR NOT NULL,
PRIMARY KEY (icid),
UNIQUE (icname)
);
CREATE TABLE mutualfund
(
mfid CHAR(4) NOT NULL,
mfname VARCHAR NOT NULL,
mfincdate DATE NOT NULL,
icid CHAR(4) NOT NULL,
PRIMARY KEY (mfid),
Copyright (c) 2020 Nenad Jukic and Prospect Press IM Chapter 05 - Page 39
Database Systems, Edition 2.0 (Jukic et. al.) Instructors Manual – Chapter 05
FOREIGN KEY (icid) REFERENCES investmentcompany(icid)
);
CREATE TABLE security
(
securityid CHAR(3) NOT NULL,
securityname VARCHAR NOT NULL,
securitytype VARCHAR NOT NULL,
PRIMARY KEY (securityid)
);
CREATE TABLE contains
(
mfid CHAR(4) NOT NULL,
securityid CHAR(3) NOT NULL,
amount INT NOT NULL,
PRIMARY KEY (mfid, securityid),
FOREIGN KEY (mfid) REFERENCES mutualfund(mfid),
FOREIGN KEY (securityid) REFERENCES security(securityid)
);
CREATE TABLE iclocation
(
icid CHAR(4) NOT NULL,
iclocation VARCHAR NOT NULL,
PRIMARY KEY (iclocation, icid),
FOREIGN KEY (icid) REFERENCES investmentcompany(icid)
);
ALTER TABLE investmentcompany ADD ceofname VARCHAR NOT NULL;
ALTER TABLE investmentcompany ADD ceolname VARCHAR NOT NULL;
INSERT INTO statements
INSERT INTO investmentcompany VALUES ('ACF', 'Acme Finance', 'Mick', 'Dempsey');
INSERT INTO investmentcompany VALUES ('TCA', 'Tara Capital', 'Ava', 'Newton');
INSERT INTO investmentcompany VALUES ('ALB', 'Albritton', 'Lena', 'Dollar');
INSERT INTO mutualfund VALUES ('BG', 'Big Growth', '1/Jan/2005', 'ACF');
INSERT INTO mutualfund VALUES ('SG', 'Steady Growth', '1/Jan/2006', 'ACF');
INSERT INTO mutualfund VALUES ('LF', 'Tiger Fund', '1/Jan/2005', 'TCA');
INSERT INTO mutualfund VALUES ('OF', 'Owl Fund', '1/Jan/2006', 'TCA');
INSERT INTO mutualfund VALUES ('JU', 'Jupiter', '1/Jan/2005', 'ALB');
INSERT INTO mutualfund VALUES ('SA', 'Saturn', '1/Jan/2006', 'ALB');
INSERT INTO security VALUES ('AE', 'Abhi Engineering', 'Stock');
INSERT INTO security VALUES ('BH', 'Blues Health', 'Stock');
INSERT INTO security VALUES ('CM', 'County Municipality', 'Bond');
INSERT INTO security VALUES ('DU', 'Downtown Utility', 'Bond');
INSERT INTO security VALUES ('EM', 'Emmitt Machines', 'Stock');
INSERT INTO contains VALUES ('BG', 'AE', 500);
INSERT INTO contains VALUES ('BG', 'EM', 300);
INSERT INTO contains VALUES ('SG', 'AE', 300);
INSERT INTO contains VALUES ('SG', 'DU', 300);
Copyright (c) 2020 Nenad Jukic and Prospect Press IM Chapter 05 - Page 40
Database Systems, Edition 2.0 (Jukic et. al.) Instructors Manual – Chapter 05
INSERT INTO contains VALUES ('LF', 'EM', 1000);
INSERT INTO contains VALUES ('LF', 'BH', 1000);
INSERT INTO contains VALUES ('OF', 'CM', 1000);
INSERT INTO contains VALUES ('OF', 'DU', 1000);
INSERT INTO contains VALUES ('JU', 'EM', 2000);
INSERT INTO contains VALUES ('JU', 'DU', 1000);
INSERT INTO contains VALUES ('SA', 'EM', 1000);
INSERT INTO contains VALUES ('SA', 'DU', 2000);
INSERT INTO iclocation VALUES ('ACF', 'Chicago');
INSERT INTO iclocation VALUES ('ACF', 'Denver');
INSERT INTO iclocation VALUES ('TCA', 'Houston');
INSERT INTO iclocation VALUES ('TCA', 'New York City');
INSERT INTO iclocation VALUES ('ALB', 'Atlanta');
INSERT INTO iclocation VALUES ('ALB', 'New York City');
Copyright (c) 2020 Nenad Jukic and Prospect Press IM Chapter 05 - Page 41
Database Systems, Edition 2.0 (Jukic et. al.) Instructors Manual – Chapter 05
MC3 Funky Bizz
ER Diagram
Relational Schema
CREATE TABLE statements
CREATE TABLE band
(
BandID CHAR(3) NOT NULL,
BandName VARCHAR NOT NULL,
BandAddress VARCHAR NOT NULL,
BandContactPerson VARCHAR NOT NULL,
PRIMARY KEY (BandID),
Copyright (c) 2020 Nenad Jukic and Prospect Press IM Chapter 05 - Page 42
Database Systems, Edition 2.0 (Jukic et. al.) Instructors Manual – Chapter 05
UNIQUE (BandName)
);
CREATE TABLE repairtechnician
(
RTSSN CHAR(9) NOT NULL,
RTName VARCHAR NOT NULL,
RTAddress VARCHAR NOT NULL,
PRIMARY KEY (RTSSN)
);
CREATE TABLE show
(
ShowVenue VARCHAR NOT NULL,
ShowDate DATE NOT NULL,
ShowType VARCHAR NOT NULL,
ShowName VARCHAR,
PRIMARY KEY (ShowVenue, ShowDate)
);
CREATE TABLE performsin
(
BandID CHAR(3) NOT NULL,
ShowVenue VARCHAR NOT NULL,
ShowDate DATE NOT NULL,
DurationInMinutes INT NOT NULL,
PRIMARY KEY (BandID, ShowVenue, ShowDate),
FOREIGN KEY (BandID) REFERENCES band(BandID),
FOREIGN KEY (ShowVenue, ShowDate) REFERENCES show(ShowVenue, ShowDate)
);
CREATE TABLE bandphone
(
BandPhone CHAR(10) NOT NULL,
BandID CHAR(3) NOT NULL,
PRIMARY KEY (BandPhone, BandID),
FOREIGN KEY (BandID) REFERENCES band(BandID)
);
CREATE TABLE rtphone
(
RTPhone CHAR(10) NOT NULL,
RTSSN CHAR(9) NOT NULL,
PRIMARY KEY (RTPhone, RTSSN),
FOREIGN KEY (RTSSN) REFERENCES repairtechnician(RTSSN)
);
CREATE TABLE instrument
(
InsSerialNo VARCHAR NOT NULL,
InsModel VARCHAR NOT NULL,
InsBrand VARCHAR NOT NULL,
InsYearMade INT NOT NULL,
BandID CHAR(3),
RTSSN CHAR(9) NOT NULL,
PRIMARY KEY (InsSerialNo),
FOREIGN KEY (BandID) REFERENCES band(BandID),
Copyright (c) 2020 Nenad Jukic and Prospect Press IM Chapter 05 - Page 43
Database Systems, Edition 2.0 (Jukic et. al.) Instructors Manual – Chapter 05
FOREIGN KEY (RTSSN) REFERENCES repairtechnician(RTSSN)
);
INSERT INTO statements
INSERT INTO band VALUES ('B11','The Flags','612 Morris St','Greg Rollins');
INSERT INTO band VALUES ('B22','Blood Dogs','739 Belmont Blvd','Jimmy Boon');
INSERT INTO band VALUES ('B33','Ian and the Threats','123 Dischord Ave','Ian
Fugazi');
INSERT INTO repairtechnician VALUES ('777777777','Aaron','59 Leo St');
INSERT INTO repairtechnician VALUES ('888888888','Ryan','200 Dove Rd');
INSERT INTO show VALUES ('Pub and Grub','26-May-20','Multi-Band Event',NULL);
INSERT INTO show VALUES ('Pub and Grub','15-Jun-20','Single-Band Event',NULL);
INSERT INTO show VALUES ('Townes Center','05-May-20','Charity Event','Rocking For
Education');
INSERT INTO show VALUES ('Townes Center','01-Jun-20','Charity Event','School Aid');
INSERT INTO show VALUES ('Townes Center','01-Jul-20','Charity Event','Townes Center
Food Drive');
INSERT INTO show VALUES ('The Mill Club','12-Jun-20','Open Mic',NULL);
INSERT INTO show VALUES ('Gilly Park Plaza','01-Jun-20','Festival','Gilly Park
Fest');
INSERT INTO performsin VALUES ('B11','Pub and Grub','26-May-20',60);
INSERT INTO performsin VALUES ('B33','Pub and Grub','26-May-20',60);
INSERT INTO performsin VALUES ('B11','Pub and Grub','15-Jun-20',90);
INSERT INTO performsin VALUES ('B33','Townes Center','05-May-20',120);
INSERT INTO performsin VALUES ('B22','Townes Center','01-Jun-20',60);
INSERT INTO performsin VALUES ('B11','Townes Center','01-Jul-20',30);
INSERT INTO performsin VALUES ('B33','Townes Center','01-Jul-20',30);
INSERT INTO performsin VALUES ('B22','The Mill Club','12-Jun-20',90);
INSERT INTO performsin VALUES ('B22','Gilly Park Plaza','01-Jun-20',45);
INSERT INTO performsin VALUES ('B33','Gilly Park Plaza','01-Jun-20',45);
INSERT INTO bandphone VALUES ('5551234567','B11');
INSERT INTO bandphone VALUES ('5551112222','B22');
INSERT INTO bandphone VALUES ('5552221111','B22');
INSERT INTO bandphone VALUES ('5553334444','B33');
INSERT INTO bandphone VALUES ('5554443333','B33');
INSERT INTO rtphone VALUES ('5559990000','777777777');
INSERT INTO rtphone VALUES ('5550009999','777777777');
INSERT INTO rtphone VALUES ('5559991111','888888888');
INSERT INTO instrument VALUES ('1234','Borg','Z2000',2005,'B22','777777777');
INSERT INTO instrument VALUES
('3456','Bender','Mellowcaster',1998,'B33','777777777');
INSERT INTO instrument VALUES ('5678','Vartin Guitars','Folk
Classic',2013,'B22','888888888');
INSERT INTO instrument VALUES ('4321','Speedy
Axes','Thrasher',2017,'B11','777777777');
INSERT INTO instrument VALUES ('5432','Atlantic Drums','Jazz Club
Pro',1991,'B22','888888888');
Copyright (c) 2020 Nenad Jukic and Prospect Press IM Chapter 05 - Page 44
Database Systems, Edition 2.0 (Jukic et. al.) Instructors Manual – Chapter 05
MC4 Snooty Fashions
ER Diagram
Relational Schema
CREATE TABLE statements
CREATE TABLE customer
(
CustID CHAR(3) NOT NULL,
CustName VARCHAR NOT NULL,
PRIMARY KEY (CustID)
);
CREATE TABLE fashionshow
(
FSID CHAR(3) NOT NULL,
FSDate DATE NOT NULL,
FSLocation VARCHAR NOT NULL,
PRIMARY KEY (FSID)
);
Copyright (c) 2020 Nenad Jukic and Prospect Press IM Chapter 05 - Page 45
Database Systems, Edition 2.0 (Jukic et. al.) Instructors Manual – Chapter 05
CREATE TABLE tailoringtechnician
(
TTSSN CHAR(9) NOT NULL,
TTLname VARCHAR NOT NULL,
TTFname VARCHAR NOT NULL,
PRIMARY KEY (TTSSN)
);
CREATE TABLE designer
(
DesID CHAR(3) NOT NULL,
DesSSN CHAR(9) NOT NULL,
DesFname VARCHAR NOT NULL,
DesLname VARCHAR NOT NULL,
PRIMARY KEY (DesID),
UNIQUE (DesSSN)
);
CREATE TABLE participatesin
(
DesID CHAR(3) NOT NULL,
FSID CHAR(3) NOT NULL,
PRIMARY KEY (DesID, FSID),
FOREIGN KEY (DesID) REFERENCES designer(DesID),
FOREIGN KEY (FSID) REFERENCES fashionshow(FSID)
);
CREATE TABLE customerphone
(
CustPhone CHAR(10) NOT NULL,
CustID CHAR(3) NOT NULL,
PRIMARY KEY (CustPhone, CustID),
FOREIGN KEY (CustID) REFERENCES customer(CustID)
);
CREATE TABLE outfit
(
OutfitID CHAR(3) NOT NULL,
OutfitPrice INT NOT NULL,
OutfitPDOC DATE NOT NULL,
CustID CHAR(3) NOT NULL,
DesID CHAR(3) NOT NULL,
PRIMARY KEY (OutfitID),
FOREIGN KEY (CustID) REFERENCES customer(CustID),
FOREIGN KEY (DesID) REFERENCES designer(DesID)
);
CREATE TABLE workson
(
OutfitID CHAR(3) NOT NULL,
TTSSN CHAR(9) NOT NULL,
DateStarted DATE NOT NULL,
PRIMARY KEY (OutfitID, TTSSN),
FOREIGN KEY (OutfitID) REFERENCES outfit(OutfitID),
FOREIGN KEY (TTSSN) REFERENCES tailoringtechnician(TTSSN)
);
Copyright (c) 2020 Nenad Jukic and Prospect Press IM Chapter 05 - Page 46
Database Systems, Edition 2.0 (Jukic et. al.) Instructors Manual – Chapter 05
INSERT INTO statements
INSERT INTO customer VALUES ('C01', 'Jeff');
INSERT INTO customer VALUES ('C02', 'Bill');
INSERT INTO customer VALUES ('C03', 'Ruth');
INSERT INTO customer VALUES ('C04', 'Nancy');
INSERT INTO fashionshow VALUES ('FS1','22-Nov-19','San Francisco');
INSERT INTO fashionshow VALUES ('FS2','13-Dec-19','New York');
INSERT INTO fashionshow VALUES ('FS3','20-Dec-19','Chicago');
INSERT INTO fashionshow VALUES ('FS4','15-Jan-20','Los Angeles');
INSERT INTO tailoringtechnician VALUES ('111111111','Sally','Swanson');
INSERT INTO tailoringtechnician VALUES ('222222222','Tim','Tillman');
INSERT INTO designer VALUES ('D10','333333333','Kelly','Klein');
INSERT INTO designer VALUES ('D20','444444444','David','Danson');
INSERT INTO designer VALUES ('D30','555555555','Nora','Neils');
INSERT INTO participatesin VALUES ('D10','FS1');
INSERT INTO participatesin VALUES ('D10','FS3');
INSERT INTO participatesin VALUES ('D10','FS4');
INSERT INTO participatesin VALUES ('D20','FS2');
INSERT INTO participatesin VALUES ('D20','FS3');
INSERT INTO participatesin VALUES ('D30','FS2');
INSERT INTO participatesin VALUES ('D30','FS3');
INSERT INTO participatesin VALUES ('D30','FS4');
INSERT INTO customerphone VALUES ('5555551000', 'C01');
INSERT INTO customerphone VALUES ('5555552000', 'C02');
INSERT INTO customerphone VALUES ('5555553000', 'C02');
INSERT INTO customerphone VALUES ('5555554000', 'C03');
INSERT INTO customerphone VALUES ('5555555000', 'C04');
INSERT INTO outfit VALUES ('OF1',100,'1-Oct-19','C01','D10');
INSERT INTO outfit VALUES ('OF2',150,'1-Oct-19','C04','D30');
INSERT INTO outfit VALUES ('OF3',300,'12-Oct-19','C03','D10');
INSERT INTO outfit VALUES ('OF4',225,'15-Oct-19','C01','D20');
INSERT INTO outfit VALUES ('OF5',75,'21-Oct-19','C02','D30');
INSERT INTO outfit VALUES ('OF6',300,'25-Oct-19','C04','D10');
INSERT INTO workson VALUES ('OF1','111111111','1-Aug-19');
INSERT INTO workson VALUES ('OF2','111111111','1-Aug-19');
INSERT INTO workson VALUES ('OF2','222222222','17-Aug-19');
INSERT INTO workson VALUES ('OF3','222222222','19-Aug-19');
INSERT INTO workson VALUES ('OF4','111111111','20-Aug-19');
INSERT INTO workson VALUES ('OF4','222222222','20-Aug-19');
INSERT INTO workson VALUES ('OF5','111111111','23-Aug-19');
INSERT INTO workson VALUES ('OF5','222222222','27-Aug-19');
INSERT INTO workson VALUES ('OF6','222222222','30-Aug-19');
Copyright (c) 2020 Nenad Jukic and Prospect Press IM Chapter 05 - Page 47
Database Systems, Edition 2.0 (Jukic et. al.) Instructors Manual – Chapter 05
MC5 Signum Libri
ER Diagram
Relational Schema
CREATE TABLE statements
CREATE TABLE agent
(
AgentID CHAR(3) NOT NULL,
AgentName VARCHAR NOT NULL,
PRIMARY KEY (AgentID)
);
CREATE TABLE writer
(
WriterID CHAR(3) NOT NULL,
WriterName VARCHAR NOT NULL,
AgentID CHAR(3) NOT NULL,
PRIMARY KEY (WriterID),
FOREIGN KEY (AgentID) REFERENCES agent(AgentID)
Copyright (c) 2020 Nenad Jukic and Prospect Press IM Chapter 05 - Page 48
Database Systems, Edition 2.0 (Jukic et. al.) Instructors Manual – Chapter 05
);
CREATE TABLE editor
(
EditorID CHAR(3) NOT NULL,
EditorName VARCHAR NOT NULL,
MentorEditorID CHAR(3),
PRIMARY KEY (EditorID),
FOREIGN KEY (MentorEditorID) REFERENCES editor(EditorID)
);
CREATE TABLE book
(
BookName VARCHAR NOT NULL,
WriterID CHAR(3) NOT NULL,
BookGenre VARCHAR NOT NULL,
BookDateOfPub DATE NOT NULL,
BookNoOfPages INT NOT NULL,
EditorID CHAR(3) NOT NULL,
PRIMARY KEY (BookName, WriterID),
FOREIGN KEY (WriterID) REFERENCES writer(WriterID),
FOREIGN KEY (EditorID) REFERENCES editor(EditorID)
);
INSERT INTO statements
INSERT INTO agent VALUES ('A11','Jeff');
INSERT INTO agent VALUES ('A22','Lucy');
INSERT INTO writer VALUES ('W10','Anne','A11');
INSERT INTO writer VALUES ('W11','Jack','A11');
INSERT INTO writer VALUES ('W12','Clive','A22');
INSERT INTO writer VALUES ('W13','Isaac','A22');
INSERT INTO editor VALUES ('E20','Melanie',NULL);
INSERT INTO editor VALUES ('E21','Elisha',NULL);
INSERT INTO editor VALUES ('E22','George','E21');
INSERT INTO editor VALUES ('E23','Marina','E20');
INSERT INTO book VALUES ('Valley of Heroes','W10','Fiction','12-Jan-17',874,'E20');
INSERT INTO book VALUES ('The Return of the Ruler','W11','Fantasy','14-Mar-
19',765,'E22');
INSERT INTO book VALUES ('An Letter from Uncle John','W12','Fiction','12-Jun-
20',258,'E20');
INSERT INTO book VALUES ('A Tale of Lions','W12','Fantasy','17-Aug-18',301,'E21');
INSERT INTO book VALUES ('eRobot','W13','Sci Fi','4-Oct-19',465,'E23');
Copyright (c) 2020 Nenad Jukic and Prospect Press IM Chapter 05 - Page 49
Database Systems, Edition 2.0 (Jukic et. al.) Instructors Manual – Chapter 05
MC6 Ava’s Consulting
ER Diagram
Relational Schema
CREATE TABLE statements
CREATE TABLE projectmanager
(
PMID CHAR(3) NOT NULL,
PMName VARCHAR NOT NULL,
PRIMARY KEY (PMID)
);
CREATE TABLE customer
(
CustID CHAR(5) NOT NULL,
CustFName VARCHAR NOT NULL,
CustLName VARCHAR NOT NULL,
Copyright (c) 2020 Nenad Jukic and Prospect Press IM Chapter 05 - Page 50
Database Systems, Edition 2.0 (Jukic et. al.) Instructors Manual – Chapter 05
CustPhone CHAR(10) NOT NULL,
PRIMARY KEY (CustID)
);
CREATE TABLE supervisor
(
SupID CHAR(2) NOT NULL,
SupName VARCHAR NOT NULL,
NofStocksOwned INT,
PRIMARY KEY (SupID)
);
CREATE TABLE contract
(
ContractID CHAR(5) NOT NULL,
ContractStartDate DATE NOT NULL,
CustID CHAR(5) NOT NULL,
PMID CHAR(3) NOT NULL,
SupID CHAR(2) NOT NULL,
PRIMARY KEY (ContractID, CustID),
FOREIGN KEY (CustID) REFERENCES customer(CustID),
FOREIGN KEY (PMID) REFERENCES projectmanager(PMID),
FOREIGN KEY (SupID) REFERENCES supervisor(SupID)
);
CREATE TABLE consultant
(
CID CHAR(5) NOT NULL,
CName VARCHAR NOT NULL,
StartDate DATE NOT NULL,
ContractID CHAR(5) NOT NULL,
CustID CHAR(5) NOT NULL,
PRIMARY KEY (CID),
FOREIGN KEY (ContractID, CustID) REFERENCES contract(ContractID, CustID)
);
CREATE TABLE assignedasadminhelp
(
CID CHAR(5) NOT NULL,
SupID CHAR(2) NOT NULL,
NoOfHoursHelping INT NOT NULL,
PRIMARY KEY (CID, SupID),
FOREIGN KEY (CID) REFERENCES consultant(CID),
FOREIGN KEY (SupID) REFERENCES supervisor(SupID)
);
CREATE TABLE consultantskill
(
Skill VARCHAR NOT NULL,
CID CHAR(5) NOT NULL,
PRIMARY KEY (Skill, CID),
FOREIGN KEY (CID) REFERENCES consultant(CID)
);
INSERT INTO statements
Copyright (c) 2020 Nenad Jukic and Prospect Press IM Chapter 05 - Page 51
Database Systems, Edition 2.0 (Jukic et. al.) Instructors Manual – Chapter 05
INSERT INTO projectmanager VALUES ('PM1','Jacob');
INSERT INTO projectmanager VALUES ('PM2','Lindsey');
INSERT INTO projectmanager VALUES ('PM3','James');
INSERT INTO projectmanager VALUES ('PM4','Janet');
INSERT INTO customer VALUES ('CUST1','Chris','Sims','5551113333');
INSERT INTO customer VALUES ('CUST2','Brenna','Carlson','5554448888');
INSERT INTO supervisor VALUES ('S1','Lucas',2);
INSERT INTO supervisor VALUES ('S2','Ashton',NULL);
INSERT INTO contract VALUES ('CONT1','19-Mar-20','CUST1','PM1','S2');
INSERT INTO contract VALUES ('CONT1','21-Apr-20','CUST2','PM3','S2');
INSERT INTO contract VALUES ('CONT2','30-May-20','CUST2','PM2','S1');
INSERT INTO consultant VALUES ('CONS1','Maria','13-Oct-19','CONT1','CUST1');
INSERT INTO consultant VALUES ('CONS2','Doug','10-Dec-19','CONT1','CUST1');
INSERT INTO consultant VALUES ('CONS3','Jared','24-Jan-20','CONT1','CUST1');
INSERT INTO consultant VALUES ('CONS4','Pam','9-Jan-20','CONT1','CUST2');
INSERT INTO consultant VALUES ('CONS5','Carla','1-Feb-20','CONT1','CUST2');
INSERT INTO consultant VALUES ('CONS6','Scarlett','14-Feb-20','CONT1','CUST2');
INSERT INTO consultant VALUES ('CONS7','Bobby','28-Feb-20','CONT2','CUST2');
INSERT INTO consultant VALUES ('CONS8','Noel','5-Mar-20','CONT2','CUST2');
INSERT INTO consultant VALUES ('CONS9','Ben','12-Mar-20','CONT2','CUST2');
INSERT INTO assignedasadminhelp VALUES ('CONS2','S1',25);
INSERT INTO assignedasadminhelp VALUES ('CONS3','S2',20);
INSERT INTO assignedasadminhelp VALUES ('CONS5','S1',40);
INSERT INTO assignedasadminhelp VALUES ('CONS7','S1',15);
INSERT INTO assignedasadminhelp VALUES ('CONS8','S2',30);
INSERT INTO consultantskill VALUES ('Legal','CONS1');
INSERT INTO consultantskill VALUES ('Financial','CONS2');
INSERT INTO consultantskill VALUES ('Marketing','CONS3');
INSERT INTO consultantskill VALUES ('Legal','CONS3');
INSERT INTO consultantskill VALUES ('Technical','CONS4');
INSERT INTO consultantskill VALUES ('Leadership','CONS5');
INSERT INTO consultantskill VALUES ('Technical','CONS6');
INSERT INTO consultantskill VALUES ('Legal','CONS7');
INSERT INTO consultantskill VALUES ('Marketing','CONS8');
INSERT INTO consultantskill VALUES ('Financial','CONS9');
Copyright (c) 2020 Nenad Jukic and Prospect Press IM Chapter 05 - Page 52
Database Systems, Edition 2.0 (Jukic et. al.) Instructors Manual – Chapter 05
MC7 ExoProtect
ER Diagram
Relational Schema
CREATE TABLE statements
CREATE TABLE softwarepackage
(
SPID CHAR(4) NOT NULL,
SPName VARCHAR NOT NULL,
PRIMARY KEY (SPID)
);
CREATE TABLE employee
Copyright (c) 2020 Nenad Jukic and Prospect Press IM Chapter 05 - Page 53
Database Systems, Edition 2.0 (Jukic et. al.) Instructors Manual – Chapter 05
(
EmpID CHAR(3) NOT NULL,
EmpName VARCHAR NOT NULL,
PRIMARY KEY (EmpID)
);
CREATE TABLE certifiedtouse
(
SPID CHAR(4) NOT NULL,
EmpID CHAR(3) NOT NULL,
PRIMARY KEY (SPID, EmpID),
FOREIGN KEY (SPID) REFERENCES softwarepackage(SPID),
FOREIGN KEY (EmpID) REFERENCES employee(EmpID)
);
CREATE TABLE employeeskill
(
EmpSkill VARCHAR NOT NULL,
EmpID CHAR(3) NOT NULL,
PRIMARY KEY (EmpSkill, EmpID),
FOREIGN KEY (EmpID) REFERENCES employee(EmpID)
);
CREATE TABLE computer
(
CompID CHAR(3) NOT NULL,
CompModel VARCHAR NOT NULL,
CompMake VARCHAR NOT NULL,
EmpID CHAR(3) NOT NULL,
MantainsEmpID CHAR(3) NOT NULL,
PRIMARY KEY (CompID),
FOREIGN KEY (EmpID) REFERENCES employee(EmpID),
FOREIGN KEY (MantainsEmpID) REFERENCES employee(EmpID)
);
CREATE TABLE installedat
(
SPID CHAR(4) NOT NULL,
CompID CHAR(3) NOT NULL,
DateOfInstall DATE NOT NULL,
PRIMARY KEY (SPID, CompID),
FOREIGN KEY (SPID) REFERENCES softwarepackage(SPID),
FOREIGN KEY (CompID) REFERENCES computer(CompID)
);
INSERT INTO statements
INSERT INTO softwarepackage VALUES ('SP11','Drive Wiper');
INSERT INTO softwarepackage VALUES ('SP22','File Finder');
INSERT INTO softwarepackage VALUES ('SP33','Encryption Wizard');
INSERT INTO softwarepackage VALUES ('SP44','Malware Eraser');
INSERT INTO employee VALUES ('E01','Tammy');
INSERT INTO employee VALUES ('E02','Aaron');
INSERT INTO employee VALUES ('E03','Bailey');
INSERT INTO employee VALUES ('E04','Chase');
Copyright (c) 2020 Nenad Jukic and Prospect Press IM Chapter 05 - Page 54
Database Systems, Edition 2.0 (Jukic et. al.) Instructors Manual – Chapter 05
INSERT INTO employee VALUES ('E05','Noah');
INSERT INTO certifiedtouse VALUES ('SP11','E02');
INSERT INTO certifiedtouse VALUES ('SP33','E02');
INSERT INTO certifiedtouse VALUES ('SP44','E02');
INSERT INTO certifiedtouse VALUES ('SP11','E03');
INSERT INTO certifiedtouse VALUES ('SP22','E03');
INSERT INTO certifiedtouse VALUES ('SP44','E04');
INSERT INTO certifiedtouse VALUES ('SP33','E05');
INSERT INTO employeeskill VALUES ('Customer Acquisition','E01');
INSERT INTO employeeskill VALUES ('Encryption','E02');
INSERT INTO employeeskill VALUES ('Backup and Recovery','E02');
INSERT INTO employeeskill VALUES ('Refurbishing','E03');
INSERT INTO employeeskill VALUES ('Startup Process','E03');
INSERT INTO employeeskill VALUES ('Backup and Recovery','E04');
INSERT INTO employeeskill VALUES ('Virus Protection','E04');
INSERT INTO employeeskill VALUES ('Encryption','E05');
INSERT INTO computer VALUES ('C10','Regen','Epsilon','E01','E02');
INSERT INTO computer VALUES ('C11','Pegasus','Rogue','E02','E05');
INSERT INTO computer VALUES ('C12','Regen','Alpha','E03','E02');
INSERT INTO computer VALUES ('C13','Regen','Omega','E05','E02');
INSERT INTO installedat VALUES ('SP11','C11','16-Jan-20');
INSERT INTO installedat VALUES ('SP33','C11','21-Jan-20');
INSERT INTO installedat VALUES ('SP44','C11','2-Feb-20');
INSERT INTO installedat VALUES ('SP11','C12','4-Dec-19');
INSERT INTO installedat VALUES ('SP22','C12','20-Nov-19');
INSERT INTO installedat VALUES ('SP33','C13','11-Mar-20');
Copyright (c) 2020 Nenad Jukic and Prospect Press IM Chapter 05 - Page 55
Database Systems, Edition 2.0 (Jukic et. al.) Instructors Manual – Chapter 05
MC8 Jones Dozers
ER Diagram
Relational Schema
CREATE TABLE statements
CREATE TABLE customer
(
CustID CHAR(3) NOT NULL,
CustName VARCHAR NOT NULL,
CustCategory VARCHAR NOT NULL,
PRIMARY KEY (CustID)
);
CREATE TABLE equipmentdetail
Copyright (c) 2020 Nenad Jukic and Prospect Press IM Chapter 05 - Page 56
Database Systems, Edition 2.0 (Jukic et. al.) Instructors Manual – Chapter 05
(
EquipDetailID CHAR(4) NOT NULL,
Type VARCHAR NOT NULL,
Make VARCHAR NOT NULL,
Model VARCHAR NOT NULL,
PRIMARY KEY (EquipDetailID)
);
CREATE TABLE equipment
(
SerialNo CHAR(4) NOT NULL,
LastInspectDate DATE NOT NULL,
DateMade DATE NOT NULL,
EquipDetailID CHAR(4) NOT NULL,
PRIMARY KEY (SerialNo),
FOREIGN KEY (EquipDetailID) REFERENCES equipmentdetail(EquipDetailID)
);
CREATE TABLE salesrep
(
SRepID CHAR(4) NOT NULL,
SRepLname VARCHAR NOT NULL,
SRepFname VARCHAR NOT NULL,
Rank VARCHAR NOT NULL,
MentorID CHAR(4),
PRIMARY KEY (SRepID),
FOREIGN KEY (MentorID) REFERENCES salesrep(SRepID)
);
CREATE TABLE rental
(
RentTransID CHAR(3) NOT NULL,
Price INT NOT NULL,
Date DATE NOT NULL,
SerialNo CHAR(4) NOT NULL,
CustID CHAR(3) NOT NULL,
SRepID CHAR(4) NOT NULL,
PRIMARY KEY (RentTransID),
FOREIGN KEY (SRepID) REFERENCES salesrep(SRepID),
FOREIGN KEY (CustID) REFERENCES customer(CustID),
FOREIGN KEY (SerialNo) REFERENCES equipment(SerialNo)
);
CREATE TABLE sale
(
SaleTransID CHAR(3) NOT NULL,
Price INT NOT NULL,
Date DATE NOT NULL,
SerialNo CHAR(4) NOT NULL,
CustID CHAR(3) NOT NULL,
SRepID CHAR(4) NOT NULL,
PRIMARY KEY (SaleTransID),
FOREIGN KEY (SerialNo) REFERENCES equipment(SerialNo),
FOREIGN KEY (SRepID) REFERENCES salesrep(SRepID),
FOREIGN KEY (CustID) REFERENCES customer(CustID)
);
Copyright (c) 2020 Nenad Jukic and Prospect Press IM Chapter 05 - Page 57
Database Systems, Edition 2.0 (Jukic et. al.) Instructors Manual – Chapter 05
INSERT INTO statements
INSERT INTO customer VALUES ('C10','Bob','Builder');
INSERT INTO customer VALUES ('C11','Carl','Contractor');
INSERT INTO customer VALUES ('C12','Cathy','Contractor');
INSERT INTO customer VALUES ('C13','Paul','Paver');
INSERT INTO customer VALUES ('C14','Lola','Landscaper');
INSERT INTO equipmentdetail VALUES ('ED11','Bulldozer','James Moose','Big Haus');
INSERT INTO equipmentdetail VALUES ('ED22','Backhoe','InchWorm','PowerDig');
INSERT INTO equipmentdetail VALUES ('ED33','Dump Truck','National','Loadster 500');
INSERT INTO equipment VALUES ('1122','13-Nov-19','12-Jun-16','ED11');
INSERT INTO equipment VALUES ('2233','14-Nov-19','19-Jun-16','ED33');
INSERT INTO equipment VALUES ('3344','14-Nov-19','17-Jun-17','ED22');
INSERT INTO equipment VALUES ('4455','20-Dec-19','13-Sep-18','ED22');
INSERT INTO equipment VALUES ('5566','17-Jan-20','15-Apr-18','ED11');
INSERT INTO equipment VALUES ('6677','18-Jan-20','29-Apr-19','ED33');
INSERT INTO salesrep VALUES ('SR10','Beeman','Noah','1',NULL);
INSERT INTO salesrep VALUES ('SR20','Strickland','Hailey','2','SR10');
INSERT INTO rental VALUES ('RT1',500,'19-Sep-19','1122','C10','SR10');
INSERT INTO rental VALUES ('RT2',1000,'19-Sep-19','2233','C10','SR10');
INSERT INTO rental VALUES ('RT3',1250,'01-Oct-19','6677','C13','SR10');
INSERT INTO rental VALUES ('RT4',1250,'27-Oct-19','6677','C11','SR20');
INSERT INTO rental VALUES ('RT5',800,'22-Nov-19','1122','C14','SR10');
INSERT INTO rental VALUES ('RT6',1000,'13-Dec-19','4455','C10','SR20');
INSERT INTO rental VALUES ('RT7',1500,'19-Dec-19','6677','C12','SR20');
INSERT INTO rental VALUES ('RT8',800,'13-Jan-20','1122','C13','SR20');
INSERT INTO sale VALUES ('ST1',125000,'16-Nov-19','3344','C12','SR10');
INSERT INTO sale VALUES ('ST2',200000,'19-Jan-20','5566','C11','SR10');
Copyright (c) 2020 Nenad Jukic and Prospect Press IM Chapter 05 - Page 58
Database Systems, Edition 2.0 (Jukic et. al.) Instructors Manual – Chapter 05
MC9 City Boomers
ER Diagram
Relational Schema
CREATE TABLE statements
CREATE TABLE game
(
GameID CHAR(2) NOT NULL,
GameDate DATE NOT NULL,
Opponent VARCHAR NOT NULL,
PRIMARY KEY (GameID)
);
CREATE TABLE salesperson
(
SPID CHAR(3) NOT NULL,
SPName VARCHAR NOT NULL,
Copyright (c) 2020 Nenad Jukic and Prospect Press IM Chapter 05 - Page 59
Database Systems, Edition 2.0 (Jukic et. al.) Instructors Manual – Chapter 05
PRIMARY KEY (SPID)
);
CREATE TABLE agent
(
AID CHAR(3) NOT NULL,
AName VARCHAR NOT NULL,
PRIMARY KEY (AID)
);
CREATE TABLE merchandise
(
MID CHAR(2) NOT NULL,
MType VARCHAR NOT NULL,
MPrice INT NOT NULL,
PRIMARY KEY (MID)
);
CREATE TABLE advertiser
(
CompanyID CHAR(3) NOT NULL,
CompanyName VARCHAR NOT NULL,
PRIMARY KEY (CompanyID)
);
CREATE TABLE promotesat
(
GameID CHAR(2) NOT NULL,
CompanyID CHAR(3) NOT NULL,
FeeCharged INT NOT NULL,
PRIMARY KEY (GameID, CompanyID),
FOREIGN KEY (GameID) REFERENCES game(GameID),
FOREIGN KEY (CompanyID) REFERENCES advertiser(CompanyID)
);
CREATE TABLE competeswith
(
CompanyID CHAR(3) NOT NULL,
CompetitorID CHAR(3) NOT NULL,
PRIMARY KEY (CompanyID, CompetitorID),
FOREIGN KEY (CompanyID) REFERENCES advertiser(CompanyID),
FOREIGN KEY (CompetitorID) REFERENCES advertiser(CompanyID)
);
CREATE TABLE ticket
(
TicketID CHAR(3) NOT NULL,
SeatType VARCHAR NOT NULL,
Price INT NOT NULL,
PaymentMethod VARCHAR NOT NULL,
GameID CHAR(2) NOT NULL,
AID CHAR(3),
PRIMARY KEY (TicketID),
FOREIGN KEY (GameID) REFERENCES game(GameID),
FOREIGN KEY (AID) REFERENCES agent(AID)
);
Copyright (c) 2020 Nenad Jukic and Prospect Press IM Chapter 05 - Page 60
Database Systems, Edition 2.0 (Jukic et. al.) Instructors Manual – Chapter 05
CREATE TABLE salestransaction
(
STID CHAR(3) NOT NULL,
PaymentMethod VARCHAR NOT NULL,
GameID CHAR(2) NOT NULL,
SPID CHAR(3) NOT NULL,
PRIMARY KEY (STID),
FOREIGN KEY (GameID) REFERENCES game(GameID),
FOREIGN KEY (SPID) REFERENCES salesperson(SPID)
);
CREATE TABLE includes
(
STID CHAR(3) NOT NULL,
MID CHAR(2) NOT NULL,
Quantity INT NOT NULL,
PRIMARY KEY (STID, MID),
FOREIGN KEY (STID) REFERENCES salestransaction(STID),
FOREIGN KEY (MID) REFERENCES merchandise(MID)
);
INSERT INTO statements
INSERT INTO game VALUES ('G1','07-Mar-20','Zoomers');
INSERT INTO game VALUES ('G2','14-Mar-20','Loomers');
INSERT INTO salesperson VALUES ('SP1','Mallory');
INSERT INTO salesperson VALUES ('SP2','Patrick');
INSERT INTO agent VALUES ('AG1','Bryce');
INSERT INTO agent VALUES ('AG2','Olivia');
INSERT INTO merchandise VALUES ('M1','Jersey',100);
INSERT INTO merchandise VALUES ('M2','Souvenir Ball',40);
INSERT INTO merchandise VALUES ('M3','Water Bottle',15);
INSERT INTO merchandise VALUES ('M4','T-Shirt',25);
INSERT INTO merchandise VALUES ('M5','Autographed Picture',75);
INSERT INTO advertiser VALUES ('AD1','Koma Cola');
INSERT INTO advertiser VALUES ('AD2','City Hardware');
INSERT INTO advertiser VALUES ('AD3','Showtime Grill');
INSERT INTO advertiser VALUES ('AD4','NAC Beverages');
INSERT INTO advertiser VALUES ('AD5','Day Street Diner');
INSERT INTO promotesat VALUES ('G1','AD1',200);
INSERT INTO promotesat VALUES ('G1','AD3',150);
INSERT INTO promotesat VALUES ('G1','AD4',300);
INSERT INTO promotesat VALUES ('G2','AD1',200);
INSERT INTO promotesat VALUES ('G2','AD4',300);
INSERT INTO promotesat VALUES ('G2','AD5',100);
INSERT INTO competeswith VALUES ('AD1','AD4');
INSERT INTO competeswith VALUES ('AD3','AD5');
INSERT INTO ticket VALUES ('T10','Sideline',150,'Credit Card','G1','AG2');
INSERT INTO ticket VALUES ('T11','Lower Bowl',100,'Cash','G1',NULL);
Copyright (c) 2020 Nenad Jukic and Prospect Press IM Chapter 05 - Page 61
Database Systems, Edition 2.0 (Jukic et. al.) Instructors Manual – Chapter 05
INSERT INTO ticket VALUES ('T12','Upper Deck',50,'Credit Card','G1','AG1');
INSERT INTO ticket VALUES ('T13','Upper Deck',50,'Credit Card','G1',NULL);
INSERT INTO ticket VALUES ('T20','Suite',250,'Cash','G2','AG1');
INSERT INTO ticket VALUES ('T21','Sideline',125,'Credit Card','G2','AG2');
INSERT INTO ticket VALUES ('T22','Lower Bowl',75,'Credit Card','G2',NULL);
INSERT INTO ticket VALUES ('T23','Lower Bowl',75,'Cash','G2',NULL);
INSERT INTO ticket VALUES ('T24','Upper Deck',30,'Credit Card','G2','AG2');
INSERT INTO salestransaction VALUES ('ST1','Credit Card','G1','SP1');
INSERT INTO salestransaction VALUES ('ST2','Credit Card','G1','SP2');
INSERT INTO salestransaction VALUES ('ST3','Cash','G2','SP2');
INSERT INTO includes VALUES ('ST1','M2',2);
INSERT INTO includes VALUES ('ST1','M3',3);
INSERT INTO includes VALUES ('ST1','M5',1);
INSERT INTO includes VALUES ('ST2','M1',2);
INSERT INTO includes VALUES ('ST3','M4',5);
INSERT INTO includes VALUES ('ST3','M2',1);
Copyright (c) 2020 Nenad Jukic and Prospect Press IM Chapter 05 - Page 62
Database Systems, Edition 2.0 (Jukic et. al.) Instructors Manual – Chapter 05
MC10 Midtown Memorial
ER Diagram
Relational Schema
CREATE TABLE statements
CREATE TABLE patient
(
PatientID CHAR(2) NOT NULL,
PatientLName VARCHAR NOT NULL,
PatientFName VARCHAR NOT NULL,
PatientGender VARCHAR NOT NULL,
PatientBDate DATE NOT NULL,
PRIMARY KEY (PatientID)
);
CREATE TABLE nurse
(
NurseID CHAR(2) NOT NULL,
NurseName VARCHAR NOT NULL,
PRIMARY KEY (NurseID)
Copyright (c) 2020 Nenad Jukic and Prospect Press IM Chapter 05 - Page 63
Database Systems, Edition 2.0 (Jukic et. al.) Instructors Manual – Chapter 05
);
CREATE TABLE drug
(
DrugID CHAR(2) NOT NULL,
DrugCoatingType VARCHAR NOT NULL,
DrugName VARCHAR NOT NULL,
PRIMARY KEY (DrugID)
);
CREATE TABLE ingredient
(
IngID CHAR(4) NOT NULL,
IngName VARCHAR NOT NULL,
PRIMARY KEY (IngID)
);
CREATE TABLE contains
(
DrugID CHAR(2) NOT NULL,
IngID CHAR(4) NOT NULL,
Dosage VARCHAR NOT NULL,
PRIMARY KEY (DrugID, IngID),
FOREIGN KEY (DrugID) REFERENCES drug(DrugID),
FOREIGN KEY (IngID) REFERENCES ingredient(IngID)
);
CREATE TABLE nottotakewith
(
DrugID CHAR(2) NOT NULL,
DoNotTakeDrugID CHAR(2) NOT NULL,
PRIMARY KEY (DrugID, DoNotTakeDrugID),
FOREIGN KEY (DrugID) REFERENCES drug(DrugID),
FOREIGN KEY (DoNotTakeDrugID) REFERENCES drug(DrugID)
);
CREATE TABLE drugintakevent
(
DIENumber CHAR(4) NOT NULL,
PatientID CHAR(2) NOT NULL,
DIETime VARCHAR NOT NULL,
DIEDate DATE NOT NULL,
NurseID CHAR(2) NOT NULL,
PRIMARY KEY (DIENumber, PatientID),
FOREIGN KEY (PatientID) REFERENCES patient(PatientID),
FOREIGN KEY (NurseID) REFERENCES nurse(NurseID)
);
CREATE TABLE includes
(
DIENumber CHAR(4) NOT NULL,
PatientID CHAR(2) NOT NULL,
DrugID CHAR(2) NOT NULL,
Quantity INT NOT NULL,
PRIMARY KEY (DrugID, DIENumber, PatientID),
FOREIGN KEY (DrugID) REFERENCES drug(DrugID),
Copyright (c) 2020 Nenad Jukic and Prospect Press IM Chapter 05 - Page 64
Database Systems, Edition 2.0 (Jukic et. al.) Instructors Manual – Chapter 05
FOREIGN KEY (DIENumber, PatientID) REFERENCES drugintakeevent(DIENumber,
PatientID)
);
INSERT INTO statements
INSERT INTO patient VALUES ('P1','Smith','Steve','M','22-Feb-62');
INSERT INTO patient VALUES ('P2','Jones','John','M','14-Jan-58');
INSERT INTO patient VALUES ('P3','Doe','Diane','F','12-May-74');
INSERT INTO patient VALUES ('P4','Tucker','Tina','F','03-Sep-70');
INSERT INTO patient VALUES ('P5','Rudd','Raymond','M','19-Jul-63');
INSERT INTO nurse VALUES ('N1','Virginia');
INSERT INTO nurse VALUES ('N2','Ted');
INSERT INTO nurse VALUES ('N3','Connie');
INSERT INTO drug VALUES ('D1','Coated','Cirpalix');
INSERT INTO drug VALUES ('D2','Uncoated','Cirpalix');
INSERT INTO drug VALUES ('D3','Coated','Droton');
INSERT INTO drug VALUES ('D4','Coated','Cendera');
INSERT INTO drug VALUES ('D5','Uncoated','Wellacin');
INSERT INTO ingredient VALUES ('ING1','Enzyme A');
INSERT INTO ingredient VALUES ('ING2','Enzyme B');
INSERT INTO ingredient VALUES ('ING3','Dromeine');
INSERT INTO ingredient VALUES ('ING4','Probiotics');
INSERT INTO contains VALUES ('D1','ING1','25mg');
INSERT INTO contains VALUES ('D1','ING3','750mg');
INSERT INTO contains VALUES ('D2','ING2','35mg');
INSERT INTO contains VALUES ('D2','ING3','600mg');
INSERT INTO contains VALUES ('D3','ING2','20mg');
INSERT INTO contains VALUES ('D3','ING4','65mg');
INSERT INTO contains VALUES ('D4','ING1','425mg');
INSERT INTO contains VALUES ('D4','ING4','375mg');
INSERT INTO contains VALUES ('D5','ING2','75mg');
INSERT INTO contains VALUES ('D5','ING1','125mg');
INSERT INTO nottotakewith VALUES ('D1','D4');
INSERT INTO nottotakewith VALUES ('D2','D3');
INSERT INTO nottotakewith VALUES ('D3','D4');
INSERT INTO drugintakeevent VALUES ('DIE1','P1','07:00AM','14-Mar-20','N1');
INSERT INTO drugintakeevent VALUES ('DIE1','P2','12:00AM','14-Mar-20','N2');
INSERT INTO drugintakeevent VALUES ('DIE2','P2','12:00AM','14-Mar-20','N2');
INSERT INTO drugintakeevent VALUES ('DIE3','P4','08:00PM','15-Mar-20','N3');
INSERT INTO includes VALUES ('DIE1','P1','D3',2);
INSERT INTO includes VALUES ('DIE1','P1','D5',1);
INSERT INTO includes VALUES ('DIE1','P2','D1',1);
INSERT INTO includes VALUES ('DIE2','P2','D2',3);
INSERT INTO includes VALUES ('DIE2','P2','D4',3);
INSERT INTO includes VALUES ('DIE3','P4','D1',1);
INSERT INTO includes VALUES ('DIE3','P4','D3',2);
Copyright (c) 2020 Nenad Jukic and Prospect Press IM Chapter 05 - Page 65