You are on page 1of 27

DBMS LAB MANUAL

ON
“SQL COMMANDS AND ER DIAGRAMS”

SUBMITTED
IN PARTIAL FULFILMENT FOR THE REQUIREMENT OF
MASTER OF BUSINESS ADMINISTRATION
(2021-2023)

SUBJECT : DATABASE MANAGEMENT SYSTEM LAB

Under the Supervision of: By:

Ms. LAKSHMI KUMARI KAREENA LUTHRA


Enrolment No.: 01215103921

Management Education and Research Institute (MERI)

Affiliated to Guru Gobind Singh Indraprastha University


DECLARATION

I KAREENA LUTHRA hereby state that this report has been submitted to
Management Education & Research Institute in partial fulfilment of the
requirement for Masters of Business Administration on the topic “SQL
COMMANDS AND ER DIAGRAMS.”

I have followed all the guidelines provided by the University in writing the report.
And I assert that the work contained in this report is original and has been done by
me under the supervision of my Mentor Ms. Lakshmi Kumari.

NAME: KAREENA LUTHRA


ENROLMENT NO: 01215103921
PLACE: NEW DELHI
ACKNOWLEDGEMENT

The success and outcome of this report required a lot of guidance and assistance
from many people and I am extremely privileged to get this all along the completion.
My sincere thanks and humble regards to Management Education and Research
Institute, Affiliated to Guru Gobind Singh Indraprastha University, New Delhi for
imparting us very valuable professional training in MBA.
My gratitude and sincere regards to Ms. Lakshmi Kumari my project guide, for
guiding me throughout the project.
I am very thankful to her as she has been a constant source of advice, motivation, and
inspiration.
I am thankful to my family and friends for constantly motivating me to complete the
report and providing me an environment which enhanced my knowledge.

NAME: KAREENA LUTHRA


COURSE: MBA SEMESTER-III
BATCH: 2021-2023
ENROLMENT NO: 01215103921
TABLE OF CONTENTS
S.NO TOPIC PAGE

NO.
EXPERIMENT-1
1. PART A: CREATION OF TABLE EMPLOYEE 8

2. PART B: INSERTION OF RECORDS IN TABLE 9


EMPLOYEE
3. PART C: ADDITION OF ATTRIBUTE HOBBY 10

4. PART D: UPDATE SALARY OF AN EMPLOYEE 12

5. PART E: SHOW EMPID, EMPNAME OF ALL 12


EMPLOYEES
6. PART F: SHOW RECORDS WHERE SALARY IS 13
GREATER THAN 25000
7. PART G: RECORDS OF THOSE WHERE EMPDEPT= 13
’FINANCE’ AND HOBBY= ’READING’ OR
‘WRITING’
8. PART H: SELECT ALL EMPLOYEES WHOSE SALARY 14
RANGES FROM 25000 TO 50000
9. PART I: SELECT NAMES OF THOSE EMPLOYEES 14
WHOSE NAMES START WITH “A”
10. PART J: SELECT AVERAGE SALARY OF AN 15
EMPLOYEE
11. PART K: COUNT THE TOTAL NUMBER OF RECORDS 15

12. PART L: CALCULATE THE TOTAL SALARY OF 16


EMPLOYEES

13. PART M: DISPLAY THE EMPLOYEE NAMES IN 16


LOWER CASE
14. PART N: DISPLAY THE EMPLOYEE NAMES IN UPPER 17
CASE

15. PART O: DISPLAY LENGTH OF HOBBY FIELD AND 17


ALSO GIVES ITS ALIAS NAME AS “LENGTH OF THE
HOBBY FIELD”
16. PART P: COUNT THE TOTAL NO OF EMPLOYEES IN 18
EACH DEPARTMENT WITH THEIR AVERAGE SALARY

17. PART Q: COUNT THE TOTAL NO OF EMPLOYEES IN 19


EACH DEPARTMENT WITH TOTAL SALARY GREATER
THAN 50000

