Sei sulla pagina 1di 105

DATA DEFINITION LANGUAGE

The DDL commands are: Create Alter Drop Rename Truncate

CUSTOMER DETAILS TABLE STRUCTURE-1

FIELD NAME CUST_ID

TYPE NUMBER

SIZE 3 14 14 14 10

CONSTRAINTS -----------

CUST_FNAME ALPHANUMERIC CUST_LNAME ALPHANUMERIC CITY PHONENO ALPHANUMERIC NUMBER

1. Create command:SYNTAX:SQL> create table <table_name>(fieldname1 Datatype1, fieldname2 Datatype2,, fieldname n Datatype n); EXAMPLE:SQL> create table custo21(id varchar2(3),fname varchar2(14),lname varchar2(14),city varchar2(14),phn number(10)); Table created. TABLE DESCRIPTION:SQL> desc custo21; Name Null? Type ----------------------- -------- ---------------------------ID VARCHAR2(3) FNAME VARCHAR2(14) LNAME VARCHAR2(14) CITY VARCHAR2(14) PHN NUMBER(10)

EMPLOYEE DETAILS Create the employee table with the following fields and assign the employee id as primary key. TABLE STRUCTURE-2

FIELD NAME EMP_ID EMP_FNAME EMP_LNAME DOJ BASIC PAY CREATE COMMAND SYNTAX:-

TYPE ALPHANUMERIC ALPHANUMERIC ALPHANUMERIC DATE NUMBER

SIZE 3 14 14 --10,2

CONSTRAINTS PK ---------

SQL> create table <table_name>(fieldname1 Datatype1 constraints <constraint_name> primary key, fieldname2 Datatype2,, fieldname n Datatype n);

EXAMPLE:SQL> create table empl21(empid varchar2(2) constraints pkemp primary key,emp_fname varchar2(14),emp_lname varchar2(14),doj date,basic number(10,2)); Table created. TABLE DESCRIPTION:SQL> desc empl21; Name Null? Type --------------------------- -------- ---------------------------EMPID NOT NULL VARCHAR2(2) EMP_FNAME VARCHAR2(14) EMP_LNAME VARCHAR2(14) DOJ DATE BASIC NUMBER(10,2) 2. Alter command:-

a) ADD SYNTAX:SQl> Alter table<table_name>add(fieldname1 Datatype1(size),, fieldname n Datatype n(size)); EXAMPLE:SQL> alter table employeel add(address varchar2(15)); Table altered. TABLE DESCRIPTION:SQL> desc empl21; Name Null? Type --------------------------- -------- ---------------------------EMPID NOT NULL VARCHAR2(2) EMP_FNAME VARCHAR2(14) EMP_LNAME VARCHAR2(14) DOJ DATE BASIC NUMBER(10,2) ADDRESS VARCHAR2(15) b) ADD CONSTRAINTS SYNTAX:SQl> Alter table<table_name>add(fieldname1 Datatype1(size) constraints <constraint_name>check(field_name between val1 and val2,, fieldnamen Datatype n(size)); EXAMPLE:SQL> alter table custo21 add(age number(2) constraints ckage check(age between 18 and 60)); Table altered. C) MODIFY SYNTAX:-

SQl> Alter table<table_name>modify(fieldname1 Datatype1(size),, fieldname n Datatype n(size)); EXAMPLE:SQL> alter table empl21 modify(emp_fname varchar2(20)); Table altered. TABLE DESCRIPTION:SQL> desc empl21; Name Null? Type ----------------------------- -------- -------------------------EMPID NOT NULL VARCHAR2(2) EMP_FNAME VARCHAR2(20) EMP_LNAME VARCHAR2(14) DOJ DATE BASIC NUMBER(10,2) ADDRESS VARCHAR2(15)

CREATE VIEW SYNTAX:SQL> create view <view_name> as select * from <table_name>; EXAMPLE:SQL> create view empview as select * from empl21; View created. TABLE DESCRIPTION:SQL> desc empview; Name Null? Type

------------------------- -------- ---------------------------EMPID NOT NULL VARCHAR2(2) EMP_FNAME VARCHAR2(20) EMP_LNAME VARCHAR2(14) DOJ DATE BASIC NUMBER(10,2) ADDRESS VARCHAR2(15)

3. Drop Command
SYNTAX:SQL> drop view <view_name>; EXAMPLE:SQL> drop view empview; View dropped.

CREATE CHILD TABLE SYNTAX:SQL>create table <table_name>(fieldname1 Datatype1 constraints <constraint_name>references mastertab_name(pkfield_name), fieldname2 Datatype2,, fieldname n Datatype n); EXAMPLE:SQL> create table empl21_child(empid varchar2(2) constraints fkemp references empl21(empid) on delete cascade); Table created. TABLE DESCRIPTION:SQL> desc empl21_child;

Name Null? Type ----------------------------- -------- ---------------------------EMPID VARCHAR2(2) ALTER -ADD CONSTRAINTS SQL> alter table empl21 add(constraints ckbasic check(basic > 0)); Table altered. CREATE DUPLICATE TABLE SYNTAX:SQL> create table <newtab_name>as select * from <oldtab_name>; EXAMPLE-1:SQL> create table empl21_temp as select * from empl21; Table created.

TABLE DESCRIPTION:SQL> desc empl21_temp; Name Null? Type --------------------------- -------- ---------------------------EMPID VARCHAR2(2) EMP_FNAME VARCHAR2(20) EMP_LNAME VARCHAR2(14) DOJ DATE BASIC NUMBER(10,2) ADDRESS VARCHAR2(15) EXAMPLE-2:SQL> create table custo21temp as select * from custo21; Table created.

4. Truncate Command

SYNTAX:SQL> truncate table <table_name>; EXAMPLE:SQL> truncate table custo21temp; Table truncated.

TABLE DESCRIPTION :SQL> desc custo21temp; Name Null? Type ----------------------------------------- -------- ---------------------------ID VARCHAR2(3) FNAME VARCHAR2(14) LNAME VARCHAR2(14) CITY VARCHAR2(14) PHN NUMBER(10) AGE NUMBER(2)

5. Rename Command
SYNATX:SQL> rename <oldtab_name> to <newtab_name>; EXAMPLE:SQL> rename custo21 to customer21; Table renamed. TABLE DESCRIPTION:SQL> desc custo21; ERROR:

ORA-04043: object custo21 does not exist SQL> desc customer21; Name Null? Type ------------------------ -------- ---------------------------ID VARCHAR2(3) FNAME VARCHAR2(14) LNAME VARCHAR2(14) CITY VARCHAR2(14) PHN NUMBER(10) AGE NUMBER(2)

DROP COMMAND SYNTAX:SQL> alter table <table_name> drop column <col_name>; EXAMPLE:SQL> alter table empl21 add(desig varchar2(14)); Table altered. TABLE DESCRIPTION:SQL> desc empl21; Name Null? Type ----------------------------------------- -------- ---------------------------EMPID NOT NULL VARCHAR2(2) EMP_FNAME VARCHAR2(20) EMP_LNAME VARCHAR2(14) DOJ DATE BASIC NUMBER(10,2) ADDRESS VARCHAR2(15) DESIG VARCHAR2(14) SQL> alter table empl21 drop column desig; Table altered. TABLE DESCRIPTION:-

SQL> desc empl21; Name Null? Type ----------------------------------------- -------- ---------------------------EMPID NOT NULL VARCHAR2(2) EMP_FNAME VARCHAR2(20) EMP_LNAME VARCHAR2(14) DOJ DATE BASIC NUMBER(10,2) ADDRESS VARCHAR2(15)

RESULT:Thus, the above DDL commands have been executed successfully.

DATA MANIPULATION LANGUAGE


The DML language comprises of the following: Insert. Delete. Update. Select.

Perform DML commands on the following table: EMPLOYEE DETAILS TABLE STRUCTURE-1

FIELD NAME EMP_ID EMP_FNAME EMP_LNAME DOJ BASIC PAY

TYPE ALPHANUMERIC ALPHANUMERIC ALPHANUMERIC DATE NUMBER

SIZE 3 14 14 --10,2

CONSTRAINTS PK ---------

1. Insert Command
SYNTAX:FOR INSERTING A SINGLE RECORD

SQL> insert into<table_name> values(val1,val2,,val n);

SYNTAX:FOR INSERTING MANY RECORDS

SQL> insert into<table_name>(fieldname1,fieldname2,.., fieldnamen)values(&fieldname1,&fieldname2,..,&fieldname n); EXAMPLE:SQL> insert into empl21(empid,emp_fname,emp_lname,doj,basic,address) values('&empid','&emp_fname','&emp_lname','&doj','&basic','&address'); Enter value for empid: 01 Enter value for emp_fname: amber Enter value for emp_lname: marienne Enter value for doj: 01/jan/2000

Enter value for basic: 18000 Enter value for address: france 1 row created. SQL> / Enter value for empid: 04 Enter value for emp_fname: kirsten Enter value for emp_lname: gray Enter value for doj: 06/may/2000 Enter value for basic: 16500 Enter value for address: usa 1 row created. SQL> / Enter value for empid: 08 Enter value for emp_fname: sofia Enter value for emp_lname: ruth Enter value for doj: 05/may/2001 Enter value for basic: 16000 Enter value for address: usa 1 row created. SQL> / Enter value for empid: 10 Enter value for emp_fname: natasha Enter value for emp_lname: jena Enter value for doj: 12/june/2001 Enter value for basic: 15800 Enter value for address: india 1 row created. SQL> / Enter value for empid: 16 Enter value for emp_fname: louisa Enter value for emp_lname: beth Enter value for doj: 19/july/2001 Enter value for basic: 15000 Enter value for address: belgium 1 row created.

TABLE-RECORDS:SQL> select * from empl21; EM EMP_FNAME EMP_LNAME DOJ --------------------- ------------------- -----------01 amber marienne 01-JAN-00 04 kirsten gray 06-MAY-00 08 sofia ruth 05-MAY-01 10 natasha jena 12-JUN-01 16 louisa beth 19-JUL-01 BASIC ADDRESS ---------- --------------18000 france 16500 usa 16000 usa 15800 india 15000 belgium

