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

SQL Lab Manual DB

Uploaded by

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

SQL Lab Manual DB

Uploaded by

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

Department of Computer Science

Lab Manual

DATABASE MANAGEMENT SYSTEM


DBMS Lab Manual 2023

INTRODUCTION TO SQL

Pronounced as SEQUEL: Structured QUERY Language

 Pure non-procedural query language


 Designed and developed by IBM, Implemented by Oracle
 1978 System/R IBM- 1st Relational DBMS
 1979 Oracle and Ingres
 1982 SQL/DS and DB2 IBM
 Accepted by both ANSI + ISO as Standard Query Language for any RDBMS
 SQL86 (SQL1) : first by ANSI and ratified by ISO (SQL-87), minor revision on 89
(SQL-89)
 SQL92 (SQL2) : major revision
 SQL99 (SQL3) : add recursive query, trigger, some OO features, and non-scholar type
 SQL2003 : XML, Window functions, and sequences (Not free)
 Supports all the three sublanguages of DBMS: DDL, DML, DCL
 Supports Aggregate functions, String Manipulation functions, Set theory operations, Date
Manipulation functions, rich set of operators ( IN, BETWEEN, LIKE)
 Supports REPORT writing features and Forms for designing GUI based applications

Page 2
DBMS Lab Manual 2023

Data Definition in SQL


CREATE, ALTER and DROP
table ................................................................ relation
row… .............................................................. tuple
column…........................................................ attribute

DATA TYPES
 Numeric: NUMBER, INTEGER, INT, FLOAT, DECIMAL

 Character: CHAR, VARCHAR(n) and Text,


 Boolean: true, false, and null

Page 3
DBMS Lab Manual 2023

SQL and PL/SQL tutorial: https://www.w3schools.com/sql/, http://www.plsqltutorial.com/

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

Write following relational algebra queries for a given set of relations.


1. Find movies made after 1997
2. Find movies made by Hanson after 1997
3. Find all movies and their ratings
4. Find all actors and directors
5. Find Coen’s movies with McDormand

Page 7
DBMS Lab Manual 2023

SOLUTION:

Common notations of Relational Algebra

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.

Union UNION is symbolized by symbol. It includes all tuples that are in


Operation(∪ ) tables A or in B.

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.

Cartesian Cartesian operation is helpful to merge columns from two relations.


Product(X)

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.

1. Find movies made after 1997


σmyear>1997(Movies)

2. Find movies made by Hanson after 1997


σmyear>1997 ∧ director=‘Hanson_(Movies)

Page 9
DBMS Lab Manual 2023

3. Find all movies and their ratings


πtitle, rating(Movies)

4. Find all actors and directors


πactor(Actors) ∪ πdirector(Directors)

Page 10
DBMS Lab Manual 2023

5. Find Coen’s movies with McDormand


e1 = πtitle(σactor=‘McDormand_ (Acts))
e2 = πtitle(σdirector=‘Coen_ (Movies))
result = e1 ∩ e2

Page 11
DBMS Lab Manual 2023

Experiment 3

Consider the Company database with following tables

Perform the following:


1. Create company database
2. Viewing all databases
3. Viewing all Tables in a Database,
4. Creating Tables (With and Without Constraints)
5. Inserting/Updating/Deleting Records in a Table
6. Saving (Commit) and Undoing (rollback)

SOLUTION:

1. Creating a Database
CREATE DATABASE Company;

2. Viewing all databases


SHOW DATABASES;

3. Viewing all Tables in a Database,

SHOW tables;

4. Creating Tables (With and Without


Constraints)

CREATE TABLE DEPARTMENT


(DNO VARCHAR2 (20) PRIMARY KEY,
DNAME VARCHAR2 (20),
MGRSTARTDATE DATE);

Page 12
DBMS Lab Manual 2023

CREATE TABLE EMPLOYEE


(SSN VARCHAR2 (20) PRIMARY KEY,
FNAME VARCHAR2 (20),
LNAME VARCHAR2 (20),
ADDRESS VARCHAR2 (20),
SEX CHAR (1),
SALARY INTEGER,
DNO FOREIGN KEY REFERENCES
DEPARTMENT (DNO));

5. Inserting/Updating/Deleting Records in a Table,

