You are on page 1of 26

Ex.

No :1
DDL COMMANDS WITH CONSTRAINTS
CREATING A TABLE( PARENT TABLE ):
create table employee(emp_no number(5) primary key,emp_name varchar2(10) not
null,emp_phone_no number(10) unique);

OUTPUT:
Table created.

DESCRIPTION OF THE TABLE:


desc employee;

OUTPUT:

CREATING A TABLE( CHILD TABLE ):


create table dept(emp_no references employee(emp_no),dept_no number(5) primary
key,dept_name varchar2(10) not null,designation varchar2(20),salary number(5));

desc dept;

OUTPUT:
ALTERING THE TABLE:
alter table employee add age number(2);

OUTPUT:
Table Altered.

DESCRIPTION OF THE TABLE:


desc employee;

OUTPUT:

DROPING A COLUMN:
alter table employee drop column age;

OUTPUT:
Table dropped.

DESCRIPTION OF THE TABLE:


desc employee;

OUTPUT:
Ex.No :2
DML COMMANDS WITH CONSTRAINTS

INSERTING VALUES INTO PARENT TABLE:


insert into employee values(1,'ABI',9876665436);

insert into employee values(2,'ANI',9676668936);

insert into employee values(3,'AMMU',9876895436);

insert into employee values(4,'RAJA',5677688908);

insert into employee values(5,'ABRANI',98765675436);

insert into employee values(6,'NAVEEN',6754665436);

insert into employee values(7,'KERTHI',768965436);

insert into employee values(8,'VANI',9675055436);

insert into employee values(9,'KUMAR',9875678436);

insert into employee values(10,'DIVYA',9878765436);

OUTPUT:
10 row(s) inserted.

SELECTION OF THE TABLE:


select*from employee;

OUTPUT:
INSERTING VALUES INTO CHILD TABLE:
insert into dept values(1,101,'PRODUCTION',12000);

insert into dept values(2,102,'SALES',15000);

insert into dept values(3,103,'PRODUCTION',12000);

insert into dept values(4,104,'SALES',15000);

insert into dept values(5,105,'PRODUCTION',12000);

insert into dept values(6,106,'SALES',15000);

insert into dept values(7,107,'PRODUCTION',12000);

insert into dept values(8,108,'SALES',15000);

insert into dept values(9,109,'PRODUCTION',12000);

insert into dept values(10,110,'SALES DEPT',15000);

OUTPUT:
10 row(s) inserted.

SELECTION OF THE TABLE:


select*from employee;

OUTPUT:

PRIMARY KEY VOILATED:


insert into dept values(11,10,'SALES',1000);
OUTPUT:

UNIQUE KEY VOILATED:


insert into employee values(11,'AVI',9876665436);

OUTPUT:

DELETING A ROW:
delete from employee where emp_no=6;

OUTPUT:
1 row(s) deleted.

SELECTION OF THE TABLE:


select*from employee;

OUTPUT:

UPDATION OF THE TABLE:


update employee set emp_name=kuzhal where emp_no =1;
OUTPUT:
1 row(s) updated.

SELECTION OF THE TABLE:


select*from employee;

OUTPUT:
Ex.No :3
SQL QUERIES
a) QUERIES:
SELECTING A SINGLE ATTRIBUTE FROM THE TABLE:
select emp_name from employee;

OUTPUT:

SELECTING TWO ATTRIBUTE FROM THE TABLE:


select emp_no,emp_name from employee;

OUTPUT:

SELECTING ONE PARTICULAR ENTRY ATTRIBUTE FROM THE


TABLE:
select *from employee where emp_no=2;

OUTPUT:

USING AND:
select*from employee where emp_no=10 AND emp_name='DIVYA';

OUTPUT:

USING OR:
select*from employee where emp_no=2 OR emp_name='ANI';

OUTPUT:

TO VIEW ONLY TWO ATTRIBUTES:


select*from employee where emp_no in (2,10);

OUTPUT:

SELECTING DISTINCT VALUES:


select distinct emp_phone_no from employee;
OUTPUT:

