0% found this document useful (0 votes)
51 views

Dbms Printing Content

1. The document describes various SQL commands used to create, manipulate and retrieve data from database tables. 2. Commands like create, insert, select, update, delete etc are demonstrated on sample tables with columns like name, roll number etc. 3. Conditions are applied using where clause, logical and relational operators to retrieve selective records from tables.

Uploaded by

hello
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views

Dbms Printing Content

1. The document describes various SQL commands used to create, manipulate and retrieve data from database tables. 2. Commands like create, insert, select, update, delete etc are demonstrated on sample tables with columns like name, roll number etc. 3. Conditions are applied using where clause, logical and relational operators to retrieve selective records from tables.

Uploaded by

hello
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 36

K.

Ramakrishnan College of Engineering, Trichy

EX. No.:1
DATA DEFINITION COMMANDS, DATA MANIPULATION COMMANDS FOR INSERTING,
DELETING, UPDATING AND RETRIEVING TABLES AND TRANSACTION CONTROL
STATEMENTS
COMMANDS
SQL> create table stud (sname varchar2(30), sid varchar2(10), sage number(2), sarea
varchar2(20));
Table created. SQL> desc stud;
Name Null? Type
----------------------------------------------------- -------- --------------------------------
SNAME VARCHAR2(30)
SID VARCHAR2(10)
SAGE NUMBER(2)
SAREA VARCHAR2(20)
SQL>alter table stud modify ( sage number(10)); Table altered.
SQL> alter table stud add ( sdept varchar2(20)); Table altered.
SQL> desc stud;
Name Null? Type
----------------------------------------------------- -------- --------------------------------
SNAME VARCHAR2(30)
SID VARCHAR2(10)
SAGE NUMBER(10)
SAREA VARCHAR2(20)
SDEPT VARCHAR2(20)
SQL> alter table stud drop ( sdept varchar2(20)); Table altered.
SQL> desc studs;
Name Null? Type
----------------------------------------------------- -------- ------------------------------------
SNAME VARCHAR2(30)
SID VARCHAR2(10)
SAGE NUMBER(10)
SAREA VARCHAR2(20)
SQL> truncate table studs; Table truncated.
SQL> desc studs;
Name Null? Type
----------------------------------------------------- -------- ------------------------------------
SNAME VARCHAR2(30)
SID VARCHAR2(10)
SAGE NUMBER(10)
SAREA VARCHAR2(20)
SDEPT VARCHAR2(20)
Department of Computer Science and Engineering Page No.
K. Ramakrishnan College of Engineering, Trichy

SQL> drop table studs; Table dropped.


DML AND TCL COMMANDS
SQL> create table tb43 (rno number(2),name varchar(10));
Table created;
SQL>insert into tb43 values(2,‟kavi‟);
1 row created.
SQL>insert into tb43 values(3,‟prami‟);
1 row created.
SQL>insert into tb43 values(4,‟geetha‟);
1 row created.
SQL> select * from tb43;
RNO NAME
---------------------------------------------------
2 kavi
3 prami
4 geetha
SQL> commit;
Commit complete.
SQL>insert into tb43 values(5,‟indhu‟);
1 row created.
SQL> select * from tb43;
RNO NAME
-----------------------------------------------------------
2 kavi
3 prami
4 geetha
5 indhu
SQL>rollback;
Rollback complete;
SQL> select * from tb43;
RNO NAME
-----------------------------------------------------
2 kavi
3 prami
4 geetha
SQL>save point s1;
Save point created;
SQL> delete * from tb43 where rno=3;
1 row deleted.
SQL> select * from tb43;

Department of Computer Science and Engineering Page No.


K. Ramakrishnan College of Engineering, Trichy

RNO NAME
--------------------------------------------------------
2 kavi
3 prami
SQL> insert into tb43 values(7,‟balu‟);
1 row created.
SQL> select * from tb43;
RNO NAME
------------------------------------------------------
2 kavi
3 prami
7 balu
SQL> rollback to s1;
Rollback complete;

SQL> select * from tb43;


RNO NAME
------------------------------------------------------
2 kavi
3 prami

SQL> commit;
Committed.

Department of Computer Science and Engineering Page No.


K. Ramakrishnan College of Engineering, Trichy

EX.NO-2

PERFORMING INSERTION, DELETION, MODIFYING, ALTERING, UPDATING AND VIEWING


