Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8949873
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T13:19:12+00:00 2026-06-15T13:19:12+00:00

I have a table called ward (dealing with hospitals): Create table ward( Wno varchar(15)

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T13:19:13+00:00Added an answer on June 15, 2026 at 1:19 pm

    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.

    Create table ward(
        Wno varchar(15) Not null 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,
        WardNo varchar(15),
        foreign key (wardno) references ward (wno)  ' -- adds the foreign key relation
     );
    

    In order to check if ward is full, you can have an insert or update trigger

    Free bed count can be obtained by following query:

    SELECT p.wardno, (w.number_of_beds - count(pid)) as freebeds
    from patient as p
    left join ward as w
    on p.wardno = w.wno
    group by wardno
    

    Now we create a trigger to check if any patient is going into a ward whre freebed count = 0.

    Updated to Oracle Version

     CREATE OR REPLACE TRIGGER FreeBedsWardTrigger
           BEFORE UPDATE OR INSERT
           ON patient
           FOR EACH ROW
        DECLARE
           max_beds    INTEGER;    -- max number of beds for the ward
           used_beds   INTEGER;    -- used beds for the ward
        BEGIN
           SELECT   COUNT (pid)
             INTO   used_beds
             FROM   patient
            WHERE   wardno = :NEW.wardno;
    
           SELECT   number_of_beds
             INTO   max_beds
             FROM   ward
            WHERE   wno = :NEW.wardno;
    
           IF (max_beds - used_beds) > 0
           THEN
              RETURN;
           ELSE
              RAISE_APPLICATION_ERROR (-100100,
                                       'No more beds available in this ward.');
           END IF;
        END;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have table name called Person with following column names P_Id(int), LastName(varchar), FirstName (varchar).
I have a table called products with primary key Id . I want to
i have table called as Support, which have a field named Name and contains
I'm dealing with (for me) huge SQL problem. I have table called timeshifts which
I have a table called employee with following fields Id | Name | PrimaryEmail1
I have a table called USERS that has a foreign key to the table
I have table called stats . In am inserting yes or no in the
I have table called Buttons. Buttons table i have column button_number . Table contain
I have table called posts in my DB. Each post has field called social_network
I have a table called 'MyTable' that looks like: ID | Item | Type

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.