0% found this document useful (0 votes)
2 views

FINAL LAB DBMS

The document outlines the practical record for a Database Management System course, detailing the creation of ER diagrams for hospital and college databases, including entities, attributes, and relationships. It provides SQL schema examples for various entities such as Patients, Employees, and Courses, along with their relationships and constraints. Additionally, it includes instructions for populating the tables with sample data for a company database.

Uploaded by

grojamani
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

FINAL LAB DBMS

The document outlines the practical record for a Database Management System course, detailing the creation of ER diagrams for hospital and college databases, including entities, attributes, and relationships. It provides SQL schema examples for various entities such as Patients, Employees, and Courses, along with their relationships and constraints. Additionally, it includes instructions for populating the tables with sample data for a company database.

Uploaded by

grojamani
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

Database Management System

PRACTICAL RECORED
Academic Year: 2024-45
Submitted by

Name of the Student :

Hall Ticket Number :

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

Identify Entities (Tables)

In a college database, common entities might include:

 Students: Information about students.


 Courses: Courses offered by the college.
 Departments: Academic departments (e.g., Computer Science, Mathematics).
 Professors: Professors teaching the courses.
 Enrollments: Relationship between students and the courses they are enrolled in.
 Classrooms: Information about classrooms used for courses.
 Semesters: Different academic semesters (e.g., Fall 2025, Spring 2025).

Establish Relationships Between Tables

Now, let’s establish relationships between tables:

 Students and Departments: A student belongs to one department, but a department


can have many students. This is a one-to-many relationship (Department -> Students).
 Courses and Departments: A course belongs to one department, but a department
can offer many courses. This is also a one-to-many relationship (Department ->
Courses).
 Professors and Departments: A professor belongs to one department, but a
department can have many professors. This is a one-to-many relationship
(Department -> Professors).
 Enrollments: This is a many-to-many relationship between Students and Courses. A
student can enroll in many courses, and a course can have many students enrolled.
The Enrollments table is used to represent this relationship.
 Courses and Professors: A course can have one or more professors, and a professor
can teach many courses. You can create a many-to-many relationship through a
junction table like CourseProfessors:
 course_id (Foreign Key referencing Courses)
 professor_id (Foreign Key referencing Professors)
 Courses and Classrooms: A course can be held in one or more classrooms, and a
classroom can host many courses. This is a many-to-many relationship managed by
the CourseSchedules table.
 Enrollments and Semesters: An enrollment is linked to a specific semester. This is a
one-to-many relationship from Semesters to Enrollments.

Example SQL Schema


CREATETABLE Departments (
department_id INTPRIMARY KEY,
department_name VARCHAR(100)
);

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

create table department (

dno int primary key,

dname varchar(50) unique,

mgr_ssn char(9),

mgr_start_date date

);

