I have a table called ward (dealing with hospitals):
Create table ward(
Wno varchar(15) Primary Key,
Name varchar(20) Not Null,
Number_of_beds integer Not Null
);
And a table for patients:
Create table patient(
Pid varchar(15) Primary Key,
Name varchar(20) Not Null,
Address varchar(50) Not Null,
Date_of_birth date Not Null
);
I need to constrain the patients somehow so that if a patient is assigned to a specific ward then the number of patients can’t exceed the number of beds in the ward.
I thought of adding the Wno as a foreign key to the patient table, but don’t really know where to go from there.
You may add the foreign key into patient table as following. Apology this query is in MYSQL. I noticed that you need Oracle though. However the logic is similar 🙂 The syntax needs changes.
And a table for patients:
In order to check if ward is full, you can have an
insertorupdate triggerFree bed count can be obtained by following query:
Now we create a trigger to check if any patient is going into a ward whre freebed count = 0.
Updated to Oracle Version