Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 11

Name: Sudip Mete

Roll: 13000119135

Assignment 7
I. Design an ER diagram for an application that models a hospital doctors treat patients,
prescribe tests, monitor progress etc. Analyze the requirements by identifying the entities,
attributes, relationships, keys, constraints etc. Apply extended entity-relationship features
to the design. Defend your design with proper assumptions and justifications. Map the ER
model into a relational model.
II. Create tables, populate with data and construct queries (advanced) in SQL to extract
information from the hospital doctor’s database.
III. Consider the following relations run the following SQL queries :

Doctor(SSN, FirstName, LastName, Specialty, YearsOfExperience, PhoneNum)


Patient(SSN, FirstName, LastName, Address, DOB, PrimaryDoctor_SSN)
Medicine(TradeName, UnitPrice, GenericFlag)
Prescription(Id, Date, Doctor_SSN, Patient_SSN)
Prescription_Medicine(Prescription Id, TradeName, NumOfUnits)

1. List the trade name of generic medicine with unit price less than $50.
2. List the first and last name of patients whose primary doctor named ʻJohn Smithʼ.
3. List the first and last name of doctors who are not primary doctors to any patient.
4. For medicines written in more than 20 prescriptions, report the trade name and the
total number of units prescribed.
5. List the SSN of patients who have ʻAspirinʼ and ʻVitaminʼ trade names in one
prescription.
6. List the SNN of distinct patients who have ʻAspirinʼ prescribed to them by doctor
named ʻJohn Smithʼ.
7. List the first and last name of patients who have no prescriptions written by doctors
other than their primary doctors.

Create Table DOCTOR:


Create table DOCTOR( Doc_SSN varchar2(6) primary key, FirstName varchar2(10) NOT NULL,
LastName varchar2(10) NOT NULL, Speciality varchar2(15), YearOfExperience number(5),
PhoneNum number(12));
Create Table PATIENTS:
create table PATIENTS(P_SSN varchar2(6) primary key, FirstName varchar2(10) NOT NULL,
LastName varchar2(10) NOT NULL, Address varchar2(20), DOB date, PrimaryDoctor_SSN
REFERENCES DOCTOR(Doc_SSN) ON DELETE CASCADE);

Create Table MEDICINE:


Create table MEDICINE( TradeName varchar2(15) primary key, UnitPrice number(10,2) NOT
NULL, GenericFlag varchar2(15));
Create Table PRESCRIPTION:
create table PRESCRIPTION(Id varchar2(6) primary key, PrescribeDate DATE, Doctor_SSN
REFERENCES DOCTOR(Doc_SSN) ON DELETE CASCADE, Patient_SSN REFERENCES
PATIENTS(P_SSN) ON DELETE CASCADE);

Create Table PRESCRIBE_MEDICINE:


create table PRESCRIBE_MEDICINE(Prescription_Id REFERENCES PRESCRIPTION(Id) ON
DELETE CASCADE,TradeName REFERENCES MEDICINE(TradeName) ON DELETE CASCADE,
NumOfUnit number(10));
Insert Data into DOCTOR:
INSERT INTO DOCTOR values('D001','Steve','Job','GeneralMedicine',5,8045620012);
INSERT INTO DOCTOR values('D002','John','Smith','Orthopedic',7,8085642578);
INSERT INTO DOCTOR values('D003','Ben','Stroke','Pediatric',2,9433378925);
INSERT INTO DOCTOR values('D004','Bendon','Macculum','Pediatric',12,9563378825);
INSERT INTO DOCTOR values('D005','Kori','Anderson','Orthopedic',10,8972583645);

Insert Data into PATIENTS:


INSERT INTO PATIENTS VALUES('P001','Sudip','Mete','Kolkata','28-dec-1997','D001');
INSERT INTO PATIENTS VALUES('P002','Subir','Nag','Kolkata','08-jan-1998','D003');
INSERT INTO PATIENTS VALUES('P003','Koushik','Roy','Mumbai','14-mar-1992','D002');
INSERT INTO PATIENTS VALUES('P004','Saikat','Pal','Hyderabad','04-jul-2005','D002');
INSERT INTO PATIENTS VALUES('P005','Payel','Khan','Ahmedabad','16-sep-2000','D005');

