You are on page 1of 18

1.

Create table named Employee with the following fields and insert the values

SQL> create table employee(empname varchar(15),empcode number(6) primary key,


address varchar(25),designation varchar(15),grade varchar(1),dateofjoin date,salary number (13,2));

Table created.

SQL> desc employee;


Name Null? Type
----------------------------------------- -------- ----------------------------
EMPNAME VARCHAR(15)
EMPCODE NOT NULL NUMBER(6)
ADDRESS VARCHAR(25)
DESIGNATION VARCHAR(15)
GRADE VARCHAR(1)
DATEOFJOIN DATE
SALARY NUMBER(13,2)

Inserting records

SQL> insert into employee


values('&empname',&empcode,'&address','&designation','&grade','&dateofjoin',&salary);
Enter value for empname: rani
Enter value for empcode: 101
Enter value for address: coimbatore
Enter value for designation: production
Enter value for grade: A
Enter value for dateofjoin: 12-feb-18
Enter value for salary: 15000

1 row created.

SQL> /
Enter value for empname: karthi
Enter value for empcode: 102
Enter value for address: erode
Enter value for designation: accounts
Enter value for grade: S
Enter value for dateofjoin: 23-mar-18
Enter value for salary: 10000
1 row created.
SQL> select * from employee;

EMPNAME EMPCODE ADDRESS DESIGNATION G DATEOFJOIN SALARY


---------------- ----------------- ---------------- ------------------- ---- ------------------- -------------
rani 101 coimbatore production A 12-FEB-18 15000

karthi 102 erode accounts S 23-MAR-18 10000

subash 103 thekkalur marketing C 30-MAY-18 8000

thiru 104 thanjavur retail B 10-JUN-18 12000

siva 105 salem CEO A 11-JAN-18 12000

a) Display name of the employees whose salary is greater than “10000”

SQL> select * from employee where salary > 10000;

EMPNAME EMPCODE ADDRESS DESIGNATION G DATEOFJOIN SALARY


---------------- ----------------- ---------------- ------------------- ---- ------------------- -------------
rani 101 coimbatore production A 12-FEB-18 15000

thiru 104 thanjavur retail B 10-JUN-18 12000

siva 105 salem CEO A 11-JAN-18 12000

b) Display the details of employees in ascending order according to employee code

SQL> select * from employee order by empcode;

EMPNAME EMPCODE ADDRESS DESIGNATION G DATEOFJOIN SALARY


---------------- ----------------- ---------------- ------------------- ---- ------------------- -------------
rani 101 coimbatore production A 12-FEB-18 15000

karthi 102 erode accounts S 23-MAR-18 10000

subash 103 thekkalur marketing C 30-MAY-18 8000

thiru 104 thanjavur retail B 10-JUN-18 12000

siva 105 salem CEO A 11-JAN-18 12000

c) Display the total salary of the employees whose grade is “A”

SQL> select sum(salary) from employee where grade = 'A';

Sum(salary)
---------------
27000
2. Create table payroll with the following fields and insert the values
SQL> create table payrollsri(empno number(8) primary key, empname varchar(15),
department varchar(10),basicpay number(11,2), hra number(9,2), da number(9,2), pf number(9,2),
netpay number(11,2));
Table created.
SQL> desc payrollsri;
Name Null? Type
----------------------------------------- -------- ----------------------------
EMPNO NOT NULL NUMBER(8)
EMPNAME VARCHAR2(15)
DEPARTMENT VARCHAR2(10)
BASICPAY NUMBER(11,2)
HRA NUMBER(9,2)
DA NUMBER(9,2)
PF NUMBER(9,2)
NETPAY NUMBER(11,2)
Inserting Records
SQL> insert into payrollsri values(&empno,'&empname','&department',&basicpay,&hr
a,&da,&pf,&netpay);
Enter value for empno: 100
Enter value for empname: raja
Enter value for department: sales
Enter value for basicpay: 50000
Enter value for hra: 1200
Enter value for da: 700
Enter value for pf: 600
Enter value for netpay: 0
1 row created.
SQL> select * from payrollsri;
EMPNO EMPNAME DEPARTMENT BASICPAY HRA DA PF NETPAY
------------- --------------- ------------------ ------------ ------ ----- ------ ---------
100 raja sales 50000 1200 700 600 0
101 rani purchase 80000 800 1000 1200 0
102 padma sales 100000 1100 700 800 0
103 hema accounts 90000 900 1000 700 0
a) Update the records to calculate the netpay
SQL> update payrollsri set netpay=basicpay+hra+da-pf 2 ;
4 rows updated.
SQL> select * from payrollsri;
EMPNO EMPNAME DEPARTMENT BASICPAY HRA DA PF NETPAY
------------- --------------- ------------------ ------------ ------ ----- ------ ---------
100 raja sales 50000 1200 700 600 51300
101 rani purchase 80000 800 1000 1200 80600
102 padma sales 100000 1100 700 800 101000
103 hema accounts 90000 900 1000 700 91200