RECORDS BASED ON CONDITIONS
commands:
SQL>create table stud (sname varchar2(30), sid varchar2(10), sage number(10), sarea
varchar2(20), sdept varchar2(20));
Table created.
SQL> insert into stud values ('ashwin',101,19,'anna nagar','aeronautical');
1 row created.
SQL> insert into stud values ('bhavesh',102,18,'nungambakkam','marine');
1 row created.
SQL> insert into stud values ('pruthvik',103,20,'anna nagar','aerospace');
1 row created.
SQL> insert into stud values ('charith',104,20,'kilpauk','mechanical'); 1 row created.
SQL> select * from stud;
SNAME SID SAGE SAREA SDEPT
------------------------------ ---------- --------- -------------------- --------------------
ashwin 101 19 anna nagar aeronautical
bhavesh 102 18 nungambakkam marine
pruthvik 103 20 anna nagar aerospace
charith 104 20 kilpauk mechanical
RENAMING THE TABLE ‘STUD’
SQL> rename stud to studs;
Table renamed.
ARITHMETIC OPERATION
SQL> select sname, sid+100 "stid" from
studs; SNAME stid
------------------------------ ---------
Ashwin 201
Bhavesh 202
Pruthvik 203
charith 204

CONCATENATION OPERATOR
SQL> select sname || ' is a ' || sdept || ' engineer. ' AS "PROFESSION" from studs;
PROFESSION
-------------------------------------------------------------------
ashwin is a aeronautical engineer.

Department of Computer Science and Engineering Page No.


K. Ramakrishnan College of Engineering, Trichy

bhavesh is a marine engineer. pruthvik is a


aerospace engineer.
charith is a mechanical engineer.
DISPLAY ONLY DISTINCT VALUES
SQL> select distinct sarea from studs; SAREA
--------------------
anna nagar kilpauk
nungambakkam

USING THE WHERE CLAUSE


SQL> select sname,sage from studs where sage<=19;
SNAME SAGE
------------------------------ ---------
ashwin 19
bhavesh 18

BETWEEN OPERATOR
SQL> select sname,sarea, sid from studs where sid between 102 and 104;
SNAME SAREA SID
------------------------------ -------------------- ----------
bhavesh nungambakkam 102
pruthvik anna nagar 103
charith kilpauk 104
IN PREDICATE
SQL> select sname,sarea , sid from studs where sid in(102,104);
SNAME SAREA SID
------------------------------ -------------------- ----------
bhavesh nungambakkam 102
charith kilpauk 104
PATTERN MATCHING
SQL> select sname, sarea from studs where sarea like '%g%';
SNAME SAREA
------------------------------ --------------------
ashwin anna nagar
bhavesh nungambakkam
pruthvik anna nagar

Department of Computer Science and Engineering Page No.


K. Ramakrishnan College of Engineering, Trichy

LOGICAL AND OPERATOR


SQL> select sname ,sid from studs where sid>102 and sarea='anna nagar';
SNAME SID
------------------------------ ----------
pruthvik 103

LOGICAL OR OPERATOR
SQL> select sname ,sid from studs where sid>102 or sarea='anna nagar';
SNAME SID
------------------------------ ----------
ashwin 101
pruthvik 103
charith 104
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;

Department of Computer Science and Engineering Page No.


K. Ramakrishnan College of Engineering, Trichy

---------
400
SQL> select min(spocket) result from studs; RESULT
--------------------
100
SQL> select count(spocket) result from studs;
---------
4
SQL> select count(*) result from studs; RESULT
---------
4
SQL> select count(spocket) result from studs where sarea='anna nagar'; RESULT
---------
2
SQL> select max(spocket) result from studs; RESULT
--------------------
750
SQL> select sum(spocket) result from studs;
RESULT
---------
1600

NUMERIC FUNCTIONS
SQL> select abs(-20) result from dual;
---------
20
SQL> select power (2,10) result from dual;
---------
1024
SQL> select round(15.359,2) result from dual;
---------
15.36
SQL> select sqrt (36) result from dual; RESULT
---------
6

STRING FUNCTIONS
SQL> select lower('ORACLE') result from dual; RESULT

Department of Computer Science and Engineering Page No.


K. Ramakrishnan College of Engineering, Trichy

------
oracle
SQL> select upper('oracle') 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
--------- ---------
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

Department of Computer Science and Engineering Page No.


K. Ramakrishnan College of Engineering, Trichy

DELETION
SQL> delete from studs where sid=101;
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> insert into product values('table',10001);
1 row created.
SQL> insert into product values('chair',10010);
1 row created.
SQL> insert into product values('desk',10110);
1 row created.
SQL> insert into product values('cot',11110);
1 row created.
SQL> insert into product values('sofa',10010);
1 row created.
SQL> insert into product values('tvstand',11010);
1 row 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.

