sql lab manual

144
DATA DEFINITION LANGUAGE The DDL commands are: Create Alter Drop Rename Truncate CUSTOMER DETAILS TABLE STRUCTURE-1 1. Create command:- SYNTAX:- SQL> create table <table_name>(fieldname1 Datatype1, FIELD NAME TYPE SIZE CONSTRAINTS CUST_ID NUMBER 3 --- CUST_FNAME ALPHANUMERIC 14 --- CUST_LNAME ALPHANUMERIC 14 --- CITY ALPHANUMERIC 14 --- PHONENO NUMBER 10 ---

Upload: siva-kumar

Post on 30-Oct-2014

87 views

Category:

Documents


2 download

DESCRIPTION

all progaramming procedure of sql is described.

TRANSCRIPT

Page 1: sql lab manual

DATA DEFINITION LANGUAGE

The DDL commands are:

Create Alter Drop Rename Truncate

CUSTOMER DETAILS

TABLE STRUCTURE-1

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.

FIELD NAME TYPE SIZE CONSTRAINTS

CUST_ID NUMBER 3 ---

CUST_FNAME ALPHANUMERIC 14 ---

CUST_LNAME ALPHANUMERIC 14 ---

CITY ALPHANUMERIC 14 ---

PHONENO NUMBER 10 ---

Page 2: sql lab manual

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 TYPE SIZE CONSTRAINTS

EMP_ID ALPHANUMERIC 3 PK

EMP_FNAME ALPHANUMERIC 14 ---

EMP_LNAME ALPHANUMERIC 14 ---

DOJ DATE --- ---

BASIC PAY NUMBER 10,2 ---

CREATE COMMAND

SYNTAX:-

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

EXAMPLE:-

Page 3: sql lab manual

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

Page 4: sql lab manual

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)

Page 5: sql lab manual

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

Page 6: sql lab manual

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:-

Page 7: sql lab manual

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)

Page 8: sql lab manual

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.

Page 9: sql lab manual

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.

Page 10: sql lab manual

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 TYPE SIZE CONSTRAINTS

EMP_ID ALPHANUMERIC 3 PK

EMP_FNAME ALPHANUMERIC 14 ---

EMP_LNAME ALPHANUMERIC 14 ---

DOJ DATE --- ---

BASIC PAY NUMBER 10,2 ---

1. Insert Command

SYNTAX:-

FOR INSERTING A SINGLE RECORD

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

Page 11: sql lab manual

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: 01Enter value for emp_fname: amberEnter value for emp_lname: marienneEnter value for doj: 01/jan/2000Enter value for basic: 18000Enter value for address: france

1 row created.

SQL> /Enter value for empid: 04Enter value for emp_fname: kirstenEnter value for emp_lname: grayEnter value for doj: 06/may/2000Enter value for basic: 16500Enter value for address: usa

1 row created.

SQL> /Enter value for empid: 08Enter value for emp_fname: sofiaEnter value for emp_lname: ruthEnter value for doj: 05/may/2001Enter value for basic: 16000Enter value for address: usa

1 row created.

SQL> /Enter value for empid: 10

Page 12: sql lab manual

Enter value for emp_fname: natashaEnter value for emp_lname: jenaEnter value for doj: 12/june/2001Enter value for basic: 15800Enter value for address: india

1 row created.

SQL> /Enter value for empid: 16Enter value for emp_fname: louisaEnter value for emp_lname: bethEnter value for doj: 19/july/2001Enter value for basic: 15000Enter value for address: belgium

1 row created.

TABLE-RECORDS:-

SQL> select * from empl21;

EM EMP_FNAME EMP_LNAME DOJ BASIC ADDRESS-- -------------------- ------------------- ------------ ---------- ---------------01 amber marienne 01-JAN-00 18000 france04 kirsten gray 06-MAY-00 16500 usa08 sofia ruth 05-MAY-01 16000 usa10 natasha jena 12-JUN-01 15800 india16 louisa beth 19-JUL-01 15000 belgium

INSERT VALUES INTO CHILD TABLE

SQL> insert into empl21_child(empid)values('&empid');Enter value for empid: 01old 1: insert into empl21_child(empid)values('&empid')new 1: insert into empl21_child(empid)values('01')

1 row created.

SQL> /

Page 13: sql lab manual

Enter value for empid: 04old 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: 08old 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: 10old 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: 16old 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----0104081016

2. Update Command