Arrange the records of employees in ascending order of their netpay


SQL> select * from payrollsri order by netpay;
EMPNO EMPNAME DEPARTMENT BASICPAY HRA DA PF NETPAY
------------- --------------- ------------------ ------------ ------ ----- ------ ---------
100 raja sales 50000 1200 700 600 51300
101 rani purchase 80000 800 1000 1200 80600
103 hema accounts 90000 900 1000 700 91200
102 padma sales 100000 1100 700 800 101000

b) Display the details of the employees whose department is sales


SQL> select * from payrollsri where department='sales';
EMPNO EMPNAME DEPARTMENT BASICPAY HRA DA PF NETPAY
------------- --------------- ------------------ ------------ ------ ----- ------ ---------
100 raja sales 50000 1200 700 600 51300
102 padma sales 100000 1100 700 800 101000

c) Select the details of employees whose HRA >= 1000 and DA <= 900
SQL> select * from payrollsri where hra >= 1000 and da<=900;
EMPNO EMPNAME DEPARTMENT BASICPAY HRA DA PF NETPAY
------------- --------------- ------------------ ------------ ------ ----- ------ ---------
100 raja sales 50000 1200 700 600 51300
102 padma sales 100000 1100 700 800 101000
d) Select the records in descending order
SQL> select * from payrollsri order by empname desc;
EMPNO EMPNAME DEPARTMENT BASICPAY HRA DA PF NETPAY
------------- --------------- ------------------ ------------ ------ ----- ------ ---------
103 hema accounts 90000 900 1000 700 91200
102 padma sales 100000 1100 700 800 101000
100 raja sales 50000 1200 700 600 51300
101 rani purchase 80000 800 1000 1200 80600
3. Create the table PRODUCT with the following fields and insert values
SQL> create table productsri(prodno number(6) primary key, pname varchar(15), unitmeasure
varchar(15), unitprice number(7,2),quantity number(7,2), amount number (11,2));
Table created.
SQL> desc productsri
Name Null? Type
----------------------------------------- -------- ----------------------------
PRODNO NOT NULL NUMBER(6)
PNAME VARCHAR2(15)
UNITMEASURE VARCHAR2(15)
UNITPRICE NUMBER(7,2)
QUANTITY NUMBER(7,2)
AMOUNT NUMBER(11,2)
Inserting Records
SQL> insert into productsri values(&prodno,'&pname','&unitmeasure',&unitprice,&q
uantity,&amount);
Enter value for prodno: 1000
Enter value for pname: rice
Enter value for unitmeasure: kg
Enter value for unitprice: 50
Enter value for quantity: 2
Enter value for amount: 0
1 row created.

SQL> select * from productsri;


PRODNO PNAME UNITMEASURE UNITPRICE QUANTITY AMOUNT
---------------- --------------- ---------------------- ----------------- --------------- ---------------
1000 rice kg 50 2 0
1001 oil lt 120 1 0
1002 sugar kg 80 3 0
1003 dhal kg 14 15 0
1004 ghee gram 300 12 0

a) Using Update statements calculate the total amount and then select the record
SQL> update productsri set amount=unitprice*quantity 2 ;
5 rows updated.
SQL> select * from productsri;
PRODNO PNAME UNITMEASURE UNITPRICE QUANTITY AMOUNT
---------------- --------------- ---------------------- ----------------- --------------- ---------------
1000 rice kg 50 2 100
1001 oil lt 120 1 120
1002 sugar kg 80 3 240
1003 dhal kg 14 15 210
1004 ghee gram 300 12 3600

b) Select the records whose unit of measure is “Kg”


SQL> select * from productsri where unitmeasure='kg';
PRODNO PNAME UNITMEASURE UNITPRICE QUANTITY AMOUNT
---------------- --------------- ---------------------- ----------------- --------------- ---------------
1000 rice kg 50 2 100
1002 sugar kg 80 3 240
1003 dhal kg 14 15 210