Department of Computer Science and Engineering Page No.


K. Ramakrishnan College of Engineering, Trichy

SQL> insert into sale values('table',801,10001);


1 row created.
SQL> insert into sale values('chair',805,10010);
1 row created.
SQL> insert into sale values('desk',809,10110);
1 row created.
SQL> insert into sale values('cot',813,11110);
1 row created.
SQL> insert into sale values('sofa',817,10010);
1 row 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

CREATING TABLES FOR DOING JOIN AND NESTED QUERY OPERATIONS


SQL> create table sstud1 ( sname varchar2(20) , place varchar2(20));
Table created.
SQL> insert into sstud1 values ( 'prajan','chennai');
1 row created.
SQL> insert into sstud1 values ( 'anand','chennai');
1 row created.

Department of Computer Science and Engineering Page No.


K. Ramakrishnan College of Engineering, Trichy

SQL> insert into sstud1 values ( 'kumar','chennai');


1 row created.
SQL> insert into sstud1 values ( 'ravi','chennai');
1 row created.
SQL> select * from sstud1;
SNAME PLACE
-------------------- --------------------
prajan chennai
anand chennai
kumar chennai
ravi chennai
TO CREATE SSTUD2 TABLE
SQL> create table sstud2 ( sname varchar2(20), dept varchar2(10), marks number(10));
Table created.
SQL> insert into sstud2 values ('prajan','cse',700);
1 row created.
SQL> insert into sstud2 values ('anand','it',650);
1 row created.
SQL> insert into sstud2 values ('vasu','cse',680);
1 row created.
SQL> insert into sstud2 values ('ravi','it',600);
1 row created.
SQL> select * from sstud2;
SNAME DEPT MARKS
-------------------- ---------- ---------
prajan cse 700
anand it 650
vasu cse 680
ravi it 600
JOIN OPERATIONS
SQL> select sstud1.sname, dept from sstud1 inner join sstud2 on ( sstud1.sname= sstud2.sname);
SNAME DEPT
-------------------- ----------
anand it
prajan cse
ravi it
SQL> select sstud1.sname, dept from sstud1 join sstud2 on ( sstud1.sname= sstud2.sname);
SNAME DEPT
Department of Computer Science and Engineering Page No.
K. Ramakrishnan College of Engineering, Trichy

-------------------- ----------
anand it
prajan cse
ravi it
SQL> select sstud1.sname, dept from sstud1 left outer join sstud2 on ( sstud1.sname= sstud2.sname);
SNAME DEPT
-------------------- ----------
prajan cse
anand it
ravi it
kumar cse
SQL> select sstud1.sname, dept from sstud1 right outer join sstud2 on ( sstud1.sname= sstud2.sname)
SNAME DEPT
-------------------- ----------
prajan cse
anand it
ravi it
SQL> select sstud1.sname, dept from sstud1 full outer join sstud2 on ( sstud1.sname= sstud2.sname);
SNAME DEPT
-------------------- ----------
prajan cse
anand it
ravi it
kumar cse

NESTED QUERIES
SQL> select sname from sstud1 where sstud1.sname in ( select sstud2.sname from 2 sstud2 );
SNAME
--------------------
anand
prajan
ravi
SQL> select sname from sstud1 where sstud1.sname not in ( select sstud2.sname from sstud2);
SNAME
--------------------
kumar
SQL> select sname from sstud2 where marks > some(select marks from sstud2 2 where
dept='cse');

Department of Computer Science and Engineering Page No.


K. Ramakrishnan College of Engineering, Trichy

SNAME
--------------------
prajan
SQL> select sname from sstud2 where marks >= some (select marks from sstud2 2 where
dept='cse' );
SNAME
--------------------
prajan vasu
SQL> select sname from sstud2 where marks > any ( select marks from sstud2 where dept='cse' );
SNAME
--------------------
prajan
SQL> select sname from sstud2 where marks >= any ( select marks from sstud2 2 where
dept='cse' );
SNAME
--------------------
prajan vasu
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 2 where
sstud1.sname=sstud2.sname );
SNAME
--------------------
prajan anand ravi
SQL> select sname from sstud1 where not exists ( select sstud2.sname from 2 sstud2
where sstud1.sname=sstud2.sname );

SNAME
--------------------
kumar

Department of Computer Science and Engineering Page No.


K. Ramakrishnan College of Engineering, Trichy

EX.NO-3
VIEWS, SEQUENCES, SYNONYMS
COMMANDS:

SQL>create table kavi1(name varchar(20),rno number(5),marks number(5),primary key (rno));


Table created.
SQL> desc kavi1;
NAME NULL? TYPE
-------------------------------------------------------------------------------------------------
NAME VARCHAR (20)
RNO NOTNULL NUMBER (5)
MARKS NUMBER (5)
SQL> insert into kavi1 values („prami‟, 1001, 350); 1 row created.
SQL> insert into kavi1 values („kavi‟, 1002, 298); 1 row created.
SQL> insert into kavi1 values („indhu‟, 1003, 375); 1 row created.
SQL> select * from kavi1;
NAME RNO MARKS
------------------------------------------------------------------------------------------ prami
1001 350
kavi 1002 298
indhu 1003 375
SQL> create table kavi2 (rollno number (5), attendance number (5), primary key (roll no));
Table created.
SQL> desc kavi2;

NAME NULL? TYPE


-----------------------------------------------------------------------------------
ROLLNO NOTNULL NUMBER (5)
ATTENDENCE NUMBER (5)
SQL> insert into kavi2 values (1001, 90);
1 row created.
SQL> insert into kavi2 values (1002, 75);
1 row created.
SQL> insert into kavi2 values (1003, 85);
1 row created.
SQL> select * from kavi2;
ROLLNO ATTENDENCE
-----------------------------------------------------------------------------------------
1001 90
1002 75
1003 85

Department of Computer Science and Engineering Page No.


K. Ramakrishnan College of Engineering, Trichy

SQL> create view kaviview as select name, rno, marks, attendance from kavi1, kavi2 where rollno=rno; view
created.
SQL> select * from kaviview;

NAME RNO MARKS ATTENDENCE


---------------------------------------------------------------------------------------------------------------------
prami 1001 350 90 kavi
1002 298 75 indhu 1003
375 85
SQL> update kaviview set name=‟kavi‟ where name=‟priya‟;
1 row updated.
SQL> update kaviview set marks=352 where name=‟prami‟;
1 row updated;
SQL> select * from kaviview;
NAME RNO MARKS ATTENDENCE
---------------------------------------------------------------------------------------------------------------------
prami 1001 352 90
priya 1002 298 75
indhu 1003 375 85

INDEX
SQL> create table persons (first name varchar (20), last name varchar(10));
Table created;
SQL> create index plndex on persons (last name); Index created.
SQL> select * from persons; No rows selected.
SQL> drop plndex on persons;
Drop index plndex on persons
*
ERROR at line1:
ORA_00950: Invalid DROP option.

SEQUENCE
SQL> create table supplier2 (supplierid number (10), supplier name varchar (20));
Table created.
SQL>insert into supplier2 values (10,‟ganesh‟);
1 row created.
SQL> select * from supplier2;
SUPPLIER ID SUPPLIERNAME
--------------------------------------------------------------------------------
10 ganesh
SQL> create sequence supplier2_seq2
2 MINVALUE 1
3 STARTWITH 1
4 INCREMENT BY 1
5 CACHE 20
Department of Computer Science and Engineering Page No.
K. Ramakrishnan College of Engineering, Trichy

Sequence created.

SQL> insert into supplier2 (supplier id, supplier name) values


(supplier2_seq.NEXTVAL,‟Kraftfoods‟);
1 row created.
SQL> select*from supplier2;
SUPPLIERID SUPPLIERNAME
----------------------------------------------------------------------------------------
10 ganesh
1 Kraftfoods
SQL> insert into supplier2 (supplier id, supplier name) values
(supplier2_seq.NEXTVAL,‟parle‟);
1 row created.
SQL> select*from supplier2;
SUPPLIERID SUPPLIERNAME
------------------------------------------------------------------------------------
10 ganesh
1 Kraft foods
2 Parley
SQL> select*from supplier2_seq2*; Error at line1:
ORA-00942: table or view does not exist;

SYNONYMS
create public synonym suppliers for app.suppliers;
Synonym created.

Department of Computer Science and Engineering Page No.


K. Ramakrishnan College of Engineering, Trichy

EX.NO-4
DATABASE PROGRAMMING: IMPLICIT AND EXPLICIT CURSORS

TO CREATE THE TABLE ‘SSEMPP’


SQL> create table ssempp( eid number(10), ename varchar2(20), job varchar2(20), sal

number (10),dnonumber(5));
Table created.
SQL> insert into ssempp values(1,'nala','lecturer',34000,11);
1 row created.
SQL> insert into ssempp values(2,'kala',' seniorlecturer',20000,12);
1 row created.
SQL> insert into ssempp values(5,'ajay','lecturer',30000,11);

