Skip to content

Latest commit

Β 

History

4 Commits

Folders and files

NameName
Last commit message
Last commit date
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Dbms-project-hospital-management-system#

πŸ₯ 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.


πŸ‘₯ Team

Name Roll Number
Krish Kumar 1024170184
Aditya Anand Boro 1024170185
Lavdeep Singh 1024170197

πŸ“‹ Table of Contents


✨ Features

  • 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

πŸ›  Tech Stack

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

πŸ—„ Database Design

Tables (8 total β€” normalized to BCNF)

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

Key Design Decisions

  • 1NF β€” eliminated repeating slot strings; extracted slots as 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 in appointments

πŸ“ Project Structure

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

πŸš€ Getting Started

Prerequisites

  • Python 3.9+
  • MySQL 8.0+
  • pip

1. Clone the repository

git clone https://github.com/Krish001122/Dbms-project-hospital-management-system.git
cd Dbms-project-hospital-management-system

2. Install Python dependencies

pip install -r requirements.txt

3. Set up the database

Open MySQL and run the SQL script to create the database, tables, views, procedures, functions, and triggers:

mysql -u root -p < hms.sql

This creates a database named hms1 and populates it with seed data.

4. Configure environment variables

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=hms1

5. Run the application

cd PROJECT
python main.py

The app will be available at http://127.0.0.1:5000.


πŸ”‘ Default Credentials

After importing hms.sql, the following accounts are available:

Role Email 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

🧠 Database Concepts Implemented

DDL

  • CREATE TABLE with InnoDB engine and utf8mb4 charset
  • AUTO_INCREMENT surrogate primary keys on all 8 tables
  • ENUM types for usertype, status, gender, and action
  • CHECK constraints β€” email format, phone length, experience β‰₯ 0, date β‰₯ 2000-01-01
  • UNIQUE keys on email, dept_name, slot_name, and user–profile links
  • ON DELETE CASCADE and ON UPDATE CASCADE for referential integrity

DML & SELECT

  • INSERT seed data for all tables
  • UPDATE via stored procedures with validation
  • DELETE with cascading cleanup
  • INNER JOIN β€” vw_appointment_details joins 6 tables
  • LEFT JOIN β€” vw_doctor_workload includes doctors with zero appointments
  • Subquery with HAVING COUNT(*) > 2 β€” patients with multiple appointments
  • Aggregate queries with GROUP BY, SUM, and COUNT per department

Views (3)

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

Stored Procedures (7)

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

Stored Functions (3)

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

Triggers (6)

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

Transactions & Exception Handling

  • All 7 stored procedures use START TRANSACTION / COMMIT / ROLLBACK
  • DECLARE EXIT HANDLER FOR SQLEXCEPTION in every procedure β€” guarantees no partial commits on failure
  • SAVEPOINT examples included in hms.sql Β§9

Cursor

  • Explicit cursor in sp_patient_appointment_history β€” DECLARE β†’ OPEN β†’ FETCH loop β†’ CLOSE β€” results stored in a temporary table and returned as a result set

πŸ‘€ Role-Based Access

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

πŸ“„ License

This project was developed for academic purposes as part of UCS310 Β· DBMS Β· Thapar Institute of Engineering and Technology Β· Jan–May 2026.

About

No description, website, or topics provided.

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages