Ex - No:1 Demonstrate DDL, DML and Aggregate Function

Download as pdf or txt
Download as pdf or txt
You are on page 1of 30

Ex.no:1 Demonstrate DDL, DML and Aggregate function.

 Create the table with necessary fields and constraints:


create table employee(eno number(10)primary key, ename varchar2(20)not null, dob date
not null, doj date not null, deptno number(10)not null, bsal number(10,2)not null);

 Insert data into the table:


o Insert into employee values
(&employeenumber,'&employeename','&dateofbirth','&dateofjoin',&departmentnumber,&basic
salar);
o Enter value for employeenumber: 101
o Enter value for employeename: anbu
o Enter value for dateofbirth: 10-jul-1995
o Enter value for dateofjoin: 11-may-2017
o Enter value for departmentnumber: 10
o Enter value for basicsalary: 20000
o /
o Enter value for employeenumber: 102
o Enter value for employeename: vinoth
o Enter value for dateofbirth: 15-aug-1985
o Enter value for dateofjoin: 1-aug-2005
o Enter value for departmentnumber: 10
o Enter value for basicsalary: 24000
o /
o Enter value for employeenumber: 103
o Enter value for employeename: malar
o Enter value for dateofbirth: 23-may-1989
o Enter value for dateofjoin: 7-may-2006
o Enter value for departmentnumber: 20
o Enter value for basicsalary: 21000
o /
o Enter value for employeenumber: 104
o Enter value for employeename: vikram
o Enter value for dateofbirth: 20-mar-1990
o Enter value for dateofjoin: 10-jul-2014
o Enter value for departmentnumber: 10
o Enter value for basicsalary: 31000
o /
o Enter value for employeenumber: 105
o Enter value for employeename: rithika
o Enter value for dateofbirth: 28-jan-2000
o Enter value for dateofjoin: 14-feb-2022
o Enter value for departmentnumber: 20
o Enter value for basicsalary: 29000
 View the data in the employee table:
select*from employee;
ENO ENAME DOB DOJ DEPTNO BSAL
101 anbu 10-JUL-95 11-MAY-17 10 20000
102 vinoth 15-AUG-85 01-AUG-05 10 24000
103 malar 23-MAY-89 07-MAY-06 20 21000
104 vikram 20-MAR-90 10-JUL-14 10 31000
105 rithika 28-JAN-00 14-FEB-22 20 29000

 View only eno and bsal (employee number and basic salary)from employee table:
select eno, bsal from employee;
ENO BSAL
101 20000
102 24000
103 21000
104 31000
105 29000

 View all the details of employee those who are in department number 10:
select*from employee where deptno=10;

ENO ENAME DOB DOJ DEPTNO BSAL


101 anbu 10-JUL-95 11-MAY-17 10 20000
102 vinoth 15-AUG-85 01-AUG-05 10 24000
104 vikram 20-MAR-90 10-JUL-14 10 31000

 Find the maximum Basic salary from employee teble;


select max(bsal) from employee;
MAX(BSAL)
31000
 Find the minimum basic salary from employee table:
select min(bsal) from employee;
MIN(BSAL)
20000
 Find the average basic salary from employee table:
select avg(bsal) from employee;
AVG(BSAL)
25000
 Find the sum of basic salary from employee table:
select sum(bsal) from employee;
SUM(BSAL)
125000
 Find the sum of salary for those who are in department 10 from employee table:
select sum(bsal) sumfordept10 from employee where deptno=10;
SUMFORDEPT10
75000

 Find the sum of salary for those who are in department 20 from employee table:
select sum(bsal) sumfordept20 from employee where deptno=20;
SUMFORDEPT20
50000

 Data in the table in Ascending order based on name:


select*from employee order by ename asc;
ENO ENAME DOB DOJ DEPTNO BSAL
101 anbu 10-JUL-95 11-MAY-17 10 20000
103 malar 23-MAY-89 07-MAY-06 20 21000
105 rithika 28-JAN-00 14-FEB-22 20 29000
104 vikram 20-MAR-90 10-JUL-14 10 31000
102 vinoth 15-AUG-85 01-AUG-05 10 24000

 Data in the table in descending order based on dob:


select*from employee order by dob desc;
ENO ENAME DOB DOJ DEPTNO BSAL
105 rithika 28-JAN-00 14-FEB-22 20 29000
101 anbu 10-JUL-95 11-MAY-17 10 20000
104 vikram 20-MAR-90 10-JUL-14 10 31000
103 malar 23-MAY-89 07-MAY-06 20 21000
102 vinoth 15-AUG-85 01-AUG-05 10 24000
Ex.no:2 Demonstration of joins using student details.
 Creating the table with necessary constraints:
1. Create table admission(adno number(10)primary key, name varchar2(20)not null, dob date not
null, fname varchar2(20)not null, phno number(10), degree varchar2(10));

2. Create table student(regno number(10)primary key, adno number(10) references


admission(adno), deptname varchar2(20)not null, yoj date not null,section varchar2(10));

3. Create table mark(regno number(10) references registration(regno), semester varchar2(10)not


null, mark1 number(5,2) check(mark1>=0 and mark1<=100), mark2 number(5,2)
check(mark2>=0 and mark2<=100), mark3 number(5,2) check(mark3>=0 and mark3<=100),
mark4 number(5,2) check(mark4>=0 and mark4<=100));
 Insert date into the tables:
1. Inserting data into the admission table:

o Insert into admission values(&adno,’&name’,’&dob’,’&fname’,&phno,’&degree’);


o Enter value for adno: 2200001
o Enter value for name: albert
o Enter value for dob: 19-FEB-04
o Enter value for fname: samuel
o Enter value for phno: 9837484748
o Enter value for degree: bsc
o /
o Enter value for adno: 2200002
o Enter value for name: aravind
o Enter value for dob: 09-MAR-04
o Enter value for fname: manin
o Enter value for phno: 9352727373
o Enter value for degree: bsc
o /
o Enter value for adno: 2200003
o Enter value for name: kiran
o Enter value for dob: 04-DEC-03
o Enter value for fname: gowtham
o Enter value for phno: 6242525201
o Enter value for degree: bsc
o /
o Enter value for adno: 2200004
o Enter value for name: Parthiban
o Enter value for dob: 10-NOV-04
o Enter value for fname: velu
o Enter value for phno: 9384442525
o Enter value for degree: bsc
o /
o Enter value for adno: 2200005
o Enter value for name: prabanjhan
o Enter value for dob: 012-DEC-03
o Enter value for fname: vikram
o Enter value for phno: 6724318652
o Enter value for degree: bsc
2. Inserting data into student table:

o insert into student values(&regno,&adno,’&deptname’,’&yoj’,’&section’);


o enter value for regno: 222500001
o enter value for adno:2200001
o enter value for deptname: computer science
o enter value for yoj:06-jun-2022
o enter value for section:A
o /
o enter value for regno: 222500002
o enter value for adno:2200002
o enter value for deptname: mathematics
o enter value for yoj:06-jun-2022
o enter value for section:B
o /
o enter value for regno: 222500003
o enter value for adno:2200003
o enter value for deptname: physics
o enter value for yoj:06-jun-2022
o enter value for section:A
o /
o enter value for regno: 222500004
o enter value for adno:2200004
o enter value for deptname: plant biology
o enter value for yoj:06-jun-2022
o enter value for section:A
o /
o enter value for regno: 222500005
o enter value for adno:2200005
o enter value for deptname: economics
o enter value for yoj:06-jun-2022
o enter value for section:C
3. Inserting data into the mark table:
o insert into mark values(&regno,’&semester’&mark1,&mark2,& mark3,&mark4);
o enter value for regno: 222500001
o enter value for semester: I
o enter value for mark1: 98.23
o enter value for mark2: 78
o enter value for mark3: 86
o enter value for mark4: 97
o /
o enter value for regno: 222500002
o enter value for semester: I
o enter value for mark1: 80
o enter value for mark2: 78
o enter value for mark3: 69
o enter value for mark4: 90

o /
o enter value for regno: 222500003
o enter value for semester: I
o enter value for mark1: 59
o enter value for mark2: 70
o enter value for mark3: 66
o enter value for mark4: 82
o /
o enter value for regno: 222500004
o enter value for semester: I
o enter value for mark1: 81
o enter value for mark2: 55
o enter value for mark3: 61
o enter value for mark4: 52
o /
o enter value for regno: 222500005
o enter value for semester: I
o enter value for mark1: 45
o enter value for mark2: 61
o enter value for mark3: 90
o enter value for mark4: 99

 View the tables:


1. Select*from admission;
ADNO NAME DOB FNAME PHNO DEGREE
2200001 albert 19-FEB-04 samuel 9837484748 bsc
2200002 aravind 09-MAR-04 manin 9352727373 bsc
2200003 kiran 04-DEC-03 gowtham 6242525201 bsc
2200004 parthiban 10-NOV-04 velu 9384442525 bsc
2200005 prabanjhan 12-DEC-03 vikram 6724318652 bsc

2. Select*from student;

REGNO ADNO DEPTNAME YOJ SECTION


222500001 2200001 computer science 06-JUN-22 A
222500002 2200002 mathematics 06-JUN-22 B
222500003 2200003 physics 06-JUN-22 A
222500004 2200004 plant biology 06-JUN-22 A
222500005 2200005 economics 06-JUN-22 C

3. Select*from mark;

REGNO SEME MARK1 MARK2 MARK3 MARK4


222500001 I 98.23 78 86 97
222500002 I 80 78 69 90
222500003 I 59 70 66 82
222500004 I 81 55 61 52
222500005 I 45 61 90 99
 View by joining the admission and student table:
select adno, regno, name, dob, fname, phno, degree, deptname, yoj, section from admission inner
join student using(adno);

ADNO REGNO NAME DOB FNAME PHNO DEGREE DEPTNAME YOJ SECTION

2200001 222500001 albert 19-FEB-04 samuel 9837484748 bsc computer science 06-JUN-22 A

2200002 222500002 aravind 09-MAR-04 manin 9352727373 bsc mathematics 06-JUN-22 B

2200003 222500003 kiran 04-DEC-03 gowtham 6242525201 bsc physics 06-JUN-22 A

2200004 222500004 parthiban 10-NOV-04 velu 9384442525 bsc plant biology 06-JUN-22 A

2200005 222500005 prabanjhan 12-DEC-03 vikram 6724318652 bsc economics 06-JUN-22 C

 Finding the total and average of the marks from mark table;
Select regno,semester,mark1+mark2+mark3+mark4 as total,(mark1+mark2+mark3+mark4)/4 as
average from mark;
REGNO SEME TOTAL AVERAGE
222500001 I 359.23 89.8075
222500002 I 266 66.5
222500003 I 277 69.25
222500004 I 202 50.5
222500005 I 295 73.75
 Add aadhar number colum and insert data to it in admission table :
o alter table admission add aadharno number(14);
o update admission set aadharno=7652-7181-3488 where adno=2200001;
o update admission set aadharno=9052-4551-3088 where adno=2200002;
o update admission set aadharno=2521-8146-0009 where adno=2200003;
o update admission set aadharno=9002-4590-9871 where adno=2200004;
o update admission set aadharno=7654-8766-0081 where adno=2200005;
select*from admission;
ADNO NAME DOB FNAME PHNO DEGREE AADHARNO
2200001 albert 19-FEB-04 samuel 9837484748 bsc 7652-7181-3488
2200002 aravind 09-MAR-04 manin 9352727373 bsc 9052-4551-3088
2200003 kiran 04-DEC-03 gowtham 6242525201 bsc 2521-8146-0009
2200004 parthiban 10-NOV-04 velu 9384442525 bsc 9002-4590-9871
2200005 prabanjhan 12-DEC-03 vikram 6724318652 bsc 7654-8766-0081

 View all the details from the three tables into a single table:
select adno, name, dob, fname, phno, degree, aadharno, regno, deptname, yoj, section, semester,
mark1, mark2, mark3, mark4,mark1+mark2+mark3+mark4 as total, (mark1+mark2+mark3+mark4)/4
as average from admission inner join student using(adno) inner join mark using(regno);
ADNO NAME DOB FNAME PHNO DEGREE AADHARNO REGNO DEPTNAME YOJ SECT SEME
2200001 albert 19-FEB-04 samuel 9837484748 bsc 7652-7181-3488 222500001 computer science 06-JUN-22 A I
2200002 aravind 09-MAR-04 manin 9352727373 bsc 9052-4551-3088 222500002 mathematics 06-JUN-22 B I
2200003 kiran 04-DEC-03 gowtham 6242525201 bsc 2521-8146-0009 222500003 physics 06-JUN-22 A I
2200004 parthiban 10-NOV-04 velu 9384442525 bsc 9002-4590-9871 222500004 plant biology 06-JUN-22 A I
2200005 prabanjhan 12-DEC-03 vikram 6724318652 bsc 7654-8766-0081 222500005 economics 06-JUN-22 C I
MARK1 MARK2 MARK3 MARK4 TOTAL AVERAGE
98.23 78 86 97 359.23 89.8075
80 78 69 39 266 66.5
59 70 66 82 277 69.25
81 30 61 30 202 50.5
45 61 90 99 295 73.75
Ex.no:3 Demonstration of Views using employee paybill Details.

 Create table with necessary constraints:


1. create table empp(eno number(10)primary key,ename varchar2(20)not null,dob date not
null,dept varchar2(20)not null,designation varchar2(20)not null);

2. create table sal(eno number(10)references empp(eno),bsal number(10,2)not null,lic


number(10,2),loan number(10,2));
 Insert data into table:
1. Inserting data into empp table
o insert into empp values(&eno,'&ename','&dob','&dept','&designation');
o Enter value for eno: 101
o Enter value for ename: karan
o Enter value for dob: 19-jan-2000
o Enter value for dept: sales
o Enter value for designation: manager
o /
o Enter value for eno: 102
o Enter value for ename: muthu
o Enter value for dob: 23-nov-2000
o Enter value for dept: sales
o Enter value for designation: assist manager
o /
o Enter value for eno: 103
o Enter value for ename: raja
o Enter value for dob: 19-mar-1994
o Enter value for dept: sales
o Enter value for designation: clerk
o /
o Enter value for eno: 104
o Enter value for ename: amutha
o Enter value for dob: 21-apr-2000
o Enter value for dept: purchase
o Enter value for designation: manager
o /
o Enter value for eno: 105
o Enter value for ename: vanathi
o Enter value for dob: 08-sep-1999
o Enter value for dept: purchase
o Enter value for designation: assist manager
o /
o Enter value for eno: 106
o Enter value for ename: kumaravel
o Enter value for dob: 05-oct-1995
o Enter value for dept: purchase
o Enter value for designation: clerk
2. Inserting data into sal table:
o insert into sal values(&eno,&bsal,&lic,&loan);
o Enter value for eno: 101
o Enter value for bsal: 65000
o Enter value for lic: 1000
o Enter value for loan: 0
o /
o Enter value for eno: 102
o Enter value for bsal: 49000
o Enter value for lic: 1500
o Enter value for loan: 1000
o /
o Enter value for eno: 103
o Enter value for bsal: 25000
o Enter value for lic: 200
o Enter value for loan: 1000
o /
o Enter value for eno: 104
o Enter value for bsal: 66000
o Enter value for lic: 2000
o Enter value for loan: 0
o /
o Enter value for eno: 105
o Enter value for bsal: 38000
o Enter value for lic: 1000
o Enter value for loan: 0
o /
o Enter value for eno: 106
o Enter value for bsal: 26000
o Enter value for lic: 0
o Enter value for loan: 0
create view salv as select*from sal;
select*from salv;
ENO BSAL LIC LOAN
101 65000 1000 0
102 49000 1500 1000
103 25000 200 1000
104 66000 2000 0
105 38000 1000 0
106 26000 0 0

 Create a view and view the data from empp table for each and every departement:
create view emppsalesv as select*from empp where dept='sales';
select*from emppsalv;
ENO ENAME DOB DEPT DESIGNATION
101 karan 19-JAN-00 sales manager
102 muthu 23-NOV-00 sales assist manager
103 raja 19-MAR-94 sales clerk
create view empppurchasev as select*from empp where dept='purchase';
select*from empppurchasev;
ENO ENAME DOB DEPT DESIGNATION
104 amutha 21-APR-00 purchase manager
105 vanathi 08-SEP-99 purchase assist manager
106 kumaravel 05-OCT-95 purchase clerk

 Insert data into the views that is created:


insert into emppsalesv values(107,'mathi','20-dec-1997','sales','poen');
select*from emppsalesv;
ENO ENAME DOB DEPT DESIGNATION
101 karan 19-JAN-00 sales manager
102 muthu 23-NOV-00 sales assist manager
103 raja 19-MAR-94 sales clerk
107 mathi 20-DEC-97 sales poen

insert into empppurchasev values(108,'sakthi','17-feb-1998','purchase','poen');


select*from empppurchasev;
ENO ENAME DOB DEPT DESIGNATION
104 amutha 21-APR-00 purchase manager
105 vanathi 08-SEP-99 purchase assist manager
106 kumaravel 05-OCT-95 purchase clerk
108 sakthi 17-FEB-98 purchase poen

Insert into salv values(107,18000, 200,1000);


Insert into salv values(108,18000,0,2000);
Select *from salv;
ENO BSAL LIC LOAN
101 65000 1000 0
102 49000 1500 1000
103 25000 200 1000
104 66000 2000 0
105 38000 1000 0
106 26000 0 0
107 18000 200 1000
108 1800 0 0 2000
 Create views and find the da, ta, hra, gross pay(gp), Employee provident fund(epf),
Employee state insurance(esi) ,total detection(totde) and net pay(npay) using the data in the
tables:

1. create view emppsalv1 as select empp.eno,ename,dob,dept,designation,bsal,bsal*30/100 as


da,bsal*10/100 as ta,bsal*20/100 as hra,bsal*12/100 as epf,bsal*1.75/100 as esi from
empp,sal where empp.eno=sal.eno;
select*from emppsalv1;
ENO ENAME DOB DEPT DESIGNATION BSAL DA TA HRA EPF ESI
101 karan 19-JAN-00 sales manager 65000 19500 6500 13000 7800 1137.5
102 muthu 23-NOV-00 sales assist manager 49000 14700 4900 9800 5880 857.5
103 raja 19-MAR-94 sales clerk 25000 7500 2500 5000 3000 437.5
104 amutha 21-APR-00 purchase manager 66000 19800 6600 13200 7920 1155
105 vanathi 08-SEP-99 purchase assist manager 38000 11400 3800 7600 4560 665
106 kumaravel 05-OCT-95 purchase clerk 26000 7800 2600 5200 3120 455
107 mathi 20-DEC-97 sales poen 18000 5400 1800 3600 2160 315
108 sakthi 17-FEB-98 purchase poen 18000 5400 1800 3600 2160 315