INSERT INTO EMPLOYEE (SSN, FNAME, LNAME, ADDRESS, SEX, SALARY)


VALUES (‗RNSECE01‘,‘JOHN‘,‘SCOTT‘,‘BANGALORE‘,‘M‘, 450000);
INSERT INTO EMPLOYEE (SSN, FNAME, LNAME, ADDRESS, SEX, SALARY)
VALUES (‗RNSCSE01‘,‘JAMES‘,‘SMITH‘,‘BANGALORE‘,‘M‘, 500000);
INSERT INTO EMPLOYEE (SSN, FNAME, LNAME, ADDRESS, SEX, SALARY)
VALUES (‗RNSCSE02‘,‘HEARN‘,‘BAKER‘,‘BANGALORE‘,‘M‘, 700000);
INSERT INTO EMPLOYEE (SSN, FNAME, LNAME, ADDRESS, SEX, SALARY)
VALUES (‗RNSCSE03‘,‘EDWARD‘,‘SCOTT‘,‘MYSORE‘,‘M‘, 500000);
INSERT INTO EMPLOYEE (SSN, FNAME, LNAME, ADDRESS, SEX, SALARY)
VALUES (‗RNSCSE04‘,‘PAVAN‘,‘HEGDE‘,‘MANGALORE‘,‘M‘, 650000);
INSERT INTO EMPLOYEE (SSN, FNAME, LNAME, ADDRESS, SEX, SALARY)
VALUES (‗RNSCSE05‘,‘GIRISH‘,‘MALYA‘,‘MYSORE‘,‘M‘, 450000);
INSERT INTO EMPLOYEE (SSN, FNAME, LNAME, ADDRESS, SEX, SALARY)
VALUES (‗RNSCSE06‘,‘NEHA‘,‘SN‘,‘BANGALORE‘,‘F‘, 800000);
INSERT INTO EMPLOYEE (SSN, FNAME, LNAME, ADDRESS, SEX, SALARY)
VALUES (‗RNSACC01‘,‘AHANA‘,‘K‘,‘MANGALORE‘,‘F‘, 350000);
INSERT INTO EMPLOYEE (SSN, FNAME, LNAME, ADDRESS, SEX, SALARY)
VALUES (‗RNSACC02‘,‘SANTHOSH‘,‘KUMAR‘,‘MANGALORE‘,‘M‘, 300000);
INSERT INTO EMPLOYEE (SSN, FNAME, LNAME, ADDRESS, SEX, SALARY)
VALUES (‗RNSISE01‘,‘VEENA‘,‘M‘,‘MYSORE‘,‘M‘, 600000);
INSERT INTO EMPLOYEE (SSN, FNAME, LNAME, ADDRESS, SEX, SALARY)
VALUES (‗RNSIT01‘,‘NAGESH‘,‘HR‘,‘BANGALORE‘,‘M‘, 500000);

Page 13
DBMS Lab Manual 2023

INSERT INTO DEPARTMENT VALUES (‗1‘,‘ACCOUNTS‘,‘01-JAN-


01‘);
INSERT INTO DEPARTMENT VALUES (‗2‘,‘IT‘,‘01-AUG-16‘)

Page 14
DBMS Lab Manual 2023

INSERT INTO DEPARTMENT VALUES (‗3‘,‘ECE‘,‘01-JUN-08‘);


INSERT INTO DEPARTMENT VALUES (‗4‘,‘ISE‘,‘01-AUG-15);
INSERT INTO DEPARTMENT VALUES (‗5‘,‘CSE‘,‘01-JUN-02’);

Update
UPDATE EMPLOYEE SET DNO=‘5‘, SUPERSSN=‘RNSCSE06‘ WHERE
SSN=‘RNSCSE05‘;

Delete entries of employee table where DNO =1;

DELETE FROM EMPLOYEE WHERE DNO=1;

Page 15
DBMS Lab Manual 2023

Experiment 4
Consider Dept table

DEPTNO DNAME LOC

Perform the following:


1. Rename the table dept as department
2. Add a new column PINCODE with not null constraints to the existing table DEPT
3. All constraints and views that reference the column are dropped automatically, along
with the column.
4. Rename the column DNAME to DEPT_NAME in dept table
5. Change the data type of column loc as CHAR with size 10
6. Delete table