INSERT VALUES INTO CHILD TABLE SQL> insert into empl21_child(empid)values('&empid'); Enter value for empid: 01 old 1: insert into empl21_child(empid)values('&empid') new 1: insert into empl21_child(empid)values('01') 1 row created. SQL> / Enter value for empid: 04 old 1: insert into empl21_child(empid)values('&empid') new 1: insert into empl21_child(empid)values('04') 1 row created. SQL> / Enter value for empid: 08 old 1: insert into empl21_child(empid)values('&empid') new 1: insert into empl21_child(empid)values('08') 1 row created. SQL> / Enter value for empid: 10 old 1: insert into empl21_child(empid)values('&empid') new 1: insert into empl21_child(empid)values('10')

1 row created.

SQL> / Enter value for empid: 16 old 1: insert into empl21_child(empid)values('&empid') new 1: insert into empl21_child(empid)values('16') 1 row created. TABLE-RECORDS:SQL> select * from empl21_child; EM ---01 04 08 10 16

2. Update Command
SYNTAX:SQL> update <table_name> set fieldname=<expression> [where condition]; EXAMPLE-1:SQL> update empl21 set basic=basic+1500; 5 rows updated. TABLE-RECORDS:SQL> select * from empl21; EM EMP_FNAME EMP_LNAME DOJ --------------------- ------------------- -----------01 amber marienne 01-JAN-00 BASIC ADDRESS ---------- --------------19500 france

04 08 10 16

kirsten sofia natasha louisa

gray ruth jena beth

06-MAY-00 18000 05-MAY-01 17500 12-JUN-01 17300 19-JUL-01 16500

usa usa india belgium

EXAMPLE-2:SQL> update empl21 set basic=basic+2000 where empid=01; 1 row updated. TABLE-RECORDS:SQL> select * from empl21; EM EMP_FNAME EMP_LNAME DOJ --------------------- ------------------- -----------*01 amber marienne 01-JAN-00 04 kirsten gray 06-MAY-00 08 sofia ruth 05-MAY-01 10 natasha jena 12-JUN-01 16 louisa beth 19-JUL-01 BASIC ADDRESS ---------- --------------21500 france 18000 usa 17500 usa 17300 india 16500 belgium

3. Select Command
1) List down employee records whose salary is >= 18,000. SYNTAX:SQL> select * from <table_name> where <condition>; EXAMPLE:SQL> select * from empl21 where basic>=18000; EM EMP_FNAME EMP_LNAME DOJ BASIC ADDRESS --------------------- ------------------- ------------ ---------- --------------*01 amber marienne 01-JAN-00 21500 france 04 kirsten gray 06-MAY-00 18000 usa 2) List down the emp_id ,employee first name and salary fields.

SYNTAX:SQL> select field_name1,.., fieldname n from <table_name> where <condition>;

EXAMPLE:SQL> select empid,emp_fname,basic from empl21; EM ---01 04 08 10 16 EMP_FNAME -------------------amber kirsten sofia natasha louisa BASIC ---------21500 18000 17500 17300 16500

3) List down the employee salaries in ascending order. SYNTAX:SQL> select field_name1.. from <table_name> order by basic; EXAMPLE:SQL> select basic from empl21 order by basic; BASIC ---------16500 17300 17500 18000 21500 4) List down the employee salaries in descending order. SYNTAX:SQL> select field_name1.. from <table_name> order by basic desc; EXAMPLE:-

SQL> select basic from empl21 order by basic desc; BASIC ---------21500 18000 17500 17300 5) Display the maximum salary. SYNTAX:SQL> select max(field_name) from <table_name>; EXAMPLE:SQL> select max(basic) from empl21; MAX(BASIC) -----------------21500

4. Delete Command
SYNTAX:SQL> delete from <table_name> [where <condition>]; EXAMPLE:SQL> delete from empl21 where empid=01; 1 row deleted. TABLE-RECORDS:SQL> select * from empl21;

EM EMP_FNAME EMP_LNAME DOJ BASIC ADDRESS --------------------- ------------------- ------------ ---------- --------------04 kirsten gray 06-MAY-00 18000 usa 10 natasha jena 12-JUN-01 17300 india 16 louisa beth 19-JUL-01 16500 belgium

RESULT:Thus, the above DML commands are executed successfully.

DATA CONTROL LANGUAGE


The DCL commands are: COMMIT: Commit command tells the DBMS to make permanent changes made to temporary copies of the data updating the permanent database tables to match the updated temporary copies. Syntax: Commit Rollback Save point Grant Revoke

SQL> set auto commit on ; SQL> set auto commit off ; SQL>commit ; Example: SQL> insert into cust values ( 12,aaa,pondy); SQL> commit; ROLLBACK: Rollback tells the DBMS to undo any changes made to the DBMS after the most recent commit. Syntax: SQL> rollback ; Example: SQL> insert into cust values(4,xxx,pondy); SQL> rollback; SAVE POINT: Save points are like markers to divide a very lengthy transaction to smaller ones. They are used to identify a point in transaction to which we can later rollback. Thus save point is used in conjunction with rollback to rollback portions of the current transaction. Syntax: SQL>Savepoint<name>; Example: SQL> delete from cust where custid =11; SQL> savepoint m1; GRANT:

Grant gives specific SQL statement access or individual data objects to a user or a group of users. Syntax: SQL>grant <privileges> on <table name> to user ; Example: SQL> grant all on cust to 05mca03; SQL> grant insert on cust to 05mca03; REVOKE: Revoke removes specific SQL statement access previously granted on individual database objects from a user or group of users. Syntax: SQL> Revoke <privileges> on <tablename> from user ; Example: SQL> revoke all on dcl from 05mca03;

TABLE CREATION: SQL> create table dcl ( rono number(3) constraints ass primary key, name varchar2(20), mark number(3) ); Table created.

TABLE DESCRIPTION: SQL> desc dcl; Name Null? Type

----------------------------------------- ---------------------------RONO NOT NULL NUMBER(3) NAME VARCHAR2(20) MARK NUMBER(3) INSERTING RECORDS: SQL> insert into dcl values(111,'kugan.t',100); 1 row created. SQL> insert into dcl values (222,'ram',200); 1 row created. COMMIT: SOL> commit; Table Committed TABLE RECORDS: SQL> select * from dcl; RONO NAME MARK ---------- -------------------- ----------111 kugan.t 100 222 ram 200

SAVEPOINT: SQL> delete from dcl where rono=111; 1 row deleted. SQL> savepoint m1; Savepoint created

SQL> select * from dcl; RONO NAME MARK ------------------------------------------222 ram 200 ROLLBACK : SQL> rollback to savepoint m1; Rollback complete. SQL> select * from dcl; RONO NAME MARK ---------- -------------------- ---------111 kugan.t 00 222 ram 200

GRANT: SQL> grant all on dcl to 05mca03; Grant succeeded. SQL> grant insert on dcl to 05mca03; Grant succeeded. SQL> grant delete on dcl to 05mca03; Grant succeeded.

REVOKE: SQL> revoke all on dcl from 05mca03;

Revoke succeeded.

RESULT: Thus the above DCL commands are executed successfully.

STUDENT MARKLIST-1
AIM: Create the following table and insert records into the table and execute the queries given below.

TABLE STRUCTURE:

FIELD NAME Roll No. Student Name Mark 1

DATATYPE Numeric Alpha Numeric Numeric

SIZE 4 14 3

CONSTRAINTS Primary Key Not Null Not Null

Mark 2 Mark 3 Total Grade

Numeric Numeric Numeric Characters

3 3 3 1

Not null Not Null ---Nil-----Nil---

TABLE CREATION: SQL> create table studentmarklist ( rollno number(4) constraints pkey primary key, sname varchar2(14) not null, mark1 number(3) not null, mark2 number(3) not null, mark3 number(3) not null, total number(3), grade varchar2(1) ); Table created. TABLE DESCRIPTION: SQL> desc studentmarklist; Name Null? Type ----------------------------------------- -------- ---------------------------ROLLNO NOT NULL NUMBER(4) SNAME NOT NULL VARCHAR2(14) MARK1 NOT NULL NUMBER(3) MARK2 NOT NULL NUMBER(3) MARK3 NOT NULL NUMBER(3) TOTAL NUMBER(3) GRADE VARCHAR2(1) INSERTING-RECORDS : SQL> insert into studentmarklist ( rollno, sname, mark1, mark2, mark3 ) values ( '&rollno', '&sname', '&mark1', '&mark2', '&mark3' ); Enter value for rollno: 111 Enter value for sname: aaa

Enter value for mark1: 85 Enter value for mark2: 89 Enter value for mark3: 90 1 row created. SQL> / Enter value for rollno: 222 Enter value for sname: bbb Enter value for mark1: 55 Enter value for mark2: 60 Enter value for mark3: 50 1 row created. SQL> / Enter value for rollno: 333 Enter value for sname: ccc Enter value for mark1: 50 Enter value for mark2: 51 Enter value for mark3: 47 1 row created. SQL> / Enter value for rollno: 444 Enter value for sname: ddd Enter value for mark1: 35 Enter value for mark2: 40 Enter value for mark3: 20 1 row created. UPDATING: SQL> update studentmarklist set total=mark1+mark2+mark3; 4 rows updated. SQL> update studentmarklist set grade = 'O 'where total>200; 1 row updated.

SQL> update studentmarklist set grade = 'A' where total >=150 and total <=200; 1 row updated. SQL> update studentmarklist set grade = 'B' where total>100 and total<=149; 1 row updated. SQL> update studentmarklist set grade='F' where total<100; 1 row updated. TABLE RECORDS: SQL> select * from studentmarklist; ROLLNO SNAME MARK1 MARK2 MARK3 TOTAL G ----------------------------------------------------------------------------------------------------111 aaa 85 89 90 264 O 222 bbb 55 60 50 165 A 333 ccc 50 51 47 148 B 444 ddd 35 40 20 95 F 4 Rows selected. QUERIES: 1) Display the students name which starts with the letter s. SQL> select sname from studentmarklist where sname like 's%'; no rows selected 2) Display the students name which starts with the letter a. SQL> select sname from studentmarklist where sname like 'a%'; SNAME -------------aaa 3) Display the students record whose name starts with letter a.

