0% found this document useful (0 votes)
34 views11 pages

DBS Oel

The document outlines Lab 11 for the CS220 Database Systems course, focusing on an Open Ended Lab scenario involving an Animals Well-being Management System at Dist Haywanaat Shifa Khana. It details objectives, business rules, entity attributes, constraints, and provides SQL code for creating and populating a relational database. The lab also includes tasks for analyzing the scenario, designing an ERD, and generating reports through SQL queries.

Uploaded by

ahmadxaidi2004
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)
34 views11 pages

DBS Oel

The document outlines Lab 11 for the CS220 Database Systems course, focusing on an Open Ended Lab scenario involving an Animals Well-being Management System at Dist Haywanaat Shifa Khana. It details objectives, business rules, entity attributes, constraints, and provides SQL code for creating and populating a relational database. The lab also includes tasks for analyzing the scenario, designing an ERD, and generating reports through SQL queries.

Uploaded by

ahmadxaidi2004
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/ 11

Faculty of Computing

CS220: Database Systems


Class: BESE-14AB
Lab 11: Open Ended Lab
Date: December 02, 2024
Time: 10:00-01:00 & 14:00-17:00
Instructor: Dr. Bilal Ali & Dr. Shah Khalid

Lab Engineer: Ms. Ayesha Asif

Members Qalam ID
Muhammad Ahmad Raza 466747
Anas Rehman 464092
Usayd bin Wasim 455549
Lab 12: Open Ended Lab
Introduction
This lab is Open Ended Lab a complete scenario is given and you are required to propose the
solutions as per your understandings.

Objectives
Objective of this lab is to understand the scenario, make ERD, write DDL , populate data through
DML and Execute DRL for reports.

Animals Well-being Management System


Akmal, a loyal client of Dist Haywanaat Shifa Khana “DHSK”, arrives with his one of the
golden retrievers (Dogs), Tommy, for Tommy's scheduled biannual vaccination. This time,
Tommy requires an oral vaccination that involves Treatment of local anesthesia. The veterinarian
accesses Tommy's comprehensive medical history and notes the need for the oral vaccination. To
ensure Tommy's comfort, the vet administers a local anesthesia treatment before proceeding with
the oral vaccination process.
During the visit, various treatments and medications are provided, including the oral vaccine, the
local anesthesia, and any additional medications prescribed for Tommy's well-being. Each
treatment, medication, its dosage, and price are very well documented.
Post-treatment, the front desk staff compile an itemized bill detailing the treatment and
medications. The total cost of the visit, including the oral vaccination, local anesthesia, and any
additional services rendered, arriving at a total of 2454/- rupees for the comprehensive treatment.
Consider the following Business Rules:
1. A customer can have many pets but must have at least one.
2. A pet must be assigned to one and only one customer.
3. A pet can have one or more treatments per visit but must have at least one.
4. A treatment can have one or more medications.
5. A bill must have details of the treatments including costs.
Task:
1: Analyze the scenario to understand the working of a DHSK .
2: Identify Entities, attributes and constraints (identify appropriate related attributes)
Entities and Attributes:

1. Customer
 Name
 Contact Details
 Phone Number
2. Pet
 Name
 Breed
 Age
 Customer Id
3. Visit
 Pet Id
 Visit Id
 Total Cost
4. Treatment
 Visit Id
 Treatment Name
 Description
 Cost
5. Bill
 Visit Id
 Total Amount
 Details
6. Medication
 Treatment Id
 Medication Name
 Dosage
 Price
Entity Constraints

1. Customer:

Primary Key: CustomerID (each customer must have a unique identifier).

A customer must have at least one pet.

2. Pet:

Primary Key: PetID (each pet must have a unique identifier).

Foreign Key: CustomerID (each pet must belong to a single customer).

Each pet must have at least one visit.


3. Visit:

Primary Key: VisitID (each visit must have a unique identifier).

Foreign Key: PetID (each visit is associated with one pet).

Each visit must include at least one treatment.

4. Treatment:

Primary Key: TreatmentID (each treatment must have a unique identifier).

Foreign Key: VisitID (each treatment is associated with one visit).

Each treatment must have at least one medication.

5. Medication:

Primary Key: MedicationID (each medication must have a unique identifier).

Foreign Key: TreatmentID (each medication is associated with one treatment).

6. Bill:

Primary Key: BillID (each bill must have a unique identifier).

Foreign Key: VisitID (each bill is associated with one visit).

Relationship Constraints

1. Customer ↔ Pet:

Cardinality: One customer can own multiple pets (1:N).

Constraint: Each pet must belong to exactly one customer.

2. Pet ↔ Visit:

Cardinality: One pet can have multiple visits (1:N).

Constraint: Each visit is linked to exactly one pet.

3. Visit ↔ Treatment:

Cardinality: One visit can include multiple treatments (1:N).


Constraint: Each treatment must belong to one visit.

4. Treatment ↔ Medication:

Cardinality: One treatment can involve multiple medications (1:N).

Constraint: Each medication must be linked to one treatment.

5. Visit ↔ Bill:

Cardinality: Each visit generates one bill (1:1).

Constraint: A bill must correspond to a single visit and include details of all associated
treatments and costs.

Other Constraints

 Non-Null Constraints:
o All primary and foreign keys must not be null.
o Essential attributes like Name, Cost, and TotalAmount must not be null.

 Unique Constraints:
o Primary keys (CustomerID, PetID, VisitID, TreatmentID, MedicationID, BillID) must be
unique.

 Referential Integrity:
o Foreign keys (CustomerID in Pet, PetID in Visit, VisitID in Treatment, TreatmentID in
Medication, VisitID in Bill) must reference existing primary keys in their respective parent
tables.

3: Design ERD to meet the requirements


4: Implement it into an executable relational database

CREATE DATABASE HaywanaatShifaKhana;


USE HaywanaatShifaKhana;

CREATE TABLE Customer (


CustomerID INT AUTO_INCREMENT PRIMARY KEY,
Name VARCHAR(100) NOT NULL,
ContactDetails VARCHAR(255),
PhoneNumber VARCHAR(15)
);

CREATE TABLE Pet (


PetID INT AUTO_INCREMENT PRIMARY KEY,
Name VARCHAR(100) NOT NULL,
Breed VARCHAR(100),
Age INT,
CustomerID INT NOT NULL,
FOREIGN KEY (CustomerID)
REFERENCES Customer(CustomerID) ON DELETE CASCADE
);

CREATE TABLE Visit (


VisitID INT AUTO_INCREMENT PRIMARY KEY,
PetID INT NOT NULL,
VisitDate DATE NOT NULL,
TotalCost DECIMAL(10, 2) NOT NULL,
FOREIGN KEY (PetID)
REFERENCES Pet(PetID) ON DELETE CASCADE
);

CREATE TABLE Treatment (


TreatmentID INT AUTO_INCREMENT PRIMARY KEY,
VisitID INT NOT NULL,
TreatmentName VARCHAR(255) NOT NULL,
Description TEXT,
Cost DECIMAL(10, 2) NOT NULL,
FOREIGN KEY (VisitID) REFERENCES Visit(VisitID) ON DELETE
CASCADE
);
CREATE TABLE Medication (
MedicationID INT AUTO_INCREMENT PRIMARY KEY,
TreatmentID INT NOT NULL,
MedicationName VARCHAR(255) NOT NULL,
Dosage VARCHAR(100),
Price DECIMAL(10, 2) NOT NULL,
FOREIGN KEY (TreatmentID) REFERENCES Treatment(TreatmentID)
ON DELETE CASCADE
);

CREATE TABLE Bill (


BillID INT AUTO_INCREMENT PRIMARY KEY,
VisitID INT NOT NULL,
TotalAmount DECIMAL(10, 2) NOT NULL,
Details TEXT,
FOREIGN KEY (VisitID) REFERENCES Visit(VisitID) ON DELETE
CASCADE
);

5: Populate the database with 3 owners, 5 pets, 3 treatments, 7 medicines, 1 old visit and 1
current visit
DML Statements:
insert into Customer (Name, ContactDetails, PhoneNumber)
values
('Ahmad', '123-456-7890', '0337-8376282'),
('Anas', '987-654-3210', '0327-9119401'),
('Ali', '456-789-1234', '0301-5271892');

insert into into Pet (Name, Breed, Age, CustomerID)


values
('Tommy', 'Golden Retriever', 3, 1),
('Jonny', 'Persian Cat', 4, 2),
('Goku', 'Bulldog', 2, 3),
('Lucy', 'Poodle', 5, 1),
('Jutt', 'Siamese Cat', 1, 2);

insert into Visit (PetID, VisitDate, TotalCost)


values
(1, '2024-06-15', 1450.00),
(1, CURDATE(), 2454.00);

insert into Treatment (VisitID, TreatmentName, Description,


Cost)
values
(1, 'General Checkup', 'Routine health check', 500.00),
(2, 'Oral Vaccination', 'Oral vaccine with local anesthesia',
1500.00),
(2, 'Additional Medication', 'Treatment for minor allergy',
300.00);

insert into Medication (TreatmentID, MedicationName, Dosage,


Price)
values
(1, 'Vitamin Supplement', '1 tablet daily', 200.00),
(2, 'Oral Vaccine', 'Single dose', 1500.00),
(2, 'Local Anesthetic', 'Topical application', 500.00),
(3, 'Pain Reliever', '2 tablets daily', 100.00),
(3, 'Antibiotic Cream', 'Apply twice daily', 150.00),
(3, 'Antihistamine', '1 tablet daily', 100.00),
(3, 'Probiotic', '1 capsule daily', 50.00);

insert into Bill (VisitID, TotalAmount, Details)


values
(1, 1450.00, 'General checkup and vitamin supplement'),
(2, 2454.00, 'Oral vaccination with local anesthesia and
additional medications');

6: Provide the reports through SQL queries for the followings:


DRL Statements
a: bill with details of the old visit

CODE:
select TotalAmount from Bill where VisitID = 1;
OUTPUT:
b: bill with details of the current visit
CODE:
select TotalAmount from Bill where VisitID = 2;
OUTPUT:

c: bill having maximum cost with details of the visit


CODE:
select Details, TotalAmount
from Bill where TotalAmount = (select MAX(TotalAmount) from
Bill);
OUTPUT:

d: Identify the Pet having maximum distinct treatments along with treatment name.
CODE:
select p.Name AS PetName, t.TreatmentName
from Pet p
join Treatment t on p.PetID = t.VisitID
where p.PetID = (
select PetID
from (
select p.PetID, count(distinct t.TreatmentName) as
DistinctTreatmentCount
from Pet p
join Treatment t on p.PetID = t.VisitID
group by p.PetID
order by DistinctTreatmentCount DESC
limit 1
) as MaxTreatments
);
OUTPUT:

Deliverables:
Provide a complete report having ERD, DDL, DML, DRL and snapshot of results.
Work Done:
ERD done by Anas Rehman.
DDL done by Usayd bin Wasim.
DML and DRL done by Muhammad Ahmad Raza.

You might also like