The tables
Ten tables in four groups: the staff, the wards, the patients’ stays and visits, and the prescriptions.
dbo.DepartmentHospital departments by name, each name once.dbo.DoctorOne row per doctor, found by license number, working in one department.dbo.RoomRooms by number, each in one department.dbo.BedThe beds in a room, lettered A, B, C; a letter once per room.dbo.PatientOne row per patient, found by medical record number.dbo.AdmissionA patient's stay in one bed under one doctor: admitted and discharged times.dbo.AppointmentA visit booked with a doctor at a start time.dbo.MedicationMedications by name, each name once.dbo.PrescriptionWhat a doctor prescribed to a patient, and on which day.dbo.PrescriptionItemOne medication on a prescription, with its dose; its key is the pair.
How the tables relate
- A department has zero or more doctors , and each doctor works in exactly one department:
DepartmentIdis NOT NULL, so the line has nothing at the department’s end . - A room has zero or more beds, and a bed’s letter is unique within its room, so room 301 has one bed A.
- An admission is one patient in one bed under one doctor.
- A patient has zero or more admissions, appointments and prescriptions; each of those names exactly one patient.
- Prescriptions and medications are many-to-many:
dbo.PrescriptionItemholds one row per pair, keyed by both columns, a composite key.
Why it is built this way
A bed holds one patient at a time
A filtered unique index on dbo.Admission (BedId) WHERE DischargedAt IS NULL lets a bed have many past stays and one current stay. A second admission to an occupied bed fails with Msg 2601. A second index does the same for the patient: one current stay each.
A doctor is booked once per start time
The alternate key (DoctorId, StartsAt) on dbo.Appointment refuses a second appointment at the same time with Msg 2627. It compares start times only; visits that overlap without starting together need a check of their own.
A stay is closed, never deleted
DischargedAt stays NULL while the patient is in. The patients in a department right now:
SELECT p.MedicalRecordNumber, p.FullName, r.RoomNumber, b.Label FROM dbo.Admission AS a JOIN dbo.Patient AS p ON p.PatientId = a.PatientId JOIN dbo.Bed AS b ON b.BedId = a.BedId JOIN dbo.Room AS r ON r.RoomId = b.RoomId WHERE a.DischargedAt IS NULL AND r.DepartmentId = @DepartmentId;
Patients have an id and a record number
PatientId is a surrogate key the other tables carry; the medical record number is an alternate key, the number staff look a patient up by, so correcting it is one changed row.
The SQL Server script
-- Table [dbo].[Department] CREATE TABLE [dbo].[Department] ( [DepartmentId] INT IDENTITY (1, 1) NOT NULL, [Name] NVARCHAR (100) NOT NULL, CONSTRAINT [PK_Department] PRIMARY KEY CLUSTERED ([DepartmentId] ASC), CONSTRAINT [AK_Department_Name] UNIQUE NONCLUSTERED ([Name] ASC) ); GO -- Table [dbo].[Medication] CREATE TABLE [dbo].[Medication] ( [MedicationId] INT IDENTITY (1, 1) NOT NULL, [Name] NVARCHAR (200) NOT NULL, CONSTRAINT [PK_Medication] PRIMARY KEY CLUSTERED ([MedicationId] ASC), CONSTRAINT [AK_Medication_Name] UNIQUE NONCLUSTERED ([Name] ASC) ); GO -- Table [dbo].[Patient] CREATE TABLE [dbo].[Patient] ( [PatientId] INT IDENTITY (1, 1) NOT NULL, [MedicalRecordNumber] VARCHAR (20) NOT NULL, [FullName] NVARCHAR (200) NOT NULL, [BirthDate] DATE NOT NULL, [Phone] VARCHAR (20) NULL, CONSTRAINT [PK_Patient] PRIMARY KEY CLUSTERED ([PatientId] ASC), CONSTRAINT [AK_Patient_MedicalRecordNumber] UNIQUE NONCLUSTERED ([MedicalRecordNumber] ASC) ); GO -- Table [dbo].[Doctor] CREATE TABLE [dbo].[Doctor] ( [DoctorId] INT IDENTITY (1, 1) NOT NULL, [LicenseNumber] VARCHAR (20) NOT NULL, [FullName] NVARCHAR (200) NOT NULL, [DepartmentId] INT NOT NULL, CONSTRAINT [PK_Doctor] PRIMARY KEY CLUSTERED ([DoctorId] ASC), CONSTRAINT [FK_Doctor_Department_DepartmentId] FOREIGN KEY ([DepartmentId]) REFERENCES [dbo].[Department] ([DepartmentId]), CONSTRAINT [AK_Doctor_LicenseNumber] UNIQUE NONCLUSTERED ([LicenseNumber] ASC) ); GO -- Table [dbo].[Room] CREATE TABLE [dbo].[Room] ( [RoomId] INT IDENTITY (1, 1) NOT NULL, [RoomNumber] VARCHAR (10) NOT NULL, [DepartmentId] INT NOT NULL, CONSTRAINT [PK_Room] PRIMARY KEY CLUSTERED ([RoomId] ASC), CONSTRAINT [FK_Room_Department_DepartmentId] FOREIGN KEY ([DepartmentId]) REFERENCES [dbo].[Department] ([DepartmentId]), CONSTRAINT [AK_Room_RoomNumber] UNIQUE NONCLUSTERED ([RoomNumber] ASC) ); GO -- Table [dbo].[Appointment] CREATE TABLE [dbo].[Appointment] ( [AppointmentId] INT IDENTITY (1, 1) NOT NULL, [PatientId] INT NOT NULL, [DoctorId] INT NOT NULL, [StartsAt] DATETIME2 (0) NOT NULL, [Reason] NVARCHAR (300) NULL, CONSTRAINT [PK_Appointment] PRIMARY KEY CLUSTERED ([AppointmentId] ASC), CONSTRAINT [FK_Appointment_Doctor_DoctorId] FOREIGN KEY ([DoctorId]) REFERENCES [dbo].[Doctor] ([DoctorId]), CONSTRAINT [FK_Appointment_Patient_PatientId] FOREIGN KEY ([PatientId]) REFERENCES [dbo].[Patient] ([PatientId]), CONSTRAINT [AK_Appointment_DoctorId_StartsAt] UNIQUE NONCLUSTERED ([DoctorId] ASC, [StartsAt] ASC) ); GO -- Table [dbo].[Bed] CREATE TABLE [dbo].[Bed] ( [BedId] INT IDENTITY (1, 1) NOT NULL, [RoomId] INT NOT NULL, [Label] CHAR (1) NOT NULL, CONSTRAINT [PK_Bed] PRIMARY KEY CLUSTERED ([BedId] ASC), CONSTRAINT [FK_Bed_Room_RoomId] FOREIGN KEY ([RoomId]) REFERENCES [dbo].[Room] ([RoomId]), CONSTRAINT [AK_Bed_RoomId_Label] UNIQUE NONCLUSTERED ([RoomId] ASC, [Label] ASC) ); GO -- Table [dbo].[Prescription] CREATE TABLE [dbo].[Prescription] ( [PrescriptionId] INT IDENTITY (1, 1) NOT NULL, [PatientId] INT NOT NULL, [DoctorId] INT NOT NULL, [IssuedOn] DATE NOT NULL, CONSTRAINT [PK_Prescription] PRIMARY KEY CLUSTERED ([PrescriptionId] ASC), CONSTRAINT [FK_Prescription_Doctor_DoctorId] FOREIGN KEY ([DoctorId]) REFERENCES [dbo].[Doctor] ([DoctorId]), CONSTRAINT [FK_Prescription_Patient_PatientId] FOREIGN KEY ([PatientId]) REFERENCES [dbo].[Patient] ([PatientId]) ); GO -- Table [dbo].[Admission] CREATE TABLE [dbo].[Admission] ( [AdmissionId] INT IDENTITY (1, 1) NOT NULL, [PatientId] INT NOT NULL, [BedId] INT NOT NULL, [DoctorId] INT NOT NULL, [AdmittedAt] DATETIME2 (0) NOT NULL, [DischargedAt] DATETIME2 (0) NULL, CONSTRAINT [PK_Admission] PRIMARY KEY CLUSTERED ([AdmissionId] ASC), CONSTRAINT [CK_Admission_DischargedAt] CHECK (DischargedAt >= AdmittedAt), CONSTRAINT [FK_Admission_Bed_BedId] FOREIGN KEY ([BedId]) REFERENCES [dbo].[Bed] ([BedId]), CONSTRAINT [FK_Admission_Doctor_DoctorId] FOREIGN KEY ([DoctorId]) REFERENCES [dbo].[Doctor] ([DoctorId]), CONSTRAINT [FK_Admission_Patient_PatientId] FOREIGN KEY ([PatientId]) REFERENCES [dbo].[Patient] ([PatientId]) ); GO CREATE UNIQUE NONCLUSTERED INDEX [UX_Admission_BedId_Open] ON [dbo].[Admission]([BedId] ASC) WHERE (DischargedAt IS NULL); GO CREATE UNIQUE NONCLUSTERED INDEX [UX_Admission_PatientId_Open] ON [dbo].[Admission]([PatientId] ASC) WHERE (DischargedAt IS NULL); GO -- Table [dbo].[PrescriptionItem] CREATE TABLE [dbo].[PrescriptionItem] ( [PrescriptionId] INT NOT NULL, [MedicationId] INT NOT NULL, [Dose] NVARCHAR (100) NOT NULL, CONSTRAINT [PK_PrescriptionItem] PRIMARY KEY CLUSTERED ([PrescriptionId] ASC, [MedicationId] ASC), CONSTRAINT [FK_PrescriptionItem_Medication_MedicationId] FOREIGN KEY ([MedicationId]) REFERENCES [dbo].[Medication] ([MedicationId]), CONSTRAINT [FK_PrescriptionItem_Prescription_PrescriptionId] FOREIGN KEY ([PrescriptionId]) REFERENCES [dbo].[Prescription] ([PrescriptionId]) ); GO
Questions
What tables does a hospital management system need?
Patients, doctors, departments and admissions at the least. Rooms and beds, appointments and prescriptions come next. This design has all ten.
How do I find the free beds?
Select the beds with no open admission: NOT EXISTS on dbo.Admission for that bed where DischargedAt is NULL.
Can I change the design?
Yes. Open it in ERDsmith, free in the browser: add a table, rename a column, and the keys and lines follow.
Does it work with MySQL or PostgreSQL?
The script is written for SQL Server. The tables, keys and relationships carry over to other databases; the types and the filtered indexes need that database's own syntax.
