You are on page 1of 10

create database Hospital;

create table Doctor (


doctorID int NOT NULL PRIMARY KEY,
doctorName varchar(50),
Dob date,
gender varchar(1),
specialization varchar(50),
patientID int,
CONSTRAINT fk_patient FOREIGN KEY (patientID)
REFERENCES Patient(patientID)
);

create table Patient (


patientID int NOT NULL PRIMARY KEY,
patientName varchar(50),
Dob date,
gender varchar(1),
admitDate date,
releaseDate date,
receptionistID int,
CONSTRAINT fk_rec FOREIGN KEY (receptionistID)
REFERENCES Receptionist(receptionistID)
);

create table Room (


roomID int NOT NULL PRIMARY KEY,
roomType varchar(50),
status varchar(25),
patientID int,
CONSTRAINT fk_patientRoom FOREIGN KEY (patientID)
REFERENCES Patient(patientID)
);

create table Nurse (


staffID int NOT NULL PRIMARY KEY,
staffName varchar(50),
gender varchar(1),
roomID int,
CONSTRAINT fk_room FOREIGN KEY (roomID)
REFERENCES Room(roomID)
);

create table [dbo].[Receptionist] (


[receptionistID] int NOT NULL PRIMARY KEY,
[receptionistName] varchar(50),
[gender] varchar(1)
);

create table Bill (


billNo int NOT NULL PRIMARY KEY,
amount decimal(10,2),
patientID int,
CONSTRAINT fk_patientBill FOREIGN KEY (patientID)
REFERENCES Patient(patientID)
);

create table Medicine (


medicineCode int NOT NULL PRIMARY KEY,
name varchar(200),
quantity int,
price int,
patientID int,
CONSTRAINT fk_patientMedicine FOREIGN KEY (patientID)
REFERENCES Patient(patientID)
);

insert into Doctor (doctorID, doctorName, dob, gender, specialization) values


(1, 'Johnny', '1990-01-14', 'M', 'Dentist'),
(2, 'Beverly', '1988-09-07', 'F', 'Cardiologist'),
(3, 'Carol', '1976-08-13', 'F', 'Dermatologist'),
(4, 'Gregory', '1977-05-29', 'M', 'ENT Specialist'),
(5, 'Eugene', '1971-07-03', 'M', 'Gynecologist'),
(6, 'Jean', '1982-04-29', 'F', 'Hematologist'),
(7, 'Lisa', '1983-12-26', 'F', 'Immunologist'),
(8, 'Ann', '1971-07-14', 'F', 'Neurologist'),
(9, 'Brian', '1978-10-27', 'M', 'Obstetrician'),
(10, 'Larry', '1974-12-29', 'M', 'Oncologist'),
(11, 'Christine', '1976-11-18', 'F', 'Radiologists'),
(12, 'Frances', '1977-12-20', 'F', 'Surgeon'),
(13, 'Daniel', '1978-04-21', 'M', 'Dentist'),
(14, 'Craig', '1989-02-24', 'M', 'Cardiologist'),
(15, 'Paul', '1982-05-03', 'M', 'Dermatologist')

select * from doctor;

insert into [dbo].[Patient] ([patientID], [patientName], [dob], [gender], [admitDate],


[releaseDate], [ReceptionistID]) values
(1, 'Stephanie', '1982-06-06', 'F', '2004-02-05', '2004-02-05', 4),
(2, 'Michelle', '1981-04-23', 'F', '2013-02-07', '2013-02-07', 43),
(3, 'Jean', '2008-08-16', 'F', '2007-04-08', '2007-04-08', 14),
(4, 'Daniel', '1991-05-30', 'M', '2006-09-11', '2006-09-11', 4),
(5, 'Marie', '1999-07-15', 'F', '2010-06-02', '2010-06-02', 42),
(6, 'Timothy', '1983-01-20', 'M', '2009-12-13', '2009-12-13', 26),
(7, 'Joyce', '2012-03-01', 'F', '2006-11-22', '2006-11-22', 47),
(8, 'Tina', '2013-07-22', 'F', '2009-09-24', '2009-09-24', 35),
(9, 'George', '1991-11-30', 'M', '2009-11-24', '2009-11-24', 12),
(10, 'Matthew', '1992-02-05', 'M', '2015-05-21', '2015-05-21', 42),
(11, 'Juan', '1990-03-31', 'M', '2013-05-27', '2013-05-27', 26),
(12, 'Albert', '1985-02-26', 'M', '2010-11-30', '2010-11-30', 39),
(13, 'Amy', '2012-12-29', 'F', '2015-08-21', '2015-08-21', 35),
(14, 'Alan', '1992-11-19', 'M', '2007-09-20', '2007-09-20', 50),
(15, 'Raymond', '1994-04-01', 'M', '2010-12-03', '2010-12-03', 41)
select * from patient;

insert into Room (roomID, roomType, status, patientID) values