a) SUB QUERIES:
CREATING A TABLE:
create table employ_1(emp_id number(5),emp_firstname varchar2(20),emp_lastname
varchar2(20),sal number(6),dept_id number(5),job varchar2(20));

OUTPUT:
Table created.

INSERTING VALUES INTO TABLE:


insert into employ_1 values(1,'PETER','PARKER',10000,10,'CREATER');

insert into employ_1 values(2,'DAVID','HALL',9500,10,'DESIGNER');

insert into employ_1 values(3,'CHRIS','PARTT',9000,10,'DEVELOPER');

insert into employ_1 values(4,'OSCAR','WILD',8500,10,'WRITER');

insert into employ_1 values(5,'WILL','SMITH',8000,10,'WRITER');

insert into employ_1 values(6,'PATRICK','SULLY',10000,10,'CREATER');

insert into employ_1 values(7,'TAYLER','FOX',9000,10,'DESIGNER');

insert into employ_1 values(8,'ELLEN','ABEL',12000,10,'CREATER');

insert into employ_1 values(9,'JACK','LIVINGSTON',6000,10,'CLERK');

insert into employ_1 values(10,'CHARLES','JOHNSON',6000,10,'CLERK');


SELECTION OF 2 COLUMNS FROM THE TABLE:
select emp_firstname,emp_lastname from employ_1 where sal>(select sal from employ_1 where
emp_id=6);

OUTPUT:

SELECTION OF 4 COLUMNS FROM THE TABLE:


select emp_firstname,emp_lastname,sal,dept_id,job from employ_1 where job=(select job from
employ_1 where emp_id=5);

OUTPUT:

b) AGGREGATE FUNCTION:
COUNT FUNCTION:
select count(*)

from dept;

OUTPUT:

COUNT DISTINCT FUNCTION:


select count(DISTINCT DEPT_NAME)

from dept;

OUTPUT:
COUNT GROUP BY FUNCTION:
SELECT DEPT_NAME,COUNT(*) FROM DEPT GROUP BY DEPT_NAME;

OUTPUT:

COUNT GROUP BY FUNCTION:


select dept_name,count(*) from dept group by dept_name having count(*)>2;

OUTPUT:

SUM FUNCTION:
select sum(salary)

from dept;

OUTPUT:

SUM USING WHERE FUNCTION:


select sum(salary)

from dept where dept_no > 1;

OUTPUT:
SUM USING HAVING FUNCTION:
select dept_name,sum(salary) from dept group by dept_name having sum(salary)>=40000;

OUTPUT:

AVERAGE FUNCTION:
select avg(salary) from dept;

OUTPUT:

MINIMUM FUNCTION:
select min(salary) from dept;

OUTPUT:

MAXIMUM FUNCTION:
select max(salary) from dept;

OUTPUT:
Ex.No :4
EXCEPTION HANDLING
CREATING A TABLE:
create table customer(id number(10),name varchar(20),addr varchar(20));

OUTPUT:
Table created.

INSERTING VALUES INTO THE TABLE:


insert into customer values(100,'anu','perambur');

insert into customer values(101,'rani','madhavaram');

insert into customer values(102,'hema','perambur');

insert into customer values(103,'nijla','MR nagar');

OUTPUT:
4 row(s) inserted.

a) PRE DEFINED EXCEPTION:


WITHOUT EXCEPTION
declare

c_id customer.id%type:=101;

c_name customer.name%type;

c_addr customer.addr%type;

begin

select name,addr into c_name,c_addr

from customer

where id=c_id;
dbms_output.put_line('Name:'||c_name);

dbms_output.put_line('Address:'||c_addr);

exception

when no_data_found then

dbms_output.put_line('No such customers!');

when others then

dbms_output.put_line('Error');

End;/

OUTPUT:
Name:rani
Address:madhavaram

Statement processed.

WITH EXCEPTION
declare

c_id customer.id%type:=110;

c_name customer.name%type;

c_addr customer.addr%type;

begin

select name,addr into c_name,c_addr

