MySQL + SQL · Lesson 119
Hospital Database Project
Project
Hospital database एक बढ़िया DBMS project है। यह patients, doctors, और उनके बीच appointments model करता है। चूँकि एक patient कई doctors देख सकता है और एक doctor कई patients, appointments table यह many-to-many संबंध हल करती है।
💡 patients ↔ doctors many-to-many है, appointments link table से हल।
Tables design करना
- patients — id, name, phone, age।
- doctors — id, name, specialty।
- appointments — id, patient_id (FK), doctor_id (FK), appointment_time, notes।
Patients और doctors appointments से जुड़ते हैं, हर एक एक visit record करती है।
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)
);
Appointments link
appointments table many-to-many link है। हर row एक visit है: कौन patient ने कौन doctor देखा, कब, और कोई notes। यह patient और doctor details उनकी अपनी tables में रखती है, बिना दोहराव।
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');
उपयोगी queries
-- किसी doctor की आज की appointments 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(); -- सारी appointments patient और 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; -- प्रति doctor appointments SELECT d.name, COUNT(*) AS total FROM appointments a JOIN doctors d ON a.doctor_id = d.id GROUP BY d.id;
आम गलतियाँ
- doctor_id सीधे patients में डालना। यह many-to-many model नहीं कर सकता; appointments इस्तेमाल कीजिए।
- Time को text store करना। DATETIME इस्तेमाल कीजिए ताकि date functions चलें।
- कोई foreign keys नहीं। appointments को असली patients और doctors से जोड़िए।
- प्रति appointment doctor details दोहराना। उन्हें doctors table में रखिए।
सारांश
- Hospital database में patients, doctors और appointments link table होती है।
- patients ↔ doctors many-to-many है, appointments से हल।
- हर appointment patient, doctor, time और notes record करती है।
- Appointment times को DATETIME इस्तेमाल कीजिए।
- Schedules और counts JOIN और GROUP BY से query कीजिए।
आगे, many-to-many दोहराइए library database project में, या ER model में।
अक्सर पूछे जाने वाले प्रश्न (FAQ)
MySQL में hospital database कैसे design करूँ?
patients, doctors और appointments tables बनाइए। appointments table एक patient और एक doctor को date और time से जोड़ती है, many-to-many संबंध हल करके — एक patient कई doctors देख सकता है और एक doctor कई patients।
Appointments table क्यों चाहिए?
क्योंकि patients और doctors का many-to-many संबंध है: एक patient के कई doctors से appointments हो सकती हैं, और हर doctor कई patients देखता है। दोनों foreign keys वाली link table (appointments) इसे सही model करती है।
किसी doctor की आज की appointments कैसे ढूँढूँ?
appointments table को doctor और date से filter कीजिए:
WHERE doctor_id = ? AND DATE(appointment_time) = CURDATE()। Times के साथ patient names दिखाने को patients से join कीजिए।प्रति doctor appointments कैसे गिनूँ?
appointments को doctor से group करके गिनिए:
SELECT doctor_id, COUNT(*) FROM appointments GROUP BY doctor_id;। Names दिखाने को doctors table से join कीजिए, workload reports को उपयोगी।क्या hospital database एक अच्छा DBMS project है?
हाँ। यह appointments से many-to-many संबंध दिखाता है, साथ में doctor का schedule, patient history, और appointment counts जैसी उपयोगी queries — सब असली hospital systems के मुख्य।
💻 लाइव कोड एडिटर
इस पेज के प्रोग्राम यहीं तैयार हैं — चलाएँ, बदलें और सीखें। कुछ भी इंस्टॉल किए बिना।
OneCompiler द्वारा संचालित। कोड एडिटर में अपने आप आ जाता है — Run दबाकर आउटपुट देखें। अगर एडिटर न खुले तो नए टैब में खोलें.