SOLUTION:

Create Table

SQL> CREATE TABLE DEPT(DEPTNO INTEGER, DNAME VARCHAR(10),LOC


VARCHAR(4), PRIMARY KEY(DEPTNO));

1. Rename the table dept as department

SQL> ALTER TABLE DEPT RENAME TO DEPARTMENT;


Table altered.

2. Add a new column PINCODE with not null constraints to the existing table DEPT

SQL> ALTER TABLE DEPARTMENT ADD(PINCODE NUMBER(6) NOT NULL);

Table altered.

SQL> DESC DEPARTMENT;


Name Null? Type

DEPTNO NOT NULL NUMBER(38)


DNAME VARCHAR2(10)
LOC VARCHAR2(4)
PINCODE NOT NULL NUMBER(6)

Page 16
DBMS Lab Manual 2023

3. All constraints and views that reference the column are dropped automatically, along
with the column.

SQL> ALTER TABLE DEPARTMENT DROP column LOC CASCADE


CONSTRAINTS;

Table altered.

SQL> desc dept


Name Null? Type

DEPTNO NOT NULL NUMBER(38)


DNAME VARCHAR2(10)
PINCODE NOT NULL NUMBER(6)

4. Rename the column DNAME to DEPT_NAME in dept table

SQL> ALTER TABLE DEPT RENAME COLUMN DNAME TO DEPT_NAME ;

Table altered.
SQL> DESC DEPARTMENT;
Name Null? Type

DEPTNO NOT NULL NUMBER(38)


DEPT_NAME VARCHAR2(10)
LOC VARCHAR2(4)
PINCODE NOT NULL NUMBER(6)

5. Change the datatype of colunm loc as CHAR with size 10

SQL> ALTER TABLE DEPARTMENT MODIFY LOC CHAR(10) ;


Table altered.
SQL> DESC DEPARTMENT;
Name Null? Type

DEPTNO NOT NULL NUMBER(38)


DEPT_NAME VARCHAR2(10)
LOC CHAR(10)
PINCODE NOT NULL NUMBER(6)

6. Delete table
SQL> DROP TABLE DEPARTMENT;
Table dropped.

Page 17
DBMS Lab Manual 2023

Experiment 5A
Consider Employee table

EMPNO EMP_NAME DEPT SALARY DOJ BRANCH


E101 Amit oduction 45000 12-Mar-00 Bangalore
E102 Amit HR 70000 03-Jul-02 Bangalore
E103 Sunita anagemen 120000 11-Jan-01 mysore
E105 Sunita IT 67000 01-Aug-01 mysore
E106 Mahesh Civil 145000 20-Sep-03 Mumbai

Perform the following


1. Display all the fields of employee table
2. Retrieve employee number and their salary
3. Retrieve average salary of all employee
4. Retrieve number of employee
5. Retrieve distinct number of employee
6. Retrieve total salary of employee which is greater than >120000
7. Display name of employee in descending order
8. Display details of employee whose name is AMIT and salary greater than 50000;

1. Display all the fields of employee table


SQL> select * from employee;
EMPNO EMP_NAME DEPT SALARY DOJ BRANCH
E101 Amit Production 45000 12-MAR-00 Bangalore
E102 Amit HR 70000 03-JUL-02 Bangalore
E103 sunita Management 120000 11-JAN-01 mysore
E105 sunita IT 67000 01-AUG-01 mysore
E106 mahesh Civil 145000 20-SEP-03 Mumbai

2. Retrieve employee number and their salary


SQL> select empno, salary from
employee; EMPNO SALARY

E101 45000
E102 70000
E103 120000
E105 67000
E106 145000

3. Retrieve average salary of all employee


SQL> select avg(salary) from

employee; AVG(SALARY)

89400

Page 18
DBMS Lab Manual 2023

4. Retrieve number of employee


SQL> select count(*) from

employee; COUNT(*)

5. Retrieve distinct number of employee


SQL> select count(DISTINCT emp_name) from
employee; COUNT(DISTINCTEMP_NAME)

6. Retrieve total salary of employee which is greater than >120000

SQL> SELECT EMP_NAME, SUM(SALARY) FROM


EMPLOYEE 2 GROUP BY(EMP_NAME)
3 HAVING

SUM(SALARY)>120000;