from customer

where id=c_id;

dbms_output.put_line('Name:'||c_name);

dbms_output.put_line('Address:'||c_addr);

exception

when no_data_found then

dbms_output.put_line('No such customers!');

when others then

dbms_output.put_line('Error');

End;/
OUTPUT:
No such customers!

Statement processed.

B) USER DEFINED EXCEPTIONS


CREATING A TABLE:
create table customers(id number(10),name varchar2(20),address varchar2(15));

OUTPUT:
Table created.

INSERTING VALUES INTO THE TABLE:


insert into customers values(1,'ram','chennai');

insert into customers values(2,'sam','manali');

insert into customers values(3,'vani','erode');

insert into customers values(4,'rani','erode');

insert into customers values(5,'sham','chennai');

OUTPUT:
5 row(s) inserted.

PROGRAM:
DECLARE

c_id customers.id%type := :cc_id;

c_name customerS.Name%type;

c_addr customers.address%type; -- user defined exception

ex_invalid_id EXCEPTION;

BEGIN
IF c_id <= 0 THEN

RAISE ex_invalid_id;

ELSE

SELECT name, address INTO c_name, c_addr

FROM customers

WHERE id = c_id;

DBMS_OUTPUT.PUT_LINE ('Name: '|| c_name);

DBMS_OUTPUT.PUT_LINE ('Address: ' || c_addr);

END IF;

EXCEPTION

WHEN ex_invalid_id THEN

dbms_output.put_line('ID must be greater than zero!');

WHEN no_data_found THEN

dbms_output.put_line('No such customer!');

WHEN others THEN

dbms_output.put_line('Error!');

END;

OUTPUT 1:

No such customer!

Statement processed.

OUTPUT 2:

ID must be greater than zero!

Statement processed.
OUTPUT 3:

Name: sham

Address: chennai

Statement processed.
Ex.No :5
PLSQL : CURSOR
CREATING A TABLE -1:
create table employ_1(emp_no number(4),emp_name char(20),job char(25),sal
number(7),commission number(7),dept_no number(4));

OUTPUT:
Table created.

CREATING A TABLE -2:


create table depart_1(dept_no number(4),d_name varchar(20),location varchar(20));

OUTPUT:
Table created.

INSERTING VALUES INTO THE TABLE -1:


insert into employ_1 values(1,'Sasi','Supervisor',1500,250,10);

insert into employ_1 values(2,'Daniel','Manager',2750,1000,20);

insert into employ_1 values(3,'Rupert','Manager',2500,1500,30);

insert into employ_1 values(4,'Smith','Supervisor',2000,500,40);

insert into employ_1 values(5,'James','Supervisor',2000,750,50);

OUTPUT:
5 row(s) inserted.

SELECTION OF THE TABLE -1:


Select*from employ_1;

OUTPUT:
INSERTING VALUES INTO THE TABLE -2:
insert into depart_1 values(10,'Purchase','First Floor');

insert into depart_1 values(20,'Purchase','First Floor');

insert into depart_1 values(30,'Sales','Second Floor');

insert into depart_1 values(40,'Sales','Second Floor');

insert into depart_1 values(50,'Purchase','First Floor');

OUTPUT:
5 row(s) inserted.

SELECTION OF THE TABLE -2:


select*from depart_1;

OUTPUT:

UPDATION OF THE TABLE USING IMPLICIT CURSOR:


declare

rows_affected char(2);

begin

update employ_1 set sal=sal+(sal*0.15) where sal<10000;

rows_affected:=to_char(sql%rowcount);

if (sql%rowcount) > 0 then

dbms_output.put_line(rows_affected||' rows updated');

else

dbms_output.put_line('No rows affected');

end if;

end;
OUTPUT:
5 rows updates.

1 row(s) updated.

SELECTION OF THE TABLE:


Select*from employ_1;

OUTPUT:

CREATING A EMPLOY_RAISE TABLE:


create table employ_raise1(name varchar2(30),sal number(30));

OUTPUT:
Table created.