(1, 'Semi Private', 'F', 45),
(2, 'Semi Private', 'F', 21),
(3, 'Emergency', 'F', 31),
(4, 'Emergency', 'M', 18),
(5, 'Emergency', 'M', 6),
(6, 'Labor & Delivery', 'F', 5),
(7, 'Private', 'F', 47),
(8, 'Semi Private', 'M', 30),
(9, 'ICU', 'M', 29),
(10, 'Semi Private', 'F', 7),
(11, 'Standard ward', 'M', 49),
(12, 'Private', 'F', 8),
(13, 'Labor & Delivery', 'F', 16),
(14, 'Private', 'F', 38),
(15, 'Standard ward', 'F', 13)

select * from room;

insert into nurse (staffID, staffName, gender, roomID) values


(1, 'Jeremy', 'M', 16),
(2, 'Helen', 'F', 38),
(3, 'Amanda', 'F', 1),
(4, 'Ann', 'F', 19),
(5, 'Raymond', 'M', 8),
(6, 'Margaret', 'F', 47),
(7, 'Jimmy', 'M', 17),
(8, 'Ashley', 'F', 8),
(9, 'Julia', 'F', 6),
(10, 'Shirley', 'F', 28),
(11, 'Martin', 'M', 25),
(12, 'Wanda', 'F', 33),
(13, 'Aaron', 'M', 27),
(14, 'Norma', 'F', 41),
(15, 'Peter', 'M', 4)

select * from nurse;

insert into Receptionist (receptionistID, receptionistName, gender) values (1, 'Keith',


'M'),
(2, 'Charles', 'M'),
(3, 'Jesse', 'M'),
(4, 'Wanda', 'F'),
(5, 'Frank', 'M'),
(6, 'Helen', 'F'),
(7, 'Matthew', 'M'),
(8, 'Elizabeth', 'F'),
(9, 'Karen', 'F'),
(10, 'Annie', 'F'),
(11, 'Thomas', 'M'),
(12, 'Brian', 'M'),
(13, 'Clarence', 'M'),
(14, 'Gary', 'M'),
(15, 'Gloria', 'F')

select * from receptionist;

insert into Medicine (medicineCode, name, quantity, price, patientID) values


(1, 'CUBA ORIGINAL', 45, 6541, 44),
(2, 'Digoxin', 37, 6971, 12),
(3, 'Clear Proof Acne System', 46, 7154, 24),
(4, 'Vancomycin Hydrochloride', 7, 8871, 41),
(5, 'Ibuprofen', 10, 5204, 16),
(6, 'PINUS STROBUS POLLEN', 37, 9983, 44),
(7, 'Alendronate Sodium', 36, 6421, 12),
(8, 'Mellow Out', 30, 1685, 50),
(9, 'Colistimethate', 18, 7856, 24),
(10, 'PERFECTION LUMIERE', 48, 6852, 30),
(11, 'Lemon', 23, 3027, 45),
(12, 'Oxaprozin', 8, 5310, 43),
(13, 'Nifedipine', 40, 6346, 38),
(14, 'Fluconazole', 8, 9516, 5),
(15, 'Gerbil Epithelium', 25, 832, 34)

select * from medicine;

insert into Bill (billNo, amount, patientID) values


(1, 83870, 16),
(2, 14175, 26),
(3, 30951, 5),
(4, 90597, 8),
(5, 56159, 40),
(6, 94170, 17),
(7, 71349, 12),
(8, 16946, 32),
(9, 60293, 31),
(10, 17114, 23),
(11, 9548, 45),
(12, 19132, 4),
(13, 48195, 45),
(14, 94595, 23),
(15, 9153, 28)

select * from bill;

/*INDEXING*/

create index IndexPatient on Patient(PatientID);

select * from patient;

select patientID, patientName


from patient
where releaseDate is NULL;

select receptionistName
from Receptionist
where gender = 'M'

select *
from Bill

select name, price


from Medicine
where quantity > 44
order by(price);

select doctorName, specialization


from doctor

select count(roomID), roomType from Room


where status like 'Empty'
group by(roomType)
order by(roomType)
/*Fuctions*/

select count(*) as Total_Count


from patient
where admitDate >= '2010-01-01';

select avg(amount) as Average


from bill
where patientID = 18;

select patientID as ID, amount as Amount


from bill
where abs(amount-9000) <= 1000;

select max(patientName) from patient;

select (p.patientName) as Result


from Patient p
INNER JOIN Doctor d
ON p.patientID = d.patientID;

select * from patient


where admitDate = (cast(curdate() as Date));

select version() connection_id(), database(), schema();

select curdate(), now();

select year('2015-12-12') as year, quarter('2012-01-01'), hour('12:08:10');

/*INNER_JOIN*/
select p.patientID, p.patientName, m.name, m.price
from patient p
INNER JOIN Medicine m
ON p.patientID = m.patientID
where m.price > 7000
order by(patientID);

select p.patientID
from patient p
INNER JOIN Room r
ON p.patientID = r.patientID
where r.roomType like 'Semi Private'
and p.admitDate >= '2005-01-01'
group by (p.patientID);

/*--LEFT_OUTER_JOIN;*/
select staffID, staffName, n.roomID, roomType
from Nurse n
LEFT OUTER JOIN Room r
ON n.roomID = r.roomID
where gender = 'M';

select n.roomID, staffID, staffName,roomType