1 row created.
SQL> insert into ssempp values(6,'vijay','lecturer',18000,11);
1 row created.
SQL> insert into ssempp values(3,'nila','professor',60000,12);
1 row created.

EID ENAME JOB SAL DNO


--------- -------------------- -------------------- --------- ---------
1 nala lecturer 34000 11
2 kala seniorlecturer 20000 12
5 ajay lecturer 30000 11
6 vijay lecturer 18000 11
3 nila professor 60000 12

SQL> set serveroutput on;


SQL> declare
2 begin
3 for emy in (select eid,ename from ssempp)
4 loop
5 dbms_output.put_line('Employee id and employee name are '|| emy.eid „and‟|| emy.ename);
6 end loop;
7 end;
8/
Employee id and employee name are 1 and nala
Employee id and employee name are 2 and kala
Employee id and employee name are 5 and ajay
Department of Computer Science and Engineering Page No.
K. Ramakrishnan College of Engineering, Trichy

Employee id and employee name are 6 and vijay


Employee id and employee name are 3 and nila
PL/SQL procedure successfully completed.

TO WRITE A PL/SQL BLOCK TO UPDATE THE SALARY OF ALL EMPLOYEES WHERE


DEPARTMENT NO IS 11 BY 5000 USING CURSOR FOR LOOP AND TO DISPLAY THE UPDATED
TABLE

SQL> set serveroutput on;


SQL> declare
2 cursor cem is select eid,ename,sal,dno from ssempp where dno=11;
3 begin
4 --open cem;
5 for rem in cem
6 loop
7 update ssempp set sal=rem.sal+5000 where eid=rem.eid;
8 end loop;

9 --close cem;

10 end;
11 /
PL/SQL procedure successfully completed.

EID ENAME JOB SAL DNO

1 nala lecturer 39000 11


2 kala seniorlecturer 20000 12

TO WRITE A PL/SQL BLOCK TO DISPLAY THE EMPLOYEE ID AND EMPLOYEE NAME WHERE
DEPARTMENT NUMBER IS 11 USING EXPLICIT CURSORS
1 declare
2 cursor cenl is select eid,sal from ssempp where dno=11;
3 ecode ssempp.eid%type;
4 esal empp.sal%type;
5 begin
6 open cenl;
7 loop
8 fetch cenl into ecode,esal;
9 exit when cenl%notfound;
10 dbms_output.put_line(' Employee code and employee salary are' || ecode „and‟|| esal);
11 end loop;

Department of Computer Science and Engineering Page No.


K. Ramakrishnan College of Engineering, Trichy

12 close cenl;
13 * end;
14 SQL>/
PL/SQL procedure successfully completed.

TO WRITE A PL/SQL BLOCK TO UPDATE THE SALARY BY 5000 WHERE THE JOB IS LECTURER ,
TO CHECK IF UPDATES ARE MADE USING IMPLICIT CURSORS AND TO DISPLAY THE
UPDATED TABLE

SQL> declare
2 county number;
3 begin

4 update ssempp set sal=sal+10000 where job='lecturer';

5 county:= sql%rowcount;
6 if county > 0 then
7 dbms_output.put_line('The number of rows are '|| county);
8 end if;

9 if sql %found then


10 dbms_output.put_line('Employee record modification successful');
11 else if sql%notfound then
12 dbms_output.put_line('Employee record is not found');
13 end if;
14 end if;
15 end;
/
PL/SQL procedure successfully completed.
The number of rows are 3
Employee record modification successful
PL/SQL procedure successfully completed.

EID ENAME JOB SAL DNO


--------- -------------- -------------------- --------- ---------
1 nala lecturer 44000 11
2 kala seniorlecturer 20000 12
5 ajay lecturer 40000 11
6 vijay lecturer 28000 11
3 nila professor 60000 12

Department of Computer Science and Engineering Page No.


K. Ramakrishnan College of Engineering, Trichy

EX.NO-5
PROCEDURES AND FUNCTIONS
COMMANDS:
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 ORDID PRODID ACTUALPRICE


--------- --------- ------- ---------
-
101 2000 500 201
102 3000 1600 202
103 4000 600 202

SQL> create procedure itsum(identity number, total number) is price number;


2 null_price exception;
3 begin
4 select actualprice into price from ititems where itemid=identity;
5 if price is null then
6 raise null_price;
7 else
8 update ititems set actualprice=actualprice+total where itemid=identity;
9 end if;
10 exception
11 when null_price then
12 dbms_output.put_line('price is null');
13 end;
14 /
Procedure created.

