SQL Lab Manual DB
SQL Lab Manual DB
Lab Manual
INTRODUCTION TO SQL
Page 2
DBMS Lab Manual 2023
DATA TYPES
Numeric: NUMBER, INTEGER, INT, FLOAT, DECIMAL
Page 3
DBMS Lab Manual 2023
Page 4
DBMS Lab Manual 2023
Experiment 1:
Consider following databases and draw ER diagram and convert entities and relationships
to relation table for a given scenario.
1. COMPANY DATABASE:
EMPLOYEE (SSN, Name, Address, Sex, Salary, SuperSSN,
DNo)
DEPARTMENT (DNo, DName, MgrSSN, MgrStartDate)
DLOCATION (DNo,DLoc)
PROJECT(PNo, PName, PLocation,
DNo)
WORKS_ON (SSN, PNo, Hours)
Page 5
DBMS Lab Manual 2023
SOLUTION:
COMPANY DATABASE:
E-R Diagram
Schema Diagram
Page 6
DBMS Lab Manual 2023
Experiment 2
Consider the MOVIE DATABASE
Page 7
DBMS Lab Manual 2023
SOLUTION:
Operation Purpose
Select(σ) The SELECT operation is used for selecting a subset of the tuples
according to a given selection condition
Projection(π) The projection eliminates all attributes of the input relation but those
mentioned in the projection list.
Set Difference(-) - Symbol denotes it. The result of A - B, is a relation which includes
all tuples that are in A but not in B.
Intersection(∩) Intersection defines a relation consisting of a set of all tuple that are
in both A and B.
Inner Join Inner join, includes only those tuples that satisfy the matching
criteria.
Theta Join(θ) The general case of JOIN operation is called a Theta join. It is
denoted by symbol θ.
EQUI Join When a theta join uses only equivalence condition, it becomes a equi
join.
Natural Join(⋈) Natural join can only be performed if there is a common attribute
(column) between the relations.
Outer Join In an outer join, along with tuples that satisfy the matching criteria.
Left Outer Join( In the left outer join, operation allows keeping all tuple in the left
) relation.
Page 8
DBMS Lab Manual 2023
Right Outer join( In the right outer join, operation allows keeping all tuple in the right
) relation.
Full Outer Join( ) In a full outer join, all tuples from both relations are included in the
result irrespective of the matching condition.
Page 9
DBMS Lab Manual 2023
Page 10
DBMS Lab Manual 2023
Page 11
DBMS Lab Manual 2023
Experiment 3
SOLUTION:
1. Creating a Database
CREATE DATABASE Company;
SHOW tables;
Page 12
DBMS Lab Manual 2023
Page 13
DBMS Lab Manual 2023
Page 14
DBMS Lab Manual 2023
Update
UPDATE EMPLOYEE SET DNO=‘5‘, SUPERSSN=‘RNSCSE06‘ WHERE
SSN=‘RNSCSE05‘;
Page 15
DBMS Lab Manual 2023
Experiment 4
Consider Dept table
SOLUTION:
Create Table
2. Add a new column PINCODE with not null constraints to the existing table DEPT
Table altered.
Page 16
DBMS Lab Manual 2023
3. All constraints and views that reference the column are dropped automatically, along
with the column.
Table altered.
Table altered.
SQL> DESC DEPARTMENT;
Name Null? Type
6. Delete table
SQL> DROP TABLE DEPARTMENT;
Table dropped.
Page 17
DBMS Lab Manual 2023
Experiment 5A
Consider Employee table
E101 45000
E102 70000
E103 120000
E105 67000
E106 145000
employee; AVG(SALARY)
89400
Page 18
DBMS Lab Manual 2023
employee; COUNT(*)
SUM(SALARY)>120000;
EMP_NAME SUM(SALY)
mahesh 145000
sunita 187000
Page 19
DBMS Lab Manual 2023
EMP_NAME
sunita
sunita
mahes
h Amit
Amit
8. Display details of employee whose name is AMIT and salary greater than 50000;
Page 20
DBMS Lab Manual 2023
Experiment 5B
For a given tables
Page 21
DBMS Lab Manual 2023
SOLUTION
Table altered.
Page 22
DBMS Lab Manual 2023
MGRSTARTDATE DATE
MGRSSN VARCHAR2(20)
Note: update entries of employee table to fill missing fields SUPERSSN and DNO
UPDATE EMPLOYEE SET SUPERSSN=NULL, DNO=‘3‘ WHERE
SSN=‘RNSECE01‘;
UPDATE EMPLOYEE SET SUPERSSN=‘RNSCSE02‘, DNO=‘5‘ WHERE
SSN=‘RNSCSE01‘;
UPDATE EMPLOYEE SET SUPERSSN=‘RNSCSE03‘, DNO=‘5‘ WHERE
SSN=‘RNSCSE02‘;
UPDATE EMPLOYEE SET SUPERSSN=‘RNSCSE04‘, DNO=‘5‘ WHERE
SSN=‘RNSCSE03‘;
Page 23
DBMS Lab Manual 2023
2. Find the sum of the salaries of all employees of the ‘Accounts’ department, as well
as the maximum salary, the minimum salary, and the average salary in this
department
SQL> SELECT SUM(E.SALARY),MAX(E.SALARY),MIN(E.SALARY),
AVG(E.SALARY)FROM EMPLOYEE1 E,DEPARTMENT D WHERE
E.DNO=D.DNUMBER AND D.DNAME='RESEARCH';
E.FNAME,E.LNAME 2
FROM EMPLOYEE1 E
Page 24
DBMS Lab Manual 2023
4. Retrieve the name of each dept and number of employees working in each
department which has at least 2 employees
SELECT DNAME, COUNT(*)
FROM EMPLOYEE E, DEPARTMENT D
WHERE D.DNO=E.DNO
AND D.DNO IN (SELECT E1.DNO
FROM EMPLOYEE E1
GROUP BY E1.DNO
having count(*)>2 )
ORDER BY DNO;
6. Retrieve the name of employees and their dept name (using JOIN)
Page 25
DBMS Lab Manual 2023
Page 26
DBMS Lab Manual 2023
Experiment 6
For a given EMPLOYEE tables
SOLUTION:
Page 27
DBMS Lab Manual 2023
Creating View
The query that defines the sales_staffview references only rows in department 5.
Furthermore, the CHECK OPTION creates the view with the constraint (named
sales_staff_cnst) that INSERT and UPDATE statements issued against the view cannot
result in rows that the view cannot select.
View created.
3. Drop View
Page 28
DBMS Lab Manual 2023
Viva Questions
1. What is SQL?
Structured Query Language
2. What is database?
A database is a logically coherent collection of data with some inherent meaning,
representing some aspect of real world and which is designed, built and populated with
data for a specific purpose.
3. What is DBMS?
It is a collection of programs that enables user to create and maintain a database. In other
words it is general-purpose software that provides the users with the processes of
defining, constructing and manipulating the database for various applications.
4. What is a Database system?
The database and DBMS software together is called as Database system.
5. Advantages of DBMS?
Redundancy is controlled.
Unauthorized access is restricted.
Providing multiple user interfaces.
Enforcing integrity constraints.
Providing backup and recovery.
Page 29
DBMS Lab Manual 2023
View level: The highest level of abstraction describes only part of entire database.
8. Define the "integrity rules"
Data independence means that ―the application is independent of the storage structure and
access strategy of data‖. In other words, the ability to modify the schema definition in one level
should not affect the schema definition in the next higher level.
Two types of Data Independence:
Physical Data Independence: Modification in physical level should not affect the logical level.
Logical Data Independence: Modification in logical level should affect the view level.
Page 30
DBMS Lab Manual 2023
Page 31
DBMS Lab Manual 2023
Page 32