Insert Data into MEDICINE:


INSERT INTO MEDICINE values ('Aspirin',50,'Yes');
INSERT INTO MEDICINE values ('Almox',45,'Yes');
INSERT INTO MEDICINE values ('Vitamin',35,'No');
INSERT INTO MEDICINE values ('Calpol',60,'Yes');
INSERT INTO MEDICINE values ('Tranexa',42,'Yes');
INSERT INTO MEDICINE values ('Zincovit',80,'No');
Insert Data into PRESCRIPTION:
INSERT INTO PRESCRIPTION VALUES('I001','26-MAY-2021','D003','P001');
INSERT INTO PRESCRIPTION VALUES('I002','02-APR-2021','D003','P003');
INSERT INTO PRESCRIPTION VALUES('I003','02-JAN-2020','D002','P004');
INSERT INTO PRESCRIPTION VALUES('I004','28-MAR-2020','D005','P005');
Insert Data into PRESCRIBE_MEDICINE:
INSERT INTO PRESCRIBE_MEDICINE VALUES('I003','Aspirin',2);
INSERT INTO PRESCRIBE_MEDICINE VALUES('I003','Vitamin',5);
INSERT INTO PRESCRIBE_MEDICINE VALUES('I001','Calpol',1);
INSERT INTO PRESCRIBE_MEDICINE VALUES('I002','Almox',6);
INSERT INTO PRESCRIBE_MEDICINE VALUES('I003','Calpol',5);
INSERT INTO PRESCRIBE_MEDICINE VALUES('I004','Calpol',7);
INSERT INTO PRESCRIBE_MEDICINE VALUES('I002','Calpol',8);
INSERT INTO PRESCRIBE_MEDICINE VALUES('I001','Almox',3);
Q1) List the trade name of generic medicine with unit price less than $50.
select TradeName from MEDICINE where GenericFlag=’Yes’ AND UnitPrice<50;

Q2) List the first and last name of patients whose primary doctor named
ʻJohn Smithʼ.
select FirstName,LastName from PATIENTS where PrimaryDoctor_SSN IN (select Doc_SSN
from DOCTOR where DOCTOR.FirstName=’John’ AND DOCTOR.LastName=’Smith’);

Q3) List the first and last name of doctors who are not primary doctors to
any patient.
select FirstName,LastName from DOCTOR where Doc_SSN NOT IN (select
PrimaryDoctor_SSN from PATIENTS);
Q4) For medicines written in more than 20 prescriptions, report the trade
name and the total number of units prescribed.

select TradeName, sum(NumOfUnit) from PRESCRIBE_MEDICINE group by TradeName


having count(Prescription_Id) > 2;

Q5) List the SSN of patients who have ʻAspirinʼ and ʻVitaminʼ trade names
in one prescription.

Select Patient_SSN from PRESCRIPTION,PRESCRIBE_MEDICINE T, PRESCRIBE_MEDICINE S


where T.Prescription_Id=S.Prescription_Id AND PRESCRIPTION.Id = T.Prescription_Id AND
T.TradeName=’Aspirin’ AND S.TradeName=’Vitamin’ ;
Q6) List the SNN of distinct patients who have ʻAspirinʼ prescribed to them
by doctor named ʻJohn Smithʼ.

select PATIENTS.P_SSN from PRESCRIBE_MEDICINE,PRESCRIPTION,PATIENTS,DOCTOR


where PRESCRIBE_MEDICINE.Prescription_Id=PRESCRIPTION.Id AND
PRESCRIPTION.Doctor_SSN=DOCTOR.Doc_SSN AND
PRESCRIPTION.Patient_SSN=PATIENTS.P_SSN AND
PRESCRIBE_MEDICINE.TradeName='Aspirin' AND DOCTOR.FirstName='John' AND
DOCTOR.LastName='Smith';
Q7) List the first and last name of patients who have no prescriptions
written by doctors other than their primary doctors.

select FirstName,LastName from PATIENTS where P_SSN NOT IN (select Patient_SSN from
PRESCRIPTION);

You might also like