c) Select the records whose quantity is greater than 10 and less than or equal to 20
SQL> select * from productsri where quantity > 10 and quantity <= 20;
PRODNO PNAME UNITMEASURE UNITPRICE QUANTITY AMOUNT
---------------- --------------- ---------------------- ----------------- --------------- ---------------
1003 dhal kg 14 15 210
1004 ghee gram 300 12 3600

d) Calculate the total amount by using sum operation


SQL> select sum(amount) from productsri;
SUM(AMOUNT)
-------------------
4270
e) Calculate the number of records whose unitprice is greater than 50 with count operation
SQL> select count(*) from productsri where unitprice > 50;
Count(*)
-----------
3
4. Create table sales_order with sorder_no and productid as primary key. Set other fields to store client
number, delivery address, delivery date, order status.
SQL> create table sales_order(sorder_no number(3) primary key,clientno number(3), delivery_address
varchar2(30), delivery_date date, order_status varchar2(10));
Table created.

SQL> desc sales_order;


Name Null? Type
----------------------------------------- -------- --------------------------
SORDER_NO NOT NULL NUMBER(3)
CLIENTNO NUMBER(3)
DELIVERY_ADDRESS VARCHAR2(30)
DELIVERY_DATE DATE
ORDER_STATUS VARCHAR2(10)

SQL> create table salesorder_products(sorder_no number(3),productid number(3) primary key, quantity


number(3),unitprice number(6,2),amount number(7,2));
Table created.

SQL> desc salesorder_products;


Name Null? Type
----------------------------------------- -------- ----------------------------
SORDER_NO NUMBER(3)
PRODUCTID NOT NULL NUMBER(3)
QUANTITY NUMBER(3)
UNITPRICE NUMBER(6,2)
AMOUNT NUMBER(7,2)

a) Add new column for storing salesman number using ALTER command
SQL> alter table sales_order add salesman_no number(3);
Table altered.
SQL> desc sales_order;
Name Null? Type
----------------------------------------- -------- ----------------------------
SORDER_NO NOT NULL NUMBER(3)
CLIENTNO NUMBER(3)
DELIVERY_ADDRESS VARCHAR2(30)
DELIVERY_DATE DATE
ORDER_STATUS VARCHAR2(10)
SALESMAN_NO NUMBER(3)

b) Set the sorder_no as foreign key as column constraints


SQL> alter table salesorder_products add foreign key(sorder_no) references sales_order(sorder_no);
Table altered.

c) Enforce the integrity rules using CHECK


SQL> alter table sales_order add check(salesman_no >= 1 and salesman_no <=50);
Table altered.

SQL> insert into sales_order values(100,200,'coimbatore','5-jan-19','pending',55);


insert into sales_order values(100,200,'coimbatore','5-jan-19','pending',55)
*
ERROR at line 1:
ORA-02290: check constraint (STUDENT.SYS_C0011378) violated

5. Student Information Using PL/SQL

SQL> create table student5(rollno number(5) primary key,name varchar2(20),oracle number(3),brm


number(3),accounts number(3),gbe number(3),total number(3),average number(6,2),result varchar2(5));

Table created.

SQL> insert into student5 values(100,’ raja’, 70, 89, 73,90,0,’’);

SQL> insert into student5 values(101,’ abishek’, 60,95,90,50,0,’’);

SQL> insert into student5 values(102,’ karthi’,60, 42,65,55,0,’’);


SQL> insert into student5 values(103,’arun’, 90, 60,98, 83,0,’’);

PL/SQL Block

begin

update student5 set total=oracle+brm+accounts+gbe;

update student5 set average=total/4;

update student5 set result=case when oracle>=50 and brm>=50 and accounts>=50 and gbe>=50 then 'pass' else
'fail' end;

end;

SQL> set serveroutput on

SQL>/

PL/SQL procedure successfully completed.

SQL> select * from student5;

ROLLNO NAME ORACLE BRM ACCOUNTS GBE TOTAL AVERAGE RESULT

-------------- ----------------- ------------- ------------ -------------- ----- ------------ ------------ ----------

100 raja 70 89 73 90 322 80.5 pass

101 abishek 60 95 90 50 295 73.75 pass

102 karthi 60 42 65 55 222 55.5 fail

103 arun 90 60 98 83 331 82.75 pass

104 rani 95 85 75 65 320 80 pass

