You are on page 1of 104

Cs2258 DBMS LAB MANUAL CSE DEPARMENT JCET Page 1

CS2258
Database Management System
Lab Manual
(R-2009)



CS2258 Database Management Systems Lab
Lab Manual

Cs2258 DBMS LAB MANUAL CSE DEPARMENT JCET Page 2

LAB OBJECTIVE

Upon successful completion of this Lab the student will be able to:

Creating database objects
Modifying database objects
Manipulating the data
Retrieving the data from the database server
Performing database operations in a procedural manner using pl/sql
Performing database operations (create, update, modify, retrieve, etc.,) using front-end tools l
Design and Develop applications like banking, reservation system, etc.,


























Cs2258 DBMS LAB MANUAL CSE DEPARMENT JCET Page 3

Introduction
SQL is structure query language.
SQL contains different data types those are
1. char(size)
2. varchar(size)
3. varchar2(size)
4. date
5. number(p,s) //** P-PRECISION S-SCALE **//
6. number(size)
7. raw(size)
8. raw/long raw(size)
Different types of commands in SQL:
A).DDL commands: - To create a database objects
B).DML commands: - To manipulate data of a database objects
C).DQL command: - To retrieve the data from a database.
D).DCL/DTL commands: - To control the data of a database
























Cs2258 DBMS LAB MANUAL CSE DEPARMENT JCET Page 4


DDL commands:

AIM:
To create a DDL to perform creation of table, alter, modify and drop column.
CREATION OF TABLE:
SYNTAX:
create table<tablename>(c olumn1 datatype,column2 datatype...);
EXAMPLE:
SQL>create table std(sno number(5),sname varchar(20),age number(5),sdob date,sm1
number(4,2),sm2 number(4,2),sm3 number(4,4));
Table created.
SQL>insert into std values(101,AAA,16,03-jul-88,80,90,98);
1 row created.
SQL>insert into std values(102,BBB,18,04-aug-89,88,98,90);
1 row created.
OUTPUT:
Select * from std;
SNO SNAME AGE SDOB SM1 SM2 SM3
101 AAA 16 03-jul-88 80 90 98
102 BBB 18 04-aug-89 88 98 90
ALTER TABLE WITH ADD:
SQL>create table student(id number(5),name varchar(10),game varchar(20));
Table created.
Cs2258 DBMS LAB MANUAL CSE DEPARMENT JCET Page 5

SQL>insert into student values(1,mercy,cricket);
1 row created.
SYNTAX:
alter table<tablename>add(col1 datatype,col2 datatype..);
EXAMPLE:
SQL>alter table student add(age number(4));
SQL>insert into student values(2,sharmi,tennis,19);
OUTPUT:
ALTER: select * from student;
ID NAME GAME
1 Mercy Cricket
ADD: select * from student;
ID NAME GAME AGE
1 Mercy cricket
2 Sharmi Tennis 19
ALTER TABLE WITH MODIFY:
SYNTAX:
Alter table<tablename>modify(col1 datatype,col2 datatype..);
EXAMPLE:
SQL>alter table student modify(id number(6),game varchar(25));
OUTPUT:
MODIFY
desc student;
Cs2258 DBMS LAB MANUAL CSE DEPARMENT JCET Page 6

NAME NULL? TYPE
Id Number(6)
Name Varchar(20)
Game Varchar(25)
Age Number(4)
DROP:
SYNTAX: drop table<tablename>;
EXAMPLE:
SQL>drop table student;
SQL>Table dropped.
RESULT:
Thus the DDL commands have been executed successfully.
DDL COMMANDS (SOME MORE EXAMPLES)
1. The Create Table Command: - it defines each column of the table uniquely. Each column has
minimum of three attributes, a name , data type and size.
Syntax:
Create table <table name>(<col1><datatype>(<size>),<col2>
<datatype><size>));
Ex:
create table emp(empno number(4) primary key, ename char(10));
2. Modifying the structure of tables.
a) Add new columns
Syntax:
Alter table <tablename>add(<new col><datatype(size),<new
col>datatype(size));
Ex:
alter table emp add(sal number(7,2));

3. Dropping a column from a table.

Syntax:
Alter table <tablename>drop column <col>;
Cs2258 DBMS LAB MANUAL CSE DEPARMENT JCET Page 7


Ex:
alter table emp drop column sal;

4. Modifying existing columns.
Syntax:
Alter table <tablename>modify(<col><newdatatype>(<newsize>));
Ex:
alter table emp modify(ename varchar2(15));


5. Renaming the tables
Syntax:
Rename <oldtable>to <new table>;
Ex:
rename emp to emp1;

6. truncating the tables.
Syntax:
Truncate table <tablename>;
Ex:
trunc table emp1;

7. Destroying tables.
Syntax:
Drop table <tablename>;
Ex:
drop table emp;

DML COMMANDS
AIM:
To create a view for the purpose of display in order to hide the data.
VIEW:
A view is a logical data table that allows us to view data from other tables and values.
Cs2258 DBMS LAB MANUAL CSE DEPARMENT JCET Page 8

SYNTAX:
Create [or replace] view viewname as query;
TABLE:
SQL>create table std(sno number(5),sname varchar(20),sm1 number(3),sm2 number(3),sm3 number(3));
SQL>insert into std values(&sno,&sname,&sm1,&sm2,&sm3);
SQL>create table sport(spoid number(5),game varchar(20));
SQL>insert into sport values(&spoid,&game);
OUTPUT: select * from std;
SNO SNAME SM1 SM2 SM3
1 Mercy 90 89 90
2 Sharmi 90 89 90
3 Priya 78 89 70
4 Amri 67 78 90
5 Divya 98 78 50
select * from sport;
SPOID GAME
1 Cricket
2 Basketball
4 Tennis

QUERIES:
SQL>select std.sno,std.sname,sport.game from std,sport where std.sno=sport.spoid;
OUTPUT:
SNO SNAME GAME
1 Mercy Cricket
2 Sharmi Basketball
Cs2258 DBMS LAB MANUAL CSE DEPARMENT JCET Page 9

4 Amri Tennis
SQL>create or replace view college as select std.sno,std.sname,sport.spoid,
sport.game from std,sport where std.sno=sport.spoid;
View created.
OUTPUT:
select * from college;
SNO SNAME SPOID GAME
1 Mercy 1 Cricket
2 Sharmi 2 Basketball
4 Amri 4 Tennis
DROP VIEW:
To remove a view from the database, one has to use drop view.
drop view viewname;
drop view college;
view dropped;
SQL> select * from college;
Table or view does not exist.

RESULT:
Thus, the view has been created successfully.





Cs2258 DBMS LAB MANUAL CSE DEPARMENT JCET Page 10



DML commands (ADDITIONAL EXAMPLES):

1. Inserting Data into Tables: - once a table is created the most natural thing to do is load this
table with data to be manipulated later.

Syntax:
insert into <tablename>(<col1>,<col2>) values(<exp>,<exp>);

2. Delete operations.

a) Remove all rows
Syntax:
delete from <tablename>;

b) Removal of a specified row/s
Syntax:
delete from <tablename>where <condition>;

3. Updating the contents of a table.

