0% found this document useful (0 votes)
69 views33 pages

Pages 4 36

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
69 views33 pages

Pages 4 36

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 33

DBMS Lab Manual 2019

Experiment 1:

Consider following databases and draw ER diagram and convert entities and relationships
to relation table for a given scenario.

1. COLLEGE DATABASE:
STUDENT (USN, SName, Address, Phone, Gender)
SEMSEC (SSID, Sem, Sec)
CLASS (USN, SSID)
SUBJECT (Subcode, Title, Sem, Credits)
IAMARKS (USN, Subcode, SSID, Test1, Test2, Test3, FinalIA)

2. 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)

Dr. Bhagirathi Halalli, Assistant Prof. GFGC-Raibag Page 4


DBMS Lab Manual 2019

SOLUTION:

College Database: E-R Diagram

Mapping
entities and
relationships
to relation
table
(Schema
Diagram)

Dr. Bhagirathi Halalli, Assistant Prof. GFGC-Raibag Page 5


DBMS Lab Manual 2019

COMPANY DATABASE:

E-R Diagram

Schema Diagram

Dr. Bhagirathi Halalli, Assistant Prof. GFGC-Raibag Page 6


DBMS Lab Manual 2019

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

Dr. Bhagirathi Halalli, Assistant Prof. GFGC-Raibag Page 7


DBMS Lab Manual 2019

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.

Dr. Bhagirathi Halalli, Assistant Prof. GFGC-Raibag Page 8


DBMS Lab Manual 2019

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)

Dr. Bhagirathi Halalli, Assistant Prof. GFGC-Raibag Page 9


DBMS Lab Manual 2019

3. Find all movies and their ratings


πtitle, rating(Movies)

4. Find all actors and directors


πactor(Actors) ∪ πdirector(Directors)

Dr. Bhagirathi Halalli, Assistant Prof. GFGC-Raibag Page 10


DBMS Lab Manual 2019

5. Find Coen’s movies with McDormand


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

Dr. Bhagirathi Halalli, Assistant Prof. GFGC-Raibag Page 11


DBMS Lab Manual 2019

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

Dr. Bhagirathi Halalli, Assistant Prof. GFGC-Raibag Page 12


DBMS Lab Manual 2019

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

NOTE: Once DEPARTMENT and EMPLOYEE tables are created we must alter department
table to add foreign constraint MGRSSN using sql command

ALTER TABLE DEPARTMENT


ADD MGRSSN REFERENCES EMPLOYEE (SSN);

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

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


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

Dr. Bhagirathi Halalli, Assistant Prof. GFGC-Raibag Page 13


DBMS Lab Manual 2019

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

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;

6. COMMIT and ROLLBACK


Before concluding this section on Data Manipulation Language commands there are two
further commands, which are very useful. Changes made to the database by INSERT,
UPDATE and DELETE commands are temporary until explicitly committed. This is
performed by the command:

COMMIT;

On execution of this command all changes to the database made by you are made
permanent and cannot be undone.
 A COMMIT is automatically executed when you exit normally from SQL*Plus.
However, it does no harm to occasionally issue a COMMIT command.
 A COMMIT does not apply to any SELECT commands as there is nothing to
commit.
 A COMMIT does not apply to any DDL commands (eg CREATE TABLE,
CREATE INDEX, etc). These are automatically committed and cannot be rolled
back.
 If you wished to rollback (ie undo) any changes made to the database since the
last commit, you can issue the command:

ROLLBACK;

A group of related SQL commands that all have to complete successfully or otherwise be
rolled back, is called a transaction. Part of your research for Outcome 3 includes
investigating transaction processing and the implications of rollback and commit.

Dr. Bhagirathi Halalli, Assistant Prof. GFGC-Raibag Page 14


DBMS Lab Manual 2019

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)

Dr. Bhagirathi Halalli, Assistant Prof. GFGC-Raibag Page 15


DBMS Lab Manual 2019

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.

Dr. Bhagirathi Halalli, Assistant Prof. GFGC-Raibag Page 16