SQL> exec itsum(101, 500);


PL/SQL procedure successfully completed.

Department of Computer Science and Engineering Page No.


K. Ramakrishnan College of Engineering, Trichy

SQL> select * from ititems;

ITEMID ACTUALPRICE ORDID PRODID


--------- ----------- --------- ---------
101 2500 500 201
102 3000 1600 202
103 4000 600 202

PROCEDURE FOR ‘IN’ PARAMETER – CREATION, EXECUTION


SQL> set serveroutput on;
SQL> create procedure yyy (a IN number) is price number;
2 begin
3 select actualprice into price from ititems where itemid=a;
4 dbms_output.put_line('Actual price is ' || price);
5 if price is null then
6 dbms_output.put_line('price is null');
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;

Department of Computer Science and Engineering Page No.


K. Ramakrishnan College of Engineering, Trichy

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);
6 end;
7/
The updated value is 8
PL/SQL procedure successfully completed.
CREATE THE TABLE ‘ITTRAIN’ TO BE USED FOR FUNCTIONS
SQL>create table ittrain ( tno number(10), tfare number(10));
Table created.
SQL>insert into ittrain values (1001, 550);

1 row created.
SQL>insert into ittrain values (1002, 600);
1 row created.
SQL>select * from ittrain;
TNO TFARE
--------- ------------
1001 550
1002 600
PROGRAM FOR FUNCTION AND IT’S EXECUTION
SQL> create function aaa (trainnumber number) return number is

2 trainfunction ittrain.tfare % type;


3 begin
4 select tfare into trainfunction from ittrain where tno=trainnumber;
5 return(trainfunction);
Department of Computer Science and Engineering Page No.
K. Ramakrishnan College of Engineering, Trichy

6 end;
7/
Function created.

SQL> set serveroutput on;


SQL> declare
2 total number;
3 begin
4 total:=aaa (1001);
5 dbms_output.put_line('Train fare is Rs. '||total);
6 end;
7/

Train fare is Rs.550


PL/SQL procedure successfully completed.
FACTORIAL OF A NUMBER USING FUNCTION — PROGRAM AND EXECUTION
SQL> create function itfact (a number) return number is
2 fact number:=1;
3 b number;
4 begin
5 b:=a;
6 while b>0
7 loop
8 fact:=fact*b;
9 b:=b-1;
10 end loop;
11 return(fact);
12 end;
13 /
Function created.

SQL> set serveroutput on;


SQL> declare
2 a number:=7;
3 f number(10);
4 begin
5 f:=itfact(a);
6 dbms_output.put_line(„The factorial of the given number is‟||f);
7 end;
8/

The factorial of the given number is 5040


Department of Computer Science and Engineering Page No.
K. Ramakrishnan College of Engineering, Trichy

EX.NO-6
TRIGGERS
COMMANDS:

SQL> create table itempls (ename varchar2(10), eid number(5), salary number(10));
Table created.
SQL> insert into itempls values('xxx',11,10000);
1 row created.
SQL> insert into itempls values('yyy',12,10500);
1 row created.
SQL> insert into itempls values('zzz',13,15500);
1 row created.
SQL> select * from itempls;
ENAME EID SALARY
---------- --------- ---------
xxx 11 10000
yyy 12 10500
zzz 13 15500

TO CREATE A SIMPLE TRIGGER THAT DOES NOT ALLOW INSERT UPDATE AND DELETE
OPERATIONS ON THE TABLE
SQL> create trigger ittrigg before insert or update or delete on itempls for each row
2 begin
3 raise_application_error(-20010,'You cannot do manipulation');
4 end;

5
6/
Trigger created.

TO DROP THE CREATED TRIGGER


SQL> drop trigger ittrigg;
Trigger dropped.

Department of Computer Science and Engineering Page No.


K. Ramakrishnan College of Engineering, Trichy

EX.NO-7
EXCEPTION HANDLING

CREATING THE TABLES ‘SPRODUCTMASTERS’ AND ‘SOLDPRICES’

SQL> create table sproductmasters( pno varchar2(10), sellprice number(10));


Table created.
SQL> insert into sproductmasters values('p1',3200);
1 row created.
SQL> insert into sproductmasters values('p2',4000);
1 row created.
SQL> insert into sproductmasters values('p3',6000);
1 row created.

SQL> select * from sproductmasters;


PNO SELLPRICE
---------- ---------
p1 3200
p2 4000
p3 6000
SQL> create table soldprices( pno varchar2(10), datechange varchar2(20),soldprices
number(10));
Table created.