SQL> select * from studentmarklist where sname like 'a%'; ROLLNO SNAME MARK1 MARK2 MARK3 TOTAL G ----------------------------------------------------------------------------------------------111 aaaa 85 89 90 264 O 4) Display the students name which ends with the letter c. SQL> select sname from studentmarklist where sname like '%c'; SNAME ----------ccc 5) Display the students record whose name ends with the letter c. SQL> select * from studentmarklist where sname like '%c'; ROLLNO SNAME MARK1 MARK2 MARK3 TOTAL G --------------------------------------------------------------------------------------------333 ccc 50 51 47 148 B 6) Display the students record who have got more than 80 in all subjects. SQL> select * from studentmarklist where mark1>80 and mark2>80 and mark3>80; ROLLNO SNAME MARK1 MARK2 MARK3 TOTAL G ---------------------------------------------------------------------------------------------111 aaa 85 89 90 264 O

7) Display the maximum total of marks obtained. SQL> select max(total)from studentmarklist; MAX(TOTAL) -----------------264 8) Display the minimum total of marks obtained. SQL> select min(total) from studentmarklist;

MIN(TOTAL) ----------------95 9) Display the students record who have got the grade F. SQL> select * from studentmarklist where grade='f'; ROLLNO SNAME MARK1 MARK2 MARK3 TOTAL G -----------------------------------------------------------------------------------------------444 ddd 35 40 20 95 F 10) Display the students record who have got the grade O. SQL> select * from studentmarklist where grade = 'O' ; ROLLNO SNAME MARK1 MARK2 MARK3 TOTAL G ------------------------------------------------------------------------------------------------111 aaa 85 89 90 264 O

RESULT: Thus the student table is created and the above queries are executed successfully.

STUDENT MARKLIST-2
AIM: Create the following table and insert records into the table and execute the queries given below.

TABLE STRUCTURE 1: Field name Rollno Name Data type Number Alphanumeric Size 3 14 Constraints Primary key Not Null

TABLE STRUCTURE 2: Field name Rollno Mark1 Mark2 Mark3 Total Grade Data type Number(3) Number(3) Number(3) Number(3) Number(3) Alphanumeric Size 3 3 3 3 3 1 Constraints Foreign Key Between 0 and 100 Between 0 and 100 Between 0 and 100 -

TABLE CREATION:
MASTER TABLE CREATION:

SQL> create table student(rollno number(3) constraints prollno primary key, sname varchar2(14) constraints non not null); Table created. TABLE DESCRIPTION: SQL> desc student; Name Null? Type --------------------------------------- ---------------- ------------------------ROLLNO NOT NULL NUMBER(3) SNAME NOT NULL VARCHAR2(14)
CHILD TABLE CREATION:

SQL> create table cstudent( rollno number(3) constraints csid references student(rollno) on delete cascade, mark1 number(3) constraints chk1 check(mark1 between 0 and100), mark2 number(3) constraints chk2 check(mark2 between 0 and 100), mark3 number(3) constraints chk3 check(mark3 between 0 and 100), total number(5), grade varchar2(1)); Table created. TABLE DESCRIPTION: SQL> desc cstudent; Name Null? Type ----------------------------------------- -------- --------------------ROLLNO NUMBER(3) MARK1 NUMBER(3) MARK2 NUMBER(3) MARK3 NUMBER(3) TOTAL NUMBER(5) GRADE VARCHAR2(1)

INSERTING RECORDS: SQL> insert into student(rollno,sname)values('&rollno','&sname'); Enter value for rollno: 1 Enter value for sname: suchi 1 row created. SQL> / Enter value for rollno: 2 Enter value for sname: sruthi 1 row created. UPDATING RECORDS: SQL> update cstudent set total=mark1+mark2+mark3; 4 rows updated. SQL> update cstudent set grade='O' where total>250; 1 row updated. SQL> update cstudent set grade='A' where total<=249 and total>200; 3 rows updated. SQL> update cstudent set grade='B' where total<=199 and total>150; 1 row updated. SQL> update cstudent set grade='C' where total<=149 and total>100; 1 row updated. SQL> update cstudent set grade='F' where total<=99; 1 row updated.

QUERIES: 1) To display the students records whose name starts with s : SQL> select * from student where sname like 's%'; ROLLNO -----------1 2 SNAME -------------suchi sruthi

2) To display the students records whose name ends with n: SQL> select * from student where sname like '%n'; ROLLNO SNAME ------------ ----------3 naveen 3) To display the students records who have secured more than 150: SQL> select * from cstudent where total>150; ROLLNO MARK1 MARK2 MARK3 TOTAL GRADE ------------ ---------- ------------ ---------- ---------- ---------1 82 75 92 249 A 3 76 45 65 186 B 4 100 78 82 260 O 4) To display the students record who have secured the maximum total: SQL> select max(total) from cstudent; MAX(TOTAL) -----------------260 5) To display the students records who have got c grade: SQL> select * from cstudent where grade='C'; ROLLNO MARK1 MARK2 MARK3 TOTAL GRADE ------------ ---------- ----------- ----------- ---------- ----------2 50 60 30 140 C

6) To display the student record who have got o grade: SQL> select * from cstudent where grade='O'; ROLLNO MARK1 MARK2 MARK3 TOTAL GRADE ------------- ---------- ----------- ---------- ---------- ----------4 100 78 82 260 O 7) To display the students records who have secured the second maximum total: SQL> select max(total) from cstudent where total <(select max(total) from cstudent); MAX(TOTAL) ------------------249

Result: Thus the student tables are created and the above queries are executed successfully.

TELEPHONE DIRECTORY MAINTENANCE


AIM: Create the following table and insert records into the table and execute the queries given below:

TABLE STRUCTURE:1

Field name
Tno Occupation

Data type
Number Varchar2

Size
10 20

Constraints
Primary key Not null

1. Create the above table. 2. Add the following fields in the created table. TABLE STRUCTURE:2

Field name
Cust_name Cust_addr Cust_city Std_con Isd_con

Data type
Varchar2 Varchar2 Varchar2 Varchar2 Varchar2

Size
20 40 10

Constraints
Not null Not null Not null Not null

1 1 Not null

TABLE CREATION: SQL> create table tele ( tno number(10) constraints t1 primary key, occu varchar2(20)constraints oc not null); Table created. TABLE DESCRIPTION: SQL> desc tele; Name -------------------------TNO OCCU TABLE ALTERATION: SQL> alter table tele add(cust_name varchar2(20) constraints c1 not null, cust_addr varchar2(40) constraints c2 not null, cust_city varchar2(10) constraints c3 not null, std_con varchar2(1) constraints c4 not null, isd_con varchar2(1) constraints c5 not null); Table altered. Null? ------------NOT NULL NOT NULL Type ---------------------------NUMBER(10) VARCHAR2(20)

TABLE DESCRIPTION: SQL> desc tele; Name ----------------------------TNO OCCU CUST_NAME CUST_ADDR CUST_CITY STD_CON ISD_CON Null? ----------NOT NULL NOT NULL NOT NULL NOT NULL NOT NULL NOT NULL NOT NULL Type ---------------------------NUMBER(10) VARCHAR2(20) VARCHAR2(20) VARCHAR2(40) VARCHAR2(10) VARCHAR2(1) VARCHAR2(1)

INSERTING RECORDS SQL> insert into tele(tno,occu,cust_name,cust_addr,cust_city,std_con,isd_con) 2 values('&tno','&occu','&cust_name','&cust_addr','&cust_city','&std_con', 3 '&isd_con'); Enter value for tno: 2343245 Enter value for occu: manager Enter value for cust_name: kayal Enter value for cust_addr: no.12,new st Enter value for cust_city: pondy Enter value for std_con: y 1 row created. SQL> / Enter value for tno: 989434523 Enter value for occu: teacher Enter value for cust_name: mano Enter value for cust_addr: no.10 main road Enter value for cust_city: chennai Enter value for std_con: y 1 row created. SQL> / Enter value for tno: 94434234 Enter value for occu: doctor

Enter value for cust_name: srinivasan Enter value for cust_addr: no.8,2nd cross Enter value for cust_city: pondy Enter value for std_con: n 1 row created. SQL> / Enter value for tno: 9344719313 Enter value for occu: student Enter value for cust_name: haran Enter value for cust_addr: mariamman Enter value for cust_city: cuddalore Enter value for std_con: n 1 row created. TABLE-RECORDS SQL> select * from tele; TNO OCCU CUST_NAME CUST_ADDR CUST_CITY STD ISD ----------------------------------------------------------------------------------------------------------2343245 manager kayal no.12,new st pondy y y 989434523 teacher mano no.10 main road chennai y n 94434234 doctor srinivasan no.8,2nd cross pondy n n 9344719313 student haran mariamman st, cuddalore n y

QUERIES: 1. List down the customers having std connection. SQL> select * from tele where std_con='y'; TNO OCCU CUST_NAME CUST_ADDR CUST_CITY STD ISD ---------------------------------------------------------------------------------------------------------2343245 manager kayal no.12,new st pondy y y 989434523 teacher mano no.10 main road chennai y n 2. List down the customers record who are having both isd and std connection. SQL> select * from tele where std_con='y' and isd_con='y';

TNO OCCU CUST_NAME CUST_ADDR CUST_CITY STD ISD ----------------------------------------------------------------------------------------------------------2343245 manager kayal no.12,new st pondy y y 3. List down the customer record whose occupation is manager and having isd connection. SQL> select * from tele where occu='manager' and isd_con='y'; TNO OCCU CUST_NAME CUST_ADDR CUST_CITY STD ISD --------------------------------------------------------------------------------------------------------2343245 manager kayal no.12,new st pondy y y

