Dbms Lab Exercise PDF
Dbms Lab Exercise PDF
Creation of a database and writing SQL queries to retrieve information from the
database.
CREATE COMMAND:
SQL> CREATE TABLE employee ( Employee_name varchar2(10),employee_no number(8),
dept_name varchar2(10),dept_no number (5),date_of_join date);
Table created.
TABLE DESCRIPTION
------------------------------SQL> desc employee;
Name
Null?
Type
------------------------------- -------- -----------------------EMPLOYEE_NAME
VARCHAR2(10)
EMPLOYEE_NO
NUMBER(8)
DEPT_NAME
VARCHAR2(10)
DEPT_NO
NUMBER(5)
DATE_OF_JOIN
DATE
INSERT COMMAND:
SQL> insert into employee values ('Vijay',345,'CSE',21,'21-jun-2006');
1 row created.
SQL>insert into employee (Employee_name,employee_no,dept_name,dept_no,date_of_join)
values ('Vishva',128, 'ECE',87,'25-dec-2006');
1 row created.
SELECT COMMAND:
SELECT Employee_name FROM employee;
SELECT Employee_name, dept_name FROM employee;
SELECT * FROM employee;
NOT IN PREDICATE
SQL> select sname, sid from studs where sid not in(102,104);
SNAME
SID
------------------------------ ---------ashwin
101
pruthvik
103
UPDATING THE TABLE
SQL> alter table studs add ( spocket varchar2(20) );
Table altered.
SQL> update studs set spocket=750 where sid=101;
1 row updated.
SQL> update studs set spocket=500 where sid=102;
1 row updated.
SQL> update studs set spocket=250 where sid=103;
1 row updated.
SQL> update studs set spocket=100 where sid=104;
1 row updated.
SQL> select * from studs;
SNAME
SID
SAGE SAREA
SDEPT
------------------------------ ---------- --------- -------------------- -------------------SPOCKET
-------------------ashwin
101
19 anna nagar
aeronautical
750
bhavesh
102
18 nungambakkam
marine
500
pruthvik
103
20 anna nagar
aerospace
250
charith
104
20 kilpauk
mechanical
100
AGGREGATE FUNCTIONS
SQL> select avg( spocket ) result from studs;
RESULT
--------400
SQL> select min(spocket) result from studs;
RESULT
-------------------100
SQL> select count(spocket) result from studs;
RESULT
--------4
SQL> select count(*) result from studs;
RESULT
--------4
4
RESULT
---------####oracle
SQL> select rpad ('oracle',10,'^') result from dual;
RESULT
---------oracle^^^^
DATE FUNCTIONS
SQL> select sysdate from dual;
SYSDATE
--------16-JUL-08
SQL> select sysdate,add_months(sysdate,4) result from dual;
SYSDATE RESULT
----------------16-JUL-08 16-NOV-08
SQL> select sysdate, last_day(sysdate) result from dual;
SYSDATE RESULT
----------------16-JUL-08 31-JUL-08
SQL> select sysdate, next_day(sysdate,'sunday') result from dual;
SYSDATE RESULT
----------------16-JUL-08 20-JUL-08
SQL> select months_between('09-aug-91','11-mar-90') result from dual;
RESULT
--------16.935484
GROUP BY CLAUSE
SQL> select sarea, sum(spocket) result from studs group by sarea;
SAREA
RESULT
------------------------------anna nagar
1000
nungambakkam
500
kilpauk
100
HAVING CLAUSE
SQL> select sarea, sum(spocket) result from studs group by sarea having spocket<600;
SAREA
RESULT
------------------------------nungambakkam
500
kilpauk
100
DELETION
SQL> delete from studs where sid=101;
6
1 row deleted.
SQL> select * from studs;
SNAME
SID
SAGE
SAREA
SDEPT
SPOCKET
------------------------------ ---------- --------- -------------------- -------------------- ------------------bhavesh
102
18
nungambakkam
marine
500
pruthvik
103
20
anna nagar
aerospace
250
charith
104
20
kilpauk
mechanical
100
CREATING TABLES FOR DOING SET OPERATIONS
TO CREATE PRODUCT TABLE
SQL> create table product(prodname varchar2(30), prodno varchar2(10));
Table created.
SQL> select * from product;
PRODNAME
PRODNO
------------------------------ ---------table
10001
chair
10010
desk
10110
cot
11110
sofa
10010
tvstand
11010
TO CREATE SALE TABLE
SQL> create table sale(prodname varchar2(30),orderno number(10),prodno varchar2(10));
Table created.
SQL> select * from sale;
PRODNAME
ORDERNO PRODNO
----------------------------------------------table
801
10001
chair
805
10010
desk
809
10110
cot
813
11110
sofa
817
10010
SET OPERATIONS
SQL> select prodname from product where prodno=10010 union select prodname from sale
where prodno=10010;
PRODNAME
-----------------------------chair
sofa
SQL> select prodname from product where prodno=11110 intersect select prodname from sale
where prodno=11110;
PRODNAME
-----------------------------cot
7
SQL> select sname from sstud2 where marks > all ( select marks from sstud2 where dept='cse' );
no rows selected
SQL> select sname from sstud2 where marks < all ( select marks from sstud2 where dept='cse' );
SNAME
-------------------anand
ravi
SQL> select sname from sstud1 where exists ( select sstud2.sname from sstud2 where
sstud1.sname=sstud2.sname );
SNAME
-------------------prajan
anand
ravi
SQL> select sname from sstud1 where not exists ( select sstud2.sname from sstud2 where
sstud1.sname=sstud2.sname );
SNAME
-------------------kumar
DEPT_NO
Haritha
AJay
Gowri
AJay
Lalitha
Gowtham
Kowsalya
Gowtham
10 rows selected.
SQL> select * from emp_manager where manager = 'AJay';
EMP_NAME
MANAGER
-------------------- -------------------Abi
AJay
Haritha
AJay
Gowri
AJay
SYNONYMS
SQL> create synonym emp for employee;
Synonym created.
SQL> select * from emp;
EMP_NO EMP_NAME
PH_NO
---------- -------------------- ---------- ---------1 Abi
445566
101
2 Banu
586987
102
3 Chitra
4578126
103
4 Dhivya
5246642
104
5 Emy
4756895
105
6 Fredey
42563478
106
7 Haritha
4523689
101
8 Gowri
4425635
101
9 Lalitha
4563563
102
10 Kowsalya
4585695
102
DEPT_NO
10 rows selected.
SQL> select * from emp where dept_no = '102';
EMP_NO EMP_NAME
PH_NO DEPT_NO
---------- -------------------- ---------- ---------2 Banu
586987
102
9 Lalitha
4563563
102
10 Kowsalya
4585695
102
SEQUENCES
SQL> create sequence order_no_seq
2 increment by 1
3 start with 1
4 maxvalue 100
5 minvalue 1
6 cycle
11
7 cache 10;
Sequence created.
SQL> create table sales_order(order_number number(4) primary key, order_amt number(5));
Table created.
SQL> insert into sales_order values(order_no_seq.nextval, 235);
1 row created.
SQL> select * from sales_order;
ORDER_NUMBER ORDER_AMT
------------ ---------1
235
After inserting 5 records
SQL> select * from sales_order;
ORDER_NUMBER ORDER_AMT
------------ ---------1
235
2
450
3
528
4
234
5
780
INDEXES:
SQL> create index manager on department (manager);
Index created.
SAVEPOINT
SQL> savepoint s1;
Savepoint created.
SQL> delete from employee where dept_no = '102';
3 rows deleted
SQL> select * from employee;
EMP_NO EMP_NAME
PH_NO
---------- -------------------- ---------- ---------1 Abi
445566
101
3 Chitra
4578126
103
4 Dhivya
5246642
104
5 Emy
4756895
105
6 Fredey
42563478
106
7 Haritha
4523689
101
8 Gowri
4425635
101
7 rows selected.
SQL> roll back s1;
Rollback complete.
DEPT_NO
12
DEPT_NO
10 rows selected.
13
Name
Null?
Type
..
EMP_ID
NUMBER(5)
DEPT_ID
NUMBER(5)
BASIC
NUMBER(7,2)
DEDUCTIONS
NUMBER(5,2)
ADDITIONS
NUMBER(5,2)
DOJ
DATE
Different Data Sets:
SQL>insert into paydeatils values(&emp_id,&dept_id,&basic,&deductions,&additions,&doj);
SQL>select * from paydeatils;
EMP_ID DEPT_ID BASIC DEDUCTIONS
ADDITIONS DOJ
..
10
101
25023.12 43.09
71.23 08-JAN-93
21
100
10500.29 23.98
40.9
01-JAN-06
30
102
6500.5
30.54
15
06-JUL-97
39
103
9700.45
32.78
65.09 08-AUG-03
87
104
15000
97.66
154.8 24-SEP-04
SQL>create table payroll(emp_id number(5)references employee(emp_id),pay_date date);
SQL>desc payroll;
Name
Null? Type
..
EMP_ID
NUMBER(5)
PAY_DATE
DATE
SQL>insert into payroll values(&emp_id,&date);
SQL>select * from payroll;
EMP_ID
PAY_DATE
.
10
31-JAN-06
21
03-FEB-06
30
15-JAN-06
39
27-JAN-06
87
04-FEB-06
c) List the employee details department wise
SQL>select empid,deptid from paydet;
EMPID DEPTID
15
401
402
403
404
405
500
200
600
400
1200
d)
List all the employee names who joined after particular date
AVINASH
NITIN
PHALGUN
e) List the details of employees whose basic salary is between 10,000 and 20,000
sqL> Select empid,empname from employee where salary between 10000 and 20000;
EMPID EMPNAME
.
402
AKHILA
403
aaaaaaaa
EMPID EMPNAME
.
AKHILA
f) Give a count of how many employees are working in each department
SQL>select count(empid),deptid from paydet group by deptid;
COUNT (EMPID)
DEPTID
1
200
1
400
1
500
1
600
1
1200
g) Give a names of the employees whose netsalary>10,000
SQL> select empname from employee where empid in(select empid from paydet where basicdeduction>10000);
EMPNAME
16
AVINASH
AKHILA
HARISH
NITIN
PHALGUN
h) List the details for an employee_id=5
SQL> select * from employee where empid=5;
EMPID
EMPNAME
-----------------------------------------5
Coulthard
DOMAIN INTEGRITY CONSTRAINTS
NOT NULL CONSTRAINT
SQL> create table empl (ename varchar2(30) not null, eid varchar2(20) not null);
Table created.
SQL> insert into empl values ('abcde',11);
1 row created.
SQL> insert into empl values ('fghij',12);
1 row created.
SQL> insert into empl values ('klmno',null);
insert into empl values ('klmno',null)
*
ERROR at line 1:
ORA-01400: cannot insert NULL into ("ITA"."EMPL"."EID")
SQL> select * from empl;
ENAME
EID
------------------------------ -------------------abcde
11
fghij
12
CHECK AS A COLUMN CONSTRAINT
SQL> create table depts ( dname varchar2(30) not null, did number(20) not null check
(did<10000));
Table created.
SQL> insert into depts values ('sales ',9876);
1 row created.
SQL> insert into depts values ('marketing',5432);
1 row created.
SQL> insert into depts values ('accounts',789645);
insert into depts values ('accounts',789645)
*
ERROR at line 1:
ORA-02290: check constraint (ITA.SYS_C003179) violated
17
fairy tales
bedtime stories
1000
1001
19
20
ALTER TABLE
SQL> alter table semp add(eddress varchar2(20));
Table altered.
SQL> update semp set eddress='10 gandhi road' where dno=11;
1 row updated.
SQL> update semp set eddress='12 m.g. road' where dno=22;
1 row updated.
SQL> select * from semp;
ENAME
DNO EDDRESS
-------------------- --------- -------------------x
11
10 gandhi road
y
22
12 m.g. road
SQL> select city, ename from depts, s2emp where depts.dno = s2emp.dno;
CITY
ENAME
-------------------- -------------------chennai
x
hyderabad
y
21
mumbai
mumbai
ENO ENAME
JOB
DNAME LOC
---------- ---------- ---------- ---------- ---------444 madhu
engineer HR
mumbai
12 rows selected.
6. Outer Join:
6.1 Left Outer Join:
SQL> select eno,ename,job,dname,loc from emp2 e left outer join dept d on(e.dno=
d.dno);
(OR)
SQL> select eno,ename,job,dname,loc from emp2 e,dept d where e.dno=d.dno(+);
ENO ENAME
JOB
DNAME LOC
---------- ---------- ---------- ---------- ---------333 jagan
manager inventory hyd
111 saketh analyst inventory hyd
222 sandeep clerk
finance bglr
444 madhu
engineer
6.2 Right Outer Join:
SQL> select eno,ename,job,dname,loc from emp2 e right outer join dept d on(e.dno
=d.dno);
(OR)
SQL> select eno,ename,job,dname,loc from emp2 e,dept d where e.dno(+)=d.dno;
ENO ENAME
JOB
DNAME LOC
---------- ---------- ---------- ---------- ---------111 saketh analyst inventory hyd
222 sandeep clerk
finance bglr
333 jagan
manager inventory hyd
HR
mumbai
6.3 Full Outer Join:
SQL> select eno,ename,job,dname,loc from emp2 e full outer join dept d on(e.dno=
d.dno);
ENO ENAME
JOB
DNAME LOC
---------- ---------- ---------- ---------- ---------333 jagan
manager inventory hyd
111 saketh analyst inventory hyd
222 sandeep clerk
finance bglr
444 madhu
engineer
HR
Mumbai
24
6.
AGE
26
15 /
Enter value for a_no: 1
old 7: a_no:=&a_no;
new 7: a_no:=1;
PL/SQL procedure successfully completed.
SQL> select * from saccount;
ACCNO NAME
BAL
--------- -------------------- --------1 mala
18000
2 kala
30000
TO CREATE TABLE SROUTES
SQL> create table sroutes ( rno number(5), origin varchar2(20), destination varchar2(20), fare
numbe
r(10), distance number(10));
Table created.
SQL> insert into sroutes values ( 2, 'chennai', 'dindugal', 400,230);
1 row created.
SQL> insert into sroutes values ( 3, 'chennai', 'madurai', 250,300);
1 row created.
SQL> insert into sroutes values ( 6, 'thanjavur', 'palani', 350,370);
1 row created.
SQL> select * from sroutes;
RNO ORIGIN
DESTINATION
FARE DISTANCE
--------- -------------------- -------------------- --------- --------2 chennai
dindugal
400
230
3 chennai
madurai
250
300
6 thanjavur
palani
350
370
SQL> set serveroutput on;
SQL> declare
2 route sroutes.rno % type;
3 fares sroutes.fare % type;
4 dist sroutes.distance % type;
5 begin
6 route:=&route;
7 select fare, distance into fares , dist from sroutes where rno=route;
8 if (dist < 250) then
9 update sroutes set fare=300 where rno=route;
10 else if dist between 250 and 370 then
11 update sroutes set fare=400 where rno=route;
12 else if (dist > 400) then
13 dbms_output.put_line('Sorry');
14 end if;
15 end if;
16 end if;
17 end;
18 /
Enter value for route: 3
29
old 6: route:=&route;
new 6: route:=3;
PL/SQL procedure successfully completed.
SQL> select * from sroutes;
RNO ORIGIN
DESTINATION
FARE DISTANCE
--------- -------------------- -------------------- --------- --------2 chennai
dindugal
400
230
3 chennai
madurai
400
300
6 thanjavur
palani
350
370
30
31
6
n_id
employee1.id%TYPE;
7
v_first_name
employee1.first_name%TYPE;
8
v_last_name
employee1.last_name%TYPE;
9
v_middle_name
employee1.middle_name%TYPE;
10 v_name
employee1.name%TYPE;
11
12 begin
13
v_first_name := 'JOHN';
14
v_middle_name := 'J.';
15
v_last_name := 'DOUGH';
16
v_name
:= rtrim(v_last_name||', '||v_first_name||' '||v_middle_name);
17
d_birth_date := to_date('19800101', 'YYYYMMDD');
18
19
begin
20
select id into n_gender_id from gender where code = 'M';
21
exception
22
when OTHERS then
23
raise_application_error(-20001, SQLERRM||' on select gender');
24
end;
25
26
begin
27
select id
28
into n_id
29
from employee1
30
where name
= v_name
31
and birth_date = d_birth_date
32
and gender_id = n_gender_id;
33
34
n_selected := sql%rowcount;
35
exception
36
when NO_DATA_FOUND then
37
n_selected := sql%rowcount;
38
DBMS_OUTPUT.PUT_LINE('Caught raised exception NO_DATA_FOUND');
39
when OTHERS then
40
raise_application_error(-20002, SQLERRM||' on select employee');
41
end;
42
43
DBMS_OUTPUT.PUT_LINE(to_char(n_selected)||' row(s) selected.');
44 end;
45 /
Caught raised exception NO_DATA_FOUND
0 row(s) selected.
PL/SQL procedure successfully completed.
33
9. Creation of Procedures.
Creating the table ititems and displaying the contents
SQL> create table ititems(itemid number(3), actualprice number(5), ordid number(4), prodid
number(4));
Table created.
SQL> insert into ititems values(101, 2000, 500, 201);
1 row created.
SQL> insert into ititems values(102, 3000, 1600, 202);
1 row created.
SQL> insert into ititems values(103, 4000, 600, 202);
1 row created.
SQL> select * from ititems;
ITEMID ACTUALPRICE ORDID PRODID
34
--------101
102
103
----------2000
3000
4000
-------500
1600
600
--------201
202
202
ORDID
--------500
1600
600
PRODID
--------201
202
202
7 end if;
8 end;
9 /
Procedure created.
SQL> exec yyy(103);
Actual price is 4000
PL/SQL procedure successfully completed.
Procedure For Out Parameter Creation, Execution
SQL> set serveroutput on;
SQL> create procedure zzz (a in number, b out number) is identity number;
2 begin
3 select ordid into identity from ititems where itemid=a;
4 if identity<1000 then
5 b:=100;
6 end if;
7 end;
8 /
Procedure created.
SQL> declare
2 a number;
3 b number;
4 begin
5 zzz(101,b);
6 dbms_output.put_line('The value of b is '|| b);
7 end;
8 /
The value of b is 100
PL/SQL procedure successfully completed.
Procedure For Inout Parameter Creation, Execution
SQL> create procedure itit ( a in out number) is
2 begin
3 a:=a+1;
4 end;
5 /
Procedure created.
SQL> declare
2 a number:=7;
3 begin
4 itit(a);
5 dbms_output.put_line(The updated value is ||a);
36
6 end;
7 /
The updated value is 8
PL/SQL procedure successfully completed.
37