PROGRAM
1 declare
2 sellingprice number(10,2);
3 begin
4 select sellprice into sellingprice from sproductmasters where pno='p1';
5 if sellingprice < 4000
6 then
7 goto add_old_price;
8 else
9 dbms_output.put_line(' Current price is '|| sellingprice);
10 end if;

Department of Computer Science and Engineering Page No.


K. Ramakrishnan College of Engineering, Trichy

11 <<add_old_price>>
12 update sproductmasters set sellprice = 4000 where pno='p1';
13 insert into soldprices values('p1',sysdate,sellingprice);
14 dbms_output.put_line(' The new price of p1 is 4000 ');
15 end;
16 /
The new price of p1 is 4000
PL/SQL procedure successfully completed

DISPLAYING THE CONTENTS OF ‘SOLDPRICES’ TABLE


SQL> select * from soldprices;
PNO DATECHANGE SOLDPRICES
---------- -------------------- ----------
p1 27-AUG-08 3200

TO CREATE THE TABLE ‘SSITEMS’ ON WHICH THE EXCEPTION HANDLING

MECHANISMS ARE GOING TO BE PERFORMED

SQL> create table ssitems( id number(10), quantity number(10), actualprice number(10));

Table created.

SQL> insert into ssitems values(100,5,5000);


1 row created.

SQL> insert into ssitems values(101,6,9000);


1 row created.

SQL> insert into ssitems values(102,4,4000);


1 row created.

SQL> insert into ssitems values(103,2,2000);


1 row created.
SQL> select * from ssitems;
ID QUANTITY ACTUALPRICE

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


100 5 5000
101 6 9000
102 4 4000

Department of Computer Science and Engineering Page No.


K. Ramakrishnan College of Engineering, Trichy

103 2 2000

PRE – DEFINED EXCEPTIONS

SQL> set serveroutput on; SQL> declare


2 price ssitems.actualprice % type;
3 begin
4 select actualprice into price from ssitems where quantity=10;
5 exception
6 when no_data_found then
7 dbms_output.put_line ('ssitems missing');
8 end;
9 9/
10 ssitems missin
PL/SQL procedure successfully completed
DISPLAYING THE UPDATED TABLE
SQL> select * from ssitems;
ID QUANTITY ACTUALPRICE

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


100 5 5000
101 6 9000
102 4 4000
103 2 2000

USER DEFINED EXCEPTONS


SQL> set serveroutput on;
SQL> declare
2 zero_price exception;
3 price number(8,2);

Department of Computer Science and Engineering Page No.


K. Ramakrishnan College of Engineering, Trichy

4 begin
5 select actualprice into price from ssitems where id=103;
6 if price=0 or price is null then
7 raise zero_price;
8 end if;
9 exception
10 when zero_price then
11 dbms_output.put_line('Failed zero price');
12 end;
13 /
PL/SQL procedure successfully completed.
DISPLAYING THE UPDATED TABLE
SQL> select * from ssitems;
ID QUANTITY ACTUALPRICE
--------- --------- -----------
100 5 5000
101 6 9000
102 4 4000
103 2 2000

Department of Computer Science and Engineering Page No.


K. Ramakrishnan College of Engineering, Trichy

EX.NO-9
DATABASE CONNECTIVITY WITH FRONT END TOOLS
PROGRAM:
SQL>create table emp(eno number primary key,enamr varchar(20),age number,addr varchar(20),DOB date,phno
number(10));
Table created.
SQL>create table salary(eno number,edesig varchar(10),basic number,da number,hra number,pf
number,mc number,met number,foreign key(eno) references emp); Table created.
TRIGGER to calculate DA,HRA,PF,MC

SQL> create or replace trigger employ


2 after insert on salary
3 declare
4 cursor cur is select eno,basic from salary;
5 begin
6 for cur1 in cur loop
7 update salary set
8 hra=basic*0.1,da=basic*0.07,pf=basic*0.05,mc=basic*0.03 where hra=0; 9 end loop;
10 end;
11 / Trigger created.
PROGRAM FOR FORM 1
Private Sub emp_Click() Form
2.Show End
Sub Private
Sub exit_Click()
Unload Me
End Sub Private
Sub salary_Click()

Department of Computer Science and Engineering Page No.


K. Ramakrishnan College of Engineering, Trichy

Form3.Show
End Sub
PROGRAM FOR FORM 2
Private Sub add_Click()
Adodc1.Recordset.AddNew MsgBox "Record added"
End Sub Private
Sub clear_Click()
Text1.Text = ""
Text2.Text = ""
Text3.Text = ""
Text4.Text = ""
Text5.Text = ""
Text6.Text = ""