2. Create view emppsalv2 as select emppsalv1.eno, ename, dob, dept, designation,


emppsalv1.bsal, da, ta, hra, emppsalv1.bsal+da+ta+hra as gp, loan ,lic ,epf ,esi
,loan+lic+esi+epf as totde from emppsalv1 ,sal where emppsalv1.eno=sal.eno;
select*from emppsalv2;
ENO ENAME DOB DEPT DESIGNATION BSAL DA TA HRA GP LOAN LIC EPF ESI TOTDE
101 karan 19-JAN-00 sales manager 65000 19500 6500 13000 104000 0 1000 7800 1137.5 9937.5
102 muthu 23-NOV-00 sales assist manager 49000 14700 4900 9800 78400 1000 1500 5880 857.5 9237.5
103 raja 19-MAR-94 sales clerk 25000 7500 2500 5000 40000 1000 200 3000 437.5 4637.5
104 amutha 21-APR-00 purchase manager 66000 19800 6600 13200 105600 0 2000 7920 1155 11075
105 vanathi 08-SEP-99 purchase assist manager 38000 11400 3800 7600 60800 0 1000 4560 665 6225
106 kumaravel 05-OCT-95 purchase clerk 26000 7800 2600 5200 41600 0 0 3120 455 3575
107 mathi 20-DEC-97 sales poen 18000 5400 1800 3600 28800 1000 200 2160 315 3675
108 sakthi 17-FEB-98 purchase poen 18000 5400 1800 3600 28800 2000 0 2160 315 4475
3. Create view emppsalv3 as select emppsalv2.eno, ename, dob, dept, designation, bsal, da, ta,
hra, gp, loan, lic, epf, esi, totde, gp-totde as ntpay from emppsalv2;
select*from emppsalv3;
ENO ENAME DOB DEPT DESIGNATION BSAL DA TA HRA GP LOAN LIC EPF ESI TOTDE NPAY
101 karan 19-JAN-00 sales manager 65000 19500 6500 13000 104000 0 1000 7800 1137.5 9937.5 94062.5
102 muthu 23-NOV-00 sales assist manager 49000 14700 4900 9800 78400 1000 1500 5880 857.5 9237.5 69162.5
103 raja 19-MAR-94 sales clerk 25000 7500 2500 5000 40000 1000 200 3000 437.5 4637.5 35362.5
104 amutha 21-APR-00 purchase manager 66000 19800 6600 13200 105600 0 2000 7920 1155 11075 94525
105 vanathi 08-SEP-99 purchase assist manager 38000 11400 3800 7600 60800 0 1000 4560 665 6225 54575
106 kumaravel 05-OCT-95 purchase clerk 26000 7800 2600 5200 41600 0 0 3120 455 3575 38025
107 mathi 20-DEC-97 sales poen 18000 5400 1800 3600 28800 1000 200 2160 315 3675 25125
108 sakthi 17-FEB-98 purchase poen 18000 5400 1800 3600 28800 2000 0 2160 315 4475 24325

 Using the previous view emppsalv3 select data where department is sales:
create view emppsalesv1 as select*from emppsalv3 where dept='sales';
select*from emppsalesv1;
ENO ENAME DOB DEPT DESIGNATION BSAL DA TA HRA GP LOAN LIC EPF ESI TOTDE NPAY
101 karan 19-JAN-00 sales manager 65000 19500 6500 13000 104000 0 1000 7800 1137.5 9937.5 94062.5
102 muthu 23-NOV-00 sales assist manager 49000 14700 4900 9800 78400 1000 1500 5880 857.5 9237.5 69162.5
103 raja 19-MAR-94 sales clerk 25000 7500 2500 5000 40000 1000 200 3000 437.5 4637.5 35362.5
107 mathi 20-DEC-97 sales poen 18000 5400 1800 3600 28800 1000 200 2160 315 3675 25125

 Using the previous view emppsalv3 select data where department is purchase:
create view empppurchasev1 as select*from emppsalv3 where dept='purchase';
select*from empppurchasev1;
ENO ENAME DOB DEPT DESIGNATION BSAL DA TA HRA GP LOAN LIC EPF ESI TOTDE NPAY
104 amutha 21-APR-00 purchase manager 66000 19800 6600 13200 105600 0 2000 7920 1155 11075 94525
105 vanathi 08-SEP-99 purchase assist manager 38000 11400 3800 7600 60800 0 1000 4560 665 6225 54575
106 kumaravel 05-OCT-95 purchase clerk 26000 7800 2600 5200 41600 0 0 3120 455 3575 38025
108 sakthi 17-FEB-98 purchase poen 18000 5400 1800 3600 28800 2000 0 2160 315 4475 24325
Ex.no:4 Display highest Salary in employee(Top 5 only) using PL/SQL.

 Creating table with necessary constraints:


create table emp2(employee_no number(10)primary key,employee_name varchar2(30)not
null,date_of_birth date not null,bsal number(10,2)check(bsal>=10000 and bsal<=10000));

 Insert data into the emp2 table:


o insert into emp2 values(&eno,'&ename','&dob',&bsal);
o Enter value for eno: 101
o Enter value for ename: hari
o Enter value for dob: 29-jan-2001
o Enter value for bsal: 30000
o /
o Enter value for eno: 102
o Enter value for ename: priya
o Enter value for dob: 21-may-2001
o Enter value for bsal: 32000
o /
o Enter value for eno: 103
o Enter value for ename: sam
o Enter value for dob: 19-dec-1998
o Enter value for bsal: 42000
o /
o Enter value for eno: 104
o Enter value for ename: sumathi
o Enter value for dob: 10-apr-1999
o Enter value for bsal: 39000
o /
o Enter value for eno: 105
o Enter value for ename: ravi
o Enter value for dob: 12-dec-1996
o Enter value for bsal: 29000
o /
o Enter value for eno: 106
o Enter value for ename: venkat
o Enter value for dob: 01-aug-1993
o Enter value for bsal: 39000
o /
o Enter value for eno: 107
o Enter value for ename: vanitha
o Enter value for dob: 15-oct-2000
o Enter value for bsal: 32000
o /
o Enter value for eno: 108
o Enter value for ename: karan
o Enter value for dob: 13-jan-1999
o Enter value for bsal: 39200