a) Updating all rows
Syntax:
Update <tablename>set <col>=<exp>,<col>=<exp>;

b) Updating selected records.
Syntax:
Update <tablename> set <col>=<exp>,<col>=<exp>
where <condition>;

4. Types of data constrains.
a) Not null constraint at column level.
Syntax:
<col><datatype>(size)not null

b) unique constraint
Syntax:
Unique constraint at column level.
<col><datatype>(size)unique;

Cs2258 DBMS LAB MANUAL CSE DEPARMENT JCET Page 11

c) unique constraint at table level:
Syntax:
Create table tablename(col=format,col=format,unique(<col1>,<col2>);

d) Primary key constraint at column level
Syntax:
<col><datatype>(size)primary key;

e) Primary key constraint at table level.
Syntax:
Create table tablename(col=format,col=format
Primary key(col1>,<col2>);

f) Foreign key constraint at column level.
Syntax:
<col><datatype>(size>) references <tablename>[<col>];

g) Foreign key constraint at table level
Syntax:
foreign key(<col>[,<col>])references <tablename>[(<col>,<col>)

h) Check constraint
check constraint constraint at column level.
Syntax: <col><datatype>(size) check(<logical expression>)

i) Check constraint constraint at table level.
Syntax: check(<logical expression>)














Cs2258 DBMS LAB MANUAL CSE DEPARMENT JCET Page 12






DCL commands
Oracle provides extensive feature in order to safeguard information stored in its tables from
unauthoraised viewing and damage. The rights that allow the user of some or all oracle resources on the
server are called privileges.

a) Grant privileges using the GRANT statement

The grant statement provides various types of access to database objects such as tables, views and
sequences and so on.

Syntax:
GRANT <object privileges>
ON <objectname>
TO<username>
[WITH GRANT OPTION];

b) Revoke permissions using the REVOKE statement:

The REVOKE statement is used to deny the Grant given on an object.

Syntax:
REVOKE<object privilege>ON FROM<user name>;

Result:
Thus the DCL commands have been successfully executed and the results are verified.
Cs2258 DBMS LAB MANUAL CSE DEPARMENT JCET Page 13

SINGLE ROW FUNCTIONS
AIM:
To create a single row functions.
QUERY:
CHARACTER FUNCTIONS:
ASCII: It returns ascii decimal equivalent to a character.
SYNTAX:
ascii (string)
Ex: SQL>select ascii(A) from dual;
OUTPUT:
ASCII(A)
65
INSTR:
It will return first position of the character x in the string.
SYNTAX:
i) instr(string/column name,x)
Ex: SQL>select instr(sample,a) as position from dual;
OUTPUT:
POSITION
2
select instr(<colname>,chr) as position from <tablename>;
Ex: SQL>select instr(name,e) as position from student;

Cs2258 DBMS LAB MANUAL CSE DEPARMENT JCET Page 14

OUTPUT:
POSITION
2
LENGTH:
It will return the length of a given string.
SYNTAX:
i) length(string)
Ex: SQL>select length(INDIA) from dual;
OUTPUT:
LENGTH(INDIA)
5

ii) Select length(fieldvalue) from <tablename>;
Ex: SQL>select length(name) from std1;
OUTPUT:
LENGTH(NAME)
3
4
6
LPAD:
It pads the leading space to the left side of the column and fill with given character to the total
width of n.
SYNTAX:
i) lpad(char1,n,[,char2])
Cs2258 DBMS LAB MANUAL CSE DEPARMENT JCET Page 15

Ex: SQL>select lpad(abc,10,*) from dual;
OUTPUT:
LPAD(ABC)
*******ABC

ii) select lpad(char1,n,[,char2]) from <tablename>;
Ex: SQL>select lpad(deptname,10,*) fromn dept;
OUTPUT:
LPAD(DEPTNAME,10,*)
*****sales
***account
****manager
CONCAT:
It concatenates the two strings x1 and x2.
SYNTAX:
i) concat(x1,x2)
Ex: SQL>select concat(good,evening) from dual;
OUTPUT:
CONCAT(GOOD,EVENING)
Goodevening

ii) select concat(field1,field2) from <tablename>;
Ex: SQL>select concat(deptno,deptname) from dept;
OUTPUT:
Cs2258 DBMS LAB MANUAL CSE DEPARMENT JCET Page 16

CONCAT(DEPTNO,DEPTNAME)
101sales
102account
103manager
CHR:
It returns the character corresponding to the value x.
SYNTAX:
i) chr(x)
Ex: SQL>select chr(97) from dual;
OUTPUT:
CHR(97)
a

SQL>select chr(field) from <tablename>;
Ex: SQL>select chr(deptno) from dept;
OUTPUT:
CHR(DEPTNO)
e
f
g

RPAD:
It pads the leading space to the right side of the column and fills with blank space to the width
of n.

Cs2258 DBMS LAB MANUAL CSE DEPARMENT JCET Page 17

SYNTAX:
i) rpad(char1,n,[,char2])
Ex: SQL>select rpad(abc,10,*) from dual;
OUTPUT:
RPAD(ABC)
abc*******

ii) select rpad(colname,n,[,char2]) from <tablename>;
Ex: SQL>select rpad(deptname,10,*) from dept;
OUTPUT:

RPAD(DEPTN AME,10,*)
Sales*****
Account***
Manager****

LTRIM:
It removes the leading occurrences of that character from the string from left side position.
SYNTAX:
i) ltrim(string[,char(s)])
Ex: SQL>select ltrim(abc,a) from dual;
OUTPUT:
LTRIM(ABC,A)
bc
Cs2258 DBMS LAB MANUAL CSE DEPARMENT JCET Page 18

ii) select ltrim(colname,[,char(s)]) from <tablename>;
Ex: SQL>select ltrim(deptname,a) from dept;
OUTPUT:
LTRIM(DEPTNAME,A)
Sales
ccount
Manager
RTRIM:
It removes the leading occurrences of the character from the string from right side position.
SYNTAX:
i) rtrim(string,[,char(s)])
Ex: SQL>select rtrim(abc,a) from dual;
OUTPUT:
RTRIM(ABC,A)
ab
ii) select rtrim(fieldname,[,char(s)]) from <tablename>;
Ex: SQL>select rtrim (deptname,t) from dept;
OUTPUT:
RTRIM(DEPTNAME,T)
Sales
Accoun
Manager
REPLACE:
It replaces the character c2 with a given string c3 and returns the character c1.
SYNTAX:
Cs2258 DBMS LAB MANUAL CSE DEPARMENT JCET Page 19

i) replace(<c1>,<c2>[<c3>])
Ex: SQL>select replace(Madurai,Ma,ij) from dual;
OUTPUT:
REPLACE(MADURAI)
ijdurai

ii) select replace(fieldname,<c1>,<c2>) from <tablename>;
Ex: SQL>select replace(deptname,a,c) from dept;
OUTPUT:
REPLACE(DEPTNAME,A,C)
Scles
ccount
Mcncger
SUBSTRING:
It returns the substring from string z, the length equal to y starting at position x.
SYNTAX:
i) substr(z,x[,y])
Ex: SQL>select substr(Madurai,2,4) from dual;
OUTPUT:
SUBSTR(MADURAI)
adur


