π₯ Hospital Management System
A full-stack web application built with Flask and MySQL, developed as a DBMS course project (UCS310 Β· Semester 4 Β· Thapar Institute of Engineering and Technology).
The system demonstrates end-to-end database engineering β normalized schema design, stored procedures, triggers, functions, cursors, transactions, and a live web application that consumes all of it.
| Name | Roll Number |
|---|---|
| Krish Kumar | 1024170184 |
| Aditya Anand Boro | 1024170185 |
| Lavdeep Singh | 1024170197 |
- Features
- Tech Stack
- Database Design
- Project Structure
- Getting Started
- Default Credentials
- Database Concepts Implemented
- Role-Based Access
- Patient portal β register, book appointments, view appointment history, manage prescriptions
- Doctor portal β view assigned appointments, update appointment status
- Admin dashboard β manage doctors, patients, appointments; view audit log and doctor workload
- Automated audit trail β every INSERT, UPDATE, and DELETE on key tables is logged automatically via triggers
- Atomic transactions β all multi-step operations (register, book, cancel) are wrapped in transactions with full rollback on failure
- Role-based access control β Admin, Doctor, and Patient roles with separate views and permissions
| Layer | Technology |
|---|---|
| Backend | Python Β· Flask 2.3.3 |
| ORM | Flask-SQLAlchemy 3.1.1 Β· SQLAlchemy 2.0.49 |
| Database | MySQL (InnoDB Β· utf8mb4) |
| DB Driver | PyMySQL 1.1.0 |
| Auth | Flask-Login 0.6.3 Β· Werkzeug password hashing |
| Frontend | Jinja2 templates Β· HTML Β· CSS |
| Config | python-dotenv 1.0.1 |
| Table | Purpose |
|---|---|
departments |
Lookup β hospital departments |
slots |
Lookup β appointment time slots (Morning / Afternoon / Evening / Night) |
users |
Authentication β stores hashed passwords, usertype |
doctor_profiles |
Doctor info linked to users + departments |
patient_profiles |
Patient demographics linked to users |
appointments |
Core booking table β links patient, doctor, slot, date |
prescriptions |
Prescriptions linked to appointments |
audit_log |
Automatic audit trail for all DML operations |
- 1NF β eliminated repeating slot strings; extracted
slotsas a lookup table - 2NF β all tables use single-column surrogate PKs (
AUTO_INCREMENT), removing partial dependencies - 3NF β separated user auth from profile data; extracted departments and slots to remove transitive dependencies
- BCNF β department is derived via
doctor_profiles β departments, never stored redundantly inappointments
Dbms-project-hospital-management-system/
β
βββ PROJECT/
β βββ main.py # Flask application (routes, models, auth)
β
βββ hms.sql # Complete database script:
β # Β§1 DROP existing tables
β # Β§2 CREATE normalized tables (DDL)
β # Β§3 Seed data (DML)
β # Β§4 Views (3 views)
β # Β§5 Stored Procedures (7)
β # Β§6 Stored Functions (3)
β # Β§7 Triggers (6)
β # Β§8 SELECT queries (joins, subqueries, aggregates)
β # Β§9 Transaction examples
β
βββ requirements.txt # Python dependencies
βββ README.md
- Python 3.9+
- MySQL 8.0+
- pip
git clone https://github.com/Krish001122/Dbms-project-hospital-management-system.git
cd Dbms-project-hospital-management-systempip install -r requirements.txtOpen MySQL and run the SQL script to create the database, tables, views, procedures, functions, and triggers:
mysql -u root -p < hms.sqlThis creates a database named hms1 and populates it with seed data.
Create a .env file inside the PROJECT/ directory:
DB_USER=root
DB_PASSWORD=your_mysql_password
DB_HOST=127.0.0.1
DB_PORT=3306
DB_NAME=hms1cd PROJECT
python main.pyThe app will be available at http://127.0.0.1:5000.
After importing hms.sql, the following accounts are available:
| Role | Password | |
|---|---|---|
| Admin | admin@hms.com | Admin@123 |
| Doctor | abc@gmail.com | Doctor@123 |
| Doctor | xyz@gmail.com | Doctor@123 |
| Doctor | pqr@gmail.com | Doctor@123 |
| Patient | ijk@gmail.com | Patient@123 |
| Patient | mno@gmail.com | Patient@123 |
| Patient | def@gmail.com | Patient@123 |
CREATE TABLEwithInnoDBengine andutf8mb4charsetAUTO_INCREMENTsurrogate primary keys on all 8 tablesENUMtypes forusertype,status,gender, andactionCHECKconstraints β email format, phone length, experience β₯ 0, date β₯ 2000-01-01UNIQUEkeys on email, dept_name, slot_name, and userβprofile linksON DELETE CASCADEandON UPDATE CASCADEfor referential integrity
INSERTseed data for all tablesUPDATEvia stored procedures with validationDELETEwith cascading cleanupINNER JOINβvw_appointment_detailsjoins 6 tablesLEFT JOINβvw_doctor_workloadincludes doctors with zero appointments- Subquery with
HAVING COUNT(*) > 2β patients with multiple appointments - Aggregate queries with
GROUP BY,SUM, andCOUNTper department
| View | Description |
|---|---|
vw_appointment_details |
6-table join β full appointment info |
vw_doctor_workload |
Per-doctor appointment counts by status |
vw_audit_summary |
Grouped audit log by table and action |
| Procedure | Purpose |
|---|---|
sp_book_appointment |
Validates and books an appointment atomically |
sp_cancel_appointment |
Cancels appointment with status validation |
sp_register_user |
Registers patient user + profile in one transaction |
sp_register_doctor |
Registers doctor user + profile in one transaction |
sp_create_admin_user |
Creates admin and auto-logs the event |
sp_update_appointment_status |
Safely transitions status with audit logging |
sp_patient_appointment_history |
Fetches appointment history using an explicit cursor |
| Function | Returns |
|---|---|
fn_doctor_appointment_count(doctor_id) |
Total appointments for a doctor |
fn_get_dept_name(dept_id) |
Department name (or 'Unknown') |
fn_has_upcoming_appointment(patient_id) |
1 if patient has a scheduled future appointment |
| Trigger | Event | Purpose |
|---|---|---|
trg_appointment_after_insert |
AFTER INSERT | Logs new booking to audit_log |
trg_appointment_after_update |
AFTER UPDATE | Logs status change to audit_log |
trg_appointment_before_delete |
BEFORE DELETE | Logs deletion to audit_log |
trg_users_before_insert |
BEFORE INSERT | Normalises email to lowercase |
trg_patient_after_insert |
AFTER INSERT | Logs new patient profile creation |
trg_patient_after_delete |
AFTER DELETE | Logs patient profile deletion |
- All 7 stored procedures use
START TRANSACTION/COMMIT/ROLLBACK DECLARE EXIT HANDLER FOR SQLEXCEPTIONin every procedure β guarantees no partial commits on failureSAVEPOINTexamples included inhms.sql Β§9
- Explicit cursor in
sp_patient_appointment_historyβDECLAREβOPENβFETCHloop βCLOSEβ results stored in a temporary table and returned as a result set
| Feature | Admin | Doctor | Patient |
|---|---|---|---|
| View all appointments | β | β | β |
| View own appointments | β | β | β |
| Book appointment | β | β | β |
| Cancel appointment | β | β | β |
| Update appointment status | β | β | β |
| Add / delete doctor | β | β | β |
| Add admin | β | β | β |
| View audit log | β | β | β |
| View doctor workload | β | β | β |
| Manage prescriptions | β | β | π view only |
This project was developed for academic purposes as part of UCS310 Β· DBMS Β· Thapar Institute of Engineering and Technology Β· JanβMay 2026.