Create Table Named Employee With The Following Fields and Insert The Values
Create Table Named Employee With The Following Fields and Insert The Values
Create Table Named Employee With The Following Fields and Insert The Values
Create table named Employee with the following fields and insert the values
Table created.
Inserting records
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;
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
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.
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
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
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)
Table created.
PL/SQL Block
begin
update student5 set result=case when oracle>=50 and brm>=50 and accounts>=50 and gbe>=50 then 'pass' else
'fail' end;
end;
SQL>/
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 := ¤t;
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;
/
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,’’);
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
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
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 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
1 row created.
1 row created.
1 row created.
1 RAM 07-MAR-19
10 DEVI 07-MAR-19
11 KAVITHA 07-MAR-19
3 rows selected.