Hospital Management System
Using SQL
INTRODUCTION
A database is a collection of information and is systematically stored in tables
in the form of rows and columns. The table in the database has unique name that
identifies its contents. The database in turn is further described in detail giving all
the fields used with the data types, constraints available, primary key and foreign
key.
Hospital Management system developed in this project is simple with general
features. Database design is used to manage large bodies of information. In this
database we describe all the 4 tables available in the software, which are used to
store all the records.
Objective of the project
To perform following operations in SQL
Data definition and data Types
eign key, referential integrity.
Statements
Device Used:
Working PC
Software Used:
XAMPP(Local Server of Server)
Command prompt or XAMPP Shell
Patient Table:
Fields Data Type Relationships
Patientid Integer Primary Key
name Varchar(20) Not Null
age Integer Not Null
gender Varchar(10) Not null
paddress Varchar(50) Not Null
department Integer Foreign Key
ward Integer Not Null
doctorid Integer Foreign Key
Doctor Table:
Fields Data Type Relationships
doctorid Varchar(5) Primary Key
name Varchar(15) Not Null
department Varchar(15) Foreign Key
NurseTable:
Fields Data Type Relationships
nurseid Varchar(5) Primary Key
nursename Varchar(5) Not Null
department Integer Foreign Key
shift Varchar(5) Not Null
Ward Table:
Fields Data Type Relationships
wardno Int Primary Key
wardtype Varchar(10) Not Null
departmrnt Integer Foreign Key
E-R Diagram
Entity relationship diagram is used in modern database software
engineering to illustrate logical structure of database. It is a relational schema
database modeling method used to model a system and approach. This approach
commonly used in database design. The diagram created using this method is
called E-R diagram.
The E-R diagram depicts the various relationships among entities
considering each object as entity. Entity is represented as diamond shape and
relationship is represented as rectangle. It depicts the relationship between data
objects. The E-R diagram is the relation that is used to conduct the data modeling
activity.
Entity:-
Entity is the thing which we want to store information. It is an elementary
basic building block of storing information about business process. An entity
represents an objects defined within the information system about which you
want to store information.
Relationship:-
A relationship is named connection or association between entities used to
relate two or more entities with some common attributes of meaningful
interaction between the object.
Attributes:-
Attributes are the properties of the entities and relationship. Descriptor of the
entity. Attributes are elementary pieces of information attached to an entity.
23
Hospital Management System
Symbols Meaning
Entity
Relationship
Attribute
Key Attribute
E1 R E2 Cardinality Ratio N: 1 for E1:
E2 in R
Creating Database using SQL
Syntax : CREATE DATABASE database_name;
Table creation in “hospital” database:
Syntax: CREATE TABLE table_name (
column1 datatype,
column2 datatype,
column3 datatype,
....
);
CREATE TABLE `patient` (
`patientid` INT NOT NULL,
`pname` varchar(40) NOT NULL,
`department` INT NOT NULL,
`age` INT(3) NOT NULL,
`gender` varchar(5) NOT NULL,
`paddress` varchar(40) NOT NULL,
`ward` INT NOT NULL,
`doctorid` INT NOT NULL,
PRIMARY KEY (`patientid`)
);
CREATE TABLE `doctor` (
`doctorid` INT NOT NULL,
`name` varchar(40),
`departmentid` INT NOT NULL,
PRIMARY KEY (`doctorid`)
);
CREATE TABLE `nurse` (
`nurseid` INT NOT NULL,
`nname` varchar(40) NOT NULL,
`departmentid` INT NOT NULL,
`shift` varchar(10) NOT NULL,
`ward` INT NOT NULL,
PRIMARY KEY (`nurseid`)
);
CREATE TABLE `ward` (
`wardid` INT NOT NULL,
`department` INT NOT NULL,
`wardtype` varchar(40) NOT NULL,
PRIMARY KEY (`wardid`)
);
CREATE TABLE `department` (
`departmentid` INT NOT NULL,
`dname` varchar(40) NOT NULL,
`doctorid` varchar(40) NOT NULL,
PRIMARY KEY (`departmentid`)
);
SQL FOREIGN KEY Constraint
A FOREIGN KEY is a key used to link two tables together.
A FOREIGN KEY is a field (or collection of fields) in one table that refers to the
PRIMARY KEY in another table.
The table containing the foreign key is called the child table, and the table
containing the candidate key is called the referenced or parent table.
SQL FOREIGN KEY on ALTER TABLE
To create a FOREIGN KEY constraint on the "PersonID" column when the "Orders"
table is already created, use the following SQL:
ALTER TABLE <table_name>
ADD CONSTRAINT <FK_ID>
FOREIGN KEY (<attribute>) REFERENCES <table_name>(<attribute>);
For above tables:
ALTER TABLE `doctor` ADD CONSTRAINT `doctor_fk0` FOREIGN KEY
(`departmentid`) REFERENCES `department`(`departmentid`);
ALTER TABLE `patient` ADD CONSTRAINT `patient_fk0` FOREIGN KEY
(`department`) REFERENCES `department`(`departmentid`);
ALTER TABLE `patient` ADD CONSTRAINT `patient_fk1` FOREIGN KEY (`ward`)
REFERENCES `ward`(`wardid`);
ALTER TABLE `patient` ADD CONSTRAINT `patient_fk2` FOREIGN KEY (`doctorid`)
REFERENCES `doctor`(`doctorid`);
ALTER TABLE `ward` ADD CONSTRAINT `ward_fk0` FOREIGN KEY (`department`)
REFERENCES `department`(`departmentid`);
ALTER TABLE `nurse` ADD CONSTRAINT `nurse_fk0` FOREIGN KEY
(`departmentid`) REFERENCES `department`(`departmentid`);
ALTER TABLE `nurse` ADD CONSTRAINT `nurse_fk1` FOREIGN KEY (`ward`)
REFERENCES `ward`(`wardid`);
Insertion of Data in the table created in “hospital” database:
Syntax:
INSERT INTO table_name
VALUES (value1, value2, value3, ...);
INSERT INTO department VALUES(1,"ENT",100);
INSERT INTO department VALUES(2,"Cardiology",200);
INSERT INTO department VALUES(3,"OPD",300);
INSERT INTO department VALUES(4,"Neurology",400);
INSERT INTO ward VALUES(1000,1,'general');
INSERT INTO ward VALUES(2000,2,'ICU');
INSERT INTO ward VALUES(4000,4,'First class');
INSERT INTO doctor Values(100,’Ramesh Giri’,1);
INSERT INTO doctor Values(200,'Bhagwan Koirala',2);
INSERT INTO doctor Values(300,'Govinda KC',3);
INSERT INTO doctor Values(400,'Rabindra Gupta',4);
INSERT INTO patient VALUES(12,'Rajan Pandit',2,25,'M','Sinamangal',2000,200);
INSERT INTO patient VALUES(13,'Pabitra Pradhan',3,16,'F','Patan',3000,300);
INSERT INTO patient VALUES(14,'Resham Pariyar',4,58,'M','Bhaktapur',4000,400);
INSERT INTO nurse values(37,'Bidhata M Thakuri',2,'morning',2000);
INSERT INTO nurse values(38,'Archana Khadka',3,'morning',3000);
INSERT INTO nurse values(39,'Sirisha Silwal',4,'morning',4000);
The SQL UPDATE Statement
The UPDATE statement is used to modify the existing records in a table.
UPDATE Syntax
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
In above table “patient” update age to 50:
Update patient
Set age=50
Where age=58;
The SQL DELETE Statement
The DELETE statement is used to delete existing records in a table.
DELETE Syntax
DELETE FROM table_name WHERE condition;
In above table “patient” delete patient having patient id as 12
Delete from patient where patientid=12;
SQL ALTER TABLE Statement
The ALTER TABLE statement is used to add, delete, or modify columns in an existing
table.
The ALTER TABLE statement is also used to add and drop various constraints on
an existing table.
ALTER TABLE - ADD Column
To add a column in a table, use the following syntax:
ALTER TABLE table_name
ADD column_name datatype;
In above table “doctor” adding new column as “salary”:
Alter table doctor
Add salary integer;
(after adding new column salary , it is aslo updated according to doctored)
ALTER TABLE - DROP COLUMN
To delete a column in a table, use the following syntax (notice that some database
systems don't allow deleting a column):
ALTER TABLE table_name
DROP COLUMN column_name;
Alter table patient
Drop column paddress;
ALTER TABLE - ALTER/MODIFY COLUMN
To change the data type of a column in a table, use the following syntax:
SQL Server
ALTER TABLE table_name
MODIFY COLUMN column_name datatype;
Alter table doctor
Modify column salary float;
Aggregate functions in SQL
In database management an aggregate function is a function where the values of
multiple rows are grouped together as input on certain criteria to form a single value
of more significant meaning.
Count():
Count(*): Returns total number of records
In table “patient” total patient is calculated as:
The SUM() function returns the total sum of a numeric column.
SUM() Syntax
SELECT SUM(column_name)
FROM table_name
WHERE condition;
In table “doctor” calculating sum of salary:
The AVG() function returns the average value of a numeric column.
SELECT AVG(column_name)
FROM table_name
WHERE condition;
In table “doctor” average salary is calculated as:
SQL JOIN
A JOIN clause is used to combine rows from two or more tables, based on a related
column between them.
SQL INNER JOIN:
The INNER JOIN keyword selects records that have matching values in both tables.
INNER JOIN Syntax
SELECT column_name(s)
FROM table1
INNER JOIN table2
ON table1.column_name = table2.column_name;
Lets join “patient” and “doctor”
Select name
From doctor
Inner join patient on doctor.departmentid=patient.department;
The SQL HAVING Clause
The HAVING clause was added to SQL because the WHERE keyword could not be
used with aggregate functions.
HAVING Syntax
SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
HAVING condition
ORDER BY column_name(s);
SQL CREATE VIEW Statement
In SQL, a view is a virtual table based on the result-set of an SQL statement.
A view contains rows and columns, just like a real table. The fields in a view are
fields from one or more real tables in the database.
You can add SQL functions, WHERE, and JOIN statements to a view and present
the data as if the data were coming from one single table.
CREATE VIEW Syntax
CREATE VIEW view_name AS
SELECT column1, column2, ...
FROM table_name
WHERE condition;
Lets create a view in table “patient”:
CREATE VIEW PatientBrief AS
SELECT patientid, pname, doctorid
FROM patient
WHERE department=1;
CONCLUSION
It was a wonderful learning experience for me while working on this project. This
project took me through the various task in SQLand gave me a real insight into the
world of database designing. The joy of working and tackling the various problems
and challenges gave me a feel of database developer. Moreover most of the SQL
operations are learned and analyzed in comprehensive way.