0% found this document useful (0 votes)
35 views8 pages

Advance Database Lab 1 Tasks

The document outlines steps to implement a database using IBM DB2. It includes SQL commands to create tables, views, add columns, and retrieve data. Various tables are created to store information about events, equipment, managers, clients and their engagements. Commands are provided to retrieve event details, list managers, count engagements by month, and more.

Uploaded by

John Ng
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views8 pages

Advance Database Lab 1 Tasks

The document outlines steps to implement a database using IBM DB2. It includes SQL commands to create tables, views, add columns, and retrieve data. Various tables are created to store information about events, equipment, managers, clients and their engagements. Commands are provided to retrieve event details, list managers, count engagements by month, and more.

Uploaded by

John Ng
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

LAB 01

1. Implement a database using IBM DB2 based on the outcome of Step #1 and #2.

CREATE TABLE EVENT


(
Event_ID char(4) NOT NULL PRIMARY KEY,
Description VARCHAR(30),
Event_Type VARCHAR(7) DEFAULT 'Indoor'
CHECK (Event_Type IN ('Indoor', 'Outdoor')),
Target_Audience VARCHAR(30)
);

CREATE TABLE Equipment


(
Equip_ID INT NOT NULL PRIMARY KEY
GENERATED ALWAYS AS IDENTITY (START WITH 10000, INCREMENT BY 1),
Description VARCHAR(30),
ChargeDay DECIMAL(7,2)
);

CREATE TABLE Event_Manager


(
Staff_ID int NOT NULL PRIMARY KEY,
Name VARCHAR(30) NOT NULL,
Phone VARCHAR(10),
Email VARCHAR(30),
Gender CHAR(1) CHECK (Gender IN ('M', 'F'))
);

CREATE TABLE Client


(
Client_ID char(5) NOT NULL PRIMARY KEY,
Name VARCHAR(30),
Person VARCHAR(40),
Email VARCHAR(30)
);

CREATE TABLE Contact


(
Client_ID char(5) NOT NULL,
Phone char(11) NOT NULL,
PRIMARY KEY (Client_ID, Phone),
FOREIGN KEY (Client_ID) REFERENCES Client
);

CREATE TABLE Engagement


(
Engage_ID char(5) NOT NULL PRIMARY KEY,
Date_Engage DATE DEFAULT CURRENT_DATE,
Event_Time TIME CHECK (Event_Time BETWEEN '00:00:00' AND
'23:59:59'),
Duration INT,
Capacity INT,
Location VARCHAR(30),
Client_ID char(5),
Staff_ID int,
Event_ID char(4),
Event_Date DATE,
FOREIGN KEY (Staff_ID) REFERENCES Event_Manager,
FOREIGN KEY (Client_ID) REFERENCES Client,
FOREIGN KEY (Event_ID) REFERENCES Event
);

CREATE TABLE EquiUse


(
Equip_ID int NOT NULL,
Engage_ID char(5) NOT NULL,
Start_Date Date,
End_Date Date,
PRIMARY KEY (Equip_ID, Engage_ID),
FOREIGN KEY (Equip_ID) REFERENCES Equipment,
FOREIGN KEY (Engage_ID) REFERENCES Engagement
);

2. Add two columns named Fee and Discount into Engagement table.

ALTER TABLE Engagement


ADD COLUMN Fee DECIMAL(10, 2);

ALTER TABLE Engagement


ADD COLUMN Discount DECIMAL(10, 2);

3. Create a view which shows only the events that is conducted outdoor. In your view
creation, make sure that it does not accept or process any events that are conducted
indoor.

CREATE VIEW Outdoor_Event AS


SELECT EVENT_ID, DESCRIPTION, EVENT_TYPE, TARGET_AUDIENCE
FROM EVENT
WHERE EVENT_TYPE = 'Outdoor'

4. What is the command used to see all the tables that you have created in the
database?

SELECT TABNAME FROM SYSCAT.TABLES WHERE TABSCHEMA = CURRENT SCHEMA;