ii) select substr(colname,x,y) from <tablename>;
Cs2258 DBMS LAB MANUAL CSE DEPARMENT JCET Page 20

Ex: SQL>select substr(deptname,3,2) from dept;
OUTPUT:
SUBSTR(DEPTNAME)
le
co
na
NUMERIC FUNCTIONS:
ABS:
It returns the absolute value.
SYNTAX:
abs(x)
Ex: SQL>select abs(-8) from dual;
OUTPUT:
ABS(-8)
8
select abs(colname) from <tablename>;
Ex: SQL>select abs(deptno) absolute from dept;
OUTPUT: ABS(DEPTNO)
101
102
103
SIN:
It returns the sine value.
SYNTAX:
sin(x)
Cs2258 DBMS LAB MANUAL CSE DEPARMENT JCET Page 21

Ex: SQL>select sin(45) from dual;
OUTPUT:
SIN(45)
.850903525
select sin(colname) from <tablename>;
Ex: SQL>select sin(deptno) from dept;
OUTPUT:
SIN(DEPTNO)
.452025787
.994826791
.622988631
COS:
It returns the cosine value.
SYNTAX:
cos(x)
Ex: SQL>select cos(60) from dual;
OUTPUT:


COS(60)
-.95241298
select cos(colname) from <tablename>;
Ex: SQL>select cos(deptno) from dept;
OUTPUT:
Cs2258 DBMS LAB MANUAL CSE DEPARMENT JCET Page 22

COS(DEPTNO)
.89200487
.101585704
-.78223089
MOD:
It returns mod value of a division operator.
SYNTAX:
mod(x,y)
Ex: SQL>select mod(10,3) as first,mod(3,4) as second from dual;
OUTPUT:
MOD(10,3): FIRST SECOND
2 3
select mod(colname,y) from <tablename>;
Ex: SQL>select mod(deptno,2) from dept;
OUTPUT:
MOD
1
0
1
POWER:
It returns a number raised to an arbitrary power.
SYNTAX:
power(x,y)
Ex: SQL>select power(3,5)power from dual;
OUTPUT:
Cs2258 DBMS LAB MANUAL CSE DEPARMENT JCET Page 23

POWER(3,5)
243

select power(colname,y) from <tablename>;
Ex: SQL>select power(deptno,2)power from dept;
OUTPUT:
POWER
10201
10404
10609
EXP:
Returns the base of natural logarithm raised to a power.
SYNTAX:
exp(x)
Ex: SQL>select exp(5) from dual;
OUTPUT:
EXP(5)
148.413159
select exp(colname) from <tablename>;
Ex: SQL>select exp(deptno) from dept;


OUTPUT:
EXP(DEPTNO)
Cs2258 DBMS LAB MANUAL CSE DEPARMENT JCET Page 24

7.3071E+43
1.9863E+44
5.3992E+44
SQRT:
It returns the square root of the given value.
SYNTAX:
sqrt(x)
Ex: select sqrt(8) as square_root from dual;
OUTPUT:
square_root
8

select sqrt(colname) as square_root from <tablename>;
Ex: select sqrt(deptno) as square_root from dept;
OUTPUT:
SQUARE_ROOT(DEPTNO)
10.0498756
10.0995049
10.1488916


TRUNC:
It truncates the given value by the width of n.
SYNTAX:
Cs2258 DBMS LAB MANUAL CSE DEPARMENT JCET Page 25

trunc(x,n)
Ex: SQL>select trunc(23.4567,3),trunc(35.678),trunc(78.453) from dual;
OUTPUT:
TRUNC(23.4567) TRUNC(35.678) TRUNC(78.453)
23.456 35 70

select trunc(colname,x) from <tablename>;
Ex: SQL>select trunc(deptno,3) from dept;
OUTPUT:
TRUNC(DEPTNO,3)
101
102
103

RESULT:
Thus, the single row functions have been executed successfully.




PL/SQL
1. CHECK THE GIVEN NO IS PRIME OR NOT
Aim: To check whether the given no is prime or not using PL/SQL
Cs2258 DBMS LAB MANUAL CSE DEPARMENT JCET Page 26