4. List down the customer records whose telephone number start with 9 and 4 SQL> select * from tele where tno like '9%4'; TNO OCCU CUST_NAME CUST_ADDR CUST_CITY STD ISD --------------------------------------------------------------------------------------------------------94434234 doctor srinivasan no.8,2nd cross pondy n n 5. List down the all customer records from the table. SQL> select * from tele; TNO OCCU CUST_NAME CUST_ADDR CUST_CITY STD ISD ----------------------------------------------------------------------------------------------------------2343245 manager kayal no.12,new st pondy y y 989434523 teacher mano no.10 main road chennai y n 94434234 doctor srinivasan no.8,2nd cross pondy n n 9344719313 student haran mariamman cuddalore n y 6. List out the customer record in the ascending order of customer name; SQL> select * from tele order by cust_name; TNO OCCU CUST_NAME CUST_ADDR CUST_CITY STD ISD ----------------------------------------------------------------------------------------------------------9344719313 student haran mariamman cuddalore n y 2343245 manager kayal no.12,new st pondy y y

989434523 94434234

teacher doctor

mano srinivasan

no.10 main road no.8,2nd cross

chennai pondy

y n

n n

7. List out the customer record in the decending order of customer name; SQL> select * from tele order by cust_name desc; TNO OCCU CUST_NAME CUST_ADDR CUST_CITY STD ISD ----------------------------------------------------------------------------------------------------------94434234 doctor srinivasan no.8,2nd cross pondy n n 989434523 teacher mano no.10 main road chennai y n 2343245 manager kayal no.12,new st pondy y y 9344719313 student haran mariamman cuddalore n y

8. List down the customers whose occupation is manager,salaesman,clerk or teacher. SQL> select * from tele where occu='manager' or occu='salesman' or occu='teacher'; TNO OCCU CUST_NAME CUST_ADDR CUST_CITY STD ISD ----------------------------------------------------------------------------------------------------------2343245 manager kayal no.12,new st pondy y y 989434523 teacher mano no.10 main road chennai y n

RESULT: Thus the above table is created and the above queries are executed successfully.

EMPLOYEE INFORMATION SYSTEM


AIM: Create the following table and insert records into the table and execute the queries given below. TABLE STRUCTURE:

Field name
Empid Emp_fname Emp_lname Desigination Doj Salary Depart no.

Data type
Number Varchar2 Varchar2 Varchar2 Date Number Number

Size
4 15 15 14 Date 7,2 4

Constraints
Primary key -

TABLE CREATION: SQL> create table emp(empid number(4) constraints ee1 primary key, empfname varchar2(15), emplname varchar2(15), desi varchar2(14), doj date, salary number(7,2),

departno number(4)); Table created.

TABLE DESCRIPTION: SQL> desc emp; Name --------------------------EMPID EMPFNAME EMPLNAME DESI DOJ SALARY DEPARTNO Null? ------------NOT NULL Type ---------------------------NUMBER(4) VARCHAR2(15) VARCHAR2(15) VARCHAR2(14) DATE NUMBER(7,2) NUMBER(4)

INSERTING RECORDS: SQL> insert into emp(empid,empfname,emplname,desi,doj,salary,departno)values ('&empid','&empfname','&emplname','&desi','&doj','&salary','&departno'); Enter value for empid: 110 Enter value for empfname: kayal Enter value for emplname: vizhi Enter value for desi: manager Enter value for doj: 5-aug-05 Enter value for salary: 10000 Enter value for departno: 330 1 row created. SQL> / Enter value for empid: 111 Enter value for empfname: mano Enter value for emplname: haran Enter value for desi: engineer Enter value for doj: 7-jan-03

Enter value for salary: 40000 Enter value for departno: 331 1 row created. SQL> / Enter value for empid: 112 Enter value for empfname: devi Enter value for emplname: priya Enter value for desi: lecturer Enter value for doj: 12-feb-04 Enter value for salary: 15000 Enter value for departno: 332 1 row created. SQL> / Enter value for empid: 113 Enter value for empfname: jackuline Enter value for emplname: mary Enter value for desi: student Enter value for doj: 3-may-01 Enter value for salary: 25000 Enter value for departno: 333 1 row created. TABLE RECORDS: SQL> select * from emp; EMPID EMPFNAME EMPLNAME DESI DOJ SALARY DEPTNO ----------------------------------------------------------------------------------------------------------110 kayal vizhi manager 05-AUG-05 10000 330 111 mano haran engineer 07-JAN-03 40000 331 112 devi priya lecturer 12-FEB-04 15000 332 113 jackuline mary student 03-MAY-01 25000 333 QUERIES: 1. List down the employee firstname in the desc-order SQL> select empfname from emp order by empfname desc; EMPFNAME

----------------mano kayal jackuline devi 2. List down the employee whose first name are five letters long that begins with kay and ends with l. SQL> select * from emp where empfname like ('kay_l'); EMPID EMPFNAME EMPLNAME DESI DOJ SALARY DEPTNO ----------------------------------------------------------------------------------------------------------110 kayal vizhi manager 05-AUG-05 10000 330 3. To count the number of employees and specify the field name as row-count. SQL> select count(*) as row_count from emp; ROW_COUNT ------------------4 4. List down emp fname with respect of department no. SQL> select departno,empfname from emp group by departno,empfname; DEPARTNO EMPFNAME --------------------------------------330 kayal 331 mano 332 devi 333 jackuline 5. Find the max salary in the emp table. SQL> select max(salary) from emp; MAX(SALARY) --------------------40000 6. Find all emp-id whose salary has got null values. SQL> select empid from emp where salary is null; EMPID

----------113

7. List down the details of the employee who gets the maximum salary. SQL> select * from emp where salary=(select max(salary) from emp); EMPID EMPFNAME EMPLNAME DESI DOJ SALARY DEPTNO -------- ---------------- ------------------ ----------- -------------------- ----------111 mano haran engineer 07-JAN-03 40000 331 8. List down the details of the employee whose desigination is not student; SQL> select * from emp where desi<>'manager'; EMPID EMPFNAME EMPLNAME DESI DOJ SALARY DEPTNO ----------------------------------------------------------------------------------------------------------111 mano haran engineer 07-JAN-03 40000 331 112 devi priya lecturer 12-FEB-04 15000 332 113 jackuline mary student 03-MAY-01 25000 333 9. Find the average salary in the emp table and specify the fields as ave-salary. SQL> select avg(salary) as avg_salary from emp; AVG_SALARY ------------------21666.6667 10. To count the employees and calculate the average annual salary for each desigination in each department. SQL> select deptno, count(*),12*avg(salary) from emp group by deptno,desi; DEPTNO COUNT(*) ------------ ---------330 1 331 1 332 1 333 1 12*AVG(SALARY) ------------------------120000 480000 180000 300000

RESULT: Thus the above table is created and the above queries are executed successfully.

ADDITION OF TWO NUMBERS


AIM: To write the PL/SQL program to find the Addition of Two Numbers. PROGRAM: SQL>set serveroutput on; SQL> 1 declare 2 x integer; 3 y integer; 4 z integer; 5 begin 6 x:=&x; 7 y:=&y; 8 z:=x+y; 9 dbms_output.put_line('Sum='|| z); 10* end; SQL> / INPUT: Enter value for x: 3 old 6: x:=&x; new 6: x:=3; Enter value for y: 3 old 7: y:=&y; new 7: y:=3; OUTPUT: Sum=6

RESULT: Thus the above PL/SQL program is executed successfully.

FACTORIAL OF A NUMBER
AIM: To write the PL/SQL program to find the Factorial of a number. PROGRAM: SQL>set serveroutput on; SQL> 1 declare 2 n number; 3 f number; 4 i number; 5 begin 6 n:=&n; 7 f: =1; 8 for i in 1..n 9 loop 10 f: =f*i; 11 end loop; 12 dbms_output.put_line ('The factorial is:'||f); 13* end; SQL> / INPUT: Enter value for n: 5 old 6: n: =&n; new 6: n: =5; OUTPUT: The factorial is: 120

RESULT: Thus the above PL/SQL program is executed successfully.

SUM OF THE SERIES[1+2++N]


AIM: To write the PL/SQL program to find the Sum of n numbers. PROGRAM: SQL>set serveroutput on; SQL> 1 declare 2 n number; 3 s number; 4 i number; 5 begin 6 n:=&n; 7 s:=0; 8 for i in 1..n 9 loop 10 s:=s+i; 11 end loop; 12 dbms_output.put_line ('Sum='||s); 13* end; INPUT: SQL> / Enter value for n: 5 old 6: n:=&n; new 6: n:=5; OUTPUT: Sum=15 RESULT: Thus the above PL/SQL program is executed successfully.

FIBONACCI SERIES
AIM: To write a PL/SQL program to generate Fibonacci series. PROGRAM: SQL>set serveroutput on; SQL> declare 2 n number; 3 a number; 4 b number; 5 c number; 6 i number; 7 begin 8 n:=&n; 9 a:=-1; 10 b:=1; 11 c:=0; 12 dbms_output.put_line('The fibonacci series'); 13 for i in 1..n 14 loop 15 c:=a+b; 16 a:=b; 17 b:=c; 18 dbms_output.put_line(c); 19 end loop; 20 end; 21 / INPUT: Enter value for n: 5 old 8: n:=&n; new 8: n:=5; OUTPUT: The fibonacci series 0 RESULT: Thus the above PL/SQL program is executed successfully. 1 1 2 3

TRIGGERS
AIM: To write a PL/SQL program using triggers to do the following: a) Insert records to another table automatically b) Delete records in another table automatically c) Update records to another table automatically TABLE STRUCTURE: Table name :Trig1 Field name Roll no Name Mark1 Mark2 Data type Numeric Alphanumeric Numeric Numeric Size 3 15 3 3

TABLE CREATION: SQL> create table trig1 (rno number(3),name varchar2(15),mark1 number(3),mark2 number(3)); Table created. TABLE DESCRIPTION: SQL> desc trig1; Name Null? Type ------------------------- -------- ---------------------------RNO NUMBER(3) NAME VARCHAR2(15) MARK1 NUMBER(3) MARK2 NUMBER(3)