6. Electricity Bill
declare
conno number(3);
name varchar2(30);
p number(4);
c number(4);
nounit number(4);
amount number(7,2);
begin
conno := &consumerno;
name := '&name';
p := &previous;
c := &current;
nounit := c - p;
if nounit <= 100 then
amount := nounit * 1;
elsif nounit > 100 and nounit <= 200 then
amount := 100 * 1 + (nounit-100) * 2;
elsif nounit > 200 then
amount := 100 * 1 + 200 * 2 + (nounit-200) * 3;
end if;
dbms_output.put_line(' ELECTRICITY BILL ');
dbms_output.put_line(' ---------------- ');
dbms_output.put_line(' consumer Number : ' || conno);
dbms_output.put_line(' Name : ' || name);
dbms_output.put_line(' Previous Reading : ' || p);
dbms_output.put_line(' Current Reading : ' || c);
dbms_output.put_line(' Number of Unit : ' || nounit);
dbms_output.put_line(' Amount : ' || amount);
end;
/

SQL> set serveroutput on


SQL> /
Enter value for consumerno: 100
old 9: conno := &consumerno;
new 9: conno := 100;
Enter value for name: padma
old 10: name := '&name';
new 10: name := 'padma';
Enter value for previous: 500
old 11: p := &previous;
new 11: p := 500;
Enter value for current: 640
old 12: c := &current;
new 12: c := 640;
ELECTRICITY BILL
----------------
consumer Number : 100
Name : padma
Previous Reading : 500
Current Reading : 640
Number of Unit : 140
Amount : 180

PL/SQL procedure successfully completed.

Ex 7: Splitting Table
SQL> create table sristudent(rollno number(3),name varchar2(30),m1 number(3), m2 number(3), m3
number(3), total number(3),result varchar2(6));
Table created.
SQL> desc sristudent;
Name Null? Type
----------------------------------------- -------- --------------------
ROLLNO NUMBER(3)
NAME VARCHAR2(30)
M1 NUMBER(3)
M2 NUMBER(3)
M3 NUMBER(3)
TOTAL NUMBER(3)
RESULT VARCHAR2(6)
SQL> create table studentpass(rollno number(3),name varchar2(30),m1 number(3), m2 number(3), m3
number(3), total number(3),result varchar2(6));
Table created.
SQL> create table studentfail(rollno number(3),name varchar2(30),m1 number(3), m2 number(3), m3
number(3), total number(3),result varchar2(6));
Table created.
SQL> insert into sristudent values(100,’kavitha’,90,97,56,0,’’);

SQL> insert into sristudent values(101,’rani’,35,87,66,0,’’);

SQL> insert into sristudent values(102,’ramya’,45,80,60,0,’’);

SQL> insert into sristudent values(103,’ramya’,55,72,32,0,’’);

SQL> insert into sristudent values(104,’raja’,65,22,92,0,’’);

SQL> select * from sristudent;


ROLLNO NAME M1 M2 M3 TOTAL RESULT
---------- ------------------------ ---------- ---------- -------------- ----------- -----------
100 kavitha 90 97 56 243 pass
101 rani 35 87 66 188 fail
102 ramya 45 80 60 185 pass
103 ramya 55 72 32 159 fail
104 raja 65 22 92 179 fail
105 gowtham 65 82 94 241 pass
106 gowri 85 62 84 231 pass
PL/SQL
declare
cursor c1 is select * from sristudent for update of result,total;
begin
for s in c1 loop
s.total:=s.m1+s.m2+s.m3;
if s.m1 >= 40 and s.m2 >= 40 and s.m3 >=40 then
s.result:='pass';
insert into studentpass values(s.rollno,s.name,s.m1,s.m2,s.m3,s.total,s.result);
else
s.result:='fail';
insert into studentfail values(s.rollno,s.name,s.m1,s.m2,s.m3,s.total,s.result);
end if;
update sristudent set total=s.total,result=s.result where current of c1;
end loop;
end;
/
SQL> set serveroutput on
SQL>/
PL/SQL procedure successfully completed.
SQL> select * from studentpass;
ROLLNO NAME M1 M2 M3 TOTAL RESULT
---------- ------------------------ ---------- ---------- -------------- ----------- -----------
100 kavitha 90 97 56 243 pass
102 ramya 45 80 60 185 pass
105 gowtham 65 82 94 241 pass
106 gowri 85 62 84 231 pass
SQL> select * from studentfail;
ROLLNO NAME M1 M2 M3 TOTAL RESULT
---------- ------------------------ ---------- ---------- -------------- ----------- -----------
101 rani 35 87 66 188 fail
103 ramya 55 72 32 159 fail
104 raja 65 22 92 179 fail