o /
o Enter value for eno: 109
o Enter value for ename: siva
o Enter value for dob: 23-feb-2000
o Enter value for bsal: 41000
o /
o Enter value for eno: 110
o Enter value for ename: chitra
o Enter value for dob: 19-mar-1994
o Enter value for bsal: 42900

 View the data in the table:


Select*from emp2;
EMPLOYEE_NO EMPLOYEE_NAME DATE_OF_BIRTH BSAL
101 hari 29-JAN-01 30000
102 priya 21-MAY-01 32000
103 sam 19-DEC-98 42000
104 sumathi 10-APR-99 39000
105 ravi 12-DEC-96 29000
106 venkat 01-AUG-93 39000
107 vanitha 15-OCT-00 32000
108 karan 13-JAN-99 39200
109 siva 23-FEB-00 41000
110 chitra 19-MAR-94 42900

 Create cursor to print the top five salary records from emp2 table:
Edit emp2.sql;
Code:
set serveroutput on
declare
i number;
cursor xyzo is select*from emp2 order by bsal desc;
myrow system.emp2%rowtype;
begin
open xyzo;
dbms_output.put_line(lpad('eno',5)||' '||lpad('ename',10)||' '||lpad('date_of_birth',14)||'
'||lpad('bsal',8));
i:=0;
loop
fetch xyzo into myrow;
exit when xyzo%notfound;
if(i<5) then
dbms_output.put_line(lpad(myrow.employee_no,5)||' '||lpad(myrow.employee_name,10)||'
'||lpad(myrow.date_of_birth,13)||' '||lpad(myrow.bsal,8));
i:=i+1;
end if;
end loop;
close xyzo;
end;
/
set serveroutput off
@emp2.sql;
eno ename date_of_birth bsal
110 chitra 19-MAR-94 42900
103 sam 19-DEC-98 42000
109 siva 23-FEB-00 41000
108 karan 13-JAN-99 39200
104 sumathi 10-APR-99 39000
Ex.no:5 Demonstration of procedure and function.

 Create a table with necessary constraints:


create table emp1(eno number(10)primary key,ename varchar2(30)not null,dept varchar2(20)not
null,designation varchar(20)not null,salary number(10,2) check(salary>=10000 and
salary<=150000));

 Inserting data into the emp1 table:


o insert into emp1 values(&eno,'&ename','&dept','&designation',&salary);
o Enter value for eno: 1001
o Enter value for ename: sakthi
o Enter value for dept: purchase
o Enter value for designation: manager
o Enter value for salary: 100000
o /
o Enter value for eno: 1002
o Enter value for ename: nandhini
o Enter value for dept: sales
o Enter value for designation: asst manager
o Enter value for salary: 80000
o /
o Enter value for eno: 1003
o Enter value for ename: barathi
o Enter value for dept: purchase
o Enter value for designation: clerk
o Enter value for salary: 25000
o /
o Enter value for eno: 1004
o Enter value for ename: meena
o Enter value for dept: sales
o Enter value for designation: manager
o Enter value for salary: 102000
o /
o Enter value for eno: 1005
o Enter value for ename: chandru
o Enter value for dept: purchase
o Enter value for designation: asst manager
o Enter value for salary: 69000

 View the table data:


Select*from emp1;
ENO ENAME DEPT DESIGNATION SALARY
1001 sakthi purchase manager 100000
1002 nandhini sales asst manager 80000
1003 barathi purchase clerk 25000
1004 meena sales manager 102000
1005 chandru purchase asst manager 69000
 Creating a procedure:
edit emp1procedure.sql;
Code:
create or replace procedure myprocedure(eid in number) is
begin
update emp1 set salary=salary+salary*10/100 where eno=eid;
end;
/

 Execute the procedure:


@emp1procedure.sql;
execute myprocedure(1003);
Select*from emp1;
ENO ENAME DEPT DESIGNATION SALARY
1001 sakthi purchase manager 100000
1002 nandhini sales asst manager 80000
1003 barathi purchase clerk 27500
1004 meena sales manager 102000
1005 chandru purchase asst manager 69000
 Creating a Function:
edit myfact.sql;
create or replace function fact(n in number) return number is
i number;
f number:=1;
begin
for i in 1..n
loop
f:=f*i;
end loop;
return(f);
end;
/
SQL> @myfact.sql
Function created.

SQL> select fact(5) from dual;

FACT(5)
----------
120
Ex.no:7 Create Employee log table using Triggers.
 Create a table with necessary attributes and constraints:
1. Create table emp(eno number(10)primary key, name varchar2(20)not null, salary
number(10,2));
2. Create table emplog(eno number(10),oldsalary number(10,2),newsalary
number(10,2),username varchar2(20),systemdate varchar2(20),foreign key(eno) references
emp(eno));
 Insert data into the table:
insert into emp values(&eno,'&name',&salary);
o Enter value for eno: 101
o Enter value for name: anita
o Enter value for salary: 33000
o /
o Enter value for eno: 102
o Enter value for name: bhavana
o Enter value for salary: 35000
o /
o Enter value for eno: 103
o Enter value for name: charan
o Enter value for salary: 30000
o /
o Enter value for eno: 104
o Enter value for name: mathan
o Enter value for salary: 32000
o /
o Enter value for eno: 105
o Enter value for name: santhi
o Enter value for salary: 40000
 View the table:
Select*from emp;

ENO NAME SALARY


101 anita 33000
102 bhavana 35000
103 charan 30000
104 mathan 32000
105 santhi 40000

 Creating trigger:
Edit emp_log.sql;
Code:
create or replace trigger mytrigger before update of salary on emp for each row
begin
insert into emplog values(:old.eno, :old.salary, :new.salary, user, sysdate);
end; /
 Run the trigger:
@emp_emplog.sql;
Trigger created

 Update the table:


o Update emp set salary=35000 where eno=101;
o Update emp set salary=45000 where eno=105;
o Update emp set salary=29000 where eno=102;

 View the emplog table after updating the emp table:


Select*from emplog;
ENO OLDSALARY NEWSALARY USERNAME SYSTEMDAT
101 33000 35000 SYSTEM 26-OCT-22
105 40000 45000 SYSTEM 26-OCT-22
102 35000 29000 SYSTEM 28-OCT-22
Ex.no:8 PL/SQL program to display employee details using cursor.

 Create a table with necessary constraints:


create table employees(employee_id number(10)primary key,employee_name varchar2(40)not
null,date_of_birth date not null,date_of_join date not null,department varchar2(20)not
null,designation varchar2(20)not null,salary number(10,2));

 Insert data into the employees table:


o insert into employees values(&eid,'&ename','&dob','&doj','&dept','&designation',&salary);
o Enter value for eid: 1001
o Enter value for ename: janath
o Enter value for dob: 22-feb-1992
o Enter value for doj: 06-apr-2017
o Enter value for dept: sales
o Enter value for designation: asst manager
o Enter value for salary: 35000
o /
o Enter value for eid: 1002
o Enter value for ename: prathap
o Enter value for dob: 12-dec-2000
o Enter value for doj: 17-mar-2020
o Enter value for dept: research
o Enter value for designation: asst
o Enter value for salary: 30000
o /
o Enter value for eid: 1003
o Enter value for ename: meena
o Enter value for dob: 09-aug-1994
o Enter value for doj: 29-jan-2019
o Enter value for dept: research
o Enter value for designation: manager
o Enter value for salary: 50000
o /
o Enter value for eid: 1004
o Enter value for ename: saravanan
o Enter value for dob: 19-may-1998
o Enter value for doj: 01-aug-2020
o Enter value for dept: sales
o Enter value for designation: clerk
o Enter value for salary: 20000
o /
o Enter value for eid: 1005
o Enter value for ename: sonam
o Enter value for dob: 20-oct-1991
o Enter value for doj: 19-sep-2012
o Enter value for dept: sales
o Enter value for designation: manager
o Enter value for salary: 50000
 Creating the cursor to display the output in designed format:
edit employees.sql;
Code:
set serveroutput on
declare
cursor newcursor is select*from employees;
myrow system.employees%rowtype;
begin
open newcursor;
dbms_output.put_line('========================================================
====================================');
dbms_output.put_line('|'||lpad('Employee_ID',11)||'|'||lpad('Employee_Name',14)||'|'||lpad('Date_of_Birt
h',14)||'|'||lpad('Date_of_Join',13)||'|'||lpad('department',11)||'|'||lpad('Designation',14)||'|'||lpad('Salary',7)||
'|');
dbms_output.put_line('|===========|==============|==============|=============|==
=========|==============|=======|');
loop
fetch newcursor into myrow;
exit when newcursor%notfound;
dbms_output.put_line('|'||lpad(myrow.employee_id,11)||'|'||lpad(myrow.employee_name,14)||'|'||lpad(m
yrow.date_of_birth,14)||'|'||lpad(myrow.date_of_join,13)||'|'||lpad(myrow.department,11)||'|'||lpad(myro
w.designation,14)||'|'||lpad(myrow.salary,7)||'|');
dbms_output.put_line('|-----------|--------------|--------------|-------------|-----------|--------------|-------|');
end loop;
close newcursor;
end;
/set serveroutput off
@employees.sql;
======================================================================
|Employee_ID| Employee_Name| Date_of_Birth| Date_of_Join| department| Designation| Salary|
|==========|=============|===========|==========|========|==========|=====|
| 1001| janath| 22-FEB-92| 06-APR-17| sales | asst manager | 35000|
|----------------|----------------------|------------------|------------------|-------------|-----------------|---------|
| 1002| prathap| 1 2-DEC-00| 17-MAR-20| research| asst| 30000|
|----------------|----------------------|------------------|------------------|-------------|-----------------|---------|
| 1003| meena| 09-AUG-94| 29-JAN-19| research| manager| 50000|
|----------------|----------------------|------------------|------------------|-------------|-----------------|---------|
| 1004| saravanan| 19-MAY-98| 01-AUG-20| sales| clerk| 20000|
|----------------|----------------------|------------------|------------------|-------------|-----------------|---------|
| 1005| sonam| 20-OCT-91| 19-SEP-12| sales| manager| 50000|
|----------------|----------------------|------------------|------------------|-------------|-----------------|---------|
Ex.no:9 PL/SQL program to display Student mark details using cursor.
 Create a table with necessary constraints:
Create table student(regno number(10)primary key, name varchar2(20)not null, dob date not null,
phys number(10,2) check(phys>=0 and phys<=100), math number(10,2) check(math>=0 and
math<=100), chem number(10,2) check(chem>=0 and chem<=100), comp number(10,2)
check(comp>=0 and comp<=100));

 Insert data into the table:


o insert into student values(&regno,’&name’,’&dob’,&phys,&math,&chem.,&comp);
o Enter value for regno: 101
o Enter value for name: karthick
o Enter value for dob: 17-oct-2002
o Enter value for phys: 99
o Enter value for math: 89.34
o Enter value for chem.: 78
o Enter value for comp: 98.99
o /
o Enter value for regno: 102
o Enter value for name: balaji
o Enter value for dob: 19-oct-2002
o Enter value for phys: 78
o Enter value for math: 89
o Enter value for chem.: 83.67
o Enter value for comp: 99
o /
o Enter value for regno: 103
o Enter value for name: jayanath
o Enter value for dob: 22-dec-2002
o Enter value for phys: 88
o Enter value for math: 57
o Enter value for chem.: 94.76
o Enter value for comp: 87.23
o /
o Enter value for regno: 104
o Enter value for name: harrys
o Enter value for dob: 22-jan-2002
o Enter value for phys: 75
o Enter value for math: 33
o Enter value for chem.: 23
o Enter value for comp: 69
o /
o Enter value for regno: 105
o Enter value for name: mani
o Enter value for dob: 13-mar-2001
o Enter value for phys:87
o Enter value for math: 43
o Enter value for chem.: 23
o Enter value for comp: 99

o /
o Enter value for regno: 106
o Enter value for name: martin
o Enter value for dob: 10-nov-2002
o Enter value for phys: 67
o Enter value for math: 69
o Enter value for chem: 45
o Enter value for comp: 40
o /
o Enter value for regno: 107
o Enter value for name: dilip
o Enter value for dob: 17-oct-2002
o Enter value for phys: 56
o Enter value for math: 44
o Enter value for chem.: 55
o Enter value for comp: 42
o /
o Enter value for regno: 108
o Enter value for name: jero
o Enter value for dob: 08-feb-2002
o Enter value for phys: 40
o Enter value for math: 34
o Enter value for chem.: 55
o Enter value for comp: 39
o /
o Enter value for regno: 109
o Enter value for name: drawid
o Enter value for dob: 20-mar-2001
o Enter value for phys: 87
o Enter value for math: 65
o Enter value for chem.: 44
o Enter value for comp: 55

 View the table student:


select*from student;
REGNO NAME DOB PHYS MATH CHEM COMP
101 karthick 17-OCT-02 99 89.34 78 98.99
102 balaji 19-OCT-02 78 89 83.67 99
103 jayanth 22-DEC-02 88 57 94.76 87.23
104 harrys 22-JAN-02 75 33 23 69
105 mani 13-MAR-01 87 43 23 99
106 martin 10-NOV-02 67 69 45 40
107 dilip 30-AUG-02 56 44 55 42
108 jero 08-FEB-02 40 34 55 39
109 drawid 20-MAR-01 87 65 44 55
 Create a plsql block with cursor to find the total, average and class of the student and
display it:
Edit studentmark.sql;
Code:
set serveroutput on
declare
total number(10,2);
average number(5,2);
result varchar2(10);
class varchar2(10);
valu number(1);
cursor xyz is select*from student;
myrow system.student%rowtype;
begin
open xyz;
dbms_output.put_line('========================================================
================================');
dbms_output.put_line('|'||lpad('regno',5)||'|'||lpad('name',10)||'|'||lpad('dob',10)||'|'||lpad('phys',6)||'|'||lpad('
math',6)||'|'||lpad('chem',6)||'|'||lpad('comp',6)||'|'||lpad('total',8)||'|'||lpad('average',6)||'|'||lpad('result',6)||'|'||l
pad('class',7)||'|');
dbms_output.put_line('|=====|==========|==========|======|======|======|======|====
====|======|======|=======|');
loop
fetch xyz into myrow;
exit when xyz%notfound;
total:=myrow.phys+myrow.math+myrow.chem+myrow.comp;
average:=total/4;
if myrow.phys>35 and myrow.math>35 and myrow.chem>35 and myrow.comp>35 then
result:='pass';
else
result:='fail';
end if;
if result='pass' then
if average>=60 then
class:='first';
elsif average>=50 then
class:='second';
elsif average>=35 then
class:='third';
end if;
else
class:='no';
end if;
dbms_output.put_line('|'||lpad(myrow.regno,5)||'|'||lpad(myrow.name,10)||'|'||lpad(myrow.dob,10)||'|'||lpa
d(myrow.phys,6)||'|'||lpad(myrow.math,6)||'|'||lpad(myrow.chem,6)||'|'||lpad(myrow.comp,6)||'|'||lpad(tota
l,8)||'|'||lpad(average,6)||'|'||lpad(result,6)||'|'||lpad(class,7)||'|');
dbms_output.put_line('|-----|----------|----------|------|------|------|------|--------|------|------|-------|');
end loop;
close xyz;
end;
/
set serveroutput off
 Execute the plsql block to view the output:
@studentmark.sql
========================================================================
| regno| name| dob| phys| math| chem| comp| total| averag| result| class|
|=====|=======|=========|======|======|======|=======|======|======|=====|=====|
| 101| karthick| 17-OCT-02| 99| 89.34| 78| 98.99| 365.33| 91.33| pass| first|
|--------|------------|---------------|----------|----------|-----------|----------|----------|----------|----------|--------|
| 102| balaji| 19-OCT-02| 78| 89| 83.67| 99| 349.67| 87.42| pass| first|
|--------|------------|---------------|----------|----------|-----------|----------|----------|----------|----------|--------|
| 103| jayanth| 22-DEC-02| 88| 57| 94.76| 87.23| 326.99| 81.75| pass| first|
|--------|------------|---------------|----------|----------|-----------|----------|----------|----------|----------|--------|
| 104| harrys| 22-JAN-02| 75| 33| 23| 69| 200| 50| fail| no|
|--------|---------- -|---------------|----------|----------|-----------|----------|----------|----------|----------|--------|
| 105| mani|13-MAR-01| 87| 43| 23| 99| 252| 63| fail| no|
|--------|------------|--------------|-----------|----------|-----------|----------|----------|----------|----------|--------|
| 106| martin|10-NOV-02| 67| 69| 45| 40| 221| 55.25| pass |second|
|--------|------------|---------------|----------|----------|-----------|----------|----------|----------|----------|--------|
| 107| dilip| 30-AUG-02| 56| 44| 55| 42| 197| 49.25| pass| third|
|--------|------------|---------------|----------|----------|-----------|----------|----------|----------|----------|--------|
| 108| jero| 08-FEB-02| 40| 34| 55| 39| 168| 42| fail| no|
|--------|------------|---------------|----------|----------|-----------|----------|----------|----------|----------|--------|
| 109| drawid| 20-MAR-01| 87| 65| 44| 55| 251| 62.75| pass| first|
|--------|------------|---------------|----------|----------|-----------|----------|----------|----------|----------|--------|
Procedure successfully completed
Ex.no:10 PL/SQL Program to display Employee pay bill details using cursor.

 Create table with necessary constraints:


Create table paybill(eno number(10) primary key, ename varchar2(20) not null, designation
varchar2(20) not null, basicpay number(10,2)not null, lic number(10,2)not null, loan number(10,2)not
null);

 Inserting data into the table:


insert into paybill values(&eno,’&ename’,’&designation’,&basicpay,&lic,&loan);
o Enter value for eno: 101
o Enter value for ename: raja
o Enter value for designation: salesman
o Enter value for basicpay:20000
o Enter value for lic: 1500
o Enter value for loan: 2000
o /
o Enter value for eno:102
o Enter value for ename: rajaram
o Enter value for designation: asst manager
o Enter value for basicpay:25000
o Enter value for lic: 2000
o Enter value for loan: 1000
o /
o Enter value for eno: 103
o Enter value for ename: murugan
o Enter value for designation: clerk
o Enter value for basicpay: 18000
o Enter value for lic: 1000
o Enter value for loan: 1300
o /
o Enter value for eno: 104
o Enter value for ename: maria
o Enter value for designation: manager
o Enter value for basicpay: 29000
o Enter value for lic: 1700
o Enter value for loan: 2000
o /
o Enter value for eno: 105
o Enter value for ename: lopez
o Enter value for designation: secratary
o Enter value for basicpay:30000
o Enter value for lic: 2000
o Enter value for loan: 1000
 View the table data:
Select*from paybill;
ENO ENAME DESIGNATION BASICPAY LIC LOAN
101 raja salesman 20000 1500 2000
102 rajaram asst manager 25000 2000 1000
103 murugan clerk 18000 1000 1300
104 maria manager 29000 1700 2000
105 lopez secratary 30000 2000 1000

 Creating a cursor to calculate the dearness allowance(da) , travel allowance (ta) ,house rent
allowance (hra) ,gross pay (g)p, employee provident fund (epf),employee state insurance
(esi), total detection (totde)and net pay (npay) for each and every employee;
Edit paybill.sql;
Code:
set serveroutput on
declare
cursor xyz is select*from paybill;
myrow system.paybill%rowtype;
da number(10,2);
ta number(10,2);
gp number(10,2);
hra number(10,2);
epf number(10,2);
esi number(10,2);
totde number(10,2);
netpay number(10,2);
begin
open xyz;
dbms_output.put_line('========================================================
===================================');
dbms_output.put_line('|'||lpad('eno',4)||'|
'||lpad('basicpay',9)||'|'||lpad('da',6)||'|'||lpad('ta',6)||'|'||lpad('hra',7)||'|'||lpad('gp',7)||'|'||lpad('epf',6)||'|'||lpad('
esi',6)||'|'||lpad('lic',6)||'|'||lpad('loan',6)||'|'||lpad('totde',6)||'|'||lpad('netpay',7)||'|');
dbms_output.put_line('|====|===========|======|======|=======|=======|======|======|
======|======|======|=======|');
loop
fetch xyz into myrow;
exit when xyz%notfound;
da:=myrow.basicpay*40/100;
ta:=myrow.basicpay*2/100;
hra:=myrow.basicpay*10/100;
gp:=myrow.basicpay+da+hra+ta;
epf:=myrow.basicpay*8.5/100;
esi:=myrow.basicpay*1.5/100;
totde:=epf+esi+myrow.lic+myrow.loan;
netpay:=gp-totde;
dbms_output.put_line('|'||lpad(myrow.eno,4)||'|'||lpad(myrow.basicpay,9)||'
|'||lpad(da,6)||'|'||lpad(ta,6)||'|'||lpad(hra,6)||'
|'||lpad(gp,7)||'|'||lpad(epf,6)||'|'||lpad(esi,6)||'|'||lpad(myrow.lic,6)||'|'||lpad(myrow.loan,6)||'|'||lpad(totde,6)|
|'|'||lpad(netpay,7)||'|');
dbms_output.put_line('|----|-----------|------|------|-------|-------|------|------|------|------|------|-------|');
end loop;
close xyz;
end;
/set serveroutput off

@paybill.sql;
========================================================================
| eno| basicpay| da| ta| hra| gp| epf| esi| lic| loan| totde| netpay |
|===|=========|=====|======|======|=====|=====|=====|=====|======|======|=======|
| 101| 20000 | 8000| 400| 2000 | 30400| 1700| 300| 1500| 2000| 5500| 24900|
|-----|--------------|--------|----------|----------|----------|--------|---------|--------|-----------|----------|-----------|
| 102| 25000 | 10000| 500| 2500 | 38000| 2125| 375| 2000| 1000| 5500| 32500|
|-----|--------------|--------|----------|----------|----------|--------|---------|--------|-----------|----------|-----------|
| 103| 18000 | 7200| 360| 1800| 27360| 1530| 270| 1000| 1300| 4100| 23260|
|-----|--------------|--------|----------|----------|----------|--------|---------|---------|----------|----------|-----------|
| 104| 29000 | 11600| 580| 2900| 44080| 2465| 435| 1700| 2000| 6600| 37480|
|-----|--------------|--------|----------|----------|----------|--------|---------|---------|----------|----------|-----------|
| 105| 30000 | 12000| 600| 3000| 45600| 2550| 450| 2000| 1000| 6000| 39600|
|-----|--------------|--------|----------|----------|----------|--------|---------|---------|----------|----------|-----------|

Procedure successfully completed


Ex.no: 6 Create package for employee details
 Create table with necessary constraints.
 Insert data into the table.
 View the table data.
 Create package specification with only declaration of my_insert,my_update and my_delete
procedures
 Create package body with definition of my_insert,my_update and my_delete procudures
 Execute the procedure with package name
.
create table emp3(eno number(5)primary key,ename varchar2(15));

insert into emp3 values(&eno,'&ename');

Enter value for eno: 101


Enter value for ename: karthick

Enter value for eno: 102


Enter value for ename: harish

Enter value for eno: 103


Enter value for ename: balaji

Enter value for eno: 104


Enter value for ename: sathish

Enter value for eno: 105


Enter value for ename: mani

select*from emp3;

ENO ENAME
101 karthick
102 harish
103 balaji
104 sathish
105 mani

edit mypackspe.sql;

CREATE or replace PACKAGE my_pack1 AS -- specification


PROCEDURE my_insert(Empno NUMBER,ename VARCHAR2);
PROCEDURE my_update(Empno NUMBER,name VARCHAR2);
PROCEDURE my_delete(Empno NUMBER);
END my_pack1;
/
@mypackspe.sql;
Package created.

edit mypackbody.sql;

CREATE or replace PACKAGE BODY my_pack1 AS -- body


PROCEDURE my_insert(Empno NUMBER,ename VARCHAR2) IS
BEGIN
INSERT INTO emp3 VALUES (empno,ename);
END my_insert;
PROCEDURE my_update(Empno NUMBER,name VARCHAR2) IS
BEGIN
Update emp3 set ename=name where eno=empno;
END my_update;
PROCEDURE my_delete(Empno NUMBER) IS
BEGIN
Delete from emp3 where eno=empno;
END my_delete;
END my_pack1;
/
@mypackbody.sql;

Package body created.


Calling the procedure:
Execute my_pack1.my_insert(106,’Raja’);
Execute my_pack1.my_update(101,’muthu’)
select*from emp3;

ENO ENAME
101 muthu
102 harish
103 balaji
104 sathish
105 Mani
106 Raja

You might also like