UPDATION OF THE TABLE USING EXPLICIT CURSOR:


declare

cursor c_emp is select emp_name,sal from employ_1 where dept_no=30;

c_name employ_1.emp_name%type;

c_sal employ_1.sal%type;

begin

open c_emp;

loop

fetch c_emp into c_name,c_sal;

exit when not(c_emp%found);

update employ_1 set sal=sal+(sal*0.05) where emp_name=c_name;

insert into employ_raise1 values(c_name,c_sal*0.05);

end loop;

dbms_output.put_line('Salaries are successfully updated'); end;


OUTPUT:
Salaries are successfully updated.

1 row(s) updated.

SELECTION OF THE TABLE:


select*from employ_raise1;

OUTPUT:

1 row(s) returned.
Ex.No :6

PLSQL: TRIGGERS

CREATING A TABLE -1:


create table employ_1(emp_no number(4),emp_name char(20),job char(25),sal

number(7),commission number(7),dept_no number(4));

OUTPUT:
Table created.

CREATING A TABLE -2:


create table emp_trans(optn varchar2(20),updt_on date);

OUTPUT:
Table created.

INSERTINGVALUESINTOTHE TABLE -1:


insert into employ_1 values(1,'Sasi','Supervisor',1500,250,10);

insert into employ_1 values(2,'Daniel','Manager',2750,1000,20);

insert into employ_1 values(3,'Rupert','Manager',2500,1500,30);

insert into employ_1 values(4,'Smith','Supervisor',2000,500,40);

insert into employ_1 values(5,'James','Supervisor',2000,750,50);

OUTPUT:
5 row(s) inserted.

CREATING A TRIGGER:
create or replace trigger emp_trans

AFTER insert or update or delete on employ_1

declare

op char(10);

begin
if inserting then

op:='Insert';

end if;

if deleting then

op:='Delete';

end if;

if updating then

op:='Update';

end if;

insert into emp_trans values(op,sysdate);

end;

OUTPUT:
Trigger created.

SELECTIONOF THE TABLE -1:


Select*from employ_1;

OUTPUT:

SELECTION OF THE TABLE -2:


select * from emp_trans;

OUTPUT:
No data found.

INSERTION AFTER TRIGGER CREATED:


insert into employ_1 values(5478,'Arun','S.Engineer',25000,5000,60);

OUTPUT:
1 row(s) inserted.

SELECTIONOF THE TABLE -2:


select * from emp_trans;

OUTPUT:

UPDATION AFTER TRIGGER CREATED:


update employ_1 set sal=sal+500 where emp_no=5;

OUTPUT:
1 row(s) updated.

SELECTION OF THE TABLE -2:


select * from emp_trans;

OUTPUT:

.DELETION AFTER TRIGGER:


delete from employ_1 where emp_no=4;

OUTPUT:
1 row(s) deleted.

SELECTIONOF THE TABLE -2:


select * from emp_trans;

OUTPUT:
Ex.No :6
PLSQL:PACKAGES

CREATION OF TABLE:
create table employees(p_id number,p_name varchar(10));

OUTPUT:
Table created.

INSERTING VALUES INTO THE TABLE:


insert into employees values(100,'RAJESH');

insert into employees values(102,'RAJ');

insert into employees values(103,'MAHESH');

OUTPUT:
3 row(s) inserted.

CREATION OF PACKAGE:
create or replace package manage_employees is

procedure add_emp(p_id number,p_name varchar2);

end manage_employees;

OUTPUT:
Package created.

CREATION OF PACKAGE BODY:


create or replace package body manage_employees is

procedure add_emp(p_id number,p_name varchar2)is

begin

dbms_output.put_line('Employee added');

end add_emp;

end;/
OUTPUT:
Package Body created

ADDING VALUES USING PACKAGE:


declare

code employees.p_id%type:=100;

BEGIN

manage_employees.add_emp(101,'SHARMA');

manage_employees.add_emp(104,'RUBIN');
end;
/

OUTPUT:
Employee added

Employee added

Statement processed.

You might also like