TABLE-2
create table employeee (

ssn number (9) primary key,

fname varchar(50) not null,

lname varchar(50) not null,

minit char(1),

address varchar(50),

birthdate date,

salary decimal(10,2),

super_ssn number(9)references employeee(ssn),

dno int not null references department(dno);

TABLE-3

create table dept_location(

dno number(10),

dlocation varchar(50),

foreign key(dno) references department(dno));

TABLE-4

create table project (

pno int primary key,

pname varchar(50),

plocation varchar(50),

dno int,

foreign key (dno) references department(dno)

);

TABLE-5

create table works_on (

essn number(20),

pno int,

hours decimal(5,2),
primary key (essn, pno),

foreign key (essn) references employeee(ssn),

foreign key (pno) references project(pnumber)

);

TABLE-6

create table dependent (

essn number(9),

dependent_name varchar(50),

sex char(1),

bdate date,

relationship varchar(50),

primary key (essn, dependent_name),

foreign key (essn) references employeee(ssn)

);

2. Populate the tables with data

insert into department values

(1, 'research', '888665555', '22-MAY-1982');

insert into department values(5, 'marketing', '999887777', '12-DEC-1990');

insert into department values(7, 'finance', '888665555', '20-FEB-1970');

insert into department values(4, 'production', '987987987', '22-apr-1985');

insert into department values(10, 'education', '999887788', '12-DEC-1980');

select *from department;

DNO DNAME MGR_SSN MGR_START_DATE


1 research 888665555 22-May-82
5 marketing 999887777 12-Dec-90
7 finance 888665555 20-Feb-70
4 production 987987987 22-Apr-85
10 education 999887788 12-Dec-80

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('453456789','jennifer', 'wallace','s', '333 willow st, bellaire','20-


JUL-72',75000,453456789,7);

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);

select *from employeee;

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 null 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
123 main
1234567 david smith j st, us 23-Oct-50 60000 1234567 10

insert into dept_location values(1, 'Bellaire');

insert into dept_location values(1, 'Sugarland');

insert into dept_location values(4, 'Stafford');

insert into dept_location values(5, 'Houston');

insert into dept_location values(7, 'bellaire');


select *from dept_location;

DNO DLOCATION
1 Bellaire
1 Sugarland
4 Stafford
5 Houston
7 bellaire

insert into project values(1, 'Product X', 'Bellaire', 1);

insert into project values(2, 'Product Y', 'Sugarland', 1);

insert into project values(3, 'Product Z', 'Stafford', 4);

insert into project values(10, 'Computerization', 'Houston', 5);

insert into project values(20, 'Reorganization', 'Bellaire', 1);

insert into project values(30, 'New Benefits', 'Stafford', 4);

select *from project;

PNO PNAME PLOCATION DNO


1 Product X Bellaire 1
2 Product Y Sugarland 1
3 Product Z Stafford 4
10 Computerization Houston 5
20 Reorganization Bellaire 1
30 New Benefits Stafford 4

insert into works_on values('123456789', 2, 20.0);

insert into works_on values('333445555', 2, 10.0);

insert into works_on values('333445555', 3, 10.0);

insert into works_on values('999887777', 3, 30.0);

insert into works_on values('999887777', 10, 10.0);

select * from works_on;

ESSN PNO HOURS


123456789 1 30
123456789 2 20
333445555 2 10
333445555 3 10
999887777 3 30
999887777 10 10

insert into dependent values

('123456789', 'alice', 'f', '15-may-1952', 'daughter');

insert into dependent values('123456789', 'robert', 'm', '03-nov-1960', 'son');

insert into dependent values('333445555', 'joy', 'f', '12-jan-1989', 'spouse');

insert into dependent values('999887777', 'abhay', 'm', '23-jul-1980', 'spouse');

select *from dependent;

ESSN DEPENDENT_NAME SEX BDATE RELATIONSHIP


123456789 alice f 15-May-52 daughter
123456789 robert m 03-Nov-60 son
333445555 joy f 12-Jan-89 spouse
999887777 abhay m 23-Jul-80 spouse

3. . . Display all the details of all employees working in the company


select *from employeee;

SSN FNAME LNAME MINIT ADDRESS BIRTHDATE SALARY SUPER_SSN DNO


123 main
123456789 john smith j st, anytown 23-Oct-50 60000 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 null 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

4. Employees in department 7
SELECT ssn, lname, fname, address FROM Employeee WHERE dno = 7;

SSN LNAME FNAME ADDRESS


6.67E+08 english joyce 101 pine st, houston
4.53E+08 wallace jennifer 333 willow st, bellaire
5. Retrieve the Birthdate and Address of the employee whose name is
'Franklin T. Wong
SELECT birthdate, address

FROM employeee

WHERE fname = 'franklin' AND lname = 'wong';

BIRTHDATE ADDRESS
17-Jun-65 456 oak ave, bellaire

6. Retrieve the name and salary of every employee


SELECT fname, lname, salary
FROM Employeee;
FNAME LNAME SALARY
john smith 60000
franklin wong 40000
ramesh narayan 55000
joyce english 45000
alicia kaye 50000
jennifer wallace 75000
robert ford 52000

7.Retrieve All Distinct Salary Values


SELECT DISTINCT salary
FROM Employeee;
SALARY
55000
52000
60000
50000
40000
45000
75000

8.Retrieve All Employee Names Whose Address Is in 'Bellaire'


SELECT fname, lname
FROM Employeee
WHERE address LIKE '%bellaire%';
FNAME LNAME
franklin wong
jennifer wallace

9. Retrieve all employees who were born during the 1950s


SELECT fname, lname
FROM Employeee A
WHERE birthdate BETWEEN '01-jan-1950' AND '31-dec-1959';
FNAME LNAME
john smith
ramesh narayan
joyce english
alicia kaye

10. Retrieve all employees in department 5 whose salary is between 50,000 and
60,000(inclusive)

SELECT fname, lname, salary


FROM Employeee
WHERE dno = 5 AND salary BETWEEN 50000 AND 60000;
FNAME LNAME SALARY
john smith 60000

11. Retrieve the names of all employees who do not have supervisors

SELECT fname, lname


FROM Employeee
WHERE super_ssn IS NULL;
FNAME LNAME
ramesh narayan

12. Retrieve SSN and department name for all employees


SELECT E.SSN, E.DNAME
FROM Employeee E
JOIN Department D ON E.dno = D.dno;
SSN DNAME
123456789 marketing
333445555 marketing
999887777 research
666884444 finance
987654321 production
453456789 finance
765432109 production

13. Retrieve the name and address of all employees who work for the 'Research'
department

SELECT fname, lname, address

FROM Employeee E

JOIN Department D ON E.DNo = D.DNo

WHERE D.DName = 'research';

FNAME LNAME ADDRESS


ramesh narayan 789 elm st, stafford

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.

SELECT P.PNO, P.DNO, E.LNAME, E.ADDRESS, E. BIRTHDATE

FROM Project P

JOIN Department D ON P.DNO = D.DNO

JOIN employeee E ON D.MGR_SSN = E.SSN

WHERE P.PLOCATION = 'Stafford';

no data found

15. For each employee, retrieve the employee's name, and the name of his or her
immediate supervisor.

SELECT E.FName, E.LName, S.FName AS SupervisorFName, S.LName AS SupervisorLName


FROM Employeee E LEFT JOIN Employeee S ON E.Super_SSN = S.SSN;

FNAME LNAME SUPERVISORFNAME SUPERVISORLNAME


john smith john smith
franklin wong franklin wong
ramesh narayan
joyce english joyce english
alicia kaye alicia kaye
jennifer wallace jennifer wallace
robert ford alicia kaye

16. Retrieve all combinations of Employee Name and Department Name

SELECT E.FName, E.LName, D.DName FROM Employeee E, Department D;

FNAME LNAME DNAME


john smith research
franklin wong research
ramesh narayan research
joyce english research
alicia kaye research
jennifer wallace research
robert ford research
john smith marketing
franklin wong marketing
ramesh narayan marketing
joyce english marketing
alicia kaye marketing
jennifer wallace marketing
robert ford marketing
john smith finance
franklin wong finance
ramesh narayan finance
joyce english finance
alicia kaye finance
jennifer wallace finance
robert ford finance
john smith production
franklin wong production
ramesh narayan production
joyce english production
alicia kaye production
jennifer wallace production
robert ford production
17. Make a list of all project numbers for projects that involve an employee whose last
name is 'Narayan’ either as a worker or as a manager of the department that controls
the project.

SELECT DISTINCT P.PNo FROM Project P

JOIN works_on W ON P.PNo = W.PNo

JOIN Employeee E ON W.ESSN = E.SSN

WHERE E.LName = 'narayan';

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.

SELECT E.FName, E.LName, P.PName FROM Employeee E


JOIN works_on W ON E.SSN = W.ESSN
JOIN Project P ON W.PNo = P.PNo
ORDER BY E.DNo, E.FName;

FNAME LNAME PNAME


ramesh narayan Product Z
ramesh narayan Computerization
franklin wong Product Y
franklin wong Product Z
john smith Product X
john smith Product Y

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.

SELECT DISTINCT W.ESSN FROM works_on W JOIN Project P ON W.PNo = P.PNo


WHERE P.PLocation IN ('Bellaire', 'Houston', '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.

SELECT SUM(SALARY) AS TotalSalary, COUNT(*) AS NumEmployees, MAX(SALARY), MIN(SALARY),


AVG(SALARY)

FROM Employeee E JOIN Department D ON E.DNo = D.DNo WHERE D.DName = 'marketing';

TOTALSALARY NUMEMPLOYEES MAX(SALARY) MIN(SALARY) AVG(SALARY)


109000 2 69000 40000 54500

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

25. Delete all dependents of employee whose ssn is ‘123456789’.


DELETE FROM Dependent WHERE essn = '123456789';

2 row(s) deleted.

ESSN DEPENDENT_NAME SEX BDATE RELATIONSHIP


333445555 joy f 12-Jan-89 spouse
999887777 abhay m 23-Jul-80 spouse

26. Perform a query using alter command to drop/add field and a constraint in
Employee table.

ALTER TABLE Employeee ADD phone_number VARCHAR(15);


ALTER TABLE Employeee DROP COLUMN phone_number;

You might also like