Page 14: sql lab manual

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 BASIC ADDRESS-- -------------------- ------------------- ------------ ---------- ---------------01 amber marienne 01-JAN-00 19500 france04 kirsten gray 06-MAY-00 18000 usa08 sofia ruth 05-MAY-01 17500 usa10 natasha jena 12-JUN-01 17300 india16 louisa beth 19-JUL-01 16500 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 BASIC ADDRESS-- -------------------- ------------------- ------------ ---------- ---------------*01 amber marienne 01-JAN-00 21500 france 04 kirsten gray 06-MAY-00 18000 usa 08 sofia ruth 05-MAY-01 17500 usa 10 natasha jena 12-JUN-01 17300 india 16 louisa beth 19-JUL-01 16500 belgium

3. Select Command

Page 15: sql lab manual

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 EMP_FNAME BASIC---- -------------------- ----------01 amber 2150004 kirsten 1800008 sofia 1750010 natasha 1730016 louisa 16500

3) List down the employee salaries in ascending order.

SYNTAX:-

SQL> select field_name1.. from <table_name> order by basic;

EXAMPLE:-

Page 16: sql lab manual

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

Page 17: sql lab manual

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 usa10 natasha jena 12-JUN-01 17300 india16 louisa beth 19-JUL-01 16500 belgium

RESULT:-

Thus, the above DML commands are executed successfully.

Page 18: sql lab manual

DATA CONTROL LANGUAGE

The DCL commands are:

Commit Rollback Save point Grant Revoke

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:

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;

Page 19: sql lab manual

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;

Page 20: sql lab manual

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:

Page 21: sql lab manual

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

Page 22: sql lab manual

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.

Page 23: sql lab manual

STUDENT MARKLIST-1

AIM:

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

TABLE STRUCTURE:

FIELD NAME DATATYPE SIZE CONSTRAINTS

Roll No. Numeric 4 Primary Key

Student Name Alpha Numeric 14 Not Null

Mark 1 Numeric 3 Not Null

Mark 2 Numeric 3 Not null

Mark 3 Numeric 3 Not Null

Total Numeric 3 ---Nil---

Grade Characters 1 ---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.

Page 24: sql lab manual

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: 111Enter value for sname: aaaEnter value for mark1: 85Enter value for mark2: 89Enter value for mark3: 90

1 row created.

SQL> /

Enter value for rollno: 222Enter value for sname: bbbEnter value for mark1: 55Enter value for mark2: 60Enter value for mark3: 50

1 row created.

SQL> /

Enter value for rollno: 333Enter value for sname: cccEnter value for mark1: 50Enter value for mark2: 51Enter value for mark3: 47

1 row created.

Page 25: sql lab manual

SQL> /

Enter value for rollno: 444Enter value for sname: dddEnter value for mark1: 35Enter value for mark2: 40Enter 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.

Page 26: sql lab manual

QUERIES:

1) Display the student’s name which starts with the letter ‘s’.

SQL> select sname from studentmarklist where sname like 's%';

no rows selected

2) Display the student’s name which starts with the letter ‘a’.

SQL> select sname from studentmarklist where sname like 'a%';

SNAME-------------- aaa

3) Display the student’s 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 student’s name which ends with the letter ‘c’.

SQL> select sname from studentmarklist where sname like '%c';

SNAME----------- ccc

5) Display the student’s 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

Page 27: sql lab manual

6) Display the student’s 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 student’s 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 student’s 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.

Page 28: sql lab manual

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 Data type Size Constraints

Rollno Number 3 Primary key

Name Alphanumeric 14 Not Null

TABLE STRUCTURE 2:

Field name Data type Size Constraints

Rollno Number(3) 3 Foreign Key

Mark1 Number(3) 3 Between 0 and 100

Mark2 Number(3) 3 Between 0 and 100

Mark3 Number(3) 3 Between 0 and 100

Total Number(3) 3 -

Grade Alphanumeric 1 -

Page 29: sql lab manual

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)

Page 30: sql lab manual

INSERTING RECORDS:

SQL> insert into student(rollno,sname)values('&rollno','&sname');Enter value for rollno: 1Enter value for sname: suchi

1 row created.

SQL> /Enter value for rollno: 2Enter 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.

Page 31: sql lab manual

QUERIES:

1) To display the students records whose name starts with s :

SQL> select * from student where sname like 's%';

ROLLNO SNAME------------ -------------- 1 suchi 2 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

Page 32: sql lab manual

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.

Page 33: sql lab manual

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 Data type Size Constraints

Tno Number 10 Primary key

Occupation Varchar2 20 Not null

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

TABLE STRUCTURE:2

Field name Data type Size Constraints

Cust_name Varchar2 20 Not null

Cust_addr Varchar2 40 Not null

Cust_city Varchar2 10Not null

