0% found this document useful (0 votes)
44 views3 pages

Hospital Database Schema and Queries

The document outlines the creation of a database named 'Hospital' with multiple tables including Hospital, patient, medical_record, Doctor, and Room, each with specified fields and relationships. It includes SQL commands for inserting data into these tables and executing various SELECT queries to retrieve information based on specific criteria. The queries demonstrate how to join tables to extract relevant patient, doctor, and hospital information, as well as medical records and room assignments.

Uploaded by

gadisakarorsa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views3 pages

Hospital Database Schema and Queries

The document outlines the creation of a database named 'Hospital' with multiple tables including Hospital, patient, medical_record, Doctor, and Room, each with specified fields and relationships. It includes SQL commands for inserting data into these tables and executing various SELECT queries to retrieve information based on specific criteria. The queries demonstrate how to join tables to extract relevant patient, doctor, and hospital information, as well as medical records and room assignments.

Uploaded by

gadisakarorsa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

create database Hospital

use Hospital
create table Hospital(Hospid varchar(6) primary key,Hospname varchar(88) not
null,
region varchar(50),city varchar(30))

create table patient(Patid varchar(10) primary key check(patid like 'pat___'),


pfname varchar(30) not null,
pmname varchar(30),plname varchar(30),sex varchar(1),town char(88),
diagnosis varchar(77),
Hospid varchar(6) foreign key references Hospital(Hospid) on delete cascade on
update cascade)

create table medical_record(Recordid varchar(10) primary key,date_of_exam


DATE,
problem varchar(100),Patid varchar(10) foreign key references patient(patid)
on update cascade on delete cascade)

create table Doctor(Docid varchar(10) primary key,Dfname varchar(67),


Dmname varchar(30),sex varchar(1),Qualification varchar(20),salary money,
Hospid varchar(6) foreign key references Hospital(Hospid) on delete cascade on
update cascade)

create table Room(RoomNo varchar(10),BedNo varchar(77),


Roomtype varchar(66),
dateoccupied datetime,
Patid varchar(10) foreign key references patient(patid) on delete cascade on
update cascade,
primary key(RoomNo,BedNo))

select * from Hospital


