Professional Documents
Culture Documents
/*INDEXING*/
select receptionistName
from Receptionist
where gender = 'M'
select *
from Bill
/*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';
/*--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';
/*--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 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);
use Hospital;
set sql_safe_updates = 0;
delimiter //
create procedure releasePatient(
IN patientID INT)
BEGIN
DECLARE releaseDat date;
if releaseDat = NULL
then
SET @patientID := 1;
call releasePatient (patientID);
select * from patient where patientID = 1;
use Hospital;
start transaction;
savepoint sp1;
rollback to sp1;
commit;
--------------------------------------
rollback to sp1;
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 ;
use Hospital;
delimiter $$
create trigger vacateRoom
after update on patient
for each row
begin
select roomId;
select patientID;
delimiter $$
create trigger deletePatient
after delete on patient
for each row
begin
end;$$
delimiter ;
SET foreign_key_checks = 0;
delete from patient where PatientID = 11;