Std_con Varchar2 1Not null

Isd_con Varchar2 1 Not null

Page 34: sql lab manual

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 Null? Type -------------------------- ------------- ---------------------------- TNO NOT NULL NUMBER(10) OCCU NOT NULL VARCHAR2(20)

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.

TABLE DESCRIPTION:

SQL> desc tele;

Name Null? Type ----------------------------- ----------- ---------------------------- TNO NOT NULL NUMBER(10) OCCU NOT NULL VARCHAR2(20) CUST_NAME NOT NULL VARCHAR2(20) CUST_ADDR NOT NULL VARCHAR2(40) CUST_CITY NOT NULL VARCHAR2(10) STD_CON NOT NULL VARCHAR2(1) ISD_CON NOT NULL VARCHAR2(1)

Page 35: sql lab manual

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: 2343245Enter value for occu: managerEnter value for cust_name: kayalEnter value for cust_addr: no.12,new stEnter value for cust_city: pondyEnter value for std_con: y

1 row created.

SQL> /Enter value for tno: 989434523Enter value for occu: teacherEnter value for cust_name: manoEnter value for cust_addr: no.10 main roadEnter value for cust_city: chennaiEnter value for std_con: y

1 row created.

SQL> /Enter value for tno: 94434234Enter value for occu: doctorEnter value for cust_name: srinivasanEnter value for cust_addr: no.8,2nd crossEnter value for cust_city: pondyEnter value for std_con: n

1 row created.

SQL> /Enter value for tno: 9344719313Enter value for occu: studentEnter value for cust_name: haranEnter value for cust_addr: mariammanEnter value for cust_city: cuddaloreEnter value for std_con: n

1 row created.

Page 36: sql lab manual

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 y989434523 teacher mano no.10 main road chennai y n94434234 doctor srinivasan no.8,2nd cross pondy n n9344719313 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 y989434523 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

Page 37: sql lab manual

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 y989434523 teacher mano no.10 main road chennai y n94434234 doctor srinivasan no.8,2nd cross pondy n n9344719313 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 y2343245 manager kayal no.12,new st pondy y y989434523 teacher mano no.10 main road chennai y n94434234 doctor srinivasan no.8,2nd cross pondy 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 n989434523 teacher mano no.10 main road chennai y n2343245 manager kayal no.12,new st pondy y y9344719313 student haran mariamman cuddalore n y

Page 38: sql lab manual

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 y989434523 teacher mano no.10 main road chennai y n

RESULT:

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

Page 39: sql lab manual

EMPLOYEE INFORMATION SYSTEM

AIM :

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

TABLE STRUCTURE :

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.

Field name Data type Size Constraints

Empid Number 4 -

Emp_fname Varchar2 15 Primary key

Emp_lname Varchar2 15 -

Desigination Varchar2 14 -

Doj Date Date -

Salary Number 7,2 -

Depart no. Number 4 -

Page 40: sql lab manual

TABLE DESCRIPTION:

SQL> desc emp;

Name Null? Type --------------------------- ------------- ---------------------------- EMPID NOT NULL NUMBER(4) EMPFNAME VARCHAR2(15) EMPLNAME VARCHAR2(15) DESI VARCHAR2(14) DOJ DATE SALARY NUMBER(7,2) DEPARTNO 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: 110Enter value for empfname: kayalEnter value for emplname: vizhiEnter value for desi: managerEnter value for doj: 5-aug-05Enter value for salary: 10000Enter value for departno: 330

1 row created.

SQL> /Enter value for empid: 111Enter value for empfname: manoEnter value for emplname: haranEnter value for desi: engineerEnter value for doj: 7-jan-03Enter value for salary: 40000Enter value for departno: 331

1 row created.

Page 41: sql lab manual

SQL> /Enter value for empid: 112Enter value for empfname: deviEnter value for emplname: priyaEnter value for desi: lecturerEnter value for doj: 12-feb-04Enter value for salary: 15000Enter value for departno: 332

1 row created.

SQL> /Enter value for empid: 113Enter value for empfname: jackulineEnter value for emplname: maryEnter value for desi: studentEnter value for doj: 3-may-01Enter value for salary: 25000Enter 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-----------------manokayaljackulinedevi

Page 42: sql lab manual

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

Page 43: sql lab manual

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(*) 12*AVG(SALARY) ------------ ---------- ------------------------- 330 1 120000 331 1 480000 332 1 180000 333 1 300000

RESULT:

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

Page 44: sql lab manual

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: 3old 6: x:=&x;new 6: x:=3;Enter value for y: 3old 7: y:=&y;new 7: y:=3;

OUTPUT:

Sum=6

RESULT:

Thus the above PL/SQL program is executed successfully.

Page 45: sql lab manual

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: 5old 6: n: =&n;new 6: n: =5;

OUTPUT:

The factorial is: 120

RESULT:

Thus the above PL/SQL program is executed successfully.

Page 46: sql lab manual

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: 5old 6: n:=&n;new 6: n:=5;

OUTPUT:

Sum=15

RESULT:

Thus the above PL/SQL program is executed successfully.

Page 47: sql lab manual

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: 5old 8: n:=&n;new 8: n:=5;

OUTPUT:

The fibonacci series 0 1 1 2 3

RESULT:

Thus the above PL/SQL program is executed successfully.

Page 48: sql lab manual

TRIGGERS

AIM:

To write a PL/SQL program using triggers to do the following:a) Insert records to another table automaticallyb) Delete records in another table automaticallyc) Update records to another table automatically

TABLE STRUCTURE:

Table name :Trig1

Field name Data type Size

Roll no Numeric 3

Name Alphanumeric 15

Mark1 Numeric 3

Mark2 Numeric 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)

Page 49: sql lab manual

Table name : trig2

Field name Data type Size

Roll no Numeric 3

Name Alphanumeric 15

Mark1 Numeric 3

Mark2 Numeric 3

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.

Page 50: sql lab manual

INPUT:

Insertion:

SQL> insert into trig1(rno,name,mark1,mark2) values ('&rno','&name','&mark1','&mark2');Enter value for rno: 1Enter value for name: suchiEnter value for mark1: 8Enter value for mark2: 89

1 row created.

SQL> /Enter value for rno: 2Enter value for name: sruthiEnter value for mark1: 87Enter 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

Page 51: sql lab manual

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

Page 52: sql lab manual

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.

Page 53: sql lab manual

TRIGGERS

AIM:

To write a PL/SQL program using triggers to restrict insert, delete and Update operation in a table.

TABLE STRUCTURE:

Field name Data type Size

Loan no Numeric 4

Amount Numeric 5

Branch name Alphanumeric 14

TABLE CREATION:

SQL> create table loan( loanno number(4),amount number(5),bn varchar2(14) );

Table created.

TABLE DESCRIPTION:

SQL> desc loan;

Name Null? Type --------------- -------- --------------------- LOANNO NUMBER(4) AMOUNT NUMBER(5) BN VARCHAR2(14)

INSERTING RECORDS:

SQL> insert into loan values( ‘111’,’4000’,'mount road' );

1 row created.

Page 54: sql lab manual

SQL> insert into loan values(‘222’,’5000’,'anna salai');

1 row created.

SQL> select * from loan;

LOANNO AMOUNT BN -------------- -------------- ------------- 111 4000 mount road 222 5000 anna salai

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 possibleORA-06512: at "05MCA45.TR_IN_UP_DE", line 3ORA-04088: error during execution of trigger '05MCA45.TR_IN_UP_DE'

Page 55: sql lab manual

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 possibleORA-06512: at "05MCA45.TR_IN_UP_DE", line 7ORA-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 possibleORA-06512: at "05MCA45.TR_IN_UP_DE", line 5ORA-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.

Page 56: sql lab manual

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.

Page 57: sql lab manual

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 Data type Size

User name Alphanumeric 15

System date Date -

Action Alphanumeric 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)

Page 58: sql lab manual

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.

Page 59: sql lab manual

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 Data type Size

User name Alphanumeric 15

System date Date -

Action Alphanumeric 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)

Page 60: sql lab manual

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 LOGDATE ACTION---------------- -------------- ------------ 05MCA02 25-SEP-06 log off

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

Page 61: sql lab manual

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 Alphanumeric 20 Notnull

Rollno Number 3 -

Marks Number 3 -

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)

Page 62: sql lab manual

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 allowedORA-06512: at "05MCA21.TRIG_RUST", line 2ORA-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 allowedORA-06512: at "05MCA21.TRIG_RUST", line 2ORA-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 allowedORA-06512: at "05MCA21.TRIG_RUST", line 2ORA-04088: error during execution of trigger '05MCA21.TRIG_RUST'

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

Page 63: sql lab manual

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

DATA TYPE

SIZE

CONSTRAINTS

Accno Alphanumeric 5

Not null

Street Alphanumeric 14 Not null

City Alphanumeric 14 Not null

Table structure-2:

FIELD NAME DATA TYPE SIZE CONSTRAINTS

Accno Alphanumeric 5 Not null

Branch_name Alphanumeric 14 Not null

Branch_code Alphanumeric 14 Not null