18. PART R: CREATION OF TABLE “DEPARTMENT” 21

19. PART S: CONCEPT OF “FOREIGN KEY” USING THE 22


ABOVE TABLES

20. PART T: DELETION OF RECORDS IN BOTH TABLES 24


AND THEN REMOVAL OF TABLES

EXPERIMENT-2

21. CASE STUDY : BOOK PUBLISHING COMPANY 27

EXPERIMENT-3

22. CASE STUDY : GENERAL HOSPITAL 34

EXPERIMENT-4

23. SQL QUERY TO IMPLEMENT JOINS 45

EXPERIMENT-5

24. SQL QUERY TO IMPLEMENT VIEWS, TRIGGER & 47


PROCEDURE

EXPERIMENT-1

A. Create a table with the following attributes:


Empid integer PRIMARY KEY
Empname character (30) NOT NULL
Empsal integer NOT NULL
Empdept character (30) NOT NULL
Dept_No. Integer FOREIGN KEY

B. Insert a new record.


C. Add an attribute “hobby” character (30) to the table employee.
D. Update salary of an employee to 35000 whose empid = 101.

E. Show empid, empname of all employees.


F. Show records of all employees where salary is greater than 25000.

G. Show records of all employees whose empdept is ‘Finance’ and hobby is


either ‘reading’ or ‘writing’ .
H. Select all employees whose salary ranges from 25000 to 50000.

I. Select the names of all employees whose names start from ‘A’.
J. Select the average salary of an employee.

K. Count the total number of records.


L. Calculate the total salary of employees.

M. Display the employee names in lower case.


N. Display the employee names in capitals.

O. Display the length of the hobby field. Also give its alias name as “Length
of the hobby field”.
P. Count the total number of employees in each department with their
average salary.

Q. Count the total number of employees in each department with total


salary greater than 50000.
R. Create a table “Department” with the following:
DEPT_NO. INTEGER PRIMARY KEY
DEPT_NAME CHARACTER UNIQUE
LOCATION CHARACTER NOT NULL
S. Explain the concept of foreign key using the above tables.
“FOREIGN KEY CONSTRAINT”

In an RDBMS, tables reference one another through common fields and to ensure
validity references, referential integrity is enforced. Referential integrity is a system
of rules that a DBMS uses to ensure that relationships between records in related
tables are valid, and that users don’t accidentally delete or change related data.
Referential integrity is ensured through FOREIGN KEY constraint.
Where two tables are related by a common column (or set of columns), then the
related columns in the ‘parent table’ (or primary table) should be either declared
a PRIMARY KEY or UNIQUE KEY and the related columns in the ‘child table’ (or
related table) should have FOREIGN KEY constraint.

For instance, in the above two tables,

EMPLOYEE (empid, empname, empsal, empdept, deptno)


DEPARTMENT (deptno, dept_name, location)

All the dept numbers i.e., deptno in the employee table are derived from
DEPARTMENT table as, DEPARTMENT is the “PARENT TABLE” whereas
EMPLOYEE is the “CHILD TABLE”. Simultaneously, dept no in the
Department table is known as “primary key” & as “foreign key” in the
Employee table.

This type of relationship between two tables and their attributes (fields) is called a
referential integrity constraint. Referential integrity constraints can be implemented
with the CREATE OR ALTER TABLE statements.
T. Delete all the records and remove both the tables.
EXPERIMENT-2

CASE STUDY: BOOK PUBLISHING COMPANY

AIM: A publishing company produces scientific books on various subjects. The books
are written by authors who specialize in one subject. The company employs editors
who, not necessarily being specialists in a particular area, each take sole
responsibility for editing one or more publications.
A publication covers essentially one of the specialist subjects and is normally written
by a single author. When writing a particular book, each author works with on editor,
but may submit another work for publication to be supervised by other editors. To
improve their competitiveness, the company tries to employ a variety of authors,
more than one author being a specialist in a particular subject.