EMP_NAME SUM(SALY)

mahesh 145000
sunita 187000

7. Display name of employee in descending order

SQL> select emp_name from


employee 2 order by emp_name
desc;

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;

SQL> select * from employee


2 where emp_name='Amit' and salary>50000;

EMPNO EMP_NAME DEPT SALARY DOJ BRANCH


E102 Amit HR 70000 03-JUL-02 Bangalore

Page 20
DBMS Lab Manual 2023

Experiment 5B
For a given tables

Create tables and perform the following


1. How the resulting salaries if every employee working on the ‘Research’ Departments is
given a 10 percent raise.
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
3. Retrieve the name of each employee Controlled by department number 5 (use EXISTS
operator).
4. Retrieve the name of each dept and number of employees working in each department
which has at least 2 employees
5. Retrieve the name of employees who born in the year 1990’s
6. Retrieve the name of employees and their dept name (using JOIN)

Page 21
DBMS Lab Manual 2023

SOLUTION

SQL> CREATE TABLE DEPARTMENT(


DNO VARCHAR2 (20) PRIMARY KEY,
DNAME VARCHAR2 (20),
MGRSTARTDATE DATE);

SQL> DESC DEPARTMENT;


Name Null? Type

DNO NOT NULL VARCHAR2(20)


DNAME VARCHAR2(20)
MGRSTARTDATE DATE

SQL> CREATE TABLE EMPLOYEE(


SSN VARCHAR2 (20) PRIMARY KEY,
FNAME VARCHAR2 (20),
LNAME VARCHAR2 (20),
ADDRESS VARCHAR2 (20),
SEX CHAR (1),
SALARY INTEGER,
SUPERSSN REFERENCES EMPLOYEE (SSN),
DNO REFERENCES DEPARTMENT (DNO));

SQL> DESC EMPLOYEE;


Name Null? Type

SSN NOT NULL VARCHAR2(20)


FNAME VARCHAR2(20)
LNAME VARCHAR2(20)
ADDRESS VARCHAR2(20)
SEX CHAR(1)
SALARY NUMBER(38)
SUPERSSN VARCHAR2(20)
DNO NUMBER(38)

SQL> ALTER TABLE DEPARTMENT


2 ADD MGRSSN REFERENCES EMPLOYEE (SSN);

Table altered.

SQL> DESC DEPARTMENT;


Name Null? Type

DNO NOT NULL VARCHAR2(20)


DNAME VARCHAR2(20)

Page 22
DBMS Lab Manual 2023

MGRSTARTDATE DATE
MGRSSN VARCHAR2(20)

INSERT INTO EMPLOYEE (SSN, FNAME, LNAME, ADDRESS, SEX, SALARY)


VALUES (‘RNSECE01‘,‘JOHN‘,‘SCOTT‘,‘BANGALORE‘,‘M‘, 450000);
INSERT INTO EMPLOYEE (SSN, FNAME, LNAME, ADDRESS, SEX, SALARY)
VALUES (‘RNSCSE01‘,‘JAMES‘,‘SMITH‘,‘BANGALORE‘,‘M‘, 500000);
INSERT INTO EMPLOYEE (SSN, FNAME, LNAME, ADDRESS, SEX, SALARY)
VALUES (‘RNSCSE02‘,‘HEARN‘,‘BAKER‘,‘BANGALORE‘,‘M‘, 700000);
INSERT INTO EMPLOYEE (SSN, FNAME, LNAME, ADDRESS, SEX, SALARY)
VALUES (‘RNSCSE03‘,‘EDWARD‘,‘SCOTT‘,‘MYSORE‘,‘M‘, 500000);
INSERT INTO EMPLOYEE (SSN, FNAME, LNAME, ADDRESS, SEX, SALARY)
VALUES (‘RNSCSE04‘,‘PAVAN‘,‘HEGDE‘,‘MANGALORE‘,‘M‘, 650000);
INSERT INTO EMPLOYEE (SSN, FNAME, LNAME, ADDRESS, SEX, SALARY)
VALUES (‘RNSCSE05‘,‘GIRISH‘,‘MALYA‘,‘MYSORE‘,‘M‘, 450000);
INSERT INTO EMPLOYEE (SSN, FNAME, LNAME, ADDRESS, SEX, SALARY)
VALUES (‘RNSCSE06‘,‘NEHA‘,‘SN‘,‘BANGALORE‘,‘F‘, 800000);
INSERT INTO EMPLOYEE (SSN, FNAME, LNAME, ADDRESS, SEX, SALARY)
VALUES (‘RNSACC01‘,‘AHANA‘,‘K‘,‘MANGALORE‘,‘F‘, 350000);
INSERT INTO EMPLOYEE (SSN, FNAME, LNAME, ADDRESS, SEX, SALARY)
VALUES (‘RNSACC02‘,‘SANTHOSH‘,‘KUMAR‘,‘MANGALORE‘,‘M‘, 300000);
INSERT INTO EMPLOYEE (SSN, FNAME, LNAME, ADDRESS, SEX, SALARY)
VALUES (‘RNSISE01‘,‘VEENA‘,‘M‘,‘MYSORE‘,‘M‘, 600000);
INSERT INTO EMPLOYEE (SSN, FNAME, LNAME, ADDRESS, SEX, SALARY)
VALUES (‘RNSIT01‘,‘NAGESH‘,‘HR‘,‘BANGALORE‘,‘M‘, 500000);