Ex 8 : Joining Table
SQL> create table studentmark(rollno number(3) primary key,name varchar2(15),total number(3));
Table created.
SQL> desc studentmark;
Name Null? Type
----------------------------------------- -------- ----------------------------
ROLLNO NOT NULL NUMBER(3)
NAME VARCHAR2(15)
TOTAL NUMBER(3)
SQL> create table personal(rollno number(3) references studentmark(rollno),address varchar2(40));
Table created.
SQL> desc personal;
Name Null? Type
----------------------------------------- -------- --------------------------
ROLLNO NUMBER(3)
ADDRESS VARCHAR2(40)
SQL> create table studentdetail(rollno number(3),name varchar2(15),total number(3),address varchar2(40));
Table created.
SQL> desc studentdetail;
Name Null? Type
----------------------------------------- -------- ------------------------
ROLLNO NUMBER(3)
NAME VARCHAR2(15)
TOTAL NUMBER(3)
ADDRESS VARCHAR2(40)
SQL> insert into studentmark values(100,'kavi',600);
SQL> insert into studentmark values(200,'kavin',500);
SQL> insert into studentmark values(201,'raji',450);
SQL> insert into studentmark values(202,'ramya',550);
SQL> insert into studentmark values(203,'rani',650);
SQL> insert into personal values(100,'coimbatore');
SQL> insert into personal values(200,'erode');
SQL> insert into personal values(201,'salem');
SQL> insert into personal values(202,'pollachi');
SQL> insert into personal values(203,'madurai');
SQL> select * from studentmark;
ROLLNO NAME TOTAL
---------- --------------- ----------
100 kavi 600
200 kavin 500
201 raji 450
202 ramya 550
203 rani 650
SQL> select * from personal;
ROLLNO ADDRESS
---------- - ---------------------------------------
100 coimbatore
200 erode
201 salem
202 pollachi
203 madurai
SQL> declare
cursor c1 is select studentmark.rollno,studentmark.name,studentmark.total,personal.address from
studentmark,personal where studentmark.rollno = personal.rollno;
begin
for s in c1 loop
insert into studentdetail values(s.rollno,s.name,s.total,s.address);
end loop;
end;
/
PL/SQL procedure successfully completed.
SQL> select * from studentdetail;
ROLLNO NAME TOTAL ADDRESS
---------- --------------- ---------- ----------------------------------------
100 kavi 600 coimbatore
200 kavin 500 erode
201 raji 450 salem
202 ramya 550 pollachi
203 rani 650 madurai

Ex 9(a) : Factorial Using Recursion


Creating function fact
create or replace function fact(n number) return number is
begin
if n=1 then
return 1;
else
return n*fact(n-1);
end if;
end fact;
/
SQL> set serveroutput on
SQL>/
Function Created
Calling function fact

declare
n number(4) := 5;
f number(8);
begin
f := fact(n);
dbms_output.put_line('Factorial = '||f);
end;
/
SQL>/
Enter value for n: 5

old 2: n number(4) := &n;


new 2: n number(4) := 5;
Factorial = 120
PL/SQL procedure successfully completed.

Ex 9(b) : Fibonacci Series Using Recursion


Creating function Fibonacci
create or replace function fibo(n number) return number is
begin
if n=0 then
return 0;
elsif n=1 then
return 1;
else
return fibo(n-1)+fibo(n-2);
end if;
end fibo;
/
SQL> set serveroutput on
SQL>/
Function Created
Calling function Fibonacci

declare
n number(4) := &n;
f number(8);
begin
dbms_output.put_line('Fibonacci Series');
for i in 0..n-1 loop
f := fibo(i);
dbms_output.put_line(f);
end loop;
end;
/

SQL> /
Enter value for n: 8
old 2: n number(4) := &n;
new 2: n number(4) := 8;
Fibonacci Series
0
1
1
2
3
5
8
13
PL/SQL procedure successfully completed.
Ex.10 Database Trigger

Creating Table Employee

SQl> create table emps(eid number(3),ename varchar2(30),hiredate date);

Creating Trigger

create or replace trigger emp_trigger before insert on emps for each row

begin

:new.ename:=UPPER(:new.ename);

:new.hiredate:=SYSDATE;

end;

SQL>/

Trigger Created

SQL> insert into emps(eid,ename) values(1,'ram');

1 row created.

SQL> insert into emps(eid,ename) values(10,'devi');

1 row created.

SQL> insert into emps(eid,ename) values(11,'kavitha');

1 row created.

SQL> select * from emps;

EID ENAME HIREDATE

---------- ------------------------------ ---------

1 RAM 07-MAR-19

10 DEVI 07-MAR-19

11 KAVITHA 07-MAR-19

3 rows selected.

You might also like