End Sub Private Sub delte_Click() Adodc1.Recordset.Delete MsgBox


"Record Deleted" If Adodc1.Recordset.EOF = True

Then Adodc1.Recordset.MovePrevious
End If
End
Sub Private Sub exit_Click()
Unload Me
End Sub
Private Sub main_Click()
Form1.Show
End Sub
Private Sub modify_Click()
Adodc1.Recordset.Update
End Sub

Department of Computer Science and Engineering Page No.


K. Ramakrishnan College of Engineering, Trichy

PROGRAM FOR FORM 3


Private Sub add_Click()
Adodc1.Recordset.AddNew MsgBox "Record added"
End Sub
Private Sub
clear_Click()

Text1.Text = ""
Text2.Text = ""
Text3.Text = ""
Text4.Text = ""
Text5.Text = ""
Text6.Text = ""
End Sub
Private Sub delte_Click()
Adodc1.Recordset.Delete MsgBox "Record Deleted"
If Adodc1.Recordset.EOF = True
Then Adodc1.Recordset.MovePrevious
End If
End Sub
Private Sub exit_Click()
Unload Me
End Sub

Private Sub main_Click()


Form1.Show
End Sub
Private Sub

Department of Computer Science and Engineering Page No.


K. Ramakrishnan College of Engineering, Trichy

modify_Click()
Adodc1.Recordset.Update
End Sub
OUTPUT:

Department of Computer Science and Engineering Page No.


K. Ramakrishnan College of Engineering, Trichy

EX.NO-10
CASE STUDY USING REAL LIFE DATABASE APPLICATIONS
CREATE A TABLE IN ORACLE
SQL>create table account(cname varchar(20),accno number(10),balance number); Table Created
SQL> insert into account values('&cname',&accno,&balance);
Enter value for cname: Mathi
Enter value for accno: 1234
Enter value for balance: 10000
old 1: insert into account values('&cname',&accno,&balance)
new 1: insert into emp values('Mathi',1234,10000) 1 row created.
SOURCE CODE FOR FORM1
Private Sub ACCOUNT_Click()
Form2.Show
End Sub
Private Sub
EXIT_Click()
Unload Me
End Sub
Private Sub
TRANSACTION_Click()
Form3.Show
End Sub
SOURCE CODE FOR FORM 2
Private Sub CLEAR_Click()
Text1.Text = ""
Text2.Text = ""
Text3.Text = ""
End Sub
Private Sub
DELETE_Click()
Adodc1.Recordset.DELETE MsgBox "record deleted"
Adodc1.Recordset.MoveNext If Adodc1.Recordset.EOF = True Then
Adodc1.Recordset.MovePrevious
End If
End Sub
Private Sub EXIT_Click()
Unload Me
End Sub
Private Sub
Department of Computer Science and Engineering Page No.
K. Ramakrishnan College of Engineering, Trichy

HOME_Click()
Form1.Show
End Sub
Private Sub
INSERT_Click() Adodc1.Recordset.AddNew
End Sub
Private Sub
TRANSACTION_Click()
Form3.Show
End Sub
Private Sub UPDATE_Click() Adodc1.Recordset.UPDATE MsgBox "record updated
successfully"
End Sub
SOURCE CODE FOR FORM 3
Private Sub ACCOUNT_Click()
Form2.Show
End Sub
Private Sub CLEAR_Click()
Text1.Text = ""
Text2.Text = ""
End Sub
Private Sub
DEPOSIT_Click()
Dim s As String s = InputBox("enter the amount to be deposited")
Text2.Text = Val(Text2.Text) + Val(s) A = Text2.Text MsgBox "CURRENT BALANCE IS Rs" + Str(A)
Adodc1.Recordset.Save Adodc1.Recordset.UPDATE
Private Sub
EXIT_Click()
Unload Me
End Sub
Private Sub
HOME_Click()
Form1.Show End
Sub Private Sub
WITHDRAW_Click()
Dim s As String s = InputBox("enter the amount to be deleted")
Text2.Text = Val(Text2.Text) - Val(s) A = Text2.Text MsgBox "current balance is Rs" +
Str(A)
Adodc1.Recordset.Save
Adodc1.Recordset.UPDATE
End Sub

Department of Computer Science and Engineering Page No.


K. Ramakrishnan College of Engineering, Trichy

Department of Computer Science and Engineering Page No.


K. Ramakrishnan College of Engineering, Trichy

Department of Computer Science and Engineering Page No.

You might also like