5. Write a SQL command to show the entity integrity and referential integrity
constraints of all the tables (one by one) that you have created.

SELECT tc.tabname AS "Table Name", tc.constname AS "Constraint Name",


kc.colname AS "Column Name", 'Primary Key' AS "Constraint Type"
FROM SYSCAT.TABCONST tc
JOIN SYSCAT.KEYCOLUSE kc ON tc.constname = kc.constname
WHERE tc.type = 'P'
UNION ALL
SELECT tc.tabname, tc.constname, kc.colname, 'Foreign Key'
FROM SYSCAT.TABCONST tc
JOIN SYSCAT.KEYCOLUSE kc ON tc.constname = kc.constname
WHERE tc.type = 'F';
LAB 02

6. Write the SQL commands to perform the following tasks:

a) Show the complete information of events that are conducted outdoor.

SELECT *
FROM EVENT
WHERE EVENT_TYPE = 'Outdoor'

b) List the name of event manager who has managed more than one engagement.

SELECT STAFF_ID, NAME


FROM EVENT_MANAGER
WHERE STAFF_ID IN (SELECT STAFF_ID
FROM ENGAGEMENT GROUP BY STAFF_ID
HAVING COUNT(STAFF_ID)>1);
c) List out the following information for all the engagements:
i. Event Description
ii. Client Name
iii. Event Type
iv. Date of Event
v. Time of Event
vi. Location of Event
vii. Name of Event Manager

SELECT e.Description AS "Event Description",


c.Name AS "Client Name",
e.Event_Type AS "Event Type",
eng.Event_Date AS "Date of Event",
eng.Event_Time AS "Time of Event",
eng.Location AS "Location of Event",
em.Name AS "Name of Event Manager"
FROM Engagement eng
JOIN Event e ON eng.Event_ID = e.Event_ID
JOIN Client c ON eng.Client_ID = c.Client_ID
JOIN Event_Manager em ON eng.Staff_ID = em.Staff_ID;
d) List out the month name and the number of engagements organized in each
month for year 2015.

SELECT
CASE MONTH(e.Event_Date)
WHEN 1 THEN 'January'
WHEN 2 THEN 'February'
WHEN 3 THEN 'March'
WHEN 4 THEN 'April'
WHEN 5 THEN 'May'
WHEN 6 THEN 'June'
WHEN 7 THEN 'July'
WHEN 8 THEN 'August'
WHEN 9 THEN 'September'
WHEN 10 THEN 'October'
WHEN 11 THEN 'November'
WHEN 12 THEN 'December'
END AS "Month",
COUNT(*) AS "Number of Engagements"
FROM
Engagement e
WHERE
YEAR(e.Event_Date) = 2015
GROUP BY
MONTH(e.Event_Date)
ORDER BY
MONTH(e.Event_Date);

e) List out the description and the charge per day for all types of canopy.

SELECT DESCRIPTION, CHARGEDAY AS "Charge Per Day"


FROM EQUIPMENT
WHERE DESCRIPTION LIKE '%Canopy'
f) For each client, list out its name, the number of events and the total fees that has
been charged for organizing all the events.

SELECT c.NAME AS "Client Name",


COUNT(e.ENGAGE_ID) AS "Number of Events",
SUM(e.FEE) AS "Total Fees Charged"
FROM CLIENT c
JOIN ENGAGEMENT e ON e.CLIENT_ID = c.CLIENT_ID
GROUP BY c.NAME;

g) List out the information of equipment that have not been used before.

SELECT *
FROM Equipment
WHERE EQUIP_ID NOT IN (
SELECT EQUIP_ID FROM EQUIUSE)
h) Show the most popular event of this company, as engaged by its clients.

SELECT e.Event_ID, ev.Description, COUNT(*) AS "Number of


Engagements"
FROM Engagement e
JOIN Event ev ON e.Event_ID = ev.Event_ID
GROUP BY e.Event_ID, ev.Description
ORDER BY COUNT(*) DESC
FETCH FIRST 1 ROWS ONLY;

You might also like