Free tutorials & notes in Hindi & English · Clean code examples 🧰 Free PDF & Image Tools →
MySQL + SQL · Lesson 119

Hospital Database Project in MySQL

In this lesson: Hospital management database in MySQL — patients, doctors, appointments tables and report queries with output. In Hindi & English.

The project

A hospital database is a great DBMS project. It models patients, doctors, and the appointments between them. Since a patient can see many doctors and a doctor sees many patients, the appointments table resolves this many-to-many relationship.

💡 patients ↔ doctors is many-to-many, resolved by an appointments link table.

Designing the tables

  • patients — id, name, phone, age.
  • doctors — id, name, specialty.
  • appointments — id, patient_id (FK), doctor_id (FK), appointment_time, notes.

Patients and doctors connect through appointments, each recording one visit.

Creating the schema

CREATE TABLE patients (
    id INT PRIMARY KEY AUTO_INCREMENT,
    name VARCHAR(50) NOT NULL,
    phone VARCHAR(15), age INT
);
CREATE TABLE doctors (
    id INT PRIMARY KEY AUTO_INCREMENT,
    name VARCHAR(50) NOT NULL,
    specialty VARCHAR(40)
);
CREATE TABLE appointments (
    id INT PRIMARY KEY AUTO_INCREMENT,
    patient_id INT, doctor_id INT,
    appointment_time DATETIME, notes VARCHAR(200),
    FOREIGN KEY (patient_id) REFERENCES patients(id),
    FOREIGN KEY (doctor_id) REFERENCES doctors(id)
);

The appointments link

The appointments table is the many-to-many link. Each row is one visit: which patient saw which doctor, when, and any notes. This keeps patient and doctor details in their own tables, without repetition.

Sample data

INSERT INTO patients (name, phone, age) VALUES ('Aman', '9990001', 30), ('Diya', '9990002', 25);
INSERT INTO doctors (name, specialty) VALUES ('Dr. Rao', 'Cardiology'), ('Dr. Iyer', 'Dermatology');
INSERT INTO appointments (patient_id, doctor_id, appointment_time, notes) VALUES
    (1, 1, '2026-09-22 10:00:00', 'Chest pain'),
    (2, 2, '2026-09-22 11:30:00', 'Skin rash');

Useful queries

-- a doctor's appointments today
SELECT p.name AS patient, a.appointment_time
FROM appointments a
JOIN patients p ON a.patient_id = p.id
WHERE a.doctor_id = 1 AND DATE(a.appointment_time) = CURDATE();

-- all appointments with patient and doctor names
SELECT p.name AS patient, d.name AS doctor, a.appointment_time
FROM appointments a
JOIN patients p ON a.patient_id = p.id
JOIN doctors d ON a.doctor_id = d.id;

-- appointments per doctor
SELECT d.name, COUNT(*) AS total
FROM appointments a JOIN doctors d ON a.doctor_id = d.id
GROUP BY d.id;

Common mistakes

  • Putting doctor_id directly in patients. That cannot model many-to-many; use appointments.
  • Storing time as text. Use DATETIME so date functions work.
  • No foreign keys. Link appointments to real patients and doctors.
  • Repeating doctor details per appointment. Keep them in the doctors table.

Summary

  • A hospital database has patients, doctors and an appointments link table.
  • patients ↔ doctors is many-to-many, resolved by appointments.
  • Each appointment records patient, doctor, time and notes.
  • Use DATETIME for appointment times.
  • Query schedules and counts with JOIN and GROUP BY.

Next, revise many-to-many in the library database project, or the ER model.

Frequently Asked Questions

How do I design a hospital database in MySQL?
Create patients, doctors and appointments tables. The appointments table links a patient and a doctor with a date and time, resolving the many-to-many relationship — a patient can see many doctors and a doctor sees many patients.
Why is an appointments table needed?
Because patients and doctors have a many-to-many relationship: one patient may have appointments with several doctors, and each doctor sees many patients. A link table (appointments) with both foreign keys models this correctly.
How do I find a doctor's appointments for today?
Filter the appointments table by doctor and date: WHERE doctor_id = ? AND DATE(appointment_time) = CURDATE(). Join to patients to show patient names alongside the times.
How do I count appointments per doctor?
Group the appointments by doctor and count: SELECT doctor_id, COUNT(*) FROM appointments GROUP BY doctor_id;. Join to the doctors table to show names, useful for workload reports.
Is a hospital database a good DBMS project?
Yes. It demonstrates a many-to-many relationship via appointments, plus useful queries like a doctor's schedule, patient history, and appointment counts — all core to real hospital systems.
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.
Next topicOnline Shopping Database Project Your progress: 119 / 151 (79%) · Lesson 120 / 151 Continue →

How was this lesson?

Your feedback helps us make the content better.

Write a review or suggestion (optional)

After approval, your name and city may appear publicly with your review. Please do not write your email or phone number.

▶ CodingEasily on YouTube

Learn with video lessons in easy Hindi

Featured video: RDBMS Part 3 – Class XI Commerce IT

▶ Subscribe – it’s free