insert into Hospital values('GH01','Adama general hospital','oromia','adama')
insert into Hospital values('RH01','Adama Medical college','oromia','adama')
insert into Hospital values('RH02','Nekemte referal
Hospital','oromia','nekemte')
insert into Hospital values('GH02','Rift valley general
hospital','finfinne','finfinne')
insert into Hospital values('RH03','Dire Dawa referal','Dire dawa','Dire
dawa')
insert into Hospital values('RH04','Black lion','finfinne','finfinne')

select * from patient


insert into patient
values('pat001','yared','girma','itafa','m','adama','blood','RH01')
insert into patient values('pat002','tufa','wayu','tolera','m','adama','urine
test','GH01')
insert into patient values('pat003','seada','muhe','tola','f','adama','CT
scan','GH01')
insert into patient values('pat004','Ebise','taka','alemu','f','finfine','CT
scan','GH02')
insert into patient
values('pat005','sena','Adugna','geleta','f','nekemte','ultrasound ','RH02')
insert into patient
values('pat006','seifu','kedir','umer','m','finfine','urine and blood
','RH04')
insert into patient values('pat007','sumeya','Tolcha','chala','f','dire
dewa','x-ray ','RH03')
select * from medical_record
insert into medical_record values('rec01',2021-12-05,'diabate','pat001')
insert into medical_record values('rec02',1/18/2022,'diabate','pat002')
insert into medical_record values('rec03',1/20/2022,'insulin','pat003')
insert into medical_record values('rec04',12/5/2021,'gastric','pat004')
insert into medical_record values('rec05',2/2/2022,'hypert','pat005')

select * from Doctor


insert into Doctor values('SDr001','marta','moti','f','dr of
surgery',50000,'RH01')
insert into Doctor values('DDr001','meron','motuma','f','dr of
daibate',40000,'GH01')
insert into Doctor values('CDr001','monera','sanyi','m','dr of
eye',450000,'RH02')
insert into Doctor values('SDr002','simbo','gudata','f','dr of eye
surgery',50000,'GH02')
insert into Doctor
values('IDr002','Hacalu','Bekema','m','Internist',50000,'RH03')
insert into Doctor
values('IDr001','milki','olana','m','Internist',50000,'RH04')
insert into Doctor values('IDr005','Dinsa','Bona','m','dr of eye
surgery',50000,'RH02')

select * from Room


insert into Room values('R01','B01','Emergency','12/5/2021','pat001')
insert into Room values('R02','B03','Emergency','1/18/2022','pat002')
insert into Room values('R03','B02','Delivery','1/20/2022','pat003')
insert into Room values('R04','B04','OPD','2/2/2022','pat004')

insert into Room values('R05','B05','OPD','2/12/2022','pat005')

SELECT [Link], medical_record.problem, [Link],


[Link]
FROM Doctor INNER JOIN
Hospital ON [Link] = [Link] INNER JOIN
patient ON [Link] = [Link] INNER JOIN
medical_record ON [Link] = medical_record.Patid
WHERE (medical_record.problem = 'diabate') AND ([Link] = 'Adama
general hospital')

SELECT [Link]+' '+[Link] as'fullname',


[Link], [Link], [Link]
FROM Doctor INNER JOIN
Hospital ON [Link] = [Link] INNER JOIN
patient ON [Link] = [Link] INNER JOIN
Room ON [Link] = [Link]
WHERE [Link] in('R03','R04','R05') AND ([Link] = 'Rift
valley general hospital')

SELECT [Link], count([Link]), [Link]


FROM Hospital INNER JOIN
patient ON [Link] = [Link] INNER JOIN
Room ON [Link] = [Link] group by
[Link],[Link]
SELECT [Link], [Link]+' '+ [Link] as full_name,
[Link], [Link], [Link], medical_record.date_of_exam
FROM patient INNER JOIN
medical_record ON [Link] = medical_record.Patid
INNER JOIN
Hospital ON [Link] = [Link] where
medical_record.date_of_exam between '1900-01-01 [Link].000' and '1905-06-28
[Link].000'

Common questions

Powered by AI

The `Room` table supports patient accommodation and management by including fields such as `RoomNo`, `BedNo`, `Roomtype`, `dateoccupied`, and `Patid`. The combination of `RoomNo` and `BedNo` serves as a composite primary key, ensuring unique identification of each bed. This structure allows tracking of room occupancy dates and linking rooms to specific patients using a foreign key relationship with the `patient` table .

To improve data retrieval efficiency, establishing indexes on frequently queried columns like `Hospid`, `Patid`, and composite keys can significantly enhance query performance. Normalizing data to minimize redundancy and implementing partitioning strategies based on common query patterns, including regional partitions for hospitals or chronological partitions for records, could also accelerate retrieval processes. Moreover, optimizing queries by restructuring them to reduce nested operations could further streamline complex query executions .

The query grouping by `Hospital.Hospname` and `Hospital.region` provides insights into hospital operations and geographies, such as the distribution of resources—rooms, beds, and patients—across regions. In the given database, it can reveal the number of beds occupied per hospital in each region, which helps in assessing capacity utilization and regional demand for medical services. This is shown by counting `Room.BedNo` in the respective groups, aiding in strategic planning and operational efficiency .

The database structure ensures referential integrity between the `patient` and `Hospital` tables by utilizing a foreign key constraint on the `Hospid` column in the `patient` table, which references the primary key `Hospid` in the `Hospital` table. This constraint is enforced with `ON DELETE CASCADE` and `ON UPDATE CASCADE`, ensuring that updates and deletions in the `Hospital` table are propagated to the `patient` table .

The query to retrieve the list of doctors with their qualifications and room numbers for rooms 'R03', 'R04', and 'R05' in 'Rift valley general hospital' is: SELECT Doctor.Dfname+' '+Doctor.Dmname AS fullname, Doctor.Qualification, Room.RoomNo, Hospital.Hospname FROM Doctor INNER JOIN Hospital ON Doctor.Hospid = Hospital.Hospid INNER JOIN patient ON Hospital.Hospid = patient.Hospid INNER JOIN Room ON patient.Patid = Room.Patid WHERE Room.RoomNo IN ('R03','R04','R05') AND Hospital.Hospname = 'Rift valley general hospital' .

If a hospital entry is deleted, cascading actions with `ON DELETE CASCADE` will automatically remove all associated doctor records linked to that hospital, ensuring data integrity by not leaving orphaned doctor entries. If the hospital ID is updated, `ON UPDATE CASCADE` ensures that all associated doctor entries will have their `Hospid` updated to reflect the change, maintaining consistency in foreign key references across the database .

Patients diagnosed with diabetes are in 'Adama general hospital' and this information is retrieved using the query: SELECT patient.pfname, medical_record.problem, Doctor.Dfname, Hospital.Hospname FROM Doctor INNER JOIN Hospital ON Doctor.Hospid = Hospital.Hospid INNER JOIN patient ON Hospital.Hospid = patient.Hospid INNER JOIN medical_record ON patient.Patid = medical_record.Patid WHERE (medical_record.problem = 'diabate') AND (Hospital.Hospname = 'Adama general hospital').

The `ON DELETE CASCADE` constraint ensures that if a record in the parent table (like `Hospital`) is deleted, all corresponding records in the child table (`patient`, `Doctor`, etc.) are also deleted, maintaining data integrity. The `ON UPDATE CASCADE` constraint ensures that if a primary key in the parent table is updated, the corresponding foreign keys in the child tables are automatically updated with the new values, preventing orphan records and maintaining referential integrity .

Having a composite primary key on fields like `RoomNo` and `BedNo` can lead to complexity in query formulation, especially when scaling-up operations or integrating other data tables. It could also lead to redundancy issues if bed renumbering occurs, requiring extensive updates. Furthermore, it might hinder efficient indexing strategies, affecting performance during complex query executions .

Challenges include ensuring data integrity while managing complex cascading operations, like `ON DELETE CASCADE`, potentially leading to the automatic removal of large volumes of interrelated data, which if not properly handled could result in loss of critical patient information. Another challenge is scalability, as frequent updates in relationships might slow down operations, necessitating efficient index management and transaction logging to prevent data corruption and ensure the robustness of database operations .

You might also like