INSERT INTO DEPARTMENT VALUES (1,‘ACCOUNTS‘,‘01-JAN-


01‘,‘RNSACC02‘);
INSERT INTO DEPARTMENT VALUES (2,‘IT‘,‘01-AUG-16‘,‘RNSIT01‘);
INSERT INTO DEPARTMENT VALUES (3,‘ECE‘,‘01-JUN-08‘,‘RNSECE01‘);
INSERT INTO DEPARTMENT VALUES (4,‘ISE‘,‘01-AUG-15‘,‘RNSISE01‘);
INSERT INTO DEPARTMENT VALUES (5,‘CSE‘,‘01-JUN-02‘,‘RNSCSE05‘);

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

UPDATE EMPLOYEE SET DNO=‘5‘, SUPERSSN=‘RNSCSE05‘ WHERE


SSN=‘RNSCSE04‘; UPDATE EMPLOYEE SET DNO=‘5‘, SUPERSSN=‘RNSCSE06‘
WHERE SSN=‘RNSCSE05‘;
UPDATE EMPLOYEE SET DNO=‘5‘, SUPERSSN=NULL WHERE
SSN=‘RNSCSE06‘;
UPDATE EMPLOYEE SET DNO=‘1‘, SUPERSSN=‘RNSACC02‘ WHERE
SSN=‘RNSACC01‘;
UPDATE EMPLOYEE SET DNO=‘1‘, SUPERSSN=NULL WHERE
SSN=‘RNSACC02‘;
UPDATE EMPLOYEE SET DNO=‘4‘, SUPERSSN=NULL WHERE
SSN=‘RNSISE01‘;
UPDATE EMPLOYEE SET DNO=‘2‘, SUPERSSN=NULL WHERE
SSN=‘RNSIT01‘;

1. How the resulting salaries if every employee working on the ‘Research’


Departments is given a 10 percent raise.
SQL> SELECT E.FNAME,E.LNAME, 1.1*E.SALARY AS INCR_SAL
2 FROM EMPLOYEE1 E,DEPARTMENT D,EMPLOYEE1 W
3 WHERE E.SSN=W.SSN
4 AND E.DNO=D.DNUMBER
5 AND D.DNAME='research';

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

3. Retrieve the name of each employee Controlled by department number 5 (use


EXISTS operator).
SQL> SELECT

E.FNAME,E.LNAME 2

FROM EMPLOYEE1 E

Page 24
DBMS Lab Manual 2023

3 WHERE EXISTS(SELECT DNO FROM EMPLOYEE1 WHERE E.DNO=5);

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;

5. Retrieve the name of employees who born in the year 1990’s

SELECT E.FNAME,E.LNAME,E.BDATE FROM EMPLOYEE1 E WHERE BDATE LIKE '196%';

6. Retrieve the name of employees and their dept name (using JOIN)

SELECT E.FNAME, E.LNAME, DNAME


FROM EMPLOYEE E NATURAL JOIN DEPARTMENT D ON E,DNO=D.DNO;

Page 25
DBMS Lab Manual 2023

