You are on page 1of 19

LAB-EXERCISE

CSE324 (PL/SQL)
Batch-B1

Note: Attempt any 10(Tables Questions Must).

1. Perform Constraints on particular field of tables (unique,


not null, in, Default value)
Ans: Query -
SQL> create table const(s_id number(5) unique,
2 sname varchar2(15) not null,
3 s_sal number(10,2) default 10000);
2. Check constraint
Ans:
Creating the table product which takes the quantity greater than 1000 and
there price greater than 100
Query: create table product(p_id number(10) primary key, pname
varchar2(10), quantity number(10) check(quantity >= 1000), price
number(10) check(price > 100));

3. How to use on delete cascade at the time of creating table?


Ans: It is used to delete the rows from the child table automatically, when
the rows from the parent table are deleted.
Query -
SQL> create table stu(s_id number(5) primary key, sname varchar2(10));
SQL> create table cou(s_id references stu(s_id) on delete cascade, cname
varchar2(10));
4. Oracle functions; (nvl(), count(), avg(), sum())
Ans:
NVL() Function – it allows to substitute a value when a null value is
encountered. Generally it is used to manage the null values.
Count() Function – it counts the number of values of a given column.

Avg() Function – it returns the average value of the values in given column.
Sum() Funtion – it return the sum of values in given column.

6. like (%, _) between and operator with example?


Ans:
Like Operator – it is used to extract records where a particular pattern is
present.
And Operator – it is used with between operator to specify the range.

Between Operator – it is used to select the values within a given range.

8. Create the following tables:


Table 1: DEP
DEPTNO (NOT NULL, NUMBER (2)), DNAME (VARCHAR2 (14)),
LOC (VARCHAR2 (13)
Creation:
SQL> create table dep(deptno number(10) Primary key,
2 dname varchar2(15),
3 loc varchar2(15));

Insertion:
SQL> insert into dep values(123,'CSE','Delhi');
SQL> insert into dep values(345,'DBMS','Gwalior');
SQL> insert into dep values(567,'MECH','Goa');
SQL> insert into dep values(789,'ELEC','Jaipur');
SQL> insert into dep values(901,'AST','Pune');

Table:

SQL> select * from dep;


DEPTNO DNAME LOC
---------- --------------- ---------------
123 CSE Delhi
345 DBMS Gwalior
567 MECH Goa
789 ELEC Jaipur
901 AST Pune

Table 2: EM
EMPNO (NOT NULL, NUMBER (4)), ENAME (VARCHAR2 (10)),
JOB (VARCHAR2 (9)), MGR (NUMBER (4)), HIREDATE (DATE),
SAL (NUMBER (7, 2)), COMM (NUMBER (7, 2)), DEPTNO (NUMBER
(2))
MGR is the empno of the employee whom the employee reports
to. DEPTNO is a foreign key.
Creation:
SQL> create table em(e_no number(4) not null,
2 ename varchar2(15),
3 job varchar2(10),
4 mgr number(5),
5 hiredate date,
6 sal number(7,2),
7 comm number(7,2),
8 deptno references dep(deptno));

Insertion:
SQL> insert into em values(12,'Devashish','coder',20,date '21-10-
01',90000,200,567);
SQL> insert into em values(22,'Mansi','Washer',25,date '21-11-
05',75000,100,345);
SQL> insert into em values(28,'Pradhumn','footer',21,date '21-09-
12',70000,240,901);
SQL> insert into em values(18,'Ambrose','shooter',20,date '21-09-
30',80000,210,567);
SQL> insert into em values(18,'Gaurav','poser',29,date '21-01-
15',85000,190,123);

Table:
SQL> select * from em;
E_NO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- --------------- ---------- ---------- --------- ---------- ---------- ----------
12 Devashish coder 20 01-OCT-21 90000 200 567
22 Mansi Washer 25 05-NOV-21 75000 100 345
28 Pradhumn footer 21 12-SEP-21 70000 240 901
18 Ambrose shooter 20 30-SEP-21 80000 210 567
18 Gaurav poser 29 15-JAN-21 85000 190 123
1. List all the employees who have at least one person reporting
to them.
Query: SQL> select ename from em where e_no in (select mgr from em);

2. List the department details in which more than 1 employees.


Query: SQL> select * from dep where deptno in (select deptno from em);

3. List the name of the employees with their immediate higher


authority.
Query: SQL> select a.ename"Employee",b.ename"Higher Authority" from
em a,em b where a.mgr=b.e_no;
4. List all the employees who do not manage any one.
Query: SQL> select * from em where e_no in (select e_no from em minus
select mgr from em);

5. List the employee details whose salary is greater than the


lowest salary of
an employee belonging to deptno 20.
Query: SQL> select * from em where sal>(select min(sal) from em group by
deptno having deptno = 20);
6. List the details of the employee earning more than the highest
paid manager.
Query: SQL> select * from em where sal>(select max(sal) from em group by
job having job = 'MANAGER');

7. List the highest salary paid for each job.


Query: SQL> select job,max(sal) from em group by job;
8. Find the most recently hired employee in each department.
Query: SQL> select * from em where (deptno,hiredate) in (select deptno,
max(hiredate) from em group by deptno);

9. Which department has the highest annual remuneration bill?


Query: SQL> select deptno,sum(sal) from em group by deptno having
sum(sal) = (select max(sum(sal)) from em group by deptno);

10. Write a correlated sub-query to list out the employees who


earn more than the average salary.
Query: SQL> select ename,sal from em e where sal>(select avg(sal) from em
f where e.deptno = f.deptno);
10. Explain Pragma Exception.
Ans: The pragma exception associates an exception name with an oracle
number. We can intercept any ORA-error and write a specific handler for it
instead of using OTHERS handler.
Syntax for declaring:
PRAGMA EXCEPTION_INIT(exception_name, -error_number);
Keywords and Parameter description -
Error number – any valid oracle error number. These are the same error
numbers which are always negative returned by the function SQL code.
Exception name – a user defined exception declared within the current
scope.
Pragma – signifies that the statement is a compiler directive.
Usage Notes – we can use EXCEPTION_INIT in the declarative part of any
PL/SQL block, subprogram or package. The pragma must appear in the same
declarative part as its associated exception, somewhere after the exception
declaration.

12. Create the following tables:


Employee
(Emp_no: varchar2, Emp_name: varchar2, Emp_city varchar2)
Creation:
SQL> create table employee(empno number(10) primary key,
2 ename varchar2(20),
3 ecity varchar2(10));

Insertion:
SQL> insert into employee values(12,'Dev','CHD');
SQL> insert into employee values(13,'Manu','CP');
SQL> insert into employee values(15,'Kin','GEL');
SQL> insert into employee values(19,'Amm','ENG');

Table:

Company
(Emp_no:Varchar2, Company_name: varchar2,Salary number)
Creation:
SQL> create table company(empno references employee(empno),
2 cname varchar2(10),
3 sal number(7,2));

Insertion:
SQL> insert into company values(19,'DEX',900);
SQL> insert into company values(13,'REX',200);
SQL> insert into company values(12,'TEX',290);
SQL> insert into company values(15,'GEX',150);
Table:

1. Write the query to display all employees working in ‘XYZ’


company.
Query: SQL> select ename from employee natural join company where
cname = 'TEX';

2. Write the query to display name of employees whose salary


is largest.
Query: SQL> select ename,sal from employee,company where sal = (select
max(sal) from employee);
3. Perform Alter and delete, truncate, drop command.
Alter: SQL> alter table employee
2 add email varchar2(200);

Delete: SQL> delete from company where empno = 15;


Truncate: SQL> truncate table company;

Drop: SQL> drop table company;

4. Commit and roll back


13. Explain the PL/ SQLand SQL *plus.
Ans:
PL/SQL -
It is a completely portable, high performance transaction processing
language. It provides a built-in, interpreted and OS independent
programming environment. It can also directly be called from the command-
line SQL*Plus interface. Direct call can also be made from external
programming language calls to database. The general syntax is based on
that of ADA and pascal programming language.

SOL*Plus -
It is an interactive program that allows us to type in and execute SQL
statements. It also enables us to type in PL/SQL code and send it to the
server to be executed. SQL*Plus is one of the most common front end used
to develop and create stored PL/SQL functions and procedures. It is
command line tool, in that which does not involve DDL, DML, DCL
commands like in SQL. It uses command to manipulate data.

15. Display the alternate row from table.


Ans: we can do this by using MOD() function which returns the remainder of
a number divided by another number.
_____________

You might also like