FINAL LAB DBMS
FINAL LAB DBMS
PRACTICAL RECORED
Academic Year: 2024-45
Submitted by
Semester :
1. Draw ER diagram for hospital administration
Entities and Attributes of the Hospital Management System
A thing in the real world with an independent existence. It is may be an object with
physical existence (ex: house, person) or with a conceptual existence (ex: course,
job). The are represented by rectangle.
Let's Defining Entities for Hospital Management System are:
1. Patient
P-ID: Unique identifier for each Patient
Name: Name of the Patient.
DOB: Date of borthf of Patient.
Gender: Gender of Patient.
Mob-No: Contact number of the Patient.
Age: Age of Patient.
2. Employee
E-ID: Unique identifier for each Employee.
Name: Name of the Employee.
Salary: Salary of Employee
Sex: Gender of Employee.
Mob-No: Contact number of the Employee.
Address: Address of Employee.
State: State of Employee
City: city of Employee
Pin-no: Pin no of Employee
3. Doctor
E-ID (Foreign Key referencing Employee):
Department: Department of doctor.
Qualification: Qualification of Doctor
4. Nurse
E-ID: E-ID is a foreign key linking a table to the Employee table through
the Employee ID.
5. Room
R-ID: It is an room id every room has different room number or ID.
Type: It define the quality of room such as deluxe, private general etc.
Capacity: It defines the number of people can stay in room.
Availability: It define the duration or Availability of room.
6. Receptionist
E-ID (Foreign Key referencing Employee): E-ID is a foreign key in a table that
references the Employee table, typically used to establish a relationship
between the two tables based on the Employee ID.
7. Test Report
R-ID (Primary Key): Unique identifier for each Room.
P-ID (Foreign Key referencing Patient): P-ID is a foreign key in a table that
references the Patient table, typically used to establish a relationship between
the two tables based on the Patient ID.
Test Type: It define the what kinf of test.
Result: It shows the test result.
8. Bill
B-ID: Unique identifier for each Bill.
P-ID (Foreign Key referencing Patient): P-ID is a foreign key in a table that
references the Patient table, typically used to establish a relationship between
the two tables based on the Patient ID.
Amount: The Amount which Patient has to pay to the Hospital.
9. Records
Record-no: Every record book has some number for each Patient.
App-no: Every app book has some number for each Patient.
Establishing Relationships
Entities have some relationships with each other. Relationships define how entities
are associated with each other.
Let's Establishing Relationships between them are:
Patient consults Doctor.
Employee have roles as a nurse, doctor and receptionist within the hospital.
Patient pays bills for medical services.
Nurse governs rooms.
Patient assigned rooms during their stay at hospital.
Receptionist maintains hospital records.
Patient has test report.
Relationships Between These Entities
1. Patient - Doctor Relationship
A patient can have a relationship with one or more doctors for consultations or
treatments.
A doctor can have multiple patients.
This is a Many-to-Many (Patient-to-Doctor) as multiple Patient can visit multiple
Doctor.
2. Nurse - Rooms Relationship
A nurse can be assigned to one or more rooms during their shift.
A room can have multiple nurses assigned to it over different shifts.
This is a many-to-many relationship between nurses and rooms, each nurse
can be assigned to multiple rooms, and each room can have multiple nurses
assigned to it.
3. Receptionist - Records Relationship
A receptionist manages records which could include patient records,
appointment schedules, or other administrative documents.
A record can be managed by one or more receptionists.
This is a many-to-many relationship between receptionists and records, each
receptionist can manage multiple records, and each record can be managed by
multiple receptionists.
4. Patient - Bills Relationship
One patient can have multiple bills.
One bill is associated with only one patient.
This is a one-to-many relationship between patients and bills, each patient can
have multiple bills, but each bill belongs to only one patient.
5. Patient - Test Report Relationship
One patient can have multiple test reports.
One test report is associated with only one patient.
This is a one-to-many relationship between patients and test reports, each
patient can have multiple test reports, but each test report belongs to only one
patient.
6. Rooms - Patient Relationship
One room can accommodate multiple patients over time.
One patient occupies one room at a time.
This is a one-to-many relationship between rooms and patients, each room can
accommodate multiple patients, but each patient occupies only one room at a
time.
2. Creation of college database and establish relationships between
tables
CREATETABLE Students (
student_id INTPRIMARY KEY,
first_name VARCHAR(100),
last_name VARCHAR(100),
email VARCHAR(100),
dob DATE,
department_id INT,
FOREIGN KEY (department_id) REFERENCES Departments(department_id)
);
CREATETABLE Courses (
course_id INTPRIMARY KEY,
course_name VARCHAR(100),
credits INT,
department_id INT,
FOREIGN KEY (department_id) REFERENCES Departments(department_id)
);
CREATETABLE Professors (
professor_id INTPRIMARY KEY,
first_name VARCHAR(100),
last_name VARCHAR(100),
email VARCHAR(100),
department_id INT,
FOREIGN KEY (department_id) REFERENCES Departments(department_id)
);
CREATETABLE Enrollments (
student_id INT,
course_id INT,
semester_id INT,
PRIMARY KEY (student_id, course_id, semester_id),
FOREIGN KEY (student_id) REFERENCES Students(student_id),
FOREIGN KEY (course_id) REFERENCES Courses(course_id),
FOREIGN KEY (semester_id) REFERENCES Semesters(semester_id)
);
CREATETABLE Semesters (
semester_id INTPRIMARY KEY,
semester_name VARCHAR(100)
);
CREATETABLE Classrooms (
classroom_id INTPRIMARY KEY,
classroom_name VARCHAR(100),
capacity INT
);
CREATETABLE CourseSchedules (
course_id INT,
classroom_id INT,
semester_id INT,
time_slot VARCHAR(50),
PRIMARY KEY (course_id, classroom_id, semester_id),
FOREIGN KEY (course_id) REFERENCES Courses(course_id),
FOREIGN KEY (classroom_id) REFERENCES Classrooms(classroom_id),
FOREIGN KEY (semester_id) REFERENCES Semesters(semester_id)
);
CREATETABLE CourseProfessors (
course_id INT,
professor_id INT,
PRIMARY KEY (course_id, professor_id),
FOREIGN KEY (course_id) REFERENCES Courses(course_id),
FOREIGN KEY (professor_id) REFERENCES Professors(professor_id)
);
3.Relational database schema of a company is given in the following
figure
1. Create above tables with relevant Primary Key, Foreign Key and other
constraints
TABLE-1
mgr_ssn char(9),
mgr_start_date date
);
TABLE-2
create table employeee (
minit char(1),
address varchar(50),
birthdate date,
salary decimal(10,2),
TABLE-3
dno number(10),
dlocation varchar(50),
TABLE-4
pname varchar(50),
plocation varchar(50),
dno int,
);
TABLE-5
essn number(20),
pno int,
hours decimal(5,2),
primary key (essn, pno),
);
TABLE-6
essn number(9),
dependent_name varchar(50),
sex char(1),
bdate date,
relationship varchar(50),
);
insert into employeee values('123456789', 'john', 'smith', 'j', '123 main st, anytown', '23-oct-1950',
60000, '123456789', 5);
insert into employeee values('333445555', 'franklin', 'wong', 't', '456 oak ave, bellaire', '17-jun-
1965', 40000, '333445555', 5);
insert into employeee values('999887777', 'ramesh', 'narayan', 'k', '789 elm st, stafford', '28-jan-
1958', 55000, null, 1);
insert into employeee values('666884444', 'joyce', 'english', 'a', '101 pine st, houston', '23-oct-1950',
45000, '666884444', 7);
insert into employeee values('987654321', 'alicia', 'kaye', 'j', '444 cherry st, houston', '15-may-1952',
50000, '987654321', 4);
insert into employeee values('765432109', 'robert', 'ford', 'd', '555 birch st, stafford', '03-nov-1960',
52000, '987654321', 4);
insert into employeee values('1234567', 'david', 'smith', 'j', '123 main st, us', '23-oct-1950', 60000,
'1234567', 10);
DNO DLOCATION
1 Bellaire
1 Sugarland
4 Stafford
5 Houston
7 bellaire
4. Employees in department 7
SELECT ssn, lname, fname, address FROM Employeee WHERE dno = 7;
FROM employeee
BIRTHDATE ADDRESS
17-Jun-65 456 oak ave, bellaire
10. Retrieve all employees in department 5 whose salary is between 50,000 and
60,000(inclusive)
11. Retrieve the names of all employees who do not have supervisors
13. Retrieve the name and address of all employees who work for the 'Research'
department
FROM Employeee E
14. For every project located in 'Stafford', list the project number, the controlling
department number, and the department manager's last name, address, and birth date.
FROM Project P
no data found
15. For each employee, retrieve the employee's name, and the name of his or her
immediate supervisor.
PNO
3
10
18. Increase the salary of all employees working on the 'Product X' project by 15%.
Retrieve employee name and increased salary of these employees.
UPDATE Employeee SET Salary = Salary * 1.15 WHERE SSN IN (SELECT ESSN FROM works_on W JOIN
Project P ON W.PNo = P.PNo WHERE P.PName = 'Product X');
1 row(s) updated.
SSN FNAME LNAME MINIT ADDRESS BIRTHDATE SALARY SUPER_SSN DNO
123 main
123456789 john smith j st, anytown 23-Oct-50 69000 123456789 5
456 oak
ave,
333445555 franklin wong t bellaire 17-Jun-65 40000 333445555 5
789 elm st,
999887777 ramesh narayan k stafford 28-Jan-58 55000 1
101 pine st,
666884444 joyce english a houston 23-Oct-50 45000 666884444 7
444 cherry
987654321 alicia kaye j st, houston 15-May-52 50000 987654321 4
333 willow
453456789 jennifer wallace s st, bellaire 20-Jul-72 75000 453456789 7
555 birch
765432109 robert ford d st, stafford 03-Nov-60 52000 987654321 4
19. Retrieve a list of employees and the project name each works in, ordered by the
employee's department, and within each department ordered alphabetically by employee
first name.
20. Select the names of employees whose salary does not match with salary of any
employee in department 10.
SELECT FName, LName FROM Employeee WHERE Salary NOT IN (SELECT Salary
FROM Employeee WHERE DNo = 10);
FNAME LNAME
john smith
franklin wong
ramesh narayan
joyce english
alicia kaye
jennifer wallace
robert ford
21. Retrieve the employee numbers of all employees who work on project
located in Bellaire, Houston, or Stafford.
ESSN
123456789
333445555
999887777
22. Find the sum of the salaries of all employees, the maximum salary, the
minimum salary, and the average salary. Display with proper headings.
SELECT SUM(Salary) AS TotalSalary, MAX(Salary) AS MaxSalary, MIN(Salary) AS MinSalary,
AVG(Salary) AS AvgSalary FROM Employeee;
TOTALSALARY MAXSALARY MINSALARY AVGSALARY
386000 75000 40000 55142.85714
23. Find the sum of the salaries and number of employees of all employees of the
‘Marketing’ department, as well as the maximum salary, the minimum salary, and the
average salary in this department.
24. Select the names of employees whose salary is greater than the average
salary of all employees in department 10.
SELECT fname, lname FROM Employeee WHERE salary > (SELECT AVG(salary) FROM
Employeee WHERE dno = 10);
FNAME LNAME
john smith
jennifer wallace
2 row(s) deleted.
26. Perform a query using alter command to drop/add field and a constraint in
Employee table.