Page 26
DBMS Lab Manual 2023

Experiment 6
For a given EMPLOYEE tables

Perform the Following


1. Creating Views (With and Without Check Option),
2. Selecting from a View
3. Dropping Views,

SOLUTION:

SQL> CREATE TABLE EMPLOYEE (


SSN VARCHAR2 (20) PRIMARY KEY,
FNAME VARCHAR2 (20),
LNAME VARCHAR2 (20),
ADDRESS VARCHAR2 (20),
SEX CHAR (1),
SALARY INTEGER,
SUPERSSN REFERENCES EMPLOYEE (SSN),
DNO REFERENCES DEPARTMENT (DNO));

SQL> DESC EMPLOYEE;


Name Null? Type

SSN NOT NULL VARCHAR2(20)


FNAME VARCHAR2(20)
LNAME VARCHAR2(20)
ADDRESS VARCHAR2(20)
SEX CHAR(1)
SALARY NUMBER(38)
SUPERSSN VARCHAR2(20)
DNO NUMBER(38)

INSERT INTO EMPLOYEE (SSN, FNAME, LNAME, ADDRESS, SEX, SALARY)


VALUES (‘RNSECE01‘,‘JOHN‘,‘SCOTT‘,‘BANGALORE‘,‘M‘, 450000);
INSERT INTO EMPLOYEE (SSN, FNAME, LNAME, ADDRESS, SEX, SALARY)
VALUES (‘RNSCSE01‘,‘JAMES‘,‘SMITH‘,‘BANGALORE‘,‘M‘, 500000);

Page 27
DBMS Lab Manual 2023

INSERT INTO EMPLOYEE (SSN, FNAME, LNAME, ADDRESS, SEX, SALARY)


VALUES (‘RNSCSE02‘,‘HEARN‘,‘BAKER‘,‘BANGALORE‘,‘M‘, 700000);
INSERT INTO EMPLOYEE (SSN, FNAME, LNAME, ADDRESS, SEX, SALARY)
VALUES (‘RNSCSE03‘,‘EDWARD‘,‘SCOTT‘,‘MYSORE‘,‘M‘, 500000);
INSERT INTO EMPLOYEE (SSN, FNAME, LNAME, ADDRESS, SEX, SALARY)
VALUES (‘RNSCSE04‘,‘PAVAN‘,‘HEGDE‘,‘MANGALORE‘,‘M‘, 650000);
INSERT INTO EMPLOYEE (SSN, FNAME, LNAME, ADDRESS, SEX, SALARY)
VALUES (‘RNSCSE05‘,‘GIRISH‘,‘MALYA‘,‘MYSORE‘,‘M‘, 450000);
INSERT INTO EMPLOYEE (SSN, FNAME, LNAME, ADDRESS, SEX, SALARY)
VALUES (‘RNSCSE06‘,‘NEHA‘,‘SN‘,‘BANGALORE‘,‘F‘, 800000);
INSERT INTO EMPLOYEE (SSN, FNAME, LNAME, ADDRESS, SEX, SALARY)
VALUES (‘RNSACC01‘,‘AHANA‘,‘K‘,‘MANGALORE‘,‘F‘, 350000);
INSERT INTO EMPLOYEE (SSN, FNAME, LNAME, ADDRESS, SEX, SALARY)
VALUES (‘RNSACC02‘,‘SANTHOSH‘,‘KUMAR‘,‘MANGALORE‘,‘M‘, 300000);
INSERT INTO EMPLOYEE (SSN, FNAME, LNAME, ADDRESS, SEX, SALARY)
VALUES (‘RNSISE01‘,‘VEENA‘,‘M‘,‘MYSORE‘,‘M‘, 600000);
INSERT INTO EMPLOYEE (SSN, FNAME, LNAME, ADDRESS, SEX, SALARY)
VALUES (‘RNSIT01‘,‘NAGESH‘,‘HR‘,‘BANGALORE‘,‘M‘, 500000);

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.

1. Creating Views (With and Without Check Option)

SQL> CREATE VIEW sales_staff AS


2 SELECT fname, ssn, dno
3 FROM employee
4 WHERE dno =5
5 WITH CHECK OPTION CONSTRAINT sales_staff_cnst;

View created.

2. Selecting from a View

SQL> select * from sales_staff;