Table name : trig2 Field name Roll no Name Mark1 Mark2 TABLE CREATION: SQL> create table trig2 (rno number(3),name varchar2(15),mark1 number(3),mark2 number(3)); Table created. TABLE DESCRIPTION: SQL> desc trig2; Name Null? Type ------------------------ --------- --------------------------RNO NUMBER(3) NAME VARCHAR2(15) MARK1 NUMBER(3) MARK2 NUMBER(3) TRIGGER: SQL> create or replace trigger trig_in_up_del after insert or delete or update on trig1 for each row begin 2 if inserting then insert into trig2 values(:new.rno,:new.name,:new.mark1,:new.mark2); 3 elsif deleting then 4 delete from trig2 where rno=:old.rno; 5 else 6 update trig2 set mark1=:new.mark1,mark2=:new.mark2; 7 end if; 8 end; 9 / Trigger created. Data type Numeric Alphanumeric Numeric Numeric Size 3 15 3 3

INPUT: Insertion: SQL> insert into trig1(rno,name,mark1,mark2) values ('&rno','&name','&mark1','&mark2'); Enter value for rno: 1 Enter value for name: suchi Enter value for mark1: 8 Enter value for mark2: 89 1 row created. SQL> / Enter value for rno: 2 Enter value for name: sruthi Enter value for mark1: 87 Enter value for mark2: 85 1 row created. TABLE-RECORDS: SQL> select * from trig1; RNO NAME MARK1 MARK2 ------------------------------------------------1 suchi 8 89 2 sruthi 87 85 3 sen 98 97 SQL> select * from trig2; RNO NAME MARK1 MARK2 -------------------------------------------------1 suchi 8 89 2 sruth 87 85 3 sen 98 97

Deletion: SQL>delete from trig1 where rno = 1; 1 row deleted. SQL> select * from trig1; RNO NAME MARK1 MARK2 ----------------------------------------------2 sruthi 87 85 3 sen 98 97 SQL> select * from trig2; RNO NAME MARK1 MARK2 ----------------------------------------------2 sruthi 87 85 3 sen 98 97 Updation: SQL>update trig1 set mark1= 97 where rno= 2; 1 row updated. SQL> select * from trig1; RNO NAME MARK1 MARK2 ----------------------------------------------2 sruthi 97 85 3 sen 98 97 SQL> select * from trig2; RNO NAME MARK1 MARK2 ----------------------------------------------2 sruthi 97 85 3 sen 98 97

ALTERNATIVE METHOD: INSERTION: SQL> create or replace trigger trig_ins after insert on trig1 for each row 2 begin 3 insert into trig2 values(:new.rno,:new.name,:new.mark1,:new.mark2); 4 end; 5 / Trigger created. DELETION: SQL> create or replace trigger trig_del after delete on trig1 for each row 2 begin 3 delete from trig2 where rno=:old.rno; 4 end; 5 / Trigger created. UPDATION: SQL> create or replace trigger trig_up after update on trig1 for each row 2 begin 3 update trig2 set mark1=:new.mark1,mark2=:new.mark2 where rno=:old.rno; 4 end; 5 / Trigger created.

RESULT: Thus the given PL/SQL program using triggers is created and executed successfully.

TRIGGERS
AIM: To write a PL/SQL program using triggers to restrict insert, delete and Update operation in a table. TABLE STRUCTURE: Field name Loan no Amount Branch name Data type Numeric Numeric Alphanumeric Size 4 5 14

TABLE CREATION: SQL> create table loan( loanno number(4),amount number(5),bn varchar2(14) ); Table created. TABLE DESCRIPTION: SQL> desc loan; Name --------------LOANNO AMOUNT BN Null? -------Type --------------------NUMBER(4) NUMBER(5) VARCHAR2(14)

INSERTING RECORDS: SQL> insert into loan values( 111,4000,'mount road' ); 1 row created.

SQL> insert into loan values(222,5000,'anna salai'); 1 row created. SQL> select * from loan; LOANNO -------------111 222 TRIGGER: SQL> create or replace trigger tr_in_up_de after insert or delete 2 or update on loan 3 begin 4 if inserting then 5 raise_application_error(-20006,'insertion is not possible'); 6 elsif deleting then 7 raise_application_error(-20007,'deletion is not possible'); 8 else 9 raise_application_error(-20008,'updation is not possible'); 10 end if; 11 end; 12 / Trigger created. OUTPUT: SQL> insert into loan values(555,7600,'chennai'); insert into loan values(555,7600,'chennai') * ERROR at line 1: ORA-20006: insertion is not possible ORA-06512: at "05MCA45.TR_IN_UP_DE", line 3 ORA-04088: error during execution of trigger '05MCA45.TR_IN_UP_DE' AMOUNT BN -------------- ------------4000 mount road 5000 anna salai

SQL> update loan set amount=5899 where loanno=111; update loan set amount=5899 where loanno=111 * ERROR at line 1: ORA-20008: updation is not possible ORA-06512: at "05MCA45.TR_IN_UP_DE", line 7 ORA-04088: error during execution of trigger '05MCA45.TR_IN_UP_DE'

SQL> delete from loan where loanno=111; delete from loan where loanno=111 * ERROR at line 1: ORA-20007: deletion is not possible ORA-06512: at "05MCA45.TR_IN_UP_DE", line 5 ORA-04088: error during execution of trigger '05MCA45.TR_IN_UP_DE' ALTERNATIVE METHOD: 1. INSERTION: SQL> create or replace trigger trig_in after insert on loan 2 begin 3 raise_application_error(-20001,'insertion is not possible'); 4 end; 5 / Trigger created. 2. DELETION: SQL> create or replace trigger trig_de after delete on loan 2 begin 3 raise_application_error(-20002,'deletion is not possible'); 4 end; 5 / Trigger created.

3. UPDATION: SQL> 1 create or replace trigger trig_updation after update on loan 2 begin 3 raise_application_error(-20005,'updation is not possible'); 4 end; 5 / Trigger created.

RESULT: Thus the given PL/SQL program using triggers is created and executed successfully.

TRIGGERS
AIM: To write a PL/SQL program using triggers to insert the username, login date and the action done by the user when logging-in Oracle. TABLE STRUCTURE:

Field name User name System date Action

Data type Alphanumeric Date Alphanumeric

Size 15 10

TABLE CREATION: SQL> create table logs (username varchar2(15), systemdate date, action varchar2(10)); Table created. TABLE DESCRIPTION: SQL> desc logs; Name Null? Type ------------------------ -------- ---------------------------USERNAME VARCHAR2(15) SYSTEMDATE DATE ACTION VARCHAR2(10)

TRIGGERS: SQL> create or replace trigger trig_login after logon on schema 2 begin 3 insert into logs logon values(user,sysdate,'Login'); 4 end; 5 / Trigger created. OUTPUT: SQL> select * from logs; USERNAME SYSTEMDAT ACTION --------------------------------- ---------05MCA51 20-SEP-06 Login

RESULT: Thus the given PL/SQL program using triggers is created and executed successfully.

TRIGGERS
AIM: To write a PL/SQL program using triggers to insert the user name, login date and the action done by the user while logging out oracle. TABLE STRUCTURE: Field name User name System date Action Data type Alphanumeric Date Alphanumeric Size 15 15

TABLE CREATION: SQL> create table logoff ( username varchar2(15), logdate date, action varchar2(15) ); Table created. TABLE DESCRIPTION: SQL> desc logoff; Name Null? Type -------------------------------------------------------------------USERNAME VARCHAR2(15) LOGDATE DATE ACTION VARCHAR2(15)

TRIGGERS: SQL> create or replace trigger tri_logoff before logoff on schema begin insert into logoff values(user,sysdate,'log off'); end; Trigger created.

OUTPUT: SQL> select * from logoff; USERNAME ---------------05MCA02 LOGDATE -------------25-SEP-06 ACTION -----------log off

RESULT: Thus the given PL/SQL program using trigger is created and executed successfully.

TRIGGERS
AIM: To write a PL/SQL program using triggers to restrict any changes in the table on a particular day. TABLE STRUCTURE:

FIELD NAME

DATATYPE

SIZE

CONSTRAINTS

Name Rollno Marks

Alphanumeric Number Number

20 3 3

Notnull -

TABLE CREATION:SQL> create table res(sname varchar2(20), rn number(3), marks number(3)); Table created. TABLE DESCRIPTION:SQL> desc res; Name Null? Type ----------------------------------------- -------- ---------------------------SNAME VARCHAR2(20) RN NUMBER(3) MARKS NUMBER(3)

TRIGGERS: SQL> create or replace trigger trig_rust after insert or delete or update on res begin if trim(to_char(sysdate,'day'))='wednesday' then raise_application_error(-20012,'Any changes to the table is not allowed); end if; end; SQL> / Trigger created. INSERTING RECORDS: SQL> insert into res values('sri',12,90); insert into res values('sri',12,90) * ERROR at line 1: ORA-20012: Any changes to the table is NOT allowed ORA-06512: at "05MCA21.TRIG_RUST", line 2 ORA-04088: error during execution of trigger '05MCA21.TRIG_RUST' UPDATING RECORDS:SQL> update res set marks=marks+10; update res set marks=marks+10 * ERROR at line 1: ORA-20012: Any changes to the table is NOT allowed ORA-06512: at "05MCA21.TRIG_RUST", line 2 ORA-04088: error during execution of trigger '05MCA21.TRIG_RUST' DELETING RECORDS:SQL> delete from res where rn=1; delete from res where rn=1 * ERROR at line 1: ORA-20012: Any changes to the table is NOT allowed ORA-06512: at "05MCA21.TRIG_RUST", line 2 ORA-04088: error during execution of trigger '05MCA21.TRIG_RUST' RESULT: Thus the above table is created and the trigger is executed successfully.

TRIGGERS
AIM: Create two tables with the following table structures and show the master-child relationship:i) Insert records into the child table automatically. ii) If any child record is deleted the corresponding record in master table should also be deleted automatically. iii) If any child record is updated the corresponding record in the master table should also be updated automatically.

Table structure-1: FIELD NAME


Accno Street

DATA TYPE

SIZE

CONSTRAIN TS
Not null

Alphanumeric

Alphanumeric Alphanumeric

14 14

Not null Not null

City