ER DIAGRAM FOR BOOK PUBLISHING COMPANY

concerns edited by
Subject Publication

is specialized in

Editor
is written by

Author

Author-Editor
works with employs
EXPERIMENT-3

CASE STUDY: GENERAL HOSPITAL

AIM: A General Hospital consists of a number of specialized wards (such as


Maternity, Paediatry, Oncology, etc). Each ward hosts a number of patients, who
were admitted on the recommendation of their own GP and confirmed by a
consultant employed by the Hospital. On admission, the personal details of every
patient are recorded.

A separate register is to be held to store the information of the tests undertaken and
the results of a prescribed treatment. A number of tests may be conducted for each
patient. Each patient is assigned to one leading consultant but may be examined by
another doctor, if required. Doctors are specialists in some branch of medicine and
may be leading consultants for a number of patients, not necessarily from the same
ward.

ER DIAGRAM FOR GENERAL HOSPITAL

specialised in
Ward Specialty

Tests

gives
is admitted to
specialised in

Test-Results Treatment

follows devises
gets

is diagnosed diagnoses
Patient Diagnosis Doctor

recommends
assigned to
0

GP Consultant
EXPERIMENT-4

INNER JOIN
The INNER JOIN keyword selects records that have matching values in both tables.

SQL QUERY TO IMPLEMENT INNER JOIN -

SELECT column_name(s)
FROM table1
INNER JOIN table2
ON table1.column_name = table2.column_name;

LEFT OUTER JOIN/ LEFT JOIN


The LEFT JOIN keyword returns all records from the left table (table1), and the
matching records from the right table (table2). The result is 0 records from the right
side, if there is no match.

SQL QUERY TO IMPLEMENT LEFT JOIN-

SELECT column_name(s)
FROM table1
LEFT JOIN table2
ON table1.column_name = table2.column_name;
RIGHT OUTER JOIN/ RIGHT JOIN
The RIGHT JOIN keyword returns all records from the right table (table2), and the
matching records from the left table (table1). The result is 0 records from the left
side, if there is no match.

SQL QUERY TO IMPLEMENT RIGHT JOIN -

SELECT column_name(s)
FROM table1
RIGHT JOIN table2
ON table1.column_name = table2.column_name;
EXPERIMENT-5

VIEWS
A view is nothing more than a SQL statement that is stored in the database with an
associated name. A view is a composition of a table in the form of a
predefined SQL query.

A view can contain all rows of a table or select rows from a table. A view can be
created from one or many tables which depends on the written SQL query to create a
view.

SQL QUERY TO IMPLEMENT VIEW -

CREATE VIEW view_name AS SELECT column1, column2..... FROM table_name


WHERE [condition];

TRIGGER
A trigger is a stored procedure in database which automatically invokes
whenever a special event in the database occurs. For example, a trigger can
be invoked when a row is inserted into a specified table or when certain table
columns are being updated.

SQL QUERY TO IMPLEMENT TRIGGER-

create trigger [trigger_name]


[before | after]
{insert | update | delete}
on [table_name]
[for each row]
[trigger_body]
PROCEDURE

A stored procedure is a group of one or more pre-compiled SQL


statements into a logical unit. It is stored as an object inside the database server.
It is a subroutine or a subprogram in the common computing language that has been
created and stored in the database.

Each procedure in SQL Server always contains a name, parameter lists, and
Transact-SQL statements. The SQL Database Server stores the stored procedures as
named objects.

SQL QUERY TO CREATE A PROCEDURE-

CREATE PROCEDURE [schema_name].procedure_name


@parameter_name data_type,
....
parameter_name data_type
AS
BEGIN
-- SQL statements
-- SELECT, INSERT, UPDATE, or DELETE statement
END

SQL QUERY TO EXECUTE A PROCEDURE-

EXEC procedure_name;
Or
EXECUTE procedure_name;

-----------------------

You might also like