From cfdb8d7d9fac4fd9362463f448b4f4dbddb1bd62 Mon Sep 17 00:00:00 2001 From: "github-classroom[bot]" <66690702+github-classroom[bot]@users.noreply.github.com> Date: Sun, 28 May 2023 13:24:30 +0000 Subject: [PATCH 1/4] Setting up GitHub Classroom Feedback From fc887f3086795123bf8981f9640e9d5b8a1c9c18 Mon Sep 17 00:00:00 2001 From: Atiqul Islam Atik <98636274+atik107@users.noreply.github.com> Date: Mon, 29 May 2023 01:56:26 +0600 Subject: [PATCH 2/4] Add files via upload --- DDL.sql | 71 +++++++++++++++++++++ DML.sql | 163 +++++++++++++++++++++++++++++++++++++++++++++++ PLSQL.sql | 184 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 418 insertions(+) create mode 100644 DDL.sql create mode 100644 DML.sql create mode 100644 PLSQL.sql diff --git a/DDL.sql b/DDL.sql new file mode 100644 index 0000000..b61b6c3 --- /dev/null +++ b/DDL.sql @@ -0,0 +1,71 @@ +drop table patient_doctor; +drop table patient_medicine; +drop table medicine; +drop table bill; +drop table doctor; +drop table patient; + + + +Create table medicine( +medicine_id number(20), +name varchar(20), +stock number(20), +primary key(medicine_id) +); + +create table patient( +patient_id number(20), +p_name varchar(20), +age number(20), +gender varchar(20), +blood_group varchar(20), +primary key(patient_id) +); + + +create table bill( +bill_id number(20), +bill_amount number(20), +patient_id number(20), +name varchar(20), +date_bill varchar(20), +primary key(bill_id), +foreign key (patient_id) references patient(patient_id) +); + + +create table doctor( +doctor_id number(20), +name varchar(20), +gender varchar(20), +designation varchar(20), +primary key(doctor_id) +); + + +Create table patient_medicine( +medicine_id number(20), +patient_id number(20), +foreign key(medicine_id) references medicine(medicine_id), +foreign key(patient_id) references patient(patient_id) +); + + +Create table patient_doctor( +doctor_id number(20), +patient_id number(20), +date_patient varchar(20), +foreign key (patient_id) references patient(patient_id), +foreign key(doctor_id) references doctor(doctor_id) +); + + +ALTER TABLE patient ADD (address VARCHAR(50)); + +ALTER TABLE patient RENAME COLUMN address TO permanent_address; + +ALTER TABLE patient DROP COLUMN permanent_address; + +set pagesize 180 +set linesize 180 \ No newline at end of file diff --git a/DML.sql b/DML.sql new file mode 100644 index 0000000..e51b67c --- /dev/null +++ b/DML.sql @@ -0,0 +1,163 @@ +-- DML + + +-- Inserting data into the `doctor` table + +INSERT INTO doctor (doctor_id, name, gender, designation) VALUES (1, 'Dr. Smith', 'Male', 'Cardiologist'); +INSERT INTO doctor (doctor_id, name, gender, designation) VALUES (2, 'Dr. Johnson', 'Female', 'Pediatrician'); +INSERT INTO doctor (doctor_id, name, gender, designation) VALUES (3, 'Dr. Lee', 'Male', 'Dermatologist'); +INSERT INTO doctor (doctor_id, name, gender, designation) VALUES (4, 'Dr. Williams', 'Female', 'Obstetrician'); +INSERT INTO doctor (doctor_id, name, gender, designation) VALUES (5, 'Dr. Brown', 'Male', 'Surgeon'); +INSERT INTO doctor (doctor_id, name, gender, designation) VALUES (6, 'Dr. Davis', 'Female', 'Neurologist'); +INSERT INTO doctor (doctor_id, name, gender, designation) VALUES (7, 'Dr. Wilson', 'Male', 'Orthopedist'); +INSERT INTO doctor (doctor_id, name, gender, designation) VALUES (8, 'Dr. Taylor', 'Female', 'Psychiatrist'); +INSERT INTO doctor (doctor_id, name, gender, designation) VALUES (9, 'Dr. Anderson', 'Male', 'Oncologist'); +INSERT INTO doctor (doctor_id, name, gender, designation) VALUES (10, 'Dr. Thomas', 'Female', 'Endocrinologist'); + + + +-- Inserting data into the `patient` table +INSERT INTO patient (patient_id, p_name, age, gender, blood_group) VALUES (1, 'John Doe', 35, 'Male', 'A+'); +INSERT INTO patient (patient_id, p_name, age, gender, blood_group) VALUES (2, 'Jane Smith', 45, 'Female', 'B-'); +INSERT INTO patient (patient_id, p_name, age, gender, blood_group) VALUES (3, 'David Lee', 28, 'Male', 'O+'); +INSERT INTO patient (patient_id, p_name, age, gender, blood_group) VALUES (4, 'Emily Chen', 62, 'Female', 'AB+'); +INSERT INTO patient (patient_id, p_name, age, gender, blood_group) VALUES (5, 'Michael Wong', 50, 'Male', 'O-'); +INSERT INTO patient (patient_id, p_name, age, gender, blood_group) VALUES (6, 'Sarah Johnson', 42, 'Female', 'A-'); +INSERT INTO patient (patient_id, p_name, age, gender, blood_group) VALUES (7, 'Robert Brown', 31, 'Male', 'B+'); +INSERT INTO patient (patient_id, p_name, age, gender, blood_group) VALUES (8, 'Lisa Davis', 39, 'Female', 'O+'); +INSERT INTO patient (patient_id, p_name, age, gender, blood_group) VALUES (9, 'William Wilson', 56, 'Male', 'AB-'); +INSERT INTO patient (patient_id, p_name, age, gender, blood_group) VALUES (10, 'Olivia Taylor', 27, 'Female', 'A+'); + + + +-- Inserting data into the `medicine` table +INSERT INTO medicine (medicine_id, name, stock) VALUES (1, 'Paracetamol', 100); +INSERT INTO medicine (medicine_id, name, stock) VALUES (2, 'Ibuprofen', 50); +INSERT INTO medicine (medicine_id, name, stock) VALUES (3, 'Amoxicillin', 75); +INSERT INTO medicine (medicine_id, name, stock) VALUES (4, 'Aspirin', 200); +INSERT INTO medicine (medicine_id, name, stock) VALUES (5, 'Omeprazole', 30); +INSERT INTO medicine (medicine_id, name, stock) VALUES (6, 'Diphenhydramine', 50); +INSERT INTO medicine (medicine_id, name, stock) VALUES (7, 'Hydrochlorothiazide', 20); +INSERT INTO medicine (medicine_id, name, stock) VALUES (8, 'Simvastatin', 100); +INSERT INTO medicine (medicine_id, name, stock) VALUES (9, 'Metformin', 80); +INSERT INTO medicine (medicine_id, name, stock) VALUES (10, 'Sertraline', 60); + + + +-- Inserting data into the `bill` table +INSERT INTO bill (bill_id,bill_amount, patient_id, name, date_bill) VALUES (1, 1500, 1, 'John Doe', '2023-05-01'); +INSERT INTO bill (bill_id,bill_amount, patient_id, name, date_bill) VALUES (2, 250, 2, 'Jane Smith', '2023-05-01'); +INSERT INTO bill (bill_id,bill_amount, patient_id, name, date_bill) VALUES (3, 300, 3, 'David Lee', '2023-05-02'); +INSERT INTO bill (bill_id,bill_amount, patient_id, name, date_bill) VALUES (4, 550, 4, 'Emily Chen', '2023-05-02'); +INSERT INTO bill (bill_id,bill_amount, patient_id, name, date_bill) VALUES (5, 250, 5, 'Michael Wong', '2023-05-05'); +INSERT INTO bill (bill_id,bill_amount, patient_id, name, date_bill) VALUES (6, 100, 6, 'Sarah Johnson', '2023-05-05'); +INSERT INTO bill (bill_id,bill_amount, patient_id, name, date_bill) VALUES (7, 500, 7, 'Robert Brown', '2023-05-07'); +INSERT INTO bill (bill_id,bill_amount, patient_id, name, date_bill) VALUES (8, 410, 8, 'Lisa Davis', '2023-05-08'); +INSERT INTO bill (bill_id,bill_amount, patient_id, name, date_bill) VALUES (9, 310, 9, 'William Wilson', '2023-05-08'); +INSERT INTO bill (bill_id,bill_amount, patient_id, name, date_bill) VALUES (10,110, 10, 'Olivia Taylor', '2023-05-10'); + + + +-- Inserting data into the `patient_doctor` table +INSERT INTO patient_doctor (doctor_id, patient_id, date_patient) VALUES (1, 1, '2023-05-01'); +INSERT INTO patient_doctor (doctor_id, patient_id, date_patient) VALUES (2, 2, '2023-05-02'); +INSERT INTO patient_doctor (doctor_id, patient_id, date_patient) VALUES (3, 3, '2023-05-03'); +INSERT INTO patient_doctor (doctor_id, patient_id, date_patient) VALUES (4, 4, '2023-05-04'); +INSERT INTO patient_doctor (doctor_id, patient_id, date_patient) VALUES (5, 5, '2023-05-05'); +INSERT INTO patient_doctor (doctor_id, patient_id, date_patient) VALUES (6, 6, '2023-05-06'); +INSERT INTO patient_doctor (doctor_id, patient_id, date_patient) VALUES (7, 7, '2023-05-07'); +INSERT INTO patient_doctor (doctor_id, patient_id, date_patient) VALUES (8, 8, '2023-05-08'); +INSERT INTO patient_doctor (doctor_id, patient_id, date_patient) VALUES (9, 9, '2023-05-09'); +INSERT INTO patient_doctor (doctor_id, patient_id, date_patient) VALUES (10, 10, '2023-05-10'); + + + +-- Inserting data into the `patient_medicine` table +INSERT INTO patient_medicine (medicine_id, patient_id) VALUES (1, 1); +INSERT INTO patient_medicine (medicine_id, patient_id) VALUES (2, 1); +INSERT INTO patient_medicine (medicine_id, patient_id) VALUES (3, 2); +INSERT INTO patient_medicine (medicine_id, patient_id) VALUES (4, 10); +INSERT INTO patient_medicine (medicine_id, patient_id) VALUES (5, 4); +INSERT INTO patient_medicine (medicine_id, patient_id) VALUES (6, 5); +INSERT INTO patient_medicine (medicine_id, patient_id) VALUES (7, 5); +INSERT INTO patient_medicine (medicine_id, patient_id) VALUES (8, 6); +INSERT INTO patient_medicine (medicine_id, patient_id) VALUES (9, 7); +INSERT INTO patient_medicine (medicine_id, patient_id) VALUES (10, 8); + + + +-- print all table data + +SELECT * FROM doctor; +SELECT * FROM patient; +SELECT * FROM medicine; +SELECT * FROM bill; +SELECT * FROM patient_doctor; +SELECT * FROM patient_medicine; + + + + +-- update +update bill set bill_amount= 1200 where bill_id= 9; + + +-- delet +INSERT INTO patient (patient_id, p_name, age, gender, blood_group) VALUES (11, 'Atik', 25, 'Male', 'O+'); +delete from patient where patient_id=11; + +-- Union +select p_name from patient where p_name like 'R%' union select p_name from patient where p_name like '%E%'; + + +-- Aggregate function +select count(p_name) as number_of_patient from patient; +select count(name) as number_of_doctor from doctor; + +select avg(bill_amount) as average_bill_of_patient from bill; +select sum(bill_amount) as total_bill_amount from bill; + +select sum(stock) as total_medicine_count from medicine; +select max(stock) as Max_medicine_count from medicine; +select min(stock) as Min_medicine_count from medicine; + + +select max(bill_amount) as Max_bill_of_patient from bill; +select min(bill_amount) as Min_bill_of_patient from bill; + +-- group by +select date_bill,avg(bill_amount) as average_bill_on_same_date from bill group by date_bill; +select date_bill,avg(bill_amount) as average_bill_on_same_date from bill group by date_bill having avg(bill_amount) >= 800; + +-- nested sub query +select patient_id,bill_amount from bill +where bill_id=(select patient_id from patient where p_name='Olivia Taylor'); + + + select * from medicine + where medicine_id= (select medicine_id from patient_medicine + where patient_id=(select patient_id from patient where p_name='Olivia Taylor')); + + +select * from bill +where bill_id=(select patient_id from patient where p_name='Olivia Taylor'); + + + +-- set Membership +select * from patient where age <=30; +select * from patient where age <=30 and p_name like '%D%'; + +-- join +select * from doctor natural join patient where doctor_id=7; +-- create view doctor_demo1 as select doctor_id,name from doctor; + + +-- string operation +SELECT * FROM patient WHERE p_name LIKE '___'; +SELECT * FROM doctor WHERE name LIKE '%D%'; +SELECT * FROM doctor WHERE name LIKE 'E%'; + +SELECT * FROM medicine WHERE name LIKE '___'; +SELECT * FROM medicine WHERE name LIKE '%A%'; +SELECT * FROM medicine WHERE name LIKE 'P%'; \ No newline at end of file diff --git a/PLSQL.sql b/PLSQL.sql new file mode 100644 index 0000000..31c77d0 --- /dev/null +++ b/PLSQL.sql @@ -0,0 +1,184 @@ + +-- PL/SQL + + +SET SERVEROUTPUT ON + +DECLARE + med_id medicine.medicine_id%TYPE; + med_name medicine.name%TYPE; + med_stock medicine.stock%TYPE; +BEGIN + SELECT medicine_id, name, stock INTO med_id, med_name, med_stock + FROM medicine + WHERE medicine_id = 1; -- Replace with the desired medicine_id + + DBMS_OUTPUT.PUT_LINE('Medicine ID: ' || med_id || ' | Name: ' || med_name || ' | Stock: ' || med_stock); +END; +/ + + + + +-- Insert and set default value + +SET SERVEROUTPUT ON + +DECLARE + medicine_id medicine.medicine_id%TYPE := 11; -- Replace with desired medicine_id + name medicine.name%TYPE := 'New Medicine'; + stock medicine.stock%TYPE := 50; +BEGIN + INSERT INTO medicine (medicine_id, name, stock) + VALUES (medicine_id, name, stock); + + DBMS_OUTPUT.PUT_LINE('New Row inserted successfully.'); +END; +/ + +select * from medicine; + + + +-- Row type + +SET SERVEROUTPUT ON + +DECLARE + med_row medicine%ROWTYPE; +BEGIN + SELECT * INTO med_row FROM medicine WHERE medicine_id = 1; -- Replace with desired medicine_id + + -- Access the individual columns of the fetched row using the variable + DBMS_OUTPUT.PUT_LINE('Medicine ID: ' || med_row.medicine_id); + DBMS_OUTPUT.PUT_LINE('Name: ' || med_row.name); + DBMS_OUTPUT.PUT_LINE('Stock: ' || med_row.stock); +END; +/ + + + + + + + +-- Cursor and row count + +SET SERVEROUTPUT ON + +DECLARE + CURSOR med_cursor IS + SELECT * FROM medicine; + + med_row medicine%ROWTYPE; +BEGIN + OPEN med_cursor; + LOOP + FETCH med_cursor INTO med_row; + EXIT WHEN med_cursor%NOTFOUND; + + -- Access the individual columns of the fetched row using the variable + DBMS_OUTPUT.PUT_LINE('Medicine ID: ' || med_row.medicine_id); + DBMS_OUTPUT.PUT_LINE('Name: ' || med_row.name); + DBMS_OUTPUT.PUT_LINE('Stock: ' || med_row.stock); + DBMS_OUTPUT.PUT_LINE('Row count: ' || med_cursor%ROWCOUNT); + END LOOP; + CLOSE med_cursor; +END; +/ + + + + + + + +-- ARRAY + +SET SERVEROUTPUT ON + +DECLARE + counter NUMBER := 1; + med_name2 medicine.name%TYPE; + TYPE NAMEARRAY IS VARRAY(5) OF medicine.name%TYPE; + A_NAME NAMEARRAY := NAMEARRAY('Medicine 1', 'Medicine 2', 'Medicine 3', 'Medicine 4', 'Medicine 5'); + -- VARRAY with a fixed size of 5 elements and initialized with medicine names +BEGIN + counter := 1; + FOR x IN 12..16 + LOOP + BEGIN + SELECT name INTO med_name2 FROM medicine WHERE medicine_id = x; + A_NAME(counter) := med_name2; + counter := counter + 1; + EXCEPTION + WHEN NO_DATA_FOUND THEN + DBMS_OUTPUT.PUT_LINE('No data found for medicine_id ' || x); + END; + END LOOP; + + counter := 1; + WHILE counter <= A_NAME.COUNT + LOOP + DBMS_OUTPUT.PUT_LINE(A_NAME(counter)); + counter := counter + 1; + END LOOP; +END; +/ + + + + +-- If else +SET SERVEROUTPUT ON + +DECLARE + counter NUMBER := 1; + med_name medicine.name%TYPE; + TYPE NAMEARRAY IS VARRAY(5) OF medicine.name%TYPE; + A_NAME NAMEARRAY := NAMEARRAY('Medicine 1', 'Medicine 2', 'Medicine 3', 'Medicine 4', 'Medicine 5'); + -- VARRAY with a fixed size of 5 elements and initialized with medicine names +BEGIN + counter := 1 ; + FOR x IN 7..10 + LOOP + SELECT name INTO med_name FROM medicine WHERE medicine_id = x; + IF med_name = 'Sertraline' THEN + DBMS_OUTPUT.PUT_LINE(med_name || ' belongs to Department A'); + ELSIF med_name = 'Metformin' THEN + DBMS_OUTPUT.PUT_LINE(med_name || ' belongs to Department B'); + ELSE + DBMS_OUTPUT.PUT_LINE(med_name || ' belongs to Other Department'); + END IF; + END LOOP; +END; +/ + + + + +CREATE OR REPLACE PROCEDURE update_stock( + med_id IN NUMBER, + new_stock IN NUMBER +) +AS +BEGIN + UPDATE medicine + SET stock = new_stock + WHERE medicine_id = med_id; + + DBMS_OUTPUT.PUT_LINE('Stock for medicine with ID ' || med_id || ' updated to ' || new_stock); +EXCEPTION + WHEN NO_DATA_FOUND THEN + DBMS_OUTPUT.PUT_LINE('Medicine with ID ' || med_id || ' not found.'); +END; +/ + + +DECLARE + med_id_input NUMBER := 12; -- Provide the medicine ID for which stock needs to be updated + new_stock_input NUMBER := 50; -- Provide the new stock value +BEGIN + update_stock(med_id_input, new_stock_input); +END; +/ From a430698298a386cae2502143082a41c86a1b9581 Mon Sep 17 00:00:00 2001 From: Atiqul Islam Atik <98636274+atik107@users.noreply.github.com> Date: Mon, 29 May 2023 02:02:36 +0600 Subject: [PATCH 3/4] Create README.md --- README.md | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..19eabe4 --- /dev/null +++ b/README.md @@ -0,0 +1,6 @@ +# Hospital Management Database project
+- ### Student Name: Atiqul Islam Atik
+- ### Roll No: 1907107
+- ### Group: B2
+- ### Year: 3rd
+- ### Semester: 1st
From 2fe6f8750032ff76c3fe014600c4e1adc84c8b14 Mon Sep 17 00:00:00 2001 From: Atiqul Islam Atik <98636274+atik107@users.noreply.github.com> Date: Sun, 9 Jul 2023 08:06:18 +0600 Subject: [PATCH 4/4] Trigger is added --- Trigger.sql | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 Trigger.sql diff --git a/Trigger.sql b/Trigger.sql new file mode 100644 index 0000000..17b723f --- /dev/null +++ b/Trigger.sql @@ -0,0 +1,39 @@ +CREATE OR REPLACE TRIGGER update_stock +AFTER INSERT ON patient_medicine +FOR EACH ROW +BEGIN + UPDATE medicine + SET stock = stock - 1 + WHERE medicine_id = :new.medicine_id; +END; +/ + + + + + +CREATE OR REPLACE TRIGGER calculate_bill_amount +BEFORE INSERT ON bill +FOR EACH ROW +BEGIN + SELECT SUM(stock) * 10 + INTO :new.bill_amount + FROM medicine + JOIN patient_medicine ON medicine.medicine_id = patient_medicine.medicine_id + WHERE patient_medicine.patient_id = :new.patient_id; +END; +/ + + + + + +CREATE OR REPLACE TRIGGER validate_age +BEFORE INSERT ON patient +FOR EACH ROW +BEGIN + IF :new.age < 0 OR :new.age > 150 THEN + RAISE_APPLICATION_ERROR(-20001, 'Invalid age'); + END IF; +END; +/