PostgreSQL
DDL ASSIGNMENT2 - Hospital Management System
Objective:
You are required to address a set of Data Definition Language (DDL) tasks for a Hospital Management System. Each query focuses on distinct DDL tasks, including the creation of tables, application of constraints, and establishment of relationships between tables.
Requirements:
You will need to complete at least 20 different DDL tasks, covering table creation, primary keys, foreign keys, constraints, and indexes. Each task represents a key part of designing the structure of the Hospital Management System.
Question 1: Create the DEPARTMENT Table with a Foreign Key
Define a DEPARTMENT table:
department_idas a primary key with auto-increment functionality.department_nameas a string of maximum length 255 characters, not null.created_dateas a timestamp field, default to current timestamp, not null.
Question 2: Create the DOCTOR Table with Constraints
Create a DOCTOR table with the following specifications:
doctor_idas a primary key with auto-increment functionality.first_nameas a string with a maximum length of 255 characters, not null.last_nameas a string with a maximum length of 255 characters, not null.specializationas a string with a maximum length of 255 characters, not null.emailas a unique string, not null.per_visit_costas a decimal (10, 2), not null.activeas a boolean, default to yes, not null.login_idcan use alpha numeric login id or email as login id, not null.created_dateas a timestamp field, default to current timestamp, not null.
Question 3: Define the DOCTOR_DEPARTMENT Table
Write the SQL statement to create a many-to-many relationship between doctors and departments. This will require the DOCTOR_DEPARTMENT table, which includes:
doctor_idas a foreign key referencing the DOCTOR table.department_idas a foreign key referencing the DEPARTMENT table.
Question 4: Create the NURSE Table
Create a NURSE table:
nurse_idas a primary key with auto-increment functionality.first_nameas a string with a maximum length of 255 characters, not null.last_nameas a string with a maximum length of 255 characters, not null.emailas a unique string, not null.hire_dateas a date field, not null.activeas a boolean, not null.login_idcan use alpha numeric login id or email as login id, not null.created_dateas a timestamp field, default to current timestamp, not null.
Question 5: Define the ROOM Table with Relationships
Write the SQL statement to create a ROOM table that includes:
room_idas a primary key with auto-increment functionality.room_numberas a string of maximum length 50 characters, not null.room_typeas a string (e.g., "General", "ICU", "Private"), not null.daily_rateas a decimal (10, 2), not null.created_dateas a timestamp field, default to current timestamp, not null.
Question 6: Create the PATIENT Table
Write a DDL SQL statement to create a PATIENT table with the following requirements:
patient_idas a primary key with auto-increment functionality.first_nameas a string of maximum length 255 characters, not null.last_nameas a string of maximum length 255 characters, not null.date_of_birthas a date field, not null.emailas a unique string, not null.phone_numberas a string, not null.created_dateas a timestamp field, default to current timestamp, not null.
Question 7: Create the PATIENT_ADMISSION Table
Define a PATIENT_ADMISSION table with the following specifications:
admission_idas a primary key with auto-increment functionality.admit_typeas a string, not null.treatment_descriptionas a string of maximum length 255 characters, not null.treatment_start_dateas a date field, not null.treatment_end_dateas a date field, null.patient_idas a foreign key referencing the PATIENT table, not null.created_dateas a timestamp field, default to current timestamp, not null.- Add a
CHECKconstraint to ensure theadmit_typeis In Patient Or Out Patient.
Question 8: Create the APPOINTMENT Table
Define an APPOINTMENT table with the following details:
appointment_idas a primary key with auto-increment functionality.appointment_dateas a date field, not null.appointment_timeas a time field, not null.statusas a string (e.g., "Scheduled", "Completed", "Cancelled"), not null.doctor_idas a foreign key referencing the DOCTOR table, not null.room_idas a foreign key referencing the ROOM table, not null.incharge_nurse_idas a foreign key referencing the NURSE table, not null.admission_idas a foreign key referencing the PATIENT_ADMISSION table, not null.created_dateas a timestamp field, default to current timestamp, not null.
Question 9: Create the ROOM_ADMIT Table
Define an ROOM_ADMIT table with the following details:
admit_idas a primary key with auto-increment functionality.start_dateas a date field, not null.end_dateas a date field, not null.room_idas a foreign key referencing the ROOM table, not null.incharge_nurse_idas a foreign key referencing the NURSE table, not null.admission_idas a foreign key referencing the PATIENT_ADMISSION table, not null.created_dateas a timestamp field, default to current timestamp, not null.
Question 10: Create the MEDICATION Table
Write a DDL SQL statement to create a MEDICATION table with the following details:
medication_idas the primary key with auto-increment functionality.medication_nameas a string of maximum length 255 characters, not null.medication_typeas a string of maximum length 100 characters, not null.dosageas a string, not null.created_dateas a timestamp field, default to current timestamp, not null.
Question 11: Create the PRESCRIPTION Table
Write a DDL SQL statement to create a PRESCRIPTION table:
prescription_idas the primary key with auto-increment functionality.prescription_dateas a date field, not null.medication_idas a foreign key referencing the MEDICATION table, not null.doctor_idas a foreign key, not null.start_dateas a date field, not null.end_dateas a date field, not null.morning_flagas a boolean, not null.noon_flagas a boolean, not null.evening_flagas a boolean, not null.admission_idas a foreign key referencing the PATIENT_ADMISSION table, not null.created_dateas a timestamp field, default to current timestamp, not null.
Question 12: Create the BILLING Table with Constraints
Write a DDL SQL statement to create a BILLING table:
billing_idas a primary key with auto-increment functionality.bill_dateas date, not null.bill_typeas string, not null.descriptionas string, not null.total_amountas a decimal (10, 2), not null.balance_amountas a decimal (10, 2), not null.admission_idas a foreign key referencing the PATIENT_ADMISSION table, not null.created_dateas a timestamp field, default to current timestamp, not null.- Add a
CHECKconstraint to ensure thebill_typeis Doctor Appointment or Medicine or Room Charge or Diagnostic or Food or Miscellaneous. - Add a
CHECKconstraint to ensure thattotal_amountis greater than 0.
Question 13: Create the PAYMENT Table with Constraints
Write a DDL SQL statement to create a PAYMENT table:
payment_idas a primary key with auto-increment functionality.bill_idas a foreign key referencing the BILLING table, not null.patient_idas a foreign key referencing the PATIENT table, not null.amount_paidas a decimal (10, 2), not null.payment_dateas a datetime, not null.- Add a
CHECKconstraint to ensure thatamount_paidis greater than 0.
Question 14: Create the USER_LOGIN Table
Write a DDL SQL statement to create an USER_LOGIN table:
user_idas a primary key with auto-increment functionality.login_idcan use alpha numeric or email as login id, not null.passwordmust encrypt the password before storing it in the database table when required.active_flaguse numeric, 0 or 1, you can lock the user login when required, not null.last_login_datetimeas timestamp, last successful login date for a given user.activeas boolean, default to yes, not null.created_dateas a timestamp field, default to current timestamp, not null.
Question 15: Create the AUDIT_LOG Table
Define an AUDIT_LOG table to track changes in the hospital management system:
log_idas a primary key with auto-increment functionality.log_dateas a date field, not null.actionas a string to describe the action performed.user_idas a foreign key referencing the DOCTOR or NURSE table.
Question 16: Add a Column to the DOCTOR Table
Write a SQL statement to add a phone_number column of type VARCHAR(15) to the DOCTOR table.
Question 17: Alter a Column in the PATIENT Table
Write a SQL statement to alter the email column in the PATIENT table, increasing its length to 300 characters.
Question 18: Drop a Column from the USER_LOGIN Table
Write a SQL statement to drop the active column from the USER_LOGIN table.
Question 19: Rename the USER_LOGIN Table
Write a SQL statement to rename the USER_LOGIN table to USER.
Question 20: Drop the USER Table
Write a SQL statement to drop the USER table.
Question 21: Add a Primary Key
Set up a composite primary key using doctor_id and department_id on DOCTOR_DEPARTMENT table.
Question 22: Add a Foreign Key to the PRESCRIPTION Table
Add a foreign key in the PRESCRIPTION table to reference the DOCTOR table.
Question 23: Add a CHECK Constraint on the TREATMENT Table
Write a SQL statement to add a CHECK constraint to the TREATMENT table ensuring that the treatment_date is not in the future.
Question 24: Add a UNIQUE Constraint on the DEPARTMENT Table
Write a SQL statement to add a UNIQUE constraint to the DEPARTMENT table ensuring that the department_name is not repeated.
Question 25: Add a DEFAULT Constraint on the AUDIT_LOG Table
Write a SQL statement to add a DEFAULT constraint to the AUDIT_LOG table to set the default value of log_date to the current system date.
Question 26: Add Indexes to the PATIENT and DOCTOR Tables
Write SQL statements to:
- Add an index on the
last_namecolumn in the PATIENT table. - Add an index on the
specializationcolumn in the DOCTOR table.
Question 27: Drop an Index from the DOCTOR Table
Write a SQL statement to drop the index on the specialization column in the DOCTOR table.
Question 28: Enforce UNIQUE constraints on all applicable tables.
Apply UNIQUE constraints across the entire database wherever duplicate data is not permitted.
GOOD LUCK WITH YOUR ASSIGNMENT!!!
Don't forget to contact us if you need any further assistance with your assignments, and most importantly, for a manual review and approval of your work.
To gain complete access, login with gmail or outlook, no need of signup. click here
Sample ERD Data Model for Hospital Management System



Comments Not Found