DBMS Lab Manual 2019

Experiment 5A
Consider Employee table

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

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 group by employee name and count similar names
7. Retrieve total salary of employee which is greater than >120000
8. Display name of employee in descending order
9. 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

Dr. Bhagirathi Halalli, Assistant Prof. GFGC-Raibag Page 17


DBMS Lab Manual 2019

3. Retrieve average salary of all employee


SQL> select avg(salary) from employee;

AVG(SALARY)
-----------
89400

4. Retrieve number of employee


SQL> select count(*) from employee;

COUNT(*)
----------
5

5. Retrieve distinct number of employee


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

6. Retrieve total salary of employee group by employee name and count similar names

SQL> SELECT EMP_NAME, SUM(SALARY),COUNT(*) FROM EMPLOYEE


2 GROUP BY(EMP_NAME);

EMP_NAME SUM(SALARY) COUNT(*)


------------------------------ ----------- ----------
mahesh 145000 1
sunita 187000 2
Amit 115000 2

7. 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(SALARY)
------------------------------ -----------
mahesh 145000
sunita 187000

8. Display name of employee in descending order

SQL> select emp_name from employee


2 order by emp_name desc;

Dr. Bhagirathi Halalli, Assistant Prof. GFGC-Raibag Page 18


DBMS Lab Manual 2019

EMP_NAME
------------------------------
sunita
sunita
mahesh
Amit
Amit

9. 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

Dr. Bhagirathi Halalli, Assistant Prof. GFGC-Raibag Page 19


DBMS Lab Manual 2019

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)

Dr. Bhagirathi Halalli, Assistant Prof. GFGC-Raibag Page 20


DBMS Lab Manual 2019

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)

Dr. Bhagirathi Halalli, Assistant Prof. GFGC-Raibag Page 21


DBMS Lab Manual 2019

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

Dr. Bhagirathi Halalli, Assistant Prof. GFGC-Raibag Page 22


DBMS Lab Manual 2019

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

Dr. Bhagirathi Halalli, Assistant Prof. GFGC-Raibag Page 23


DBMS Lab Manual 2019

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;

Dr. Bhagirathi Halalli, Assistant Prof. GFGC-Raibag Page 24


DBMS Lab Manual 2019

Experiment 5C

Perform the String Functions, Date functions and Mathematical functions supported
by Oracle

SQL> select ascii('t') from dual;

ASCII('T')
----------
116

SQL> select ascii('a') from dual;

ASCII('A')
----------
97

SQL> select ascii('A') from dual;

ASCII('A')
----------
65

SQL> select ascii('Z') from dual;

ASCII('Z')
----------
90

SQL> select ascii('z') from dual;

ASCII('Z')
----------
122

SQL> SELECT UPPER('bldea sb arts and kcp science college') from dual;

