Professional Documents
Culture Documents
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.
OUTPUT:
desc dept;
OUTPUT:
ALTERING THE TABLE:
alter table employee add age number(2);
OUTPUT:
Table Altered.
OUTPUT:
DROPING A COLUMN:
alter table employee drop column age;
OUTPUT:
Table dropped.
OUTPUT:
Ex.No :2
DML COMMANDS WITH CONSTRAINTS
OUTPUT:
10 row(s) inserted.
OUTPUT:
INSERTING VALUES INTO CHILD TABLE:
insert into dept values(1,101,'PRODUCTION',12000);
OUTPUT:
10 row(s) inserted.
OUTPUT:
OUTPUT:
DELETING A ROW:
delete from employee where emp_no=6;
OUTPUT:
1 row(s) deleted.
OUTPUT:
OUTPUT:
Ex.No :3
SQL QUERIES
a) QUERIES:
SELECTING A SINGLE ATTRIBUTE FROM THE TABLE:
select emp_name from employee;
OUTPUT:
OUTPUT:
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:
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.
OUTPUT:
OUTPUT:
b) AGGREGATE FUNCTION:
COUNT FUNCTION:
select count(*)
from dept;
OUTPUT:
from dept;
OUTPUT:
COUNT GROUP BY FUNCTION:
SELECT DEPT_NAME,COUNT(*) FROM DEPT GROUP BY DEPT_NAME;
OUTPUT:
OUTPUT:
SUM FUNCTION:
select sum(salary)
from dept;
OUTPUT:
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.
OUTPUT:
4 row(s) inserted.
c_id customer.id%type:=101;
c_name customer.name%type;
c_addr customer.addr%type;
begin
from customer
where id=c_id;
dbms_output.put_line('Name:'||c_name);
dbms_output.put_line('Address:'||c_addr);
exception
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
from customer
where id=c_id;
dbms_output.put_line('Name:'||c_name);
dbms_output.put_line('Address:'||c_addr);
exception
dbms_output.put_line('Error');
End;/
OUTPUT:
No such customers!
Statement processed.
OUTPUT:
Table created.
OUTPUT:
5 row(s) inserted.
PROGRAM:
DECLARE
c_name customerS.Name%type;
ex_invalid_id EXCEPTION;
BEGIN
IF c_id <= 0 THEN
RAISE ex_invalid_id;
ELSE
FROM customers
WHERE id = c_id;
END IF;
EXCEPTION
dbms_output.put_line('Error!');
END;
OUTPUT 1:
No such customer!
Statement processed.
OUTPUT 2:
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.
OUTPUT:
Table created.
OUTPUT:
5 row(s) inserted.
OUTPUT:
INSERTING VALUES INTO THE TABLE -2:
insert into depart_1 values(10,'Purchase','First Floor');
OUTPUT:
5 row(s) inserted.
OUTPUT:
rows_affected char(2);
begin
rows_affected:=to_char(sql%rowcount);
else
end if;
end;
OUTPUT:
5 rows updates.
1 row(s) updated.
OUTPUT:
OUTPUT:
Table created.
c_name employ_1.emp_name%type;
c_sal employ_1.sal%type;
begin
open c_emp;
loop
end loop;
1 row(s) updated.
OUTPUT:
1 row(s) returned.
Ex.No :6
PLSQL: TRIGGERS
OUTPUT:
Table created.
OUTPUT:
Table created.
OUTPUT:
5 row(s) inserted.
CREATING A TRIGGER:
create or replace trigger emp_trans
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;
end;
OUTPUT:
Trigger created.
OUTPUT:
OUTPUT:
No data found.
OUTPUT:
1 row(s) inserted.
OUTPUT:
OUTPUT:
1 row(s) updated.
OUTPUT:
OUTPUT:
1 row(s) deleted.
OUTPUT:
Ex.No :6
PLSQL:PACKAGES
CREATION OF TABLE:
create table employees(p_id number,p_name varchar(10));
OUTPUT:
Table created.
OUTPUT:
3 row(s) inserted.
CREATION OF PACKAGE:
create or replace package manage_employees is
end manage_employees;
OUTPUT:
Package created.
begin
dbms_output.put_line('Employee added');
end add_emp;
end;/
OUTPUT:
Package Body created
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.