0% found this document useful (0 votes)
7 views

DBMS Exp7

Database Management

Uploaded by

garvgoyal2022
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

DBMS Exp7

Database Management

Uploaded by

garvgoyal2022
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

VIT BHOPAL UNIVERSITY

CSE3001 – Database Management Systems

22BCE10781
REG.NO :_________________________
NAME :_________________________
Garv Goyal

BTech CSE
BRANCH :_________________________
Fall Semester 2024-25
SEMESTER:_________________________
Raise an exception if prescription and treatment received from
EXP.NO: 07
same doctor

Query

Assuming a patient
should not receive both treatment and prescription from the same doctor, write
a program to find out all the doctor who provide both treatment and
prescription to the same patient. In addition, raise and display an exception
if this situation occurs.

Table Creation
Create the doctors table:
CREATE TABLE doctors (
doctor_id INT PRIMARY KEY,
doctor_name VARCHAR(100)
);

INSERT INTO doctors (doctor_id, doctor_name) VALUES


(1, 'Dr. Smith'),
(2, 'Dr. Johnson');

doctors Table:

Doctor_id Doctor_name
1 Dr. Smith
2 Dr. Johnson

Create the patients table:


CREATE TABLE patients (
patient_id INT PRIMARY KEY,
patient_name VARCHAR(100)
);

INSERT INTO patients (patient_id, patient_name) VALUES


(1, 'John Doe'),
(2, 'Jane Doe');

Patients Table:

Patient_id Patient_name
1 John Doe
2 Jane Doe
Create the treatments table:
CREATE TABLE treatments (
treatment_id INT PRIMARY KEY,
patient_id INT,
doctor_id INT,
treatment_date DATE,
FOREIGN KEY (patient_id) REFERENCES patients(patient_id),
FOREIGN KEY (doctor_id) REFERENCES doctors(doctor_id)
);

INSERT INTO treatments (treatment_id, patient_id, doctor_id, treatment_date) VALUES


(1, 1, 1, '2024-08-01'),
(2, 2, 2, '2024-08-02');

Treatments table:

Treatment_id Patient_id Doctor_id Treatment_date


1 1 1 2024-08-01
2 2 2 2024-08-02

Create the prescriptions table:


CREATE TABLE prescriptions (
prescription_id INT PRIMARY KEY,
patient_id INT,
doctor_id INT,
prescription_date DATE,
FOREIGN KEY (patient_id) REFERENCES patients(patient_id),
FOREIGN KEY (doctor_id) REFERENCES doctors(doctor_id)
);

INSERT INTO prescriptions (prescription_id, patient_id, doctor_id, prescription_date) VALUES


(1, 1, 1, '2024-08-03'),
(2, 2, 1, '2024-08-04');

Prescriptions table:

Prescription_id Patient_id Doctor_id Prescription_date


1 1 1 2024-08-03
2 2 1 2024-08-04
Create Trigger:
CREATE TRIGGER before_insert_prescriptions
BEFORE INSERT ON prescriptions
FOR EACH ROW
BEGIN
DECLARE doctor_id INT;

SELECT t.doctor_id
INTO doctor_id
FROM treatments t
WHERE t.patient_id = NEW.patient_id AND t.doctor_id = NEW.doctor_id;

IF doctor_id IS NOT NULL THEN


SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Doctor provides both treatment and
prescription to the same patient';
END IF;
END //

You might also like