Ex - No:1 Demonstrate DDL, DML and Aggregate Function
Ex - No:1 Demonstrate DDL, DML and Aggregate Function
Ex - No:1 Demonstrate DDL, DML and Aggregate Function
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;
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
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
2. Select*from student;
3. Select*from mark;
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
2200004 222500004 parthiban 10-NOV-04 velu 9384442525 bsc plant biology 06-JUN-22 A
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 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
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.
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
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.
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;
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
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
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|
|-----|--------------|--------|----------|----------|----------|--------|---------|---------|----------|----------|-----------|
select*from emp3;
ENO ENAME
101 karthick
102 harish
103 balaji
104 sathish
105 mani
edit mypackspe.sql;
edit mypackbody.sql;
ENO ENAME
101 muthu
102 harish
103 balaji
104 sathish
105 Mani
106 Raja