from Room r
LEFT OUTER JOIN Nurse n
ON n.roomID = r.roomID
where roomType = 'Emergency';

/*--RIGHT_OUTER_JOIN*/
select m.name as Medicine_Name, p.patientID, p.patientName as Patient_Name, m.price
from medicine m
RIGHT OUTER JOIN Patient p
ON m.patientID = p.patientID
where admitDate >= '2014-01-01';

select p.patientID, p.patientName as Patient_Name, admitDate, d.doctorName as


Doctor_Name, d.specialization
from Patient p
RIGHT OUTER JOIN Doctor d
ON p.patientID = d.patientID
order by (p.patientID);

/*--FULL JOIN*/
select *
from Nurse n
LEFT OUTER JOIN Room r
ON n.roomID = r.roomID
UNION
select *
from Nurse n
RIGHT OUTER JOIN Room r
ON n.roomID = r.roomID;

/*--CROSS JOIN*/
select *
from Patient p, Doctor d

/*Sub-queries*/
select receptionistID, receptionistName
from receptionist
where receptionistID IN (select receptionistID from patient where patientID = 7)
order by (receptionistID);

select patientID, patientName


from patient
where patientID IN (select patientID from bill where amount > 80000)
order by (patientID);

select doctorName
from doctor
where patientID IN (select patientID from bill
where amount = (select max(amount) from bill));

select patientName
from patient
where patientID < ANY (select patientID from doctor);

DROP USER 'admin'@'localhost';

use Hospital;
set sql_safe_updates = 0;

delimiter //
create procedure releasePatient(
IN patientID INT)
BEGIN
DECLARE releaseDat date;

select patient.releaseDate into @releaseDat from patient where patient.patientID =


patientID;

if releaseDat = NULL
then

Update patient set patient.releaseDate = (cast(CURDATE() as Date))


where patient.patientID = patientID;
end if;
end;//
delimiter ;

SET @patientID := 1;
call releasePatient (patientID);
select * from patient where patientID = 1;

use Hospital;

start transaction;
savepoint sp1;

update bill set amount = (amount - ((0.1) * amount))


where patientID = 5;

select * from bill where patientID = 5;

rollback to sp1;

select * from bill where patientID = 5;

update bill set amount = amount - ((0.1) * amount)


where patientID = 5;

update bill set amount = amount + ((0.1) * amount)


where patientID = 8;

commit;
--------------------------------------

LOCK table bill read;


start transaction;
savepoint sp1;

update bill set amount = (amount - ((0.1) * amount))


where patientID = 5;

rollback to sp1;

select * from bill where patientID = 5;


update bill set amount = amount + ((0.1) * amount)
where patientID = 8;

commit;

Unlock tables;

use Hospital;

DELIMITER $$
CREATE TRIGGER getRoom
AFTER INSERT
on Patient
For each row
BEGIN
IF EXISTS (SELECT * FROM Patient where admitDate = (cast(CURDATE() as Date)))
THEN
update room set patientID = new.patientID, status = 'Filled'
where status = 'Empty' LIMIT 1;
END IF;
END; $$
DELIMITER ;

insert into patient (patientID, patientName, dob, gender, admitDate) value


(51, 'Shivam', '1989-04-19', 'M', CURDATE());

select * from patient where patientID = 51;

select * from room where patientID = 51;

select * from nurse where roomID = 4;

use Hospital;

delimiter $$
create trigger vacateRoom
after update on patient
for each row
begin

declare patientID INT;


declare releaseDate date;
declare newroomId INT;

SET patientID = old.PatientID;


set releaseDate= new.outDate;
SET newroomId = (select RoomID from room where room.PatientID = patientID);
end;$$
delimiter ;

select roomId;
select patientID;

drop trigger vacateRoom;


use Hospital;

drop trigger deletePatient;

delimiter $$
create trigger deletePatient
after delete on patient
for each row
begin

declare newPatId INT;


declare newroomId INT;

set newPatientId= old.PatientID;


SET newroomId = (select RoomID from room where room.PatientID = newpatientID);

update room set room.Status = 'Empty',


PatientID = null where RoomID= newroomId;

delete from medicine where medicine.PatientID = newPatientId;

delete from bill where bill.PatientID = newPatientId;

end;$$
delimiter ;

SET foreign_key_checks = 0;
delete from patient where PatientID = 11;

select * from room WHERE roomID = (select newroomId) ;


select * from medicine WHERE Medicine.PatientID = (select newPatientId) ;
select * from bill WHERE bill.PatientID = (select newPatientId) ;

create view doctor_specialization AS


select doctorID, doctorName, specialization
from doctor
where specialization = 'Cardiologist';

select * from doctor_specialization;

create view Room_View AS


select n.roomID, staffID, staffName,roomType
from Room r
LEFT OUTER JOIN Nurse n
ON n.roomID = r.roomID
where roomType = 'Emergency'
and status = 'Empty';

select * from room_view;

create view Doctor_View AS


select doctorID as ID, doctorName as Name
from doctor
where patientID IN (select patientID from bill
where amount >= (select avg(amount) from bill));

select * from doctor_view;

You might also like