Table structure-2: FIELD NAME


Accno Branch_name Branch_code

DATA TYPE
Alphanumeric Alphanumeric Alphanumeric

SIZE
5 14 14

CONSTRAINTS
Not null Not null Not null

TABLE CREATION: SQL> create table cus ( accno varchar2(5) constraints prk primary key, street varchar2(14), city varchar2(14) ); Table created. TABLE DESCRIPTION: SQL> desc cus; Name ------------ACCNO STREET CITY Null? Type ------------- ---------------------NOT NULL VARCHAR2(5) VARCHAR2(14) VARCHAR2(14)

TABLE CREATION: SQL> create table account(accno varchar2(5) constraints frk references cus(accno) on delete cascade, branch_name varchar2(14), branch_code varchar2(14)); Table created. TABLE DESCRIPTION: SQL> desc account; Name Null? -------------------------ACCNO BRANCH_NAME BRANCH_CODE Type ------------VARCHAR2(5) VARCHAR2(14) VARCHAR2(14)

Triggers [insertion to table 2]: SQL> create or replace trigger trig_i1 after insert on cus for each row begin insert into account values(:new.accno,'Mount road','chennai'); end; SQL> / Trigger created. INSERTING RECORDS: SQL> insert into cus values('a123','nehru st','pondy'); 1 row created. TABLE-RECORDS: SQL> select * from cus; ACCNO ----------a123 STREET -------------nehru st CITY -------------pondy

SQL> select * from account; ACCNO ----------a123 BRANCH_NAME ------------------------Mount road BRANCH_CODE ----------------------chennai

DISABLE TRIGGERS: SQL> alter table cus disable all triggers; Table altered.

Triggers [insertion to table 1]: SQL> create or replace trigger ti2 after insert on account for each row begin insert into cus values(:new.accno,'zigzag rd','delhi'); end; SQL> / Trigger created. INSERTING RECORDS: SQL> insert into account values('12','xyz','x222'); 1 row created. TABLE RECORDS: SQL> select * from account; ACCNO ----------a123 12 BRANCH_NAME ------------------------Mount road xyz BRANCH_CODE ----------------------chennai x222

TABLE-RECORDS: SQL> select * from cus; ACCNO STREET ----------- -------------a123 nehru st 12 zigzag rd CITY ------------pondy delhi

Triggers [updation to table 2]: SQL> create or replace trigger tu1 after update on cus for each row begin update account set branch_name='hyd' where accno=:old.accno; end; SQL> / Trigger created.

UPDATING RECORDS: SQL> update cus set city='hyd' where accno='a123'; 1 row updated. TABLE-RECORDS : SQL> select * from cus; ACCNO STREET CITY ----------- -------------- -------------a123 nehru st hyd 12 zigzag rd delhi TABLE-RECORDS : SQL> select * from account; ACCNO ----------a123 12 BRANCH_NAME -----------------------hyd xyz BRANCH_CODE -----------------------chennai

DISABLE TRIGGERS: SQL> alter table cus disable all triggers; Table altered. Triggers [updation to table 1]: SQL> create or replace trigger tu2 after update on account for each row begin update cus set street='jain st' where accno=:old.accno; end; SQL> / Trigger created.

UPDATING RECORDS: SQL> update account set branch_name='banglore' where accno='12'; 1 row updated. TABLE -RECORDS: SQL> select * from cus; ACCNO ----------a123 12 STREET CITY -------------- -------------nehru st hyd jain st delhi

TABLE -RECORDS: SQL> select * from account; ACCNO ----------a123 12 BRANCH_NAME BRANCH_CODE -------------------------- -----------------------hyd chennai banglore x222

DISABLE TRIGGERS: SQL> alter table cus disable all triggers; Table altered DISABLE FOREIGN KEY: SQL> alter table account disable constraints frk; Table altered. DISABLE PRIMARY KEY: SQL> alter table cus disable constraints prk; Table altered

Triggers [deletion from table 2]: SQL> create or replace trigger td1 after delete on cus for each row begin delete from account where accno=:old.accno; end; SQL> / Trigger created. DELETING RECORDS: SQL> delete from cus where accno='a123'; 1 row deleted. TABLE-RECORDS : SQL> select * from cus; ACCNO STREET CITY ----------- -------------- -------------12 jain st delhi TABLE-RECORDS: SQL> select * from account; ACCNO ----------12 BRANCH_NAME BRANCH_CODE ------------------------ ------------------------banglore x222

INSERTING RECORDS: SQL> insert into cus values('a222','kgu st','hyd'); 1 row created. SQL> insert into account values('a345','vivek vihar','b167'); 1 row created. Triggers [deletion from table 1]: SQL> create or replace trigger td3 after delete on account for each row begin delete from cus where accno=:old.accno; end; Trigger created.

DELETING RECORDS: SQL> delete from account where accno='12'; 1 row deleted. TABLE RECORDS: SQL> select * from cus; ACCNO -----------a222 STREET --------------kgu st CITY ------------hyd

TABLE RECORDS: SQL> select * from account; ACCNO ----------a222 BRANCH_NAME BRANCH_CODE ------------------------- -------------------------vivek vihar b167

RESULT: Thus the above tables are created and the triggers are executed successfully.

STUDENT MARKLIST MAINTENANCE


AIM: To write a program to design the Student Mark list maintenance using Visual Basic-Oracle. TABLE CREATION : SQL> create table student(rollno varchar2(10) constraints pk11 primary key, name varchar2(20), gend varchar2(7), dob date, course varchar2(10), m1 number(3), m2 number(3), m3 number(3), m4 number(3), m5 number(3), tot number(3), percentage number(5,2), result varchar2(10));

FORM DESIGN:

CODING: Dim cn As New ADODB.Connection Dim rs As New ADODB.Recordset Private Sub ADD_Click() Call txtclear rs.AddNew Text1.SetFocus End Sub Private Sub DELETE_Click() Dim Y Y = InputBox(RTrim(LTrim(" ENTER THE ROLL NUMBER TO BE DELETED "))) rs.MoveFirst rs.Find ("rollno='" & Y & "'") If rs.EOF Then MsgBox (" NO SUCH RECORD ") Else Call dbtotxt rs.DELETE End If End Sub Private Sub EDIT_Click() Text1.SetFocus End Sub Private Sub first_Click() Call dbtotxt rs.MoveFirst End Sub Private Sub Form_Load() cn.Open ("dsn=kaja;uid=05mca17;pwd=*****;") rs.Open ("select * from student"), cn, adOpenKeyset, adLockOptimistic If rs.EOF And rs.BOF Then MsgBox " RECORD NOT FOUND " End If End Sub Private Sub last_Click() Call dbtotxt rs.MoveLast End Sub

Private Sub next_Click() rs.MoveNext If Not rs.EOF Then Call dbtotxt Else rs.MoveLast End If End Sub Private Sub previous_Click() rs.MovePrevious If Not rs.EOF Then Call dbtotxt Else rs.MoveFirst End If End Sub Private Sub QUIT_Click() End End Sub Private Sub REPORT_Click() DataReport1.Show End Sub Private Sub SAVE_Click() Call txttodb rs.Update MsgBox " SAVED SUCCESSFULLY " End Sub Public Sub txtclear() Text1.Text = "" Text2.Text = "" Text3.Text = "" Text4.Text = "" Text5.Text = "" Text6.Text = "" Text7.Text = "" Text8.Text = "" Text9.Text = "" Text10.Text = "" Text11.Text = "" Text12.Text = "" Text13.Text = ""

End Sub Public Sub txttodb() rs.Fields(0) = Text1.Text rs.Fields(1) = Text2.Text rs.Fields(2) = Text3.Text rs.Fields(3) = Text4.Text rs.Fields(4) = Text5.Text rs.Fields(5) = Text6.Text rs.Fields(6) = Text7.Text rs.Fields(7) = Text8.Text rs.Fields(8) = Text9.Text rs.Fields(9) = Text10.Text rs.Fields(10) = Text11.Text rs.Fields(11) = Text12.Text rs.Fields(12) = Text13.Text End Sub Public Sub dbtotxt() Text1.Text = rs.Fields(0) Text2.Text = rs.Fields(1) Text3.Text = rs.Fields(2) Text4.Text = rs.Fields(3) Text5.Text = rs.Fields(4) Text6.Text = rs.Fields(5) Text7.Text = rs.Fields(6) Text8.Text = rs.Fields(7) Text9.Text = rs.Fields(8) Text10.Text = rs.Fields(9) Text11.Text = rs.Fields(10) Text12.Text = rs.Fields(11) Text13.Text = rs.Fields(12) End Sub Private Sub SEARCH_Click() Dim Y Y = InputBox(RTrim(LTrim(" ENTER THE ROLL NUMBER TO BE DELETED "))) rs.MoveFirst rs.Find ("rollno= ' " & Y & "'") If rs.EOF Then MsgBox (" NO SUCH RECORD ") Else Call dbtotxt End If End Sub

Private Sub Text10_Change() Text11.Text = Val(Text6.Text) + Val(Text7.Text) + Val(Text8.Text) + Val(Text9.Text) + Val(Text10.Text) Text12.Text = Val(Text11.Text) / 5 If (Val(Text6) >= 40 And Val(Text7) >= 40 And Val(Text8) >= 40 And Val(Text9) >= 40 And Val(Text10) >= 40) Then Text13.Text = "PASS" Else Text13.Text = "FAIL" End If End Sub REPORT:

RESULT: Thus the given program is executed successfully.

GAS BOOKING SYSTEM

AIM: To write a program to design the gas booking maintenance using Visual Basic-Oracle.

TABLE CREATION : Customer Details Table: SQL> create table gas ( custno number(4) constraints pkg1 primary key, custname varchar2(14), custadd varchar2(14), custcity varchar2(10), custcity number(10)); Table created.

Booking Details Table: SQL> create table gas ( custno number(4) constraints forg2 references gas(custno) on delete cascade, bookdate date, amount number(3,2), deldate date); Table created.

FORM DESIGN:

CODING: FORM 1: Private Sub cmdbookdetails_Click() Form3.Show End Sub Private Sub Cmdcustdetails_Click() Form2.Show End Sub Private Sub CMDEXIT_Click() End End Sub Private Sub cmdreport_Click() DataReport1.Show End Sub

FORM 2: Dim cn1 As New ADODB.Connection Dim rs1 As New ADODB.Recordset Private Sub CMDADD_Click() Call txtoclear rs1.AddNew Text1.SetFocus End Sub Private Sub CMDDEL_Click() Dim z z = InputBox(RTrim(LTrim("enter the cusotmer no.:"))) rs1.MoveFirst rs1.Find ("custno='" & z & "'") If rs1.EOF Then MsgBox "no such customer no." Else Call dbtotxt rs1.Delete End If Call txtoclear End Sub Private Sub CMDMAIN_Click() Form1.Show End Sub Private Sub CMDUPD_Click() Call txttodb rs1.Update MsgBox "record uupdated" End Sub Public Sub txttodb() rs1.Fields(0) = Text1.Text rs1.Fields(1) = Text2.Text rs1.Fields(2) = Text3.Text rs1.Fields(3) = Text4.Text rs1.Fields(4) = Text5.Text End Sub

Public Sub dbtotxt() Text1.Text = rs1.Fields(0) Text2.Text = rs1.Fields(1) Text3.Text = rs1.Fields(2) Text4.Text = rs1.Fields(3) Text5.Text = rs1.Fields(4) End Sub Private Sub CMDVIEW_Click() Dim z z = InputBox(RTrim(LTrim("enter the customer no. : "))) rs1.MoveFirst rs1.Find ("custno='" & z & "'") If rs1.EOF Then MsgBox "no such customer no." Else Call dbtotxt End If End Sub Private Sub Form_Load() cn1.Open ("dsn=anudeep;uid=05mca02;pwd=********;") rs1.Open ("select * from gas"), cn1, adOpenDynamic, adLockOptimistic If rs1.EOF And rs1.BOF Then MsgBox "no record exists" End If End Sub Public Sub txtoclear() Text1.Text = "" Text2.Text = "" Text3.Text = "" Text4.Text = "" Text5.Text = "" End Sub

FORM 3: Dim cn2 As New ADODB.Connection Dim rs2 As New ADODB.Recordset Private Sub CMDADD1_Click() Call txtoclear rs2.AddNew Text1.SetFocus End Sub Private Sub CMDDEL1_Click() Dim z z = InputBox(RTrim(LTrim("enter the cusotmer no.:"))) rs2.MoveFirst rs2.Find ("custno='" & z & "'") If rs2.EOF Then MsgBox "no such customer no." Else Call dbtotxt rs2.Delete MsgBox "record deleted" End If Call txtoclear End Sub Private Sub cmdmain1_Click() Form1.Show End Sub Private Sub CMDUPDATE1_Click() Call txttodb rs2.Update MsgBox "record uupdated" End Sub Public Sub txttodb() rs2.Fields(0) = Text1.Text rs2.Fields(1) = Text2.Text rs2.Fields(2) = Text3.Text rs2.Fields(3) = Text4.Text End Sub

Public Sub dbtotxt() Text1.Text = rs2.Fields(0) Text2.Text = rs2.Fields(1) Text3.Text = rs2.Fields(2) Text4.Text = rs2.Fields(3) End Sub Private Sub CMDVIEW1_Click() Dim z z = InputBox(RTrim(LTrim("enter the customer no. : "))) rs2.MoveFirst rs2.Find ("custno='" & z & "'") If rs2.EOF Then MsgBox "no such customer no." Else Call dbtotxt End If End Sub Private Sub Form_Load() cn2.Open ("dsn=anudeep;uid=05mca02;pwd=*******;") rs2.Open ("select * from gas1"), cn2, adOpenDynamic, adLockOptimistic If rs2.EOF And rs2.BOF Then MsgBox "no record exists" End If End Sub Public Sub txtoclear() Text1.Text = "" Text2.Text = "" Text3.Text = "" Text4.Text = "" End Sub

REPORT:

RESULT: Thus the given program is executed successfully.

BANK TRANSACTION SYSTEM


AIM: To write a program to design a bank transaction system using Visual BasicOracle. TABLE CREATION: SQL> create table tt(accno varchar2(4),min_bala number(4)); Table created. SQL> create table trans1(accno varchar2(4),min_bala number(4)); Table created. SQL> create table trans2(accno varchar2(4),min_bala number(4),dat date); Table created.

TRIGGERS: SQL> create or replace trigger trig_del11 after delete on tt for each row begin delete from trans1 where accno=:old.accno; end; Trigger created. SQL> create or replace trigger trig_up11 after update on tt for each row begin update trans1 set min_bala=:new.min_bala where accno=:old.accno; end; Trigger created. SQL> create or replace trigger trig_u22 after insert or update on tt for each row begin insert into trans2 values(:new.accno,:new.min_bala,sysdate); end; Trigger created.

FORM DESIGN:

CODING: FORM -1 Dim i As Integer Private Sub Command1_Click() If i < 3 Then If Trim(Text1.Text) = "admin" Or Trim(Text1.Text) = "ADMIN" And Trim(Text2.Text) = "MCA" Or Trim(Text2.Text) = "mca" Then Form2.Show Form1.Hide Else MsgBox "invalid user id and pwd", vbExclamation End If i=i+1 Else MsgBox "invalied access to login", vbCritical End End If End Sub Private Sub Command2_Click() Form3.Show Form1.Hide End Sub

FORM-2 Dim cn As New ADODB.Connection Dim rs As New ADODB.Recordset Dim rs1 As New ADODB.Recordset Private Sub Command1_Click() cn.Execute "insert into tt values('" & Text1.Text & "', '" & Text2.Text & "')" cn.Execute "insert into trans1(accno,min_bala) values('" & Text1.Text & "', '" & Text2.Text & "')" MsgBox "Record is inserted", vbDefaultButton4 End Sub Private Sub Command3_Click() Dim a a = InputBox(LTrim(RTrim("enter the acc no to be deleted"))) rs.MoveFirst rs.Find "accno='" & a & "'" If rs.EOF Then MsgBox "invalid acc no" Else cn.Execute "delete from tt where accno='" & a & "'" MsgBox "record deleted" End If End Sub Private Sub Command4_Click() End End Sub Private Sub Command5_Click() Form2.Show Form1.Hide End Sub Private Sub Command6_Click() DataReport1.Refresh DataReport1.Show End Sub

Private Sub Form_Load() cn.Open "dsn=usha;uid=05mca21;pwd=******;" rs.Open "select * from tt", cn, adOpenDynamic, adLockOptimistic rs1.Open "select * from trans1", cn, adOpenDynamic, adLockOptimistic If rs.EOF And rs.BOF Then MsgBox "no records" End If End Sub FORM-3 Dim cn As New ADODB.Connection Dim rs As New ADODB.Recordset Private Sub Command1_Click() rs.MoveFirst rs.Find "accno='" & Text1.Text & "'" If rs.EOF Then MsgBox "Invalid account no", vbApplicationModal Else Frame1.Enabled = True Text1.Enabled = False End If End Sub Private Sub Command2_Click() End End Sub Private Sub Form_Load() cn.Open "dsn=usha;uid=05mca21;pwd=******;" rs.Open "select * from tt", cn, adOpenDynamic, adLockOptimistic If rs.EOF And rs.BOF Then MsgBox "no records" End If Frame1.Enabled = False End Sub Private Sub Option1_Click() Form4.Show Form3.Hide End Sub

Private Sub Option2_Click() Form5.Show Form3.Hide End Sub FORM-4 Dim cn As New ADODB.Connection Dim rs As New ADODB.Recordset Private Sub Command1_Click() Dim X, Y, amt As Integer rs.MoveFirst X = LTrim(RTrim(Text1.Text)) rs.Find "accno='" & X & "'" If rs.EOF Then MsgBox "invalid acc no" Else Y = Val(Text2.Text) amt = rs.Fields(1) - Y cn.Execute "update tt set min_bala='" & amt & "' where accno='" & X & "'" Label3.Caption = amt MsgBox "record upt" End If End Sub Private Sub Command2_Click() Form3.Show Form4.Hide End Sub Private Sub Command3_Click() Frame2.Enabled = True Dim a a = InputBox(LTrim(RTrim("enter the acc no="))) rs.MoveFirst rs.Find "accno='" & a & "'" If rs.EOF Then MsgBox "no such account no", vbCritical Else Label3.Caption = "Rs." & rs.Fields(1) Label4.Caption = rs.Fields(0) End If End Sub

Private Sub Form_Load() cn.Open "dsn=usha;uid=05mca21;pwd=******;" rs.Open "select * from tt", cn, adOpenDynamic, adLockOptimistic If rs.EOF And rs.BOF Then MsgBox "no records" End If Frame2.Enabled = False End Sub FORM-5 Dim cn As New ADODB.Connection Dim rs As New ADODB.Recordset Private Sub Command1_Click() Dim X, Y, amt rs.MoveFirst X = LTrim(RTrim(Text1.Text)) rs.Find "accno='" & X & "'" If rs.EOF Then MsgBox "invalid acc no" Else Y = Val(Text2.Text) amt = rs.Fields(1) + Y cn.Execute "update tt set min_bala='" & amt & "' where accno='" & X & "'" Label4.Caption = amt MsgBox "record upt" End If 'Dim x, acc, amt 'x = Val(Text1.Text) 'acc = rs.Fields(0) 'amt = rs.Fields(1) - x 'cn.Execute "update tt set bal='" & amt & "' where accno='" & acc & "'" 'MsgBox "record upt" End Sub Private Sub Command2_Click() Form3.Show Form5.Hide End Sub

Private Sub Command3_Click() Frame2.Enabled = True Dim a a = InputBox(LTrim(RTrim("enter the acc no="))) rs.MoveFirst rs.Find "accno='" & a & "'" If rs.EOF Then MsgBox "no such account no", vbCritical Else Label4.Caption = "Rs." & rs.Fields(1) Label6.Caption = rs.Fields(0) End If End Sub Private Sub Form_Load() cn.Open "dsn=usha;uid=05mca21;pwd=******;" rs.Open "select * from tt", cn, adOpenDynamic, adLockOptimistic If rs.EOF And rs.BOF Then MsgBox "no records" End If Frame2.Enabled = False End Sub REPORT:

RESULT: Thus the given program is executed successfully.

CURSORS
AIM: Create a PL/SQL block that deletes students records whose department is EEE and display the number of records deleted and the remaining number of records. TABLE CREATION: SQL> create table stue21(rollno number(3),name varchar2(15),dept varchar2(14),marks number(3),total number(3)); Table created. TABLE INSERTION: SQL> insert into stue21(rollno,name,dept,marks,total)values(&rollno,'&name','&dept',&marks,&total); Enter value for rollno: 1 Enter value for name: aaa Enter value for dept: ee Enter value for marks: 67 Enter value for total: 78 old 1: insert into stue21(rollno,name,dept,marks,total)values(&rollno,'&name','&dept',&marks,&tota new 1: insert into stue21(rollno,name,dept,marks,total)values(1,'aaa','ee',67,78) 1 row created. SQL> / Enter value for rollno: 2 Enter value for name: bbb Enter value for dept: eee Enter value for marks: 56 Enter value for total: 67 old 1: insert into stue21(rollno,name,dept,marks,total)values(&rollno,'&name','&dept',&marks,&tota new 1: insert into stue21(rollno,name,dept,marks,total)values(2,'bbb','eee',56,67) 1 row created.

SQL> / Enter value for rollno: 3 Enter value for name: ccc Enter value for dept: ece Enter value for marks: 78 Enter value for total: 89 old 1: insert into stue21(rollno,name,dept,marks,total)values(&rollno,'&name','&dept',&marks,&tota new 1: insert into stue21(rollno,name,dept,marks,total)values(3,'ccc','ece',78,89) 1 row created. SQL> / Enter value for rollno: 4 Enter value for name: ddd Enter value for dept: cse Enter value for marks: 56 Enter value for total: 67 old 1: insert into stue21(rollno,name,dept,marks,total)values(&rollno,'&name','&dept',&marks,&tota new 1: insert into stue21(rollno,name,dept,marks,total)values(4,'ddd','cse',56,67) 1 row created. SQL> / Enter value for rollno: 5 Enter value for name: eee Enter value for dept: it Enter value for marks: 78 Enter value for total: 89 old 1: insert into stue21(rollno,name,dept,marks,total)values(&rollno,'&name','&dept',&marks,&tota new 1: insert into stue21(rollno,name,dept,marks,total)values(5,'eee','it',78,89) 1 row created. TABLE-RECORDS: SQL> select * from stue21; ROLLNO NAME ------------- ------------1 aaa 2 bbb 3 ccc 4 ddd 5 eee DEPT MARKS TOTAL ------------ ------------- ---------ee 67 78 eee 56 67 ece 78 89 cse 56 67 it 78 89

CODING: SQL> declare 2 cursor c is select * from studente21; 3 a%rowtype; 4 n number:=0; 5 begin 6 open c; 7 loop 8 fetch c into a; 9 exit when c% notfound; 10 if a.dept='EEE' then 11 delete from student where dept='EEE'; 12 n:=n+1; 13 endif; 14 end loop; 15 dbms_output.putline('Deleted Records'||n); 16 dbms_output.putline('Remaining Records'||(c%rowcount-n)); 17 close c; 18 commit 19 end; 20 / deleted records 1 Remaining records 4 SQL> select * from stue21; ROLLNO NAME ------------- ------------1 aaa 3 ccc 4 ddd 5 eee DEPT MARKS TOTAL ------------ ------------- ---------ee 67 78 ece 78 89 cse 56 67 it 78 89

RESULT: Thus, the above PL/SQL program using cursors is executed successfully.

CURSORS
AIM: Create a PL/SQL block to determine the top 5 scores from the student table and to insert these records into a new table. TABLE CREATION: SQL> create table stuee21(rollno number(3),name varchar2(15),dept varchar2(10),total number(3)); Table created. SQL> create table tempee22(rollno number(3),name varchar2(14),dept varchar2(14),total number(3)); Table created. TABLE INSERTION: SQL> insert into stuee21(rollno,name,dept,total)values(&rollno,'&name','&dept',&total) Enter value for rollno: 1 Enter value for name: aaa Enter value for dept: eee Enter value for total: 78 old 1: insert into stuee21(rollno,name,dept,total)values(&rollno,'&name','&dept',&total) new 1: insert into stuee21(rollno,name,dept,total)values(1,'aaa','eee',78) 1 row created. SQL> / Enter value for rollno: 2 Enter value for name: xxx Enter value for dept: ece Enter value for total: 89 old 1: insert into stuee21(rollno,name,dept,total)values(&rollno,'&name','&dept',&total) new 1: insert into stuee21(rollno,name,dept,total)values(2,'xxx','ece',89) 1 row created.

SQL> / Enter value for rollno: 3 Enter value for name: ddd Enter value for dept: it Enter value for total: 98 old 1: insert into stuee21(rollno,name,dept,total)values(&rollno,'&name','&dept',&total) new 1: insert into stuee21(rollno,name,dept,total)values(3,'ddd','it',98) 1 row created. SQL> / Enter value for rollno: 4 Enter value for name: rrr Enter value for dept: mca Enter value for total: 99 old 1: insert into stuee21(rollno,name,dept,total)values(&rollno,'&name','&dept',&total) new 1: insert into stuee21(rollno,name,dept,total)values(4,'rrr','mca',99) 1 row created. SQL> / Enter value for rollno: 5 Enter value for name: fff Enter value for dept: cse Enter value for total: 67 old 1: insert into stuee21(rollno,name,dept,total)values(&rollno,'&name','&dept',&total) new 1: insert into stuee21(rollno,name,dept,total)values(5,'fff','cse',67) 1 row created. TABLE-RECORDS: SQL> select * from stuee21; ROLLNO ---------------1 2 3 4 5 NAME ---------aaa xxx ddd rrr fff DEPT TOTAL ----------- --------------eee 78 ece 89 it 98 mca 99 cse 67

CODING: SQL> set serveroutput on; SQL> declare 2 cursor c is select * from stuee21 order by total desc; 3 a stuee21%rowtype; 4 n number:=0; 5 begin 6 open c; 7 loop 8 fetch c into a; 9 exit when c%notfound or c%rowcount>5; 10 insert into tempee22 values(a.rollno,a.name,a.dept,a.total); 11 end loop; 12 close c; 13 commit; 14 end; 15 / PL/SQL procedure successfully completed. DISPLAY TABLE: SQL> select * from tempee22; ROLLNO NAME DEPT TOTAL -------------- ----------- -------------- -------------4 rrr mca 99 3 ddd it 98 2 xxx ece 89 1 aaa eee 78 5 fff cse 67

RESULT: Thus, the above PL/SQL program using cursors is executed successfully.

PROCEDURE
AIM: Create a procedure to insert the total grade values into the student table. TABLE CREATION: SQL> create table stue23(rollno number(3),name varchar2(15),dept varchar2(15),m1 number(3),m2 number (3),m3 number(3),total number(3),grade varchar2(2)); Table created. TABLE DESCRIPTION: SQL> desc stue23; Name Null? Type ---------------------------------------------------------ROLLNO NUMBER(3) NAME VARCHAR2(15) M1 NUMBER(3) M2 NUMBER(3) M3 NUMBER(3) TOTAL NUMBER(3) GRADE VARCHAR2(2)

INSERTING RECORDS: SQL> insert into stue23(rollno,name,dept,m1,m2,m3)values(&rollno,'&name','&dept',&m1,&m2,&m3); Enter value for rollno: 1 Enter value for name: aaa Enter value for dept: eee Enter value for m1: 78 Enter value for m2: 77 Enter value for m3: 75 1 row created.

SQL> / Enter value for rollno: 2 Enter value for name: bbb Enter value for dept: ece Enter value for m1: 56 Enter value for m2: 67 Enter value for m3: 78 1 row created. SQL> / Enter value for rollno: 3 Enter value for name: ccc Enter value for dept: it Enter value for m1: 89 Enter value for m2: 98 Enter value for m3: 87 1 row created. SQL> / Enter value for rollno: 4 Enter value for name: ddd Enter value for dept: cse Enter value for m1: 78 Enter value for m2: 89 Enter value for m3: 98 1 row created. SQL> / Enter value for rollno: 5 Enter value for name: eee Enter value for dept: bme Enter value for m1: 78 Enter value for m2: 85 Enter value for m3: 64 1 row created.

TABLE - RECORDS: SQL> select * from stue23; ROLLNO NAME DEPT M1 M2 M3 TOTAL GR ------------ ----------- -------- ----- ----- ----- ---------- -----1 aaa eee 78 77 75 2 bbb ece 56 67 78 3 ccc it 89 98 87 4 ddd cse 78 89 98 5 eee bme 78 85 64

CODING: SQL> create or replace procedure p1 is cursor c is select rollno,name,m1,m2,m3 from stue23; 2 a c%rowtype; 3 tot number; 4 g char; 5 begin 6 open c; 7 loop 8 fetch c into a; 9 exit when c%notfound; 10 tot:=a.m1+a.m2+a.m3; 11 if a.m1>50 and a.m2>50 and a.m3>50 then 12 g:='A'; 13 else 14 g:='F'; 15 end if; 16 update stue23 set total=tot,grade=g where rollno=a.rollno; 17 end loop; 18 commit; 19 close c; 20* end; SQL> / Procedure created. SQL> exec p1; OR execute p1; PL/SQL procedure successfully completed.

TABLE-RECORDS: SQL> select * from stue23; ROLLNO NAME DEPT M1 M2 M3 TOTAL GR ------------------------------------------------------------------------1 aaa eee 78 77 75 230 A 2 bbb ece 56 67 78 201 A 3 ccc it 89 98 87 274 A 4 ddd cse 78 89 98 265 A 5 eee bme 78 85 64 227 A

RESULT: Thus, the above procedure inserts the total grade values into the student table using procedures successfully.

Potrebbero piacerti anche