3. Drop View

SQL>DROP VIEW sales_staff

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.

6. Disadvantage in File Processing System?


Data redundancy & inconsistency.
Difficult in accessing data.
Data isolation.
Data integrity.
Concurrent access is not possible.
Security Problems.
7. Describe the three levels of data abstraction?

There are three levels of abstraction:


Physical level: The lowest level of abstraction describes how data are stored.
Logical level: The next higher level of abstraction, describes what data are stored
in database and what relationship among those data.

Page 29
DBMS Lab Manual 2023

View level: The highest level of abstraction describes only part of entire database.
8. Define the "integrity rules"

There are two Integrity rules.


Entity Integrity: States that ―Primary key cannot have NULL value‖
Referential Integrity: States that ―Foreign Key can be either a NULL value or should be
Primary Key value of other relation.
9. What is Data Independence?

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.

NOTE: Logical Data Independence is more difficult to achieve


10. What is E-R model?
This data model is based on real world that consists of basic objects called entities and of
relationship among these objects. Entities are described in a database by a set of attributes.
11. What is an Entity?
It is an 'object' in the real world with an independent existence.
12. What is an Entity type?
It is a collection (set) of entities that have same attributes.
13. What is an Entity set?
It is a collection of all entities of particular entity type in the database.
14. What is an attribute?
It is a particular property, which describes the entity.
15. What is a Relation Schema and a Relation?
A relation Schema denoted by R(A1, A2, …, An) is made up of the relation name R and the
list of attributes Ai that it contains. A relation is defined as a set of tuples. Let r be the relation
which contains set tuples (t1, t2, t3, ...,tn). Each tuple is an ordered list of n-values t=(v1,v2, ...,
vn).
16. What is degree of a Relation?
It is the number of attribute of its relation schema.
17. What is Relationship?
It is an association among two or more entities.
18. What is Relationship type?
Relationship type defines a set of associations or a relationship set among a given set of
entity types.

Page 30
DBMS Lab Manual 2023

19. What is degree of Relationship type?


It is the number of entity type participating.
20. What is DDL (Data Definition Language)?
A data base schema is specified by a set of definitions expressed by a special language
called DDL.
21. What is VDL (View Definition Language)?
It specifies user views and their mappings to the conceptual schema.
22. What is SDL (Storage Definition Language)?
This language is to specify the internal schema. This language may specify the mapping
between two schemas.
23. What is Data Storage - Definition Language?
The storage structures and access methods used by database system are specified by a
set of definition in a special type of DDL called data storage- definition language.
24. What is DML (Data Manipulation Language)?
This language that enable user to access or manipulate data as organized by appropriate data
model.
Procedural DML or Low level: DML requires a user to specify what data are needed and how
to get those data.
Non-Procedural DML or High level: DML requires a user to specify what data are needed
without specifying how to get those data.
25. What is DML Compiler?
It translates DML statements in a query language into low-level instruction that the
query evaluation engine can understand.
26. What is Relational Algebra?
It is a procedural query language. It consists of a set of operations that take one or two
relations as input and produce a new relation.
27. What is normalization?
It is a process of analyzing the given relation schemas based on their Functional
Dependencies (FDs) and primary key to achieve the properties
Minimizing redundancy
Minimizing insertion, deletion and update anomalies.
28. What is Functional Dependency?
A Functional dependency is denoted by X Y between two sets of attributes X and Y that are
subsets of R specifies a constraint on the possible tuple that can form a relation state r of R. The
constraint is for any two tuples t1 and t2 in r if t1[X] = t2[X] then they have t1[Y] = t2[Y]. This
means the value of X component of a tuple uniquely determines the value of component Y.
29. What is Multivalued dependency?
30. What is fully functional dependency?
It is based on concept of full functional dependency. A functional dependency X Y is fully
functional dependency if removal of any attribute A from X means that the dependency does not
hold any more.
31. What is Partial Functional dependency?
32. What is 1 NF (Normal Form)?
The domain of attribute must include only atomic (simple, indivisible) values.
33. What is 2NF?
34. What is 3NF?

Page 31
DBMS Lab Manual 2023

35. What is BCNF (Boyce-Codd Normal Form)?


36. What is 4NF?
37. What is 5NF?

Page 32

You might also like