UPPER('BLDEASBARTSANDKCPSCIENCECOLLEG
-------------------------------------
BLDEA SB ARTS AND KCP SCIENCE COLLEGE

SQL> select LOWER('welcome to dbms lab') from dual;

LOWER('WELCOMETODBM
-------------------
welcome to dbms lab

Dr. Bhagirathi Halalli, Assistant Prof. GFGC-Raibag Page 25


DBMS Lab Manual 2019

SQL> select LOWER('WELCOME TO DBMSLAB') from dual;

LOWER('WELCOMETODB
------------------
welcome to dbmslab

SQL> SELECT REPLACE('HELLO','H','K') FROM DUAL;

REPLA
-----
KELLO

SQL> SELECT REPLACE('COMPUTER','C','K') FROM DUAL;

REPLACE(
--------
KOMPUTER

SQL> SELECT REPLACE('HELLO','L','A') FROM DUAL;

REPLA
-----
HEAAO

SQL> SELECT TRIM('A' FROM 'ANACONDA') FROM DUAL;

TRIM('
--
NACOND

SQL> SELECT LTRIM('ANACONDA','A') FROM DUAL;

LTRIM('
-------
NACONDA

SQL> SELECT LTRIM('ANIL','A') FROM DUAL;

LTR
---
NIL

SQL> SELECT RTRIM('ANITA','A') FROM DUAL;

RTRI
----

Dr. Bhagirathi Halalli, Assistant Prof. GFGC-Raibag Page 26


DBMS Lab Manual 2019

ANIT

SQL> SELECT RTRIM('ANACONDA','A') FROM DUAL;

RTRIM('
-------
ANACOND

SQL> SELECT RTRIM('ANACONDA ','A') FROM DUAL;

RTRIM('ANAC
-----------
ANACONDA

Date Functions
SQL> SELECT CURRENT_DATE FROM DUAL;

CURRENT_D
---------
14-AUG-19

SQL> SELECT EXTRACT(YEAR FROM SYSDATE) FROM DUAL;

EXTRACT(YEARFROMSYSDATE)
------------------------
2019

SQL> SELECT EXTRACT(DAY FROM SYSDATE) FROM DUAL;

EXTRACT(DAYFROMSYSDATE)
-----------------------
14

SQL> SELECT EXTRACT(MONTH FROM SYSDATE) FROM DUAL;

EXTRACT(MONTHFROMSYSDATE)
-------------------------
8

SQL> SELECT SYSDATE FROM DUAL;

SYSDATE
---------
14-AUG-19

Dr. Bhagirathi Halalli, Assistant Prof. GFGC-Raibag Page 27


DBMS Lab Manual 2019

Mathematical Functions
SQL> select ABS(-100) from dual;
ABS(-100)
----------
100
SQL> select ABS(-6) from dual;

ABS(-6)
----------
6
SQL> select FLOOR(2345.78) FROM DUAL;
FLOOR(2345.78)
--------------
2345
SQL> SELECT GREATEST(23,67,90,123,78,50) FROM DUAL;
GREATEST(23,67,90,123,78,50)
----------------------------
123

SQL> SELECT LEAST(34, 21,67,11,89,9) FROM DUAL;

LEAST(34,21,67,11,89,9)
-----------------------
9

SQL> SELECT LENGTH('RAJESHWARI') FROM DUAL;


LENGTH('RAJESHWARI')
--------------------
10

SQL> SELECT LENGTH(17245637) FROM DUAL;


LENGTH(17245637)
----------------
8

SQL> SELECT SQRT(16) FROM DUAL;


SQRT(16)
----------
4

SQL> SELECT SQRT(99) FROM DUAL;


SQRT(99)
----------
9.94987437

Dr. Bhagirathi Halalli, Assistant Prof. GFGC-Raibag Page 28


DBMS Lab Manual 2019

SQL> SELECT POWER(2,4) FROM DUAL;


POWER(2,4)
----------
16

SQL> SELECT POWER(2,10) FROM DUAL;


POWER(2,10)
-----------
1024

SQL> SELECT power(2,10) FROM DUAL;


POWER(2,10)
-----------
1024

SQL> SELECT ROUND(5.86) FROM DUAL;

ROUND(5.86)
-----------
6

SQL> SELECT ROUND(1001.6) FROM DUAL;


ROUND(1001.6)
-------------
1002

SQL> SELECT ROUND(1001.3) FROM DUAL;


ROUND(1001.3)
-------------
1001

SQL> SELECT SIN(90) FROM DUAL;


SIN(90)
----------
.893996664

SQL> SELECT COS(45) FROM DUAL;


COS(45)
----------
.525321989

SQL> SELECT TAN(30) FROM DUAL;


TAN(30)
----------
-6.4053312

SQL> SELECT TAN(90) FROM DUAL;


TAN(90)

Dr. Bhagirathi Halalli, Assistant Prof. GFGC-Raibag Page 29


DBMS Lab Manual 2019

----------
-1.9952004

SQL> SELECT TAN(180) FROM DUAL;


TAN(180)
----------
1.33869021

SQL> SELECT SIGN(-128) FROM DUAL;


SIGN(-128)
----------
-1

SQL> SELECT SIGN(10) FROM DUAL;


SIGN(10)
----------
1

SQL> SELECT SIGN(0) FROM DUAL;


SIGN(0)
----------
0

SQL> SELECT LN(100) FROM DUAL;


LN(100)
----------
4.60517019

SQL> SELECT LN(10) FROM DUAL;


LN(10)
----------
2.30258509

SQL> SELECT LOG(10,100) FROM DUAL;


LOG(10,100)
-----------
2

SQL> SELECT LOG(100,10) FROM DUAL;


LOG(100,10)
-----------
.5

SQL> SELECT MOD(4,3) FROM DUAL;

MOD(4,3)
----------
1

Dr. Bhagirathi Halalli, Assistant Prof. GFGC-Raibag Page 30


DBMS Lab Manual 2019

SQL> SELECT MOD(4,2) FROM DUAL;

MOD(4,2)
----------
0
SQL> SELECT EXP(2) FROM DUAL;

EXP(2)
----------
7.3890561

SQL> SELECT EXP(-2) FROM DUAL;

EXP(-2)
----------
.135335283

SQL> SELECT EXP(0) FROM DUAL;

EXP(0)
----------
1

Dr. Bhagirathi Halalli, Assistant Prof. GFGC-Raibag Page 31


DBMS Lab Manual 2019

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

Dr. Bhagirathi Halalli, Assistant Prof. GFGC-Raibag Page 32


DBMS Lab Manual 2019

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;

Dr. Bhagirathi Halalli, Assistant Prof. GFGC-Raibag Page 33


DBMS Lab Manual 2019

Experiment 7
Write a Pl/SQL program to print integers from 1 to 10 by using PL/SQL FOR loop

SOLUTION:

PL/SQL Block
SET SERVEROUTPUT ON SIZE 1000000;
DECLARE
n_times NUMBER := 10;
BEGIN
FOR n_i IN 1..n_times LOOP
DBMS_OUTPUT.PUT_LINE(n_i);
END LOOP;
END;

Output Table

Dr. Bhagirathi Halalli, Assistant Prof. GFGC-Raibag Page 34


DBMS Lab Manual 2019

Experiment 8
Given the table EMPLOYEE (EmpNo, Name, Salary, Designation, DeptID) write a cursor to
select the five highest paid employees from the table.

EMPLOYEE (EmpNo, Name, Salary, Designation, DeptID)

SOLUTION:

CREATE TABLE EMPLOYEE


(EMPNO INTEGER PRIMARY KEY,
NAME VARCHAR(20),
SALARY NUMBER(7,2),
DESIGNATION VARCHAR(10),
DEPTID INTEGER);

get e:/p8.sql;
1 declare
2 i number:=0;
3 cursor ec is select empno,name,salary from employee order by gross_salary desc;
4 r ec%rowtype;
5 begin
6 open ec;
7 loop
8 exit when i=5;
9 fetch ec into r;
10 dbms_output.put_line(r.emp_no||' '||r.employee_name||' '||r.salary);
11 i:=i+1;
12 end loop;
13 close ec;
14* end;
15 .

SQL> /

1 rajesh 31000

2 paramesh 15000

3 pushpa 14000

4 vijaya 14000

5 keerthi 13000

PL/SQL procedure successfully completed.

Dr. Bhagirathi Halalli, Assistant Prof. GFGC-Raibag Page 35


DBMS Lab Manual 2019

Experiment 10

Given an integer i, write a PL/SQL procedure to insert the tuple (i, 'xxx') into a given relation.

SOLUTION:

CREATE TABLE T2 (
a INTEGER,
b CHAR(10));

CREATE OR REPLACE PROCEDURE addtuple1(x IN NUMBER)


AS
BEGIN
INSERT INTO T2 VALUES(x, 'xxx');
END addtuple1;
.
run;

Dr. Bhagirathi Halalli, Assistant Prof. GFGC-Raibag Page 36

You might also like