PROGRAM:
set serveroutput on;
declare
n number(10);
i number(10);
m number(10);
f number(10):=0;
begin
n:=&n;
m:=n;
for i in 2..m/2
loop
if(n mod i=0)then
f:=f+1;
end if;
end loop;
if(f=0) then
dbms_output.put_line('the given number is prime');
else
dbms_output.put_line('the given number is not
prime');
end if;
Cs2258 DBMS LAB MANUAL CSE DEPARMENT JCET Page 27

end;
OUTPUT:
Enter value for n: 5
The given number is prime

PL/SQL procedure successfully completed.


RESULT:
Thus the PL/SQL program has been executed successfully.











2.CHECK PALINDROME OF A GIVEN NUMBER
Cs2258 DBMS LAB MANUAL CSE DEPARMENT JCET Page 28

Aim: To check whether the given no is palindrome or not using PL/SQL

PROGRAM:
set serveroutput on;
declare
n number(10);
m number(10);
s number(10);
r number(10);
begin
n:=&n;
m:=n;
s:=0;
while(n>0)
loop
r:=n mod 10;
s:=(s*10)+r;
n:=floor(n/10);
end loop;
if(m=s) then
dbms_output.put_line('The given number is a
palindrome');
Cs2258 DBMS LAB MANUAL CSE DEPARMENT JCET Page 29

else
dbms_output.put_line('The given number is not a
palindrome');
end if;
end;

OUTPUT:

Enter value for n: 121
The given number is a palindrome

PL/SQL procedure successfully completed.


RESULT:
Thus the PL/SQL program has been executed successfully.






Cs2258 DBMS LAB MANUAL CSE DEPARMENT JCET Page 30


3 NO_DATA_FOUND EXCEPTION
Aim: To check ascertain NO_DATA_FOUND exception using PL/SQL

TABLE:
SQL>create table emp5(empno number(3),ename varchar(10),deptno number(3));
Table created.

SQL>insert into emp5 values(&empno,'&ename',&deptno);

SQL>select * from emp5;

EMPNO ENAME DEPTNO
101 aaa 1
102 bbb 2

PROGRAM:
set serveroutput on;
declare
veno emp5.empno%type:=&empno;
vename emp5.ename%type:='&ename';
vdeptno emp5.deptno%type;
begin
Cs2258 DBMS LAB MANUAL CSE DEPARMENT JCET Page 31

select empno,ename,deptno into veno,vename,vdeptno from
emp5 where empno=veno and ename=vename;
dbms_output.put_line('deptno is '||vdeptno);
exception
when no_data_found then
dbms_output.put_line('no records found');
end;

OUTPUT:
Enter value for eno: 101
Enter value for ename: aaa
deptno is1
PL/SQL procedure successfully completed.

Enter value for eno: 103
Enter value for ename: sss
no records found
PL/SQL procedure successfully completed.
RESULT:
Thus the PL/SQL program has been executed successfully.


Cs2258 DBMS LAB MANUAL CSE DEPARMENT JCET Page 32



4. DIVIDE BY ZERO EXCEPTION
Aim: To ascertain divide by zero exception using PL/SQL
PROGRAM:
set serveroutput on;
declare
a number(3);
begin
a:=&a;
a:=a/0;
exception
when zero_divide then
dbms_output.put_line('divide by zero error');
end;
OUTPUT:
Enter value for a: 5
divide by zero error
PL/SQL procedure successfully completed.
RESULT:

Thus the PL/SQL program has been executed successfully.
Cs2258 DBMS LAB MANUAL CSE DEPARMENT JCET Page 33

5. TOO_MANY_ROWS EXCEPTION
Aim: To ascertain TOO_MANY_ROWS exception using PL/SQL
TABLE:
SQL>create table emp5(empno number(3),ename varchar(10),deptno number(3));
Table created.
SQL>insert into emp5 values(&empno,'&ename',&deptno);
SQL>select * from emp5;
EMPNO ENAME DEPTNO
101 aaa 1
102 bbb 2
PROGRAM:
set serveroutput on;
declare
veno emp5.empno%type:=&empno;
vename emp5.ename%type:='&ename';
vdeptno emp5.deptno%type;
begin
select empno,ename,deptno into veno,vename,vdeptno from emp5 where empno=veno
and ename=vename;
dbms_output.put_line('deptno is '||vdeptno);
select empno,ename,deptno into veno,vename,vdeptno from
emp5 where deptno=4;
dbms_output.put_line('eno is ' ||veno);
Cs2258 DBMS LAB MANUAL CSE DEPARMENT JCET Page 34

dbms_output.put_line('ename is ' ||vename);
exception
when too_many_rows then
dbms_output.put_line('too many rows in dept 4');
when others then
dbms_output.put_line('error');
end;

OUTPUT:
Enter value for empno:101
Enter value for ename:aaa
deptno is 1
eno is 101
ename is aaa
PL/SQL procedure successfully completed.
Enter value for empno:101
Enter value for ename:priya
error
PL/SQL procedure successfully completed.

RESULT:
Thus the PL/SQL program has been executed successfully.
Cs2258 DBMS LAB MANUAL CSE DEPARMENT JCET Page 35

7. USER DEFINED EXCEPTION
Aim: To ascertain user defined exception using PL/SQL
TABLE:
SQL> create table emp7(empno number(4),ename varchar(15),job varchar(20),sal number(6),deptno
number(3),comm number(5));
Table created.
SQL>insert into emp7(empno, ename, job, sal, deptno) values(&empno,'&ename','&job',&sal,&deptno);
SQL>select * from emp7;
EMPNO ENAME J OB SAL DEPTNO COMM
101 Vijay Manager 9500 1
102 Nitin Account 5000 2
103 J ansi Technician 10000 3
PROGRAM:
set serveroutput on;
declare
vsal emp7.sal%type:=4000;
vcomm emp7.comm%type:=2000;
invalid_commission exception;
begin
if vcomm<vsal then
insert into emp7 values(110,'ajitha','manager',4000,6,100);
else
raise invalid_commission;
end if;
Cs2258 DBMS LAB MANUAL CSE DEPARMENT JCET Page 36

exception
when invalid_commission then
dbms_output.put_line('commission value is invalid');
end;
OUTPUT:
Enter value for sal: 1500
Enter value for comm: 500
PL/SQL procedure successfully completed.
select * from emp7;
EMPNO ENAME J OB SAL DEPTNO COMM
101 Vijay Manager 9500 1
102 Nitin Account 5000 2
103 J ansi Technician 10000 3
110 Ajitha Manager 4000 6 100
Enter value for sal: 2000
Enter value for comm: 3000
commission value is invalid
PL/SQL procedure successfully completed.
select * from emp7;
EMPNO ENAME J OB SAL DEPTNO COMM
101 Vijay Manager 9500 1
102 Nitin Account 5000 2
103 J ansi Technician 10000 3
110 Ajitha Manager 4000 6 100
RESULT: Thus the PL/SQL program has been executed successfully.
Cs2258 DBMS LAB MANUAL CSE DEPARMENT JCET Page 37

CURSORS
To execute the following programs using cursors
1.CALCULATING INTEREST FOR FIXED DEPOSIT AMOUNT USING CURSORS
Aim: To Calculate Interest for Fixed Deposit Amount Using Cursors

TABLE:
SQL>create table fixed(accno number(5),years number(4),amount number(6),interest number(3));
Table created.
SQL>insert into fixed(accno,years,amount) values(&accno,&years,&amount);
SQL>select * from fixed;
ACCNO YEARS AMOUNT INTEREST
101 3 1000
102 2 500
103 6 5000
PROGRAM:
set serveroutput on;
declare
cursor cur is select * from fixed;
begin
for i in cur
loop
if i.years<=2 then
update fixed set interest=i.amount*0.09*(1/12)
Cs2258 DBMS LAB MANUAL CSE DEPARMENT JCET Page 38

where accno=i.accno;
elsif i.years>=2 and i.years<=5 then
update fixed set interest=i.amount*0.11*(1/12)
where accno=i.accno;
else
update fixed set interest=i.amount*0.15*(1/12) where
accno=i.accno;
end if;
end loop;
end;

OUTPUT:
PL/SQL procedure successfully completed.
SQL>select * from fixed;
ACCNO YEARS AMOUNT INTEREST
101 3 1000 9
102 2 500 4
103 6 5000 62
RESULT:

Thus the PL/SQL program has been executed successfully.


Cs2258 DBMS LAB MANUAL CSE DEPARMENT JCET Page 39

2. CALCULATING ELECTRICITY BILL USING CURSORS
Aim: To Calculate Electricity Bill Using Cursors

TABLE:
SQL>create table eb_cal(ebno number(5),name varchar(15),units number(4),charges number(3));
Table created.
SQL>insert into eb_cal(ebno,name,units)values(&ebno,'&name',&units);
SQL>select * from eb_cal;
EBNO NAME UNITS CHARGES
1 aaa 150
2 bbb 80
3 ccc 300

PROGRAM:
set serveroutput on;
declare
cursor cur is select * from eb_cal;
begin
for i in cur
loop
if i.units<=100 then
update eb_cal set charges=i.units*1 where
ebno=i.ebno;
Cs2258 DBMS LAB MANUAL CSE DEPARMENT JCET Page 40

elsif i.units>100 and i.units<=200 then
update eb_cal set charges=i.units*2 where
ebno=i.ebno;
else
update eb_cal set charges=i.units*3 where
ebno=i.ebno;
end if;
end loop;
end;


OUTPUT:
PL/SQL procedure successfully completed.

SQL>select * from eb_cal;
EBNO NAME UNITS CHARGES
1 aaa 150 450
2 bbb 80 80
3 ccc 300 600
RESULT:

Thus the PL/SQL program has been executed successfully.

Cs2258 DBMS LAB MANUAL CSE DEPARMENT JCET Page 41

STUDENT MARKLIST USING CURSOR

Aim: To Prepare Students Mark List Using Cursors
TABLE:

SQL>create table stu11(regno number(6) primary key, mark1 number(3),mark2 number(3),mark3
number(3),mark4 number(3),mark5 number(3),tot number(3),avg number(3,2));
Table created.
SQL>insert into stu11(regno,mark1,mark2,mark3,mark4,mark5)
values(&regno,&mark1,&mark2,&mark3,&mark4,&mark5);
PROGRAM:
set serveroutput on;
declare
cursor c is select * from stu11;
x stu11.regno%type;
a stu11.mark1%type;
b stu11.mark2%type;
f stu11.mark3%type;
d stu11.mark4%type;
e stu11.mark5%type;
t stu11.tot%type;
v stu11.avg%type;
begin
Cs2258 DBMS LAB MANUAL CSE DEPARMENT JCET Page 42

open c;
loop
fetch c into x,a,b,f,d,e,t,v;
exit when c% notfound;
dbms_output.put_line('Rollno:'||x);
dbms_output.put_line('mark1:'||a);
dbms_output.put_line('mark2:'||b);
dbms_output.put_line('mark3:'||f);
dbms_output.put_line('mark4:'||d);
dbms_output.put_line('mark5:'||e);
dbms_output.put_line('update the tot');
dbms_output.put_line('update the avg');
update stu11 set
tot=(mark1+mark2+mark3+mark4+mark5)where regno=x;
update stu11 set avg=(tot/5) where regno=x;
end loop;
close c;
end;

OUTPUT:
roll: 101
m1: 50
Cs2258 DBMS LAB MANUAL CSE DEPARMENT JCET Page 43

m2: 60
m3: 70
m4: 80
m5: 90
update the tot
update the avg
roll: 102
m1: 30
m2: 40
m3: 50
m4: 60
m5: 70
update the tot
update the avg
roll: 103
m1: 45
m2: 55
m3: 65
m4: 75
m5: 85
update the tot
update the avg
Cs2258 DBMS LAB MANUAL CSE DEPARMENT JCET Page 44

roll: 104
m1: 55
m2: 65
m3: 75
m4: 85
m5: 95
update the tot
update the avg
PL/SQL procedure successfully completed.
SQL>select * from stu11;
ROLL M1 M2 M3 M4 M5 TOT AVG
101 50 60 70 80 90 350 70
102 30 40 50 60 70 250 50
103 45 55 65 75 85 325 65
104 55 65 75 85 95 375 75
RESULT:

Thus the PL/SQL program has been executed successfully.





Cs2258 DBMS LAB MANUAL CSE DEPARMENT JCET Page 45

COPYING A TABLE USING CURSORS
Aim: Copying of tables using Cursors
TABLE:
SQL>create table stu11(regno number(6) primary key, mark1 number(3),mark2 number(3),mark3
number(3),mark4 number(3),mark5 number(3),tot number(3),avg number(3,2));
Table created.
SQL>insert into stu11(regno,mark1,mark2,mark3,mark4,mark5)
values(&regno,&mark1,&mark2,&mark3,&mark4,&mark5);

SQL>select * from stu11;
ROLL M1 M2 M3 M4 M5 TOT AVG
101 50 60 70 80 90 350 70
102 30 40 50 60 70 250 50
103 45 55 65 75 85 325 65
104 55 65 75 85 95 375 75

SQL>create table st11(regno number(6) primary key, mark1 number(3),mark2 number(3),mark3
number(3),mark4 number(3),mark5 number(3),tot number(3),avg number(3,2));
Table created.
PROGRAM:
set serveroutput on;
declare
cursor cur is select * from stu11;
begin
for i in cur
Cs2258 DBMS LAB MANUAL CSE DEPARMENT JCET Page 46

loop
insert into st11 values(i.regno,i.mark1,i.mark2,i.mark3,i.mark4,i.mark5,
i.tot,i.avg);
end loop;
end;
OUTPUT:
PL/SQL procedure successfully completed.
SQL>select* from st11;

ROLL M1 M2 M3 M4 M5 TOT AVG
101 50 60 70 80 90 350 70
102 30 40 50 60 70 250 50
103 45 55 65 75 85 325 65
104 55 65 75 85 95 375 75
RESULT:
Thus the PL/SQL program has been executed successfully.







Cs2258 DBMS LAB MANUAL CSE DEPARMENT JCET Page 47

Functions

AIM:

To create a program to prepare reports for an application using functions.

Algorithm:

1. Define a function along with the arguments with its appropriate data types.

2. Select command, re level, max level from the table and copy the same values to the new
variables which have been declared.

3. Add qty hand and re level is less than max level and if again max value to arguments.

4. If it is not less, add the values of qty hand and re level and assign into arguments.

5. Close the cursor and stop the execution.

6. Close the if condition.

7. Stop the execution.


Queries:

* Class work Practice.







Cs2258 DBMS LAB MANUAL CSE DEPARMENT JCET Page 48

BANKING APPLICATION USING PROCEDURES
Aim: To Perform Banking Operations Using Procedures
TABLE:
SQL>create table acc(ac_no number(5), name varchar(20), balance number(6));
Table created.
SQL>insert into acc values(&ac_no,'&name',&balance);
SQL>select * from acc;
AC_NO NAME BALANCE
101 aaa 10000
102 bbb 6000
103 ccc 15000
104 ddd 5500

PROCEDURE DEFINITION:
set serveroutput on;
create or replace procedure withdraww (ac_no1 in number, amount1 in
number) is
begin
update acc set balance=balance - amount1 where
ac_no=ac_no1;
end;
create or replace procedure depositt (ac_no1 in number, amount1 in number) is
begin
update acc set balance=balance +amount1 where
Cs2258 DBMS LAB MANUAL CSE DEPARMENT JCET Page 49

ac_no=ac_no1;
end;

PROGRAM:
set serveroutput on;
declare
choice number;
ac_no1 number(5);
amount number(5);
begin
ac_no1:=&ac_no1;
choice:=&choice;
amount:=&amount;
if choice=1 then
depositt(ac_no1,amount);
else
withdraww(ac_no1,amount);
end if;
end;

OUTPUT:
Enter value for ac_no1: 103
Cs2258 DBMS LAB MANUAL CSE DEPARMENT JCET Page 50

Enter value for choice: 1
Enter value for amount: 2000
PL/SQL procedure successfully completed.
Enter value for ac_no1: 103
Enter value for choice: 2
Enter value for amount: 1000
PL/SQL procedure successfully completed.

SQL>select * from acc;

AC_NO NAME BALANCE
101 aaa 10000
102 bbb 6000
103 ccc 16000
104 ddd 5500

RESULT:

Thus the procedure has been created successfully.




Cs2258 DBMS LAB MANUAL CSE DEPARMENT JCET Page 51

PAYROLL APPLICATION USING PROCEDURES
Aim: To carryout payroll application using procedures
TABLE:
SQL>create table pay(eno number(10),ename varchar(10),hra number(10),da number(10));
Table created.
SQL>insert into pay values(&eno,'&ename',&hra,&da);

SQL>select * from pay;

ENO ENAME HRA DA
1 Kalidass 1200 1000
2 Ravi 2000 1000
3 David 5000 500
4 Dravid 10000 2000
5 Dass 30000 5000
SQL>alter table pay add netamt number(10);
Table altered.
SQL>select * from pay;
ENO ENAME HRA DA NETAMT
1 Kalidass 1200 1000
2 Ravi 2000 1000
3 David 5000 500
4 Dravid 10000 2000
5 Dass 30000 5000

PROCEDURE DEFINITION:
Cs2258 DBMS LAB MANUAL CSE DEPARMENT JCET Page 52


set serveroutput on;
create or replace procedure net(eno1 in number) is
begin
update pay set netamt=hra +da where eno=eno1;
end;

PROGRAM:

declare
choice number;
eno1 number(5);
begin
eno1:=&eno1;
choice:=&choice;
if choice=1 then
net(eno1);
else
dbms_output.put_line('PLEASE ENTER CHOICE=1');
end if;
end;

Cs2258 DBMS LAB MANUAL CSE DEPARMENT JCET Page 53

OUTPUT:

Enter value for eno1: 1
Enter value for choice: 1

PL/SQL procedure successfully completed.

SQL>select * from pay;

ENO ENAME HRA DA NETAMT
1 Kalidass 1200 1000 2200
2 Ravi 2000 1000
3 David 5000 500
4 Dravid 10000 2000
5 Dass 30000 5000
RESULT:

Thus the procedure has been created successfully.






Cs2258 DBMS LAB MANUAL CSE DEPARMENT JCET Page 54

CHECK WHETHER A NUMBER IS ARMSTRONG OR NOT USING FUNCTIONS
Aim: To Check whether a number is Armstrong or not using functions
FUNCTION BODY:
set serveroutput on;
create or replace function armstrong(n number) return number is
r number(10);
a number(10);
b number(10);
c number(10);
begin
b:=0;
c:=n;
while(c>0)
loop
r:=c mod 10;
b:=b+(r*r*r);
c:=floor(c/10);
end loop;
return b;
end armstrong;

PROGRAM:
Cs2258 DBMS LAB MANUAL CSE DEPARMENT JCET Page 55

declare
n number(10);
m number(10);
o number(10);
begin
n:=&n;
o:=n;
m:=armstrong(n);
if(m=o) then
dbms_output.put_line('Given no is armstrong number');
else
dbms_output.put_line('Given no is not an armstrong
number');
end if;
end;
OUTPUT:
Enter value for n: 153
Given no is Armstrong number
PL/SQL procedure successfully completed.
RESULT:

Thus the function has been created successfully.
Cs2258 DBMS LAB MANUAL CSE DEPARMENT JCET Page 56

DATABASE CONNECTIVITY
AIM:
To write an algorithm to perform database connectivity using MS Access.
ALGORITHM:
Step 1:
Open VB 6.0.
Step 2:
Open a form in Visual Basic. Go to Addins -Visual Data manager- New Database->
Microsoft Access->version 7.0 mdb
Step 3:
Give the name for database and right click it and give->New Table.
Provide a name for the table.
Click Add fields->and add the required fields with appropriate data types..
Goto record source tab. And choose the 2-adCmdTable option. Select the table.
Step 5:
Place a Data Control and set the properties like data source and recordset

Design the form with controls and set the properties




Cs2258 DBMS LAB MANUAL CSE DEPARMENT JCET Page 57

DATABASE CONNECTION
Coding :
Dim dbstu As Database
Dim rsstu As Recordset

Private Sub Command1 ADD_Click()
Add ="insert into sm values('" & Text1 & "'," & Val(Text2) & "," & Val(Text3) & "," & Val(Text4) & ","
& Val(Text5) & "," & Val(Text6) & "," & Val(Text7) & "," & Val(Text8) & ",'" & Text9 & "')"
dbstu.execute Add
MsgBox "record is added sucessfully"
Text1.Text =" "
Text2.Text =" "
Text3.Text =""
Text4.Text =" "
Text5.Text =" "
Text6.Text =" "
Text7.Text =" "
Text8.Text =" "
Text9.Text =" "
Text1.SetFocus
End Sub
Private Sub Command2FIND_Click()
stname =InputBox("enter student name...:", " input requiered")
Set rsstu =dbstu.OpenRecordset(" select * from sm where sname ='" & stname & "' ")
Cs2258 DBMS LAB MANUAL CSE DEPARMENT JCET Page 58

If rsstu.EOF =True Then
MsgBox "no record found"
Else
Text1 =rsstu(0)
Text2 =rsstu(1)
Text3 =rsstu(2)
Text4 =rsstu(3)
Text5 =rsstu(4)
Text6 =rsstu(5)
Text7 =rsstu(6)
Text8 =rsstu(7)
End If
If Not rsstu Is Nothing Then
Set rsstu =Nothing
Else
rsstu.Close
End If
End Sub

Private Sub Command3CLEAR_Click()
Text1.Text =" "
Text2.Text =" "
Text3.Text =" "
Cs2258 DBMS LAB MANUAL CSE DEPARMENT JCET Page 59

Text4.Text =" "
Text5.Text =" "
Text6.Text =" "
Text7.Text =" "
Text8.Text =" "
Text9.Text =" "
Text1.SetFocus
End Sub

Private Sub Command4UPDATE_Click()
Set rsstu =dbstu.open.Recordset(" select * fromsm where sno =" & Text2 & " ")
If rsstu.recordcount =0 Then
MsgBox "no record found"
Else
rsstu.edit 'active edit mode'
rsstu(0) =Text1
rsstu(1) =Val(Text2.Text)
rsstu(2) =Val(Text3.Text)
rsstu(3) =Val(Text4.Text)
rsstu(4) =Val(Text5.Text)
rsstu(5) =Val(Text6.Text)
rsstu(6) =Val(Text7.Text)
rsstu(7) =Val(Text3.Text) +Val(Text4.Text) +Val(Text5.Text) +Val(Text6.Text) +Val(Text7.Text)
Cs2258 DBMS LAB MANUAL CSE DEPARMENT JCET Page 60

rsstu(8) =Text9
rsstu.Update
End If
End Sub


Private Sub Command5EXIT_Click()
Unload Me
End Sub
Private Sub Form_Load()
Set dbstu =opendatabase("d:\mca\smark.mdb")
Set rsstu =dbstu.OpenRecordset("select * from sm ")
Text8.Enabled =False
Text9.Enabled =False
End Sub
Private Sub Text7_LostFocus()
Text8.Text =Val(Text3.Text) +Val(Text4.Text) +Val(Text5.Text) +Val(Text6.Text) +Val(Text7.Text)
If Val(Text3.Text) >=50 And Val(Text4.Text) >=50 And Val(Text5.Text) >=50 And Val(Text6.Text) >=
50 And Val(Text7.Text) >=50 Then
Text9.Text ="pass"
Else
Text9.Text ="fail"
End If
End Sub
Cs2258 DBMS LAB MANUAL CSE DEPARMENT JCET Page 61

FORM WINDOW :



DATABASE CONNECTION :




Cs2258 DBMS LAB MANUAL CSE DEPARMENT JCET Page 62

ADD THE RECORD :

FIND THE RECORD


Cs2258 DBMS LAB MANUAL CSE DEPARMENT JCET Page 63

CLEAR THE RECORD:

Cs2258 DBMS LAB MANUAL CSE DEPARMENT JCET Page 64

UPDATE THE RECORD:

Cs2258 DBMS LAB MANUAL CSE DEPARMENT JCET Page 65

EXIT THE FORM :





Result: Thus the database connectivity using VB/MS Access in successfully executed and verified




Cs2258 DBMS LAB MANUAL CSE DEPARMENT JCET Page 66

MENUS AND CREATION OF MENUS
AIM:

To write an algorithm for adding menus or creation of menus.

ALGORITHM:

Adding a file menu and edit menu

Click the menu editor button on the toolbar or right click on the form a pop up menu appears from
that choose menu editor.
The menu editor appears, now youll add to the program a file menu that includes new, open,
save, close and exit.
Click the caption text box, type &File, press tab, type mnufile and then click the next button.
4. Type &new, press tab, type mnunewitem and then click the next button.
5 Similarly add the other three sub menus of the menu File.
6. Follow the step 3 and 4 to add edit menu that includes cut, copy and paste.

Adding a Common Dialog Control to the Toolbox

1. On the project menu, click the components command and then click the control tab.
2. Scroll down the list of controls until u see the Microsoft common dialog control 6.0. and
Microsoft Rich Textbox control 6.0
Cs2258 DBMS LAB MANUAL CSE DEPARMENT JCET Page 67

3. Click the check box next to the control name and then click ok.
4. Now common dialog control appears in your tool box.
PROGRAM:
Dim UnsavedChanges As Boolean
Private Sub mnunew_Click()
rt1.Text =" "
End Sub
Private Sub mnuopen_Click()
Dim filter As String
On Error GoTo errortrap
filter ="all files(*.*) | *.*"
cd1.filter =filter
cd1.Action =1
rt1.LoadFile (cd1.FileName)
Exit Sub
errortrap:
MsgBox "dialog box is canceled"
Exit Sub
End Sub

Private Sub mnusave_Click()
Dim name As String
Cs2258 DBMS LAB MANUAL CSE DEPARMENT JCET Page 68

cd1.ShowSave
name =cd1.FileName
rt1.SaveFile name, 1
End Sub

Private Sub mnucut_Click()
Clipboard.SetText rt1.SelRTF
rt1.SelRTF =""
End Sub

Private Sub mnucopy_Click()
Clipboard.SetText rt1.SelRTF
End Sub

Private Sub mnupaste_Click()
rt1.SelRTF =Clipboard.GetText
End Sub

Private Sub rt1_Change()
UnsavedChanges =True
End Sub

Cs2258 DBMS LAB MANUAL CSE DEPARMENT JCET Page 69


Private Sub mnucloseitem_Click()
Dim Prompt As String
Dim reply As Integer
cd1.CancelError =True
If UnsavedChanges =True Then
Prompt ="would u like to save change?"
reply =MsgBox(Prompt, vbYesNo)
If reply =vbYes Then
cd1.ShowSave
rt1.SaveFile cd1.FileName, rtfRTF
End If
End If
rt1.Text =" "
UnsavedChanges =False
End Sub
Private Sub mnuexit_Click()
End
End Sub

Result: Thus the menu has been created , executed and verified

Cs2258 DBMS LAB MANUAL CSE DEPARMENT JCET Page 70

REPORTS GENERATION
AIM:

To write an algorithm to generate reports.

ALGORITHM:

1. Create a table in oracle.
2. Open Microsoft Visual Basic.
3. Choose Project->Components->Designer tab->Tick Data Environment and Data Report
Checkbox and click ok.
4. Go to Project Menu -> Add Data Environment
5. It will display a page, from that right click Connection Object and choose its properties.
6. It will open data link properties from that choose appropriate provider and Test the connection.
7. Now right click the Connection and choose Add Command.
8. Right click the Command Object and choose properties.
9. In General tab it will ask for source of data
10. Select a database object as Table
11. Choose object name (ex: 098001.employee)
12. Project->Add Data Report
13. Drag the command over the form and data report.
14. In the Data Report Properties change the Data Source Property - Data Environment1 and
Data Member Property - Command1.
15. Go to Project ->Project Properties and set Startup object as data Report
16. Run the program.
RESULT:
Thus the program has been executed successfully.

Cs2258 DBMS LAB MANUAL CSE DEPARMENT JCET Page 71










Cs2258 DBMS LAB MANUAL CSE DEPARMENT JCET Page 72










Cs2258 DBMS LAB MANUAL CSE DEPARMENT JCET Page 73










Cs2258 DBMS LAB MANUAL CSE DEPARMENT JCET Page 74







Cs2258 DBMS LAB MANUAL CSE DEPARMENT JCET Page 75









Cs2258 DBMS LAB MANUAL CSE DEPARMENT JCET Page 76








Cs2258 DBMS LAB MANUAL CSE DEPARMENT JCET Page 77










Cs2258 DBMS LAB MANUAL CSE DEPARMENT JCET Page 78









Cs2258 DBMS LAB MANUAL CSE DEPARMENT JCET Page 79











Cs2258 DBMS LAB MANUAL CSE DEPARMENT JCET Page 80









Cs2258 DBMS LAB MANUAL CSE DEPARMENT JCET Page 81






Private Sub datareport_Click()
DataReport1.Show
End Sub





Cs2258 DBMS LAB MANUAL CSE DEPARMENT JCET Page 82

MENU USING VISUAL BASIC


Aim: To create a menu using VB

Working with Menus in VB6
EXAMPLE
The File menu, shown below, will have the following level-two items below it: New, Open, Save,
Save As, Print, and Exit. Note that separator bars appear above the Save, Print, and Exit items.

The Help menu contains just one level-two item below it, About.

To build a menu for use with your VB program, you use the Menu Editor, which appears as an
icon in the toolbar of the VB IDE. It is the circled item in the screen shot below:
Cs2258 DBMS LAB MANUAL CSE DEPARMENT JCET Page 83


Alternatively, you can invoke the Menu Editor from the Tools menu item as shown below:

To build the menu described above, perform the following steps.
1. Start a new VB project and invoke the Menu Editor using either method shown above (click the
Menu Editor toolbar icon or select the Menu Editor option from the Tools menu). The
Menu Editor screen appears, as shown below:

2. For "Caption", type &File (by placing the ampersand to the left of the "F", we establish "F" as
an access key for the File item it enables the user to drop down the File menu by keying
"Alt+F" on the keyboard in addition to clicking the "File" item with the mouse).
Cs2258 DBMS LAB MANUAL CSE DEPARMENT JCET Page 84

For "Name", type mnuFile.
Your Menu Editor screen should look like this:

Click the Next button.
3. Click the "right-arrow" button (shown circled below). A ellipsis (...) will appear as the next item
in the menu list, indicating that this item is a level-two item (below "File").

For "Caption", type &New; for "Name", type mnuNew, and for "Shortcut", select Ctrl+N.
By specifying a shortcut, you allow the user to access the associated menu item by pressing
that key combination.
Cs2258 DBMS LAB MANUAL CSE DEPARMENT JCET Page 85

So here, you are providing the user three ways of invoking the "New" function: (1) clicking
File, then clicking New on the menu; (2) keying Alt+F,N (because we set up an access key for
"N" by placing an ampersand to left of "N" in "New"); or (3) keying Ctrl+N. At this point,
your Menu Editor screen should look like this:

Click the Next button.
4. For "Caption", type &Open; for "Name", type mnuOpen, and for "Shortcut", select Ctrl+O.
Your Menu Editor screen should look like this:
Cs2258 DBMS LAB MANUAL CSE DEPARMENT JCET Page 86


Click the Next button.
5. For "Caption", type - (a hyphen), and for "Name", type mnuFileBar1. A single hyphen as the
Caption for a menu item tells VB to create a separator bar at that location. Your Menu
Editor screen should look like this:

Click the Next button.
Cs2258 DBMS LAB MANUAL CSE DEPARMENT JCET Page 87

6. For "Caption", type &Save; for "Name", type mnuSave, and for "Shortcut", select Ctrl+S.
Your Menu Editor screen should look like this:

Click the Next button.
7. For "Caption", type Save &As ..., and for "Name", type mnuSaveAs. Your Menu Editor screen
should look like this:
Cs2258 DBMS LAB MANUAL CSE DEPARMENT JCET Page 88


Click the Next button.
8. For "Caption", type -, and for "Name", type mnuFileBar2. Your Menu Editor screen should
look like this:

Click the Next button.
Cs2258 DBMS LAB MANUAL CSE DEPARMENT JCET Page 89

9. For "Caption", type &Print;for "Name", type mnuPrint; and for "Shortcut", select Ctrl+P.
Your Menu Editor screen should look like this:

Click the Next button.
10. For "Caption", type -; and for "Name", type mnuFileBar3. Your Menu Editor screen should
look like this:

Click the Next button.
11. For "Caption", type E&xit, and for "Name", type mnuExit. Your Menu Editor screen should
look like this:
Cs2258 DBMS LAB MANUAL CSE DEPARMENT JCET Page 90


Click the Next button.
12. Click the "left-arrow" button (shown circled below). The ellipsis (...) no longer appears,
meaning we are back to the top-level items.


For "Caption", type &Help; and for "Name", type mnuHelp. Your Menu Editor screen should
look like this:
Cs2258 DBMS LAB MANUAL CSE DEPARMENT JCET Page 91


Click the Next button.
13. Click the "right-arrow" button to create a level-two item below "Help". For "Caption", type
&About; and for "Name", type mnuAbout. Your Menu Editor screen should look like this:

14. At this point, we are done creating our menu entries, so click the OK button. That will dismiss
the menu editor and return focus to the VB IDE.
15. Back in the VB IDE, your form will now have a menu, based on what you have set up in the
Menu Editor. If you click on a top-level menu item (File for example), the level-two menu
will drop down:
Cs2258 DBMS LAB MANUAL CSE DEPARMENT JCET Page 92


16. Click on the New menu item. The code window for the mnuFileNew_Click event opens, as
shown below. Note: Click is the only event that a menu item can respond to.



In thePlace mnuFileNew_Click event, place the code you want to execute when the user clicks the
New menu item. Since this is just a demo, we will place a simple MsgBox statement in the
event procedure:
MsgBox "Code for 'New' goes here.", vbInformation, "Menu Demo"



Cs2258 DBMS LAB MANUAL CSE DEPARMENT JCET Page 93

17. Code similar MsgBox statements for the Open, Save, Save As, and Print menu items:
Private Sub mnuFileOpen_Click()
MsgBox "Code for 'Open' goes here.", vbInformation, "Menu Demo"
End Sub
Private Sub mnuFileSave_Click()
MsgBox "Code for 'Save' goes here.", vbInformation, "Menu Demo"
End Sub
Private Sub mnuFileSaveAs_Click()
MsgBox "Code for 'Save As' goes here.", vbInformation, "Menu Demo"
End Sub
Private Sub mnuFilePrint_Click()
MsgBox "Code for 'Print' goes here.", vbInformation, "Menu Demo"
End Sub
18. For the Exit menu item Click event, code the statement Unload Me.

Private Sub mnuFileExit_Click()
Unload Me
End Sub
19. For the About menu item Click event, code as shown below:
Private Sub mnuHelpAbout_Click()
MsgBox "Menu Demo" & vbCrLf _
& "Copyright " & Chr$(169) & " 2004 thevbprogrammer.com", , _
Cs2258 DBMS LAB MANUAL CSE DEPARMENT JCET Page 94

"About"
End Sub
20. Run the program. Note how the code executes when you click on the various menu
items. Also test the use of the access keys (e.g., Alt+F, N) and shortcut keys (e.g., Ctrl-O).

21. Save the program and exit VB.



CODING


Private Sub mnuABOUT_Click()
MsgBox "this is file about option"
Form7.Show
End Sub

Private Sub mnuEXIT_Click()
Unload Me
End Sub

Private Sub mnuNEW_Click()
MsgBox "this is file new option"
Form2.Show
Cs2258 DBMS LAB MANUAL CSE DEPARMENT JCET Page 95

End Sub

Private Sub mnuOPEN_Click()
MsgBox "this is file open option"
Form3.Show
End Sub

Private Sub mnuPRINT_Click()
MsgBox "this is file print option"
Form6.Show
End Sub

Private Sub mnuSAVE_Click()
MsgBox "this is file save option"
Form4.Show
End Sub

Private Sub mnuSAVEAS_Click()
MsgBox "this is file saveas option"
Form5.Show
End Sub




SUB FORMS :



Private Sub Form2_Click()
Form1.Show
End Sub
Private Sub Image1_Click()
Form1.Show
End Sub
Private Sub Form3_Click()
Form1.Show
End Sub
Private Sub Image1_Click()
Form1.Show
End Sub
Cs2258 DBMS LAB MANUAL CSE DEPARMENT JCET Page 96

Private Sub Form4_Click()
Form1.Show
End Sub
Private Sub Form5_Click()
Form1.Show
End Sub
Private Sub Form6_ Click ()
Form1.Show
End Sub
Private Sub Form7_Click()
Form1.Show
End Sub




OUTPUT:



FORM WINDOW




Cs2258 DBMS LAB MANUAL CSE DEPARMENT JCET Page 97











NEW OPTION :




FORM 2:

Cs2258 DBMS LAB MANUAL CSE DEPARMENT JCET Page 98




CLICK .OPEN OPTION:





FORM 3 :
Cs2258 DBMS LAB MANUAL CSE DEPARMENT JCET Page 99




SAVE OPTION:






FORM4 WINDOW :

Cs2258 DBMS LAB MANUAL CSE DEPARMENT JCET Page 100


click
SAVE AS OPTION:



FORM5:

Cs2258 DBMS LAB MANUAL CSE DEPARMENT JCET Page 101


click


PRINT OPTION:





FORM 6:
Cs2258 DBMS LAB MANUAL CSE DEPARMENT JCET Page 102


s

EXIT OPTION:
Cs2258 DBMS LAB MANUAL CSE DEPARMENT JCET Page 103






HELP MENU :

ABOUT OPTION :

Form 7:

Cs2258 DBMS LAB MANUAL CSE DEPARMENT JCET Page 104






EXIT FORM :

You might also like