Page 64: sql lab manual

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 Null? Type ------------- ------------- ---------------------- ACCNO NOT NULL VARCHAR2(5) STREET VARCHAR2(14) CITY 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? Type ---------------- ----------- ------------- ACCNO VARCHAR2(5) BRANCH_NAME VARCHAR2(14) BRANCH_CODE VARCHAR2(14)

Page 65: sql lab manual

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 STREET CITY----------- -------------- --------------a123 nehru st pondy

SQL> select * from account;

ACCNO BRANCH_NAME BRANCH_CODE----------- ------------------------- -----------------------a123 Mount road chennai

DISABLE TRIGGERS:

SQL> alter table cus disable all triggers;

Table altered.

Page 66: sql lab manual

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 BRANCH_NAME BRANCH_CODE----------- ------------------------- -----------------------a123 Mount road chennai12 xyz x222

TABLE-RECORDS:

SQL> select * from cus;

ACCNO STREET CITY----------- -------------- -------------a123 nehru st pondy12 zigzag rd 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.

Page 67: sql lab manual

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 BRANCH_NAME BRANCH_CODE----------- ------------------------ ------------------------ a123 hyd chennai

12 xyz Q

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.

Page 68: sql lab manual

UPDATING RECORDS:

SQL> update account set branch_name='banglore' where accno='12';

1 row updated.

TABLE -RECORDS:

SQL> select * from cus;

ACCNO STREET CITY----------- -------------- -------------- a123 nehru st hyd 12 jain st delhi

TABLE -RECORDS:

SQL> select * from account;

ACCNO BRANCH_NAME BRANCH_CODE----------- -------------------------- ------------------------ a123 hyd chennai 12 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

Page 69: sql lab manual

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 BRANCH_NAME BRANCH_CODE----------- ------------------------ ------------------------- 12 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.

Page 70: sql lab manual

DELETING RECORDS:

SQL> delete from account where accno='12';

1 row deleted.

TABLE RECORDS:

SQL> select * from cus;

ACCNO STREET CITY------------ --------------- ------------- a222 kgu st hyd

TABLE RECORDS:

SQL> select * from account;

ACCNO BRANCH_NAME BRANCH_CODE----------- ------------------------- -------------------------- a222 vivek vihar b167

RESULT:

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

Page 71: sql lab manual

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));

Page 72: sql lab manual

FORM DESIGN:

Page 73: sql lab manual

CODING:

Dim cn As New ADODB.ConnectionDim rs As New ADODB.Recordset

Private Sub ADD_Click()Call txtclearrs.AddNewText1.SetFocusEnd Sub

Private Sub DELETE_Click()Dim YY = InputBox(RTrim(LTrim(" ENTER THE ROLL NUMBER TO BE DELETED ")))rs.MoveFirstrs.Find ("rollno='" & Y & "'")If rs.EOF Then MsgBox (" NO SUCH RECORD ")Else Call dbtotxt rs.DELETEEnd IfEnd Sub

Private Sub EDIT_Click()Text1.SetFocusEnd Sub

Private Sub first_Click()Call dbtotxtrs.MoveFirstEnd Sub

Private Sub Form_Load()cn.Open ("dsn=kaja;uid=05mca17;pwd=*****;")rs.Open ("select * from student"), cn, adOpenKeyset, adLockOptimisticIf rs.EOF And rs.BOF Then MsgBox " RECORD NOT FOUND "End IfEnd Sub

Private Sub last_Click()Call dbtotxtrs.MoveLastEnd Sub

Page 74: sql lab manual

Private Sub next_Click()rs.MoveNextIf Not rs.EOF Then Call dbtotxtElse rs.MoveLastEnd IfEnd Sub

Private Sub previous_Click()rs.MovePreviousIf Not rs.EOF Then Call dbtotxtElse rs.MoveFirstEnd IfEnd Sub

Private Sub QUIT_Click()EndEnd Sub

Private Sub REPORT_Click()DataReport1.ShowEnd Sub

Private Sub SAVE_Click()Call txttodbrs.UpdateMsgBox " SAVED SUCCESSFULLY "End SubPublic 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 = ""

Page 75: sql lab manual

End SubPublic Sub txttodb()rs.Fields(0) = Text1.Textrs.Fields(1) = Text2.Textrs.Fields(2) = Text3.Textrs.Fields(3) = Text4.Textrs.Fields(4) = Text5.Textrs.Fields(5) = Text6.Textrs.Fields(6) = Text7.Textrs.Fields(7) = Text8.Textrs.Fields(8) = Text9.Textrs.Fields(9) = Text10.Textrs.Fields(10) = Text11.Textrs.Fields(11) = Text12.Textrs.Fields(12) = Text13.TextEnd 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 YY = InputBox(RTrim(LTrim(" ENTER THE ROLL NUMBER TO BE DELETED ")))rs.MoveFirstrs.Find ("rollno= ' " & Y & "'")If rs.EOF Then MsgBox (" NO SUCH RECORD ")Else Call dbtotxtEnd IfEnd Sub

Page 76: sql lab manual

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) / 5If (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 IfEnd Sub

REPORT:

RESULT:

Thus the given program is executed successfully.

Page 77: sql lab manual

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.

Page 78: sql lab manual

FORM DESIGN:

Page 79: sql lab manual

CODING:

FORM 1:

Private Sub cmdbookdetails_Click()Form3.ShowEnd Sub

Private Sub Cmdcustdetails_Click()Form2.ShowEnd Sub

Private Sub CMDEXIT_Click()EndEnd Sub

Private Sub cmdreport_Click()DataReport1.ShowEnd Sub

Page 80: sql lab manual

FORM 2:

Dim cn1 As New ADODB.ConnectionDim rs1 As New ADODB.Recordset Private Sub CMDADD_Click()Call txtoclearrs1.AddNewText1.SetFocusEnd Sub

Private Sub CMDDEL_Click()Dim zz = InputBox(RTrim(LTrim("enter the cusotmer no.:")))rs1.MoveFirstrs1.Find ("custno='" & z & "'")If rs1.EOF ThenMsgBox "no such customer no."ElseCall dbtotxtrs1.DeleteEnd IfCall txtoclearEnd Sub

Private Sub CMDMAIN_Click()Form1.ShowEnd Sub

Private Sub CMDUPD_Click()Call txttodbrs1.UpdateMsgBox "record uupdated"End Sub

Public Sub txttodb()rs1.Fields(0) = Text1.Textrs1.Fields(1) = Text2.Textrs1.Fields(2) = Text3.Textrs1.Fields(3) = Text4.Textrs1.Fields(4) = Text5.TextEnd Sub

Page 81: sql lab manual

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 zz = InputBox(RTrim(LTrim("enter the customer no. : ")))rs1.MoveFirstrs1.Find ("custno='" & z & "'")If rs1.EOF ThenMsgBox "no such customer no."ElseCall dbtotxtEnd IfEnd Sub

Private Sub Form_Load()cn1.Open ("dsn=anudeep;uid=05mca02;pwd=********;")rs1.Open ("select * from gas"), cn1, adOpenDynamic, adLockOptimisticIf rs1.EOF And rs1.BOF ThenMsgBox "no record exists"End IfEnd Sub

Public Sub txtoclear()Text1.Text = ""Text2.Text = ""Text3.Text = ""Text4.Text = ""Text5.Text = ""End Sub

Page 82: sql lab manual

FORM 3:

Dim cn2 As New ADODB.ConnectionDim rs2 As New ADODB.Recordset

Private Sub CMDADD1_Click()Call txtoclearrs2.AddNewText1.SetFocusEnd Sub

Private Sub CMDDEL1_Click()Dim zz = InputBox(RTrim(LTrim("enter the cusotmer no.:")))rs2.MoveFirstrs2.Find ("custno='" & z & "'")If rs2.EOF ThenMsgBox "no such customer no."ElseCall dbtotxtrs2.DeleteMsgBox "record deleted"End IfCall txtoclearEnd Sub

Private Sub cmdmain1_Click()Form1.ShowEnd Sub

Private Sub CMDUPDATE1_Click()Call txttodbrs2.UpdateMsgBox "record uupdated"End Sub

Public Sub txttodb()rs2.Fields(0) = Text1.Textrs2.Fields(1) = Text2.Textrs2.Fields(2) = Text3.Textrs2.Fields(3) = Text4.TextEnd Sub

Page 83: sql lab manual

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 zz = InputBox(RTrim(LTrim("enter the customer no. : ")))rs2.MoveFirstrs2.Find ("custno='" & z & "'")If rs2.EOF ThenMsgBox "no such customer no."ElseCall dbtotxtEnd IfEnd Sub

Private Sub Form_Load()cn2.Open ("dsn=anudeep;uid=05mca02;pwd=*******;")rs2.Open ("select * from gas1"), cn2, adOpenDynamic, adLockOptimisticIf rs2.EOF And rs2.BOF ThenMsgBox "no record exists"End IfEnd Sub

Public Sub txtoclear()Text1.Text = ""Text2.Text = ""Text3.Text = ""Text4.Text = ""End Sub

Page 84: sql lab manual

REPORT:

RESULT:

Thus the given program is executed successfully.

Page 85: sql lab manual

BANK TRANSACTION SYSTEM

AIM: To write a program to design a bank transaction system using Visual Basic- Oracle.

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.

Page 86: sql lab manual

FORM DESIGN:

Page 87: sql lab manual
Page 88: sql lab manual

CODING:

FORM -1

Dim i As Integer

Private Sub Command1_Click()If i < 3 ThenIf Trim(Text1.Text) = "admin" Or Trim(Text1.Text) = "ADMIN" And Trim(Text2.Text) = "MCA" Or Trim(Text2.Text) = "mca" Then Form2.Show Form1.HideElse MsgBox "invalid user id and pwd", vbExclamation End Ifi = i + 1Else MsgBox "invalied access to login", vbCritical EndEnd IfEnd Sub

Private Sub Command2_Click()Form3.ShowForm1.HideEnd Sub

Page 89: sql lab manual

FORM-2

Dim cn As New ADODB.ConnectionDim rs As New ADODB.RecordsetDim 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", vbDefaultButton4End Sub

Private Sub Command3_Click()Dim aa = InputBox(LTrim(RTrim("enter the acc no to be deleted")))rs.MoveFirstrs.Find "accno='" & a & "'"If rs.EOF Then MsgBox "invalid acc no"Else cn.Execute "delete from tt where accno='" & a & "'" MsgBox "record deleted"End IfEnd Sub

Private Sub Command4_Click()EndEnd Sub

Private Sub Command5_Click()Form2.ShowForm1.HideEnd Sub

Private Sub Command6_Click()DataReport1.RefreshDataReport1.ShowEnd Sub

Page 90: sql lab manual

Private Sub Form_Load()cn.Open "dsn=usha;uid=05mca21;pwd=******;"rs.Open "select * from tt", cn, adOpenDynamic, adLockOptimisticrs1.Open "select * from trans1", cn, adOpenDynamic, adLockOptimisticIf rs.EOF And rs.BOF Then MsgBox "no records" End IfEnd Sub

FORM-3

Dim cn As New ADODB.ConnectionDim rs As New ADODB.Recordset

Private Sub Command1_Click()rs.MoveFirstrs.Find "accno='" & Text1.Text & "'"If rs.EOF Then MsgBox "Invalid account no", vbApplicationModal Else Frame1.Enabled = True Text1.Enabled = FalseEnd IfEnd Sub

Private Sub Command2_Click()EndEnd Sub

Private Sub Form_Load()cn.Open "dsn=usha;uid=05mca21;pwd=******;"rs.Open "select * from tt", cn, adOpenDynamic, adLockOptimisticIf rs.EOF And rs.BOF Then MsgBox "no records" End If Frame1.Enabled = FalseEnd Sub

Private Sub Option1_Click()Form4.ShowForm3.HideEnd Sub

Page 91: sql lab manual

Private Sub Option2_Click()Form5.ShowForm3.HideEnd Sub

FORM-4

Dim cn As New ADODB.ConnectionDim rs As New ADODB.Recordset

Private Sub Command1_Click()Dim X, Y, amt As Integerrs.MoveFirstX = 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 IfEnd Sub

Private Sub Command2_Click()Form3.ShowForm4.HideEnd Sub

Private Sub Command3_Click()Frame2.Enabled = TrueDim aa = InputBox(LTrim(RTrim("enter the acc no=")))rs.MoveFirstrs.Find "accno='" & a & "'"If rs.EOF Then MsgBox "no such account no", vbCriticalElseLabel3.Caption = "Rs." & rs.Fields(1)Label4.Caption = rs.Fields(0)End IfEnd Sub

Page 92: sql lab manual

Private Sub Form_Load()cn.Open "dsn=usha;uid=05mca21;pwd=******;"rs.Open "select * from tt", cn, adOpenDynamic, adLockOptimisticIf rs.EOF And rs.BOF Then MsgBox "no records" End IfFrame2.Enabled = FalseEnd Sub

FORM-5

Dim cn As New ADODB.ConnectionDim rs As New ADODB.Recordset

Private Sub Command1_Click()Dim X, Y, amtrs.MoveFirstX = 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.ShowForm5.HideEnd Sub

Page 93: sql lab manual

Private Sub Command3_Click()Frame2.Enabled = TrueDim aa = InputBox(LTrim(RTrim("enter the acc no=")))rs.MoveFirstrs.Find "accno='" & a & "'"If rs.EOF Then MsgBox "no such account no", vbCriticalElseLabel4.Caption = "Rs." & rs.Fields(1)Label6.Caption = rs.Fields(0)End IfEnd Sub

Private Sub Form_Load()cn.Open "dsn=usha;uid=05mca21;pwd=******;"rs.Open "select * from tt", cn, adOpenDynamic, adLockOptimisticIf rs.EOF And rs.BOF Then MsgBox "no records" End IfFrame2.Enabled = FalseEnd Sub

REPORT:

RESULT:

Thus the given program is executed successfully.

Page 94: sql lab manual

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: 1Enter value for name: aaaEnter value for dept: eeEnter value for marks: 67Enter value for total: 78old 1: insert into stue21(rollno,name,dept,marks,total)values(&rollno,'&name','&dept',&marks,&totanew 1: insert into stue21(rollno,name,dept,marks,total)values(1,'aaa','ee',67,78)

1 row created.

SQL> /Enter value for rollno: 2Enter value for name: bbbEnter value for dept: eeeEnter value for marks: 56Enter value for total: 67old 1: insert into stue21(rollno,name,dept,marks,total)values(&rollno,'&name','&dept',&marks,&totanew 1: insert into stue21(rollno,name,dept,marks,total)values(2,'bbb','eee',56,67)

1 row created.

Page 95: sql lab manual

SQL> /Enter value for rollno: 3Enter value for name: cccEnter value for dept: eceEnter value for marks: 78Enter value for total: 89old 1: insert into stue21(rollno,name,dept,marks,total)values(&rollno,'&name','&dept',&marks,&totanew 1: insert into stue21(rollno,name,dept,marks,total)values(3,'ccc','ece',78,89)1 row created.

SQL> /Enter value for rollno: 4Enter value for name: dddEnter value for dept: cseEnter value for marks: 56Enter value for total: 67old 1: insert into stue21(rollno,name,dept,marks,total)values(&rollno,'&name','&dept',&marks,&totanew 1: insert into stue21(rollno,name,dept,marks,total)values(4,'ddd','cse',56,67)

1 row created.

SQL> /Enter value for rollno: 5Enter value for name: eeeEnter value for dept: itEnter value for marks: 78Enter value for total: 89old 1: insert into stue21(rollno,name,dept,marks,total)values(&rollno,'&name','&dept',&marks,&totanew 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 DEPT MARKS TOTAL------------- ------------- ------------ ------------- ---------- 1 aaa ee 67 78 2 bbb eee 56 67 3 ccc ece 78 89 4 ddd cse 56 67 5 eee it 78 89

Page 96: sql lab manual

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 1Remaining records 4

SQL> select * from stue21;

ROLLNO NAME DEPT MARKS TOTAL------------- ------------- ------------ ------------- ---------- 1 aaa ee 67 78 3 ccc ece 78 89 4 ddd cse 56 67 5 eee it 78 89

RESULT:

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

Page 97: sql lab manual

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: 1Enter value for name: aaaEnter value for dept: eeeEnter value for total: 78old 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: 2Enter value for name: xxxEnter value for dept: eceEnter value for total: 89old 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.

Page 98: sql lab manual

SQL> /Enter value for rollno: 3Enter value for name: dddEnter value for dept: itEnter value for total: 98old 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: 4Enter value for name: rrrEnter value for dept: mcaEnter value for total: 99old 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: 5Enter value for name: fffEnter value for dept: cseEnter value for total: 67old 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 NAME DEPT TOTAL---------------- ---------- ----------- --------------- 1 aaa eee 78 2 xxx ece 89 3 ddd it 98 4 rrr mca 99 5 fff cse 67

Page 99: sql lab manual

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.

Page 100: sql lab manual

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: 1Enter value for name: aaaEnter value for dept: eeeEnter value for m1: 78Enter value for m2: 77Enter value for m3: 75

1 row created.

Page 101: sql lab manual

SQL> /Enter value for rollno: 2Enter value for name: bbbEnter value for dept: eceEnter value for m1: 56Enter value for m2: 67Enter value for m3: 78

1 row created.

SQL> /Enter value for rollno: 3Enter value for name: cccEnter value for dept: itEnter value for m1: 89Enter value for m2: 98Enter value for m3: 87

1 row created.

SQL> /Enter value for rollno: 4Enter value for name: dddEnter value for dept: cseEnter value for m1: 78Enter value for m2: 89Enter value for m3: 98

1 row created.

SQL> /Enter value for rollno: 5Enter value for name: eeeEnter value for dept: bmeEnter value for m1: 78Enter value for m2: 85Enter value for m3: 64

1 row created.

Page 102: sql lab manual

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.

Page 103: sql lab manual

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.