cs8481 DBMS Lab Manual
cs8481 DBMS Lab Manual
cs8481 DBMS Lab Manual
Name : …………………………………….
Semester :……………………………………
CERTIFICATE
This is to Certify that the bonafide record of the practical work done
by………………………………… Register Number of IV year B.E (Computer Science and
Engineering) submitted for the B.E-Degree practical examination(VII Semester) in CS8481– DATABASE
MANAGEMENT SYSTEMS during the academic year 2020 – 2021.
6 Triggers
12.7.2021
Exception Handling
7 15.7.2021
Database Design using ER modeling,
8 15.7.2021 normalization and Implementation for any
application
9 20.7.2021 Database Connectivity with Front End Tools
AIM:
To create a database and write SQL queries to retrieve information from the
database.
DESCRIPTION:
Data Definition Language
DDL (Data Definition Language) statements are used to create, change the objects
of a database. Typically a database administrator is responsible for using DDL
statements or pro-duction databases in a large database system. The commands used
are:
• Create - It is used to create a table.
• Alter - This command is used to add a new column, modify the existing column
defi-nition and to include or drop integrity constraint.
• Drop - It will delete the table structure provided the table should be empty.
• Truncate - If there is no further use of records stored in a table and the structure
has to be retained, and then the records alone can be deleted.
• Desc - This is used to view the structure of the table.
PROCEDURE:
Step 1: Create table by using create table command with column name, data type and size.
Step 2: Display the table by using desc command.
Step 3: Add any new column with the existing table by alter table command.
Step 4: Modify the existing table with modify command.
Step 5: Delete the records in files by truncate command.
Step 6: Delete the Entire table by drop command
1. Create
Syntax:
Create table tablename(column_name1 datatype(size),
column_name2 datatype(size), column_name3
datatype(size),………);
Example:
STUD_NAME CHAR(20)
STUD_ID VARCHAR2(10)
STUD_DEPT VARCHAR2(20)
STUD_AGE NUMBER(5)
2. Alter
table Syntax:
Alter table tablename add(column_name datatype(size));
Alter table tablename modify(column_name datatype(size));
Alter table tablename drop(column_name);
Example:
STUD_NAME CHAR(20)
STUD_ID VARCHAR2(10)
STUD_DEPT VARCHAR2(20)
STUD_AGE NUMBER(5)
STUD_ADDR VARCHAR2(20)
SQL> Alter table Student modify (Stud_age number(10));
Table altered.
SQL> desc Student;
STUD_NAME CHAR(20)
STUD_ID VARCHAR2(10)
STUD_DEPT VARCHAR2(20)
STUD_AGE NUMBER(10)
STUD_ADDR VARCHAR2(20)
STUD_NAME CHAR(20)
STUD_ID VARCHAR2(10)
STUD_DEPT VARCHAR2(20)
STUD_ADDR VARCHAR2(20)
3. Truncate Table
Syntax:
Truncate table tablename;
Example:
SQL> Truncate table Student;
Table truncated.
SQL> desc Student
STUD_NAME CHAR(20)
STUD_ID VARCHAR2(10)
STUD_DEPT VARCHAR2(20)
STUD_AGE NUMBER(10)
STUD_ADDR VARCHAR2(20)
4. Drop
Table Syntax:
Drop table tablename;
Example:
AIM:
To Study and Practice Insertion, Deletion, Modifying, Altering, Updating and
Viewing records based on conditions in RDBMS.
DESCRIPTION:
Data Manipulation Language
DML commands are the most frequently used SQL commands and is used to
query and manipulate the existing database objects. Some of the commands are
1. Insert
2. Select
3. Update
4. Delete
PROCEDURE:
Step 1: Create table by using create table command.
Step 2: Insert values into the table
Step 3: Delete any records from the table
Step 4: Update any values in the table.
Step 5: Display the values from the table by using select command.
SQL> Create table Student(Stud_name char(20), Stud_id varchar2(10), Stud_
dept varchar2(20), Stud_age number(5));
Table created.
STUD_NAME CHAR(20)
STUD_ID VARCHAR2(10)
STUD_DEPT VARCHAR2(20)
STUD_AGE NUMBER(5)
1. Insert: This is used to add one or more rows to a table. The values are separated by
com-mas and the data types char and date are enclosed in apostrophes. The values must
br entered in the same order as they are defined.
Syntax:
Insert into tablename values(‘&column_name1’, ‘&column_name1’,
‘&column_name1’,…..);
Example:
3. Update Command: It is used to alter the column values in a table. A single column
may be updated or more than one column could be updated.
Syntax:
Update tablename set column_name=’value’ where condition;
Example:
SAVEPOINT:
Syntax for save point:
Savepoint created.
COMMIT
Syntax for commit:
SQL> COMMIT;
SQL> COMMIT;
Commit complete.
DCL (DATA CONTROL LANGUAGE)
CREATING A USER
SQL>CONNECT SYSTEM/MANAGER; SQL>CREATE USER "USERNAME"
IDENTIFIED BY "PASSWORD" SQL>GRANT DBA TO "USERNAME"
SQL>CONNECT "USERNAME"/"PASSWORD";
EXAMPLE
CREATING A USER
SQL>CONNECT SYSTEM/MANAGER;
6 rows selected.
9 rows selected.
14 rows selected.
14 rows selected.
RESULT:
Thus the Insertion, Deletion, Modifying, Altering, Updating and Viewing
records based on conditions in RDBMS were executed and verifie
Ex No: 2 Database Querying – Simple queries, Nested queries, Sub
queries and Joins
AIM:
To Study and Practice Constraints, Nested Queries and joining records based on
conditions in RDBMS.
A) CONSTRAINTS :
1. NOT NULL:
Used to set the not null constraint to the specified column name which will not allow
null values.
Syntax:
Create table tablename(fieldname1 datatype(constraint)not null,fieldname2 datatype,
…………….fieldnamen datatype);
Example:
SQL> create table notnull (eno varchar2(10) not null,ename varchar2(10),esalary
SQL> /
Enter value for eno:
Enter value for ename: raj
Enter value for esalary: 30000
old 1: insert into notnull values(‘&eno’,’&ename’,’&esalary’)
new 1: insert into notnull values(‘’,’raj’,’30000’)
insert into notnull values(‘’,’raj’,’30000’)
*
ERROR at line 1:
ORA-01400: cannot insert NULL into (“SCOTT”.”NOTNULL”.”ENO”)
2. CHECK:
Check constraint specify conditions that each tuple must satisfy.
Syntax:
Create table tablename(Fieldname1 datatype(constraint),Fieldname2 datatype,
………………….Fieldname3 datatype);
Example:
SQL> create table con( empid varchar2(20) not null,empname
varchar2(20),empsalary number(10) check(empsalary>10000));
Table created.
SQL> insert into con
empid: 1
3. UNIQUE:
Used to set unique constraint to the specified column name which will not allow
redundant values
Syntax:
Create table tablename(fieldname1 datatype(constraint)unique,fieldname2 datatype,
…………….Fieldname3 datatype);
Example:
SQL> create table conn(eno varchar2(10) unique,ename varchar2(20));
Table created.
1 row created.
SQL> /
Enter value for eno: 1
Enter value for ename: hi
old 1: insert into conn values(‘&eno’,’&ename’)
new 1: insert into conn values(‘1’,’hi’)
insert into conn values(‘1’,’hi’)
*
ERROR at line 1:
ORA-00001: unique constraint (SCOTT.SYS_C0010285) violated
4. PRIMARY KEY:
Primary key is a constraint for both unique and not null.
Syntax:
Create table tablename(Fieldname1 datatype(constraint)unique,fieldname2 datatype,
…………….Fieldname3 datatype);
Example:
SQL> create table con(empid varchar2(10),empname varchar2(20)
empid: 1
5. ADDING CONSTRIANT:
Used to set any constraint to the specified column at the last by specifying the constraint
type and field name.
Syntax:
Create table tablename(Fieldname1 datatype(constraint)unique,fieldname2 datatype,
constraint constraintname constrainttype(fieldname));
Example:
SQL> create table con(empid varchar2(10),empname varchar2(10),constraint c1
primary key(empid));
Table created.
empid: 1
6. ADD CONSTRAINT(ALTER)
Used to set the constraint for the table already created by using alter command.
Syntax:
Alter table tablename add constraint constraintname (fieldname)datatype,primary key.
Example:
SQL> create table con(empid varchar2(10),empname
---
NOT NULL
EMPID VARCHAR2(10)
EMPNAME VARCHAR2(10)
7. DROP CONSTRAINT:
Used to drop the constraint.
Syntax:
Alter table tablename drop constraint constraintname.
Example:
SQL> alter table con drop constraint c1;
Table altered.
EMPID VARCHAR2(10)
EMPNAME VARCHAR2(10)
8. REFERENTIAL INTEGRITY:
Used to refer the primary key of the parent table from the childtable.
Syntax:
a) Create table tablename(Fieldname1 datatype primary key,fieldname2 datatype,
…………….Fieldname3 datatype);
b) Create table tablename(Fieldname1 datatype references,Parent tablename(fieldname)
…………….Fieldname n datatype);
Example:
SQL> create table parent(eno varchar2(10),ename varchar2(10)
SQL> /
Enter value for eno: 2
Enter value for ename: bala
old 1: insert into parent values(‘&eno’,’&ename’)
new 1: insert into parent values(‘2’,’bala’)
1 row created.
1 row created.
SQL> /
Enter value for eno: 2
Enter value for ename: balaji
old 1: insert into child values(‘&eno’,’&ename
Example:
SQL> create table parent(eno varchar2(10),ename varchar2(10)
1 a
2 a
SQL> delete from parent where eno=1;
1 row deleted.
SQL> select * from parent;
no rows selected
SQL> select * from child;
no rows selected
B) SUBQUERY:
1. UNION:
Syntax:
Select <fieldlist> from <tablename1> where (condition) union
2. INTERSECT:
Syntax:
Select <fieldlist> from <tablename1> where (condition) intersect
3. IN:
Syntax:
Select <fieldlist> from <tablename1> where (condition) in
4. BETWEEN:
Syntax:
Select <fieldlist> from <tablename1> where (condition) between
5. LIKE:
Syntax:
Select <fieldlist> from <tablename> where <fieldname> like <expression>;
6. NOT LIKE:
Syntax:
Select <fieldlist> from <tablename> where <fieldname> not like <expression>;
7. ALL:
Syntax:
Select <fieldlist> from <tablename1>where
where (condition);
8. ANY:
Syntax:
Select <fieldlist> from <tablename1> where
where(condition);
C) JOINS:
1. EQUI JOIN:
It retrieves rows from two tables having a common column using ‘=’ operators.
Syntax:
Select <tablename1.fieldlist,tablename2.fieldlist> from
<tablename1><tablename2> where
<tablename1.keyfield>=<tablename2.keyfield>;
2. INNER JOIN :
Inner join returns the matching rows from the tables that are being joined.
Syntax:
Select tablename1.fieldlist,tablename2.fieldlist from tablename1 inner join
tablename2 on tablename1.keyfield = tablename2.keyfield;
3. OUTER JOIN :
Outer join returns the both matching and non-matching rows for the tables that are
being joined. An outer join is an extended form of the inner join. The rows in one table
having no matching rows in the other table will also appear in the result table with nulls.
LEFT OUTER JOIN :
Syntax:
Select tablename1.fieldlist,tablename2.fieldlist from tablename1 left outer join
tablename2 on tablename1.keyfield = tablename2.keyfield;
RIGHT OUTER JOIN :
Syntax:
Select tablename1.fieldlist,tablename2.fieldlist from tablename1 right outer join
tablename2 on tablename1.keyfield = tablename2.keyfield;
FULL OUTER JOIN :
Syntax:
Select tablename1.fieldlist,tablename2.fieldlist from tablename1 full outer join
tablename2 on tablename1.keyfield = tablename2.keyfield;
1 arun 10000
2 bala 20000
EMPNO EMPNAME
EMPSALARY
1 arun 10000
3 chitra 40000
Output for Subquery:
SQL> select * from employee where empsalary=(select min(empsalary)from employee1);
EMPNO EMPNAME EMPSALARY
1 arun 10000
UNION:
SQL> select empname,empno from employee where(empsalary>10000)
union select empname,empno from employee1 where(empsalary>10000);
EMPNAME EMPNO
bala 2
chitra 3
INTERSECT:
SQL> select empname,empno from employee where(empsalary>9000)
intersect select empname,empno from employee1 where(empsalary>9000);
EMPNAME EMPNO
arun 1
IN:
SQL> select empname from employee where empsalary in(select empsalary from
employee1);
EMPNAME
arun
SQL> select empno,empname from employee where empno in(select
empsalary from employee1);
no rows selected
SQL> select empno,empname from employee where empno in(select empno from
employee1);
EMPNO EMPNAME
1 Arun
BETWEEN:
SQL> select empno,empname from employee where empsalary between 10000 and 30000;
EMPNO EMPNAME
1 arun
2bala
LIKE:
SQL> select empname,empno from employee where empname like ‘b%’;
EMPNAME EMPNO
bala 2
NOT LIKE:
SQL> select empname,empno from employee where empname not like ‘b%’;
EMPNAME EMPNO
arun 1
ALL:
SQL> select empname,empsalary from employee1 where empsalary > all (select
empsalary from employee where empsalary>10000);
EMPNAME EMPSALARY
chitra 40000
ANY:
SQL> select empname,empsalary from employee1 where empsalary>any
(select min(empsalary) from employee);
EMPNAME EMPSALARY
chitra 40000
OUTPUT / EXAMPLE(JOINS):
SQL> create table employee (empid varchar2(10),empname varchar2(10),deptid
varchar2(10) primary key);
Table created.
SQL> /
Enter value for empid: 10ece02
Enter value for empname: bala
Enter value for deptid: ece
old 1: insert into employee values(‘&empid’,’&empname’,’&deptid’)
new 1: insert into employee values(‘10ece02’,’bala’,’ece’)
1 row created.
SQL> /
Enter value for empid: 10mech03
Enter value for empname: karthi
Enter value for deptid: MECH
old 1: insert into employee values(‘&empid’,’&empname’,’&deptid’)
new 1: insert into employee values(‘10mech03’,’karthi’,’MECH’)
1 row created.
cse
SQL> /
Enter value for deptid: ece
Enter value for deptname: electronics
old 1: insert into department values(‘&deptid’,’&deptname’)
new 1: insert into department values(‘ece’,’electronics’)
1 row created.
SQL> /
Enter value for deptid: mech
ece electronics
cse computerscience
mech mechanical
EQUI JOIN
SQL> select
employee.empid,employee.empname,department.deptid,department.deptname from
employee,department where employee.deptid = department.deptid;
EMPID EMPNAME DEPTID DEPTNAME ---
INNER JOIN:
SQL> select employee.empid,department.deptname from employee inner join
department on employee.deptid = department.deptid;
EMPID DEPTNAME
10ece02 electronics
10cse01 computerscience
EMPNAME DEPTNAME
anand computerscience
bala electronics
karthi
department on employee.deptid=department.deptid;
EMPNAME DEPTNAME
bala electronics
anand computerscience
mechanical
EMPNAME DEPTNAME
anand computerscience
bala electronics
karthi mechanical
RESULT:
Thus the Constraints, Nested Queries and joining records based on conditions in
RDBMS were executed and verified
Ex. No: 3 Views, Sequences ,Synonyms
AIM:
DESCRIPTION:
In SQL, a view is a virtual table based on the result-set of an SQL statement. A view
con-tains rows and columns, just like a real table. The fields in a view are fields from
one or more real tables in the database. A view is an object that gives the user the
logical view of data from the underlying table. Any relation that is not part of the logical
model but is made visible to the user as a virtual relation is called a view. They are
generally used to avoid duplication of data. Views are created for the following reasons,
• Data simplicity
• To provide data security
• Structural simplicity (because view contains only limited number of rows and
col-umns)
•
PROCEDURE:
Step 1: Create table with some attributes.
Step 2: Create view from single table with all attributes.
Step 3: Create view from single table with selected attributes.
Step 4: Create View from two tables with all attributes.
Step 5: Create View from two tables with selected attributes.
Step 6: Use DML commands in views.
Step 7: Drop the View.
1. Create a view from single table containing all columns from the
EID NUMBER(3)
ENAME VARCHAR2(10)
AGE NUMBER(3)
SALARY NUMBER(6)
DOB DATE
EID NUMBER(3)
ENAME VARCHAR2(10)
AGE NUMBER(3)
SALARY NUMBER(6)
DOB DATE
3. Create view from two table containing all columns from the base
table. SQL> create view viiew2 as select * from emp1 full natural
SALARY NUMBER(6)
EID NUMBER(3)
ENAME VARCHAR2(10)
AGE NUMBER(3)
DOB DATE
EMPID NUMBER(3)
EMPNAME VARCHAR2(10)
ENAME VARCHAR2(10)
AGE NUMBER(3)
EMPID NUMBER(3)
DML Commands in Views:
1. INSERT:
SQL> insert into viiew1 values(‘&eid’,’&ename’,’&salary’);
Enter value for eid: 1
Enter value for ename: anu
Enter value for salary: 25000
old 1: insert into viiew1 values(‘&eid’,’&ename’,’&salary’)
new 1: insert into viiew1 values(‘1’,’anu’,’25000’)
1 row created.
SQL> /
Enter value for eid: 2
Enter value for ename: abi
Enter value for salary: 20000
old 1: insert into viiew1 values(‘&eid’,’&ename’,’&salary’)
new 1: insert into viiew1 values(‘2’,’abi’,’20000’)
1 row created.
SQL> /
Enter value for eid: 3
Enter value for ename: cibi
Enter value for salary: 37000
old 1: insert into viiew1 values(‘&eid’,’&ename’,’&salary’)
SQL> /
Enter value for eid: 5
Enter value for ename: soman
Enter value for salary: 27000
old 1: insert into viiew1 values(‘&eid’,’&ename’,’&salary’)
new 1: insert into viiew1 values(‘5’,’soman’,’27000’)
1 row created.
SQL> /
Enter value for eid: 6
Enter value for ename: billu
Enter value for salary: 28000
old 1: insert into viiew1 values(‘&eid’,’&ename’,’&salary’)
new 1: insert into viiew1 values(‘6’,’billu’,’28000’)
1 row created
SQL> select * from viiew1;
EID ENAME SALARY
1 anu 25000
2 abi 20000
3 cibi 37000
4 babu 24000
5 soman 27000
6 billu 28000
2. UPDATE:
SQL>update viiew1 set eid=’8’ where
1 anu 25000
8 abi 20000
3 cibi 37000
4 babu 24000
5 soman 27000
6 billu 28000
1 anu 25000
2 abi 20000
3 cibi 37000
4 babu 24000
6 billu 28000
3. DELETE
SQL> delete from viiew1 where eid=’5’;
1 row deleted.
1 anu 25000
2 abi 20000
3 cibi 37000
4 babu 24000
6 billu 28000
5 rows selected.
DROP VIEW:
SQL> drop view viiew2;
View dropped.
SQL> desc viiew2;
ERROR:
ORA-04043: object viiew2 does not exist
SYNONYMS
AIM
To write query for synonyms to minimize the impact of moving or renaming the
database objects.
DESCRIPTION:
PROCEDURE:
Step 1: Create the database which contains several tables using DDL
EXAMPLE
In your database you have a schema called pacet. This schema contains a
table called EMPLOYEES.
AIM:
To write query for sequence to database object from which multiple users may
generate unique integers.
DESCRIPTION:
PROCEDURE:
Step 1: Create the sequence using create sequence command.
Step 2: Delete the sequence using drop sequence command.
Step 3: Save the current point transaction using savepoint command.
Step 4: Perform undo operation using rollback command.
1. Creat
e
Syntax:
CREATE SEQUENCE
sequence_name MINVALUE value
MAXVALUE value
START WITH value
INCREMENT BY
value CACHE value;
Example:
CREATE SEQUENCE employee
MINVALUE 1
MAXVALUE 888888888888888888888
START WITH 1
INCREMENT BY 1
CACHE 20;
2. Drop
Syntax
:
DROP SEQUENCE
sequence_name; Example:
DROP SEQUENCE employee;
3. SAVEPOINT
Syntax:
SAVEPOINT identifier
RELEASE SAVEPOINT identifier
Example:
UPDATE employees
SET salary = 7000
WHERE last_name = ‘SURESH’;
SAVEPOINT sureshsave;
UPDATE employees
SET salary = 12000
WHERE last_name = ‘SUNDAR’;
SAVEPOINT sundarsave;
4. ROLLBACK
Syntax:
ROLLBACK [WORK] TO [SAVEPOINT] identifier
Example:
SELECT SUM (salary) FROM employees;
ROLLBACK TO SAVEPOINT sureshsave;
UPDATE employees
SET salary = 11000
WHERE last_name = ‘SUNDAR’;
COMMIT;
RESULT:
Thus the views,sequences and synonyms was executed and verified successfully.
EX NO 4 DATABASE PROGRAMMING: IMPLICIT AND
EXPLICIT CURSORS
IMPLICIT CURSORS
SYNTAX:
Exercise:
Write a PL/ SQL code for calculating total mark, percentage, grade for all the
students in a student management system using implicit cursors.
SQL> create table student(id number, name varchar2(10), dept varchar2(10), percent
number,m1 number,m2 number, m3 number, tot number, g varchar2(1));
Table created.
SQL> select * from student;
1 Anu it 0 90 89 80 0
2 Beena cse 0 98 91 95 0
3 Bindhu it 0 87 67 86 0
4 Varun it 0 67 46 50 0
5 Rahul cse 0 81 82 83 0
SQL> declare
3. ctot number;
4. cgra varchar2(1);
3. cper number;
4. begin
5. for I in c
6. loop
7. ctot= i.m1+i.m2+i.m3;
8. cper :=ctot/3;
23. else
1 end loop;
2 end;
3 /
3 Bindhu it 80 87 67 86 240 B
SYNTAX:
open cursor_name;
close cursor_name;
Exercise:
Write PL/ SQL code for calculating hra , da, netsalary for all the employees in the Payroll
Processing using Explicit cursor(uses employee table).
4 i employee% rowtype;
5 hrasal number;
6 dasal number;
7 pfsal number;
8 netsalary number;
9 begin
10 open c;
11 loop;
12 fetch c into i;
14 endif;
15 hrasal:=i.basicpay*0.1;
16 dasal:=i.basicpay*0.08;
17 pfsal:=i.basicpay*0.12;
19 update employee set hra = hrasal, da= dasal, pf= pfsal, netsal= netsalaray where
empno=i.empno;
20 end loop;
21 close c;
22 end;
23 /
PL/ SQL procedure successfully completed.
SQL> select * from employee;
Thus the implicit and explicit cursor was executed and verified successfully.
Ex: No: 5 PROCEDURES AND FUNCTIONS
AIM:
To implement and execute PL/SQL Block that handles all types of exceptions in
Oracle Database using Procedural Language concepts.
PROCEDURE:
Step 1: Declare the necessary variables.
Step 2: Begin the program.
Step 3: Assign Values to the variables.
Step 4: Write the looping condition
Step 5: Perform calculation.
Step 6: End loop.
Step 7: Execute the procedure
1. Create
Syntax:
CREATE [OR REPLACE] PROCEDURE proc_name [list of
BEGIN
Execution section
EXCEPTION
Exception section
END;
Example:
SQL> create or replace procedure sum(n
number) is i number;
sum1 number;
begin
sum1:=0;
i:=0;
while i<=n
loop
sum1:=sum1+i;
i:=i+1;
end loop;
dbms_output.put_line(sum1);
end;
/
Procedure created.
Execution of procedure:
2. Drop:
Syntax:
Drop Procedure procedure_name;
Example:
Drop Procedure Sum;
3. Exception Handling:
Syntax:
DECLARE
Declaration section
BEGIN
Exception section
EXCEPTION
WHEN ex_name1 THEN
-Error handling statements
WHEN ex_name2 THEN
-Error handling statements
WHEN Others THEN
-Error handling statements
END;
BEGIN
Execution section
EXCEPTION
WHEN NO_DATA_FOUND THEN
dbms_output.put_line (‘A SELECT...INTO did not return any
row.’); END;
DECLARE
Child_rec_exception EXCEPTION;
PRAGMA
EXCEPTION_INIT (Child_rec_exception, -2292);
BEGIN
Delete FROM product where product_id= 104;
EXCEPTION
WHEN Child_rec_exception
THEN Dbms_output.put_line(‘Child records are present for this
product_id.’); END;
c) User-defined Exceptions
Let’s consider the product table and order_items table from sql joins to explain
user-defined exception.
Let’s create a business rule that if the total no of units of any particular product
sold is more than 20, then it is a huge quantity and a special discount should be
provided.
DECLARE
huge_quantity EXCEPTION;
CURSOR product_quantity is
SELECT p.product_name as name, sum(o.total_units) as units
FROM order_tems o, product p
WHERE o.product_id = p.product_id;
quantity order_tems.total_units%type;
up_limit CONSTANT order_tems.total_units%type
BEGIN
FOR product_rec in product_quantity LOOP
quantity := product_rec.units;
IF quantity > up_limit THEN
message := ‘The number of units of product ‘ || product_rec.name ||
‘ is more than 20. Special discounts should be
skipped. ‘
RAISE huge_quantity;
ELSIF quantity < up_limit THEN
v_message:= ‘The number of unit is below the discount limit.’;
END IF;
dbms_output.put_line (message);
END LOOP;
EXCEPTION
WHEN huge_quantity THEN
dbms_output.put_line (message);
END;
FUNCTIONS
Aim :
Procedure:
6 for i in 1..n
7 loop
8 b:=b*i;
9 end loop;
10 return b;
11 end;
12 /
Function created.
SQL> Declare
2 n number:=&n;
3 y number;
4 begin
5 y:=fnfact(n);
6 dbms_output.put_line(y);
7 end;
8 /
Procedure:
Step 1: start
Step 2: create the table with essential attributes.
Step 3: initialize the function to carry out the searching procedure..
Step 4: frame the searching procedure for both positive and negative searching.
Step 5: execute the function for both positive and negative result .
Step 6: stop
Execution:
SQL> create table phonebook(phone_no number(6) primary key,username
varchar2(30),doorno varchar2(10), street varchar2(30),place varchar2(30),pincode
char(6));
Table created.
1 row created.
1 row created.
bharathi
20312 vijay 120/5D street NGO colony 629002
29467 vasanth 39D4 RK bhavan sarakkal vilai 629002
begin
select username||’,’||doorno ||’,’||street ||’,’||place||’,’||pincode into address from phonebook
where phone_no=phone;
return address;
exception
when no_data_found then return ’address not found’;
end;
/
Function created.
SQL>declare
address varchar2(100);
begin
address:=findaddress(20312);
dbms_output.put_line(address);
end;
/
Output 1:
vijay,120/5D,bharathi street,NGO colony,629002
Output2:
address not found
PL/SQL procedure successfully completed.
RESULT:
Thus the Procedure and Function process was executed and verified successfully.
Ex.No: 6 TRIGGERS
AIM:
To Study and Practice Creation, Insertion, Deletion and updating triggers based on
condi-tions in RDBMS.
DESCRIPTION:
Database Triggers
A trigger is a statement that is executed automatically by the system as a side effect
of a modification to the database. The parts of a trigger are,
• Trigger statement: Specifies the DML statements and fires the trigger body. It
also specifies the table to which the trigger is associated.
• Trigger body or trigger action: It is a PL/SQL block that is executed when the
trig-gering statement is used.
• Trigger restriction: Restrictions on the trigger can be achieved
Syntax:
Create or replace trigger <Trigger_name> before/after insert/delete/update on
<Table_ name> for each row/statement
DELCARE
<Declaration statements>
BEGIN
<Executable Statements>
END:
EID VARCHAR2(20)
ENAME CHAR(25)
AGE NUMBER(10)
SALARY NUMBER(10)
SQL> /
Enter value for eid: e2
Enter value for ename: abi
Enter value for age: 24
Enter value for salary: 25700
old 1: insert into emply101 values(‘&eid’,’&ename’,’&age’,’&salary’)
new 1: insert into emply101 values(‘e2’,’abi’,’24’,’25700’)
1 row created.
SQL> /
Enter value for eid: e3
Enter value for ename: babu
Enter value for age: 30
Enter value for salary: 34000
old 1: insert into emply101 values(‘&eid’,’&ename’,’&age’,’&salary’)
new 1: insert into emply101 values(‘e3’,’babu’,’30’,’34000’)
1 row created.
SQL> /
Enter value for eid: e4
Enter value for ename: cibi
Enter value for age: 32
Enter value for salary: 32000
old 1: insert into emply101 values(‘&eid’,’&ename’,’&age’,’&salary’)
new 1: insert into emply101 values(‘e4’,’cibi’,’32’,’32000’)
1 row created.
SQL> /
Enter value for eid: e5
Enter value for ename: brindha
Enter value for age: 28
Enter value for salary: 31000
old 1: insert into emply101 values(‘&eid’,’&ename’,’&age’,’&salary’)
new 1: insert into emply101 values(‘e5’,’brindha’,’28’,’31000’)
1 row created.
e1 anu 25 20000
e2 abi 24 25700
e3 babu 30 34000
e4 cibi 32 32000
e5 brindha 23 31000
EID VARCHAR2(20)
ENAME CHAR(25)
AGE NUMBER(10)
SALARY NUMBER(10)
SQL> create or replace trigger tfg1 before insert on emply101 for each row
2 begin
3 insert into emply102 values(:new.eid,:new.ename,:new.age,:new.salary);
4 end;
5 /
Trigger created.
e7 thara 28 30000
--
e7 thara 28 30000
e8 siju 29 36000
SQL> create or replace trigger trgrr15 after delete or update on emply101 for each row
2 begin
3 if deleting then
4 insert into emply102 values(:old.eid,:old.ename,:old.age,:old.salary);
5 elsif updating then
6 insert into emply102 values(:old.eid,:old.ename,:old.age,:old.salary);
7 end if;
8 end;
9 /
Trigger created.
e1 anu 25 20000
e2 abi 24 25700
e3 babu 30 34000
e4 cibi 32 32000
e5 brindha 23 31000
e7 thara 28 30000
e8 siju 29 s36000
7 rows selected.
--
e7 thara 28 30000
e8 siju 29 36000
e3 babu 30 34000
e7 thara 28 30000
e8 siju 29 36000
e3 babu 30 34000
e5 brindha 23 31000
RESULT:
Thus the Study and Practice Creation, Insertion, Deletion and updating triggers based on
conditions in RDBMS was executed and verified
EX NO 7 EXCEPTION HANDLING
SYNTAX:
DECLARE
<declaration section>
BEGIN
<executable commands>
EXECPTION
<exception handling>
End:
SQL>declare
2 b number
3 n number
4 balanlow exception
5 balanok exception
4. begin
5. n:=&n;
6. select bal into b from bank where accno=n;
7. if (b<500)then
8. raise balanlow;
9. else
12. exception
17. end;
Old 7: n:=&n;
New 7: n:=102;
SQL>
AIM:
To Study about Front End tools.
INTRODUCTION:
Visual Basic is an object oriented programming development system for
creating applications.
FORMS:
In visual basic, a window is called form.
CONTROLS:
The icons with which the user interacts are called controls.
OBJECTS:
The forms and controls are collectively called as objects.
PROPERTIES:
Objects include properties that generally define their appearance or behavior.
METHODS:
Some objects also include special program statements called methods.
TITLE BAR:
It includes the project name, an icon that closes, minimize, maximize VB at the top
right
corner.
MENU BAR:
It consist of menus such as File, Edit, View, Project, Format,……..
TOOL BAR:
It consists of icons which duplicates several of the more commonly used menu
selections like forms, save, open and so on.
FORM DESIGN AND PROJECT CONTAINER WINDOW:
The form design window is where the user interface is actually designed. This is
accomplished by selecting the desired control icons from the toolbox and placing them
in the form design.
TOOLBOX:
The toolbox contains icons that represent commonly used controls, such as label,
text box, command button, picture box, frame, checkbox, combo box, option button,
scroll bar, and so on.
PROPERTIES WINDOW:
Each object has its own unique list of properties. The properties window allows
you to change or assign the properties associated with a particular object.
PROJECT WINDOW:
The project window displays the hierarchical list of files associated with a given
project.
LINE:
Used to draw straight line segments within forms.
LIST BOX:
Provides a collection of text items.
OLE CONTAINER:
Allows a data object to be transferred from another windows application and
embedded within the visual basic applications.
OPTION BUTTON:
Provides a means of selecting one of several different options. Within a group of
option buttons one and only one can be selected.
SHAPE:
Used to draw circles, ellipse, squares and rectangles within the forms.
TEXT BOX:
It is used to enter text.
Tools in the Visual Basic Toolbox
Icon Tool Name What This Tool Does
Pointer Selects objects
Text box Draws a box that can display text and let the user type in text
Draws a drive list box that displays all the disk drives
Drive list box available
RESULT:
Thus the Front end tools were studied.
EX: NO: 9 FORM DESIGN
AIM
To design a form using Visual Basic.
PROCEDURE
Step 1: Start
Step 2: Create the form with essential controls in tool box.
Step 3: Write the code for doing the appropriate functions.
Step 4: Save the forms and project.
Step 5: Execute the form.
Step 6: Stop.
EXECUTION OF BIODATA
FORM1
Private Sub Command1_Click()
List1.AddItem Text1.Text
List1.AddItem Text2.Text
If Option1.Value = True Then
gender = “male”
End If
If Option2.Value = True Then
gender = “female”
End If
List1.AddItem gender
List1.AddItem Text3.Text
If Check1.Value = 1 And Check2.Value = 0 Then
area = “software Engineering”
End If
If Check1.Value = 1 And Check2.Value = 1 Then
FORM1
Private Sub Command1_Click()
Dim a As Integer
a = Val(Text1.Text) + Val(Text2.Text)
MsgBox (“Addition of Two numbers is” + Str(a))
End Sub
Private Sub Command2_Click()
Dim b As Integer
b = Val(Text1.Text) - Val(Text2.Text)
MsgBox (“Subraction of Two numbers is” + Str(b))
End Sub
Private Sub Command3_Click()
Dim c As Integer
c = Val(Text1.Text) * Val(Text2.Text)
MsgBox (“Multiplication of Two numbers is” + Str(c))
End Sub
Private Sub Command4_Click()
Dim d As Integer
d = Val(Text1.Text) / Val(Text2.Text)
MsgBox (“Division of Two numbers is” + Str(d))
End Sub
Private Sub Command5_Click()
End
End Sub
SNAPSHOT:
RESULT:
Thus the program for resume and calculator using Visual Basic was executed and
verified.
EX.NO: 10 MENU DESIGN
AIM:
To design a menu using Visual Basic.
PROCEDURE:
STEP 1: Start
STEP 2: Create the form with essential controls and insert the menu using menu editor.
STEP 3: Write the code for doing the appropriate functions.
STEP 4: Save the forms and project.
STEP 5: Execute the form.
STEP 6: Stop
EXECUTION:
FORM 1
Private Sub mapple_Click()
MsgBox (“You selected Apple”)
End Sub
Private Sub mblue_Click()
MsgBox (“You selected Blue color”)
End Sub
Private Sub mcircle_Click()
MsgBox (“You selected Circle”)
End Sub
Private Sub mexit_Click()
End
End Sub
Private Sub mgrapes_Click()
MsgBox (“You selected Grapes”)
End Sub
Private Sub mgreen_Click()
MsgBox (“You selected Green color “)
End Sub
Private Sub morange_Click()
MsgBox (“You selected Orange”)
End Sub
Private Sub mrectangle_Click()
MsgBox (“You selected Rectangle”)
End Sub
Private Sub mred_Click()
MsgBox (“You selected Red color”)
End Sub
Private Sub mtriangle_Click()
MsgBox (“You selected Triangle”)
End Sub
SNAPSHOT:
RESULT:
Thus the program for menu creation with menu editor has been developed and
executed successfully.
EX NO: 11 HOSPITAL MANAGEMENT SYSTEM
AIM
To develop a Hospital Management Application for Maintaining the Hospital
Process-ing.
PROCEDURE
Step1: Using Visual Studio 6.0 creates a necessary form to interacting with the database.
Step4: Click the Project Menu and then click the components.
Step5: Click the Control Tab; it will display number controls with checkbox for
choosing appropriate component.
Step6: Mark the Check box of Microsoft ADO Data Control 6.0(OLEDB), then click
Apply and ok button.
Step7: The ADO control is added in the Toolbox.
Step8: Place the ADO Control into the form in which the data base is required to connect.
Step9: Right click on the Control -> Click the ADODC properties->Property window
page appear.
Step10: Go to the General tab in that source of connection frame, select the use
connection string radio button, then click the build button.
Step11: Select the Provider tab, it show different number of providers, select the
Microsoft Jet 4.0 OLE DB provider.
Step12: Connect the data control to the form. Give the database name, user name and
pass-word
Step13: Click the test connection button. If entered correct password and username and
server name. It shows a Message Box test connection was succeeded.
Step14: Click the Record source tab, select command type combo box and choose
command type as 2-adCmdTable
Step15: Click the Table or Stored procedure combo box it will shows the tables from
the database. We can select the table which is going to be manipulated from the front
end.
Step16: Finally Click Apply and ok button.
Step17: Click the textbox and choose data source and data field for the
corresponding table.
Step18: Write the appropriate code for the necessary controls to manipulate
Step19: Run the program by click run button in the standard toolbar.
EXECUTION
FORM1
Private Sub Command1_Click()
If Text1.Text = “sundar” And Text2.Text = “1234” Then
Form2.Show
Unload Me
Else
MsgBox (“Login Failed! Try Again. .. ”)
Me.Refresh
End If
End Sub
Private Sub Command2_Click()
Me.Refresh
End Sub
FORM2
Private Sub Add_Doctorname_Click()
Form3.Show
End Sub
Private Sub Add_Medicinename_Click()
Form5.Show
End Sub
Private Sub Add_Patientname_Click()
Form4.Show
End Sub
Private Sub Delete_Doctorname_Click()
Form3.Show
End Sub
Private Sub Delete_Medicinename_Click()
Form5.Show
End Sub
Private Sub Delete_Patientname_Click()
Form4.Show
End Sub
Private Sub Search_Doctorname_Click()
Form3.Show
End Sub
Private Sub Search_Medicinename_Click()
Form5.Show
End Sub
Private Sub Search_Patientname_Click()
Form4.Show
End Sub
FORM3
Private Sub Command1_Click()
On Error GoTo Arr
Adodc1.Recordset.AddNew
MsgBox “Successfully Added”
Exit Sub
Arr:
MsgBox “Error in Adding”
End Sub
Private Sub Command2_Click(Index As Integer)
On Error GoTo Arr
Adodc1.Recordset.Update
MsgBox “Successfully updated”
Exit Sub
Arr:
MsgBox “Error in updating”
End Sub
Private Sub Command3_Click(Index As Integer)
On Error GoTo Arr
Adodc1.Recordset.Delete
MsgBox “Successfully Deleted”
Exit Sub
Arr:
MsgBox “Error in Deleting”
End Sub
Private Sub Command4_Click(Index As Integer)
Adodc1.Recordset.MoveFirst
End Sub
Private Sub Command5_Click(Index As Integer)
Adodc1.Recordset.MoveNext
End Sub
Private Sub Command6_Click(Index As Integer)
Adodc1.Recordset.MovePrevious
End Sub
Private Sub Command7_Click(Index As Integer)
Adodc1.Recordset.MoveLast
End Sub
Private Sub Command8_Click(Index As Integer)
Adodc1.Refresh
DataGrid1.Refresh
Adodc1.Recordset.Find “D_Id=” & Val(Text5.Text)
End Sub
Private Sub Form_Load()
Adodc1.Visible = False
End Sub
FORM4
Private Sub Command1_Click()
On Error GoTo Arr
Adodc2.Recordset.AddNew
MsgBox “Successfully Added”
Exit Sub
Arr:
MsgBox “Error in Adding”
End Sub
Private Sub Command2_Click(Index As Integer)
On Error GoTo Arr
Adodc2.Recordset.Update
MsgBox “Successfully updated”
Exit Sub
Arr:
MsgBox “Error in updating”
End Sub
Private Sub Command3_Click(Index As Integer)
On Error GoTo Arr
Adodc2.Recordset.Delete
MsgBox “Successfully Deleted”
Exit Sub
Arr:
MsgBox “Error in Deleting”
End Sub
Private Sub Command4_Click(Index As Integer)
Adodc2.Recordset.MoveFirst
End Sub
Private Sub Command5_Click(Index As Integer)
Adodc2.Recordset.MoveNext
End Sub
Private Sub Command6_Click(Index As Integer)
Adodc2.Recordset.MovePrevious
End Sub
Private Sub Command7_Click(Index As Integer)
Adodc2.Recordset.MoveLast
End Sub
Private Sub Command8_Click(Index As Integer)
Adodc2.Refresh
DataGrid2.Refresh
Adodc2.Recordset.Find “P_Id=” & Val(Text5.Text)
End Sub
FORM5
Private Sub Command1_Click()
On Error GoTo Arr
Adodc3.Recordset.AddNew
MsgBox “Successfully Added”
Exit Sub
Arr:
MsgBox “Error in Adding”
End Sub
Private Sub Command2_Click(Index As Integer)
On Error GoTo Arr
Adodc3.Recordset.Update
MsgBox “Successfully updated”
Exit Sub
Arr:
MsgBox “Error in updating”
End Sub
Private Sub Command3_Click(Index As Integer)
On Error GoTo Arr
Adodc3.Recordset.Delete
MsgBox “Successfully Deleted”
Exit Sub
Arr:
MsgBox “Error in Deleting”
End Sub
Private Sub Command4_Click(Index As Integer)
Adodc3.Recordset.MoveFirst
End Sub
Private Sub Command5_Click(Index As Integer)
Adodc3.Recordset.MoveNext
End Sub
Private Sub Command6_Click(Index As Integer)
Adodc3.Recordset.MovePrevious
End Sub
Private Sub Command7_Click(Index As Integer)
Adodc3.Recordset.MoveLast
End Sub
Private Sub Command8_Click(Index As Integer)
Adodc3.Refresh
DataGrid3.Refresh
Adodc3.Recordset.Find “M_Id=” & Val(Text5.Text)
End Sub
Private Sub Form_Load()
Adodc3.Visible = False
End Sub
SCREENSHOTS:
RESULT:
Thus the project for hospital management system was designed in Visual Basic
using Microsoft Access as backend and successfully executed.
EX NO: 12 RAILWAY RESERVATION SYSTEM
AIM
To develop a railway reservation application for maintains the reservation processing.
PROCEDURE
Step1: Using Visual Studio 6.0 creates a necessary form to interacting with the database.
Step4: Click the Project Menu and then click the components.
Step5: Click the Control Tab; it will display number controls with checkbox for
choosing appropriate component.
Step6: Mark the Check box of Microsoft ADO Data Control 6.0(OLEDB), then click
Apply and ok button.
Step7: The ADO control is added in the Toolbox.
Step8: Place the ADO Control into the form in which the data base is required to connect.
Step9: Right click on the Control -> Click the ADODC properties->Property window
page appear.
Step10: Go to the General tab in that source of connection frame, select the use
connection string radio button, then click the build button.
Step11: Select the Provider tab, it show different number of providers, select the
Microsoft Jet 4.0 OLE DB provider.
Step12: Connect the data control to the form. Give the database name, user name and
pass-word
Step13: Click the test connection button. If entered correct password and username and
server name. It shows a Message Box test connection was succeeded.
Step14: Click the Record source tab, select command type combo box and choose
command type as 2-adCmdTable
Step15: Click the Table or Stored procedure combo box it will shows the tables from
the da-tabase. We can select the table which is going to be manipulated from the front
end.
Step16: Finally Click Apply and ok button.
Step17: Click the textbox and choose data source and data field for the
corresponding table.
Step18: Write the appropriate code for the necessary controls to manipulate
Step19: Run the program by click run button in the standard toolbar.
EXECUTION:
FORM1
Private Sub Command1_Click()
If Text3.Text = “sundar” And Text4.Text = “123” Then
Unload Me
MDIForm1.Show
Else
MsgBox (“Invalid Username/Password”)
End If
End Sub
Private Sub Command2_Click()
End
End Sub
FORM2
Combo1.Text = “”
Combo2.Text = “”
Combo3.Text = “”
Combo4.Text = “”
Combo5.Text = “”
Combo6.Text = “”
Combo7.Text = “”
Check1.Value = False
Check2.Value = False
Check3.Value = False
Check4.Value = False
Check5.Value = False
Option6.Value = False
End Sub
rs1.AddNew
rs2.AddNew
rs3.AddNew
rs4.AddNew
rs5.AddNew
rs6.AddNew
Text1.Text = Temp1
Adodc1.Recordset.Find “Train_No =” & Text1.Text, 0, adSearchForward
Text2.Text = Text1.Text
Text3.Text = Text18.Text
Text4.Text = Text19.Text
Text17.Text = Text20.Text
Text24.Text = Text1.Text
Text25.Text = Text18.Text
Text26.Text = Text19.Text
Text27.Text = Text20.Text
Text31.Text = Text1.Text
Text32.Text = Text18.Text
Text33.Text = Text19.Text
Text34.Text = Text20.Text
Text38.Text = Text1.Text
Text39.Text = Text18.Text
Text40.Text = Text19.Text
Text41.Text = Text20.Text
Text45.Text = Text1.Text
Text46.Text = Text18.Text
Text47.Text = Text19.Text
Text48.Text = Text20.Text
Text52.Text = Text1.Text
Text53.Text = Text18.Text
Text54.Text = Text19.Text
Text55.Text = Text20.Text
Adodc2.Recordset.MoveLast
Text60.Text = Text59 + 1
Text61.Text = Text59 + 1
Text62.Text = Text59 + 1
Text63.Text = Text59 + 1
Text64.Text = Text59 + 1
Text65.Text = Text59 + 1
End Sub
Private Sub check1_Click()
If Text11.Text < 60 Then
MsgBox (“Age Should Be More Than 60”)
Check1.Value = False
End If
End Sub
Form3
Private Sub Text10_Change()
If Val(Text10.Text) > 0 And Val(Text10.Text) < 18 Then
Text2.Text = Text2.Text + 1
ElseIf Val(Text10.Text) > 18 And Val(Text10.Text) < 60 Then
Text1.Text = Text1.Text + 1
ElseIf Val(Text10.Text) > 60 Then
Text3.Text = Text3.Text + 1
End If
End Sub
End Sub
FORM4
Private Sub Combo1_Click()
Adodc1.Refresh
Adodc1.Recordset.Find “Train_No =” & Combo1.Text, 0,
adSearchForward If Adodc1.Recordset.EOF = True Then MsgBox
(“Train not Available”)
End If
End Sub
Private Sub Command1_Click()
Temp1 = Combo1.Text
Unload Me
Load Form2
Form2.Show
End Sub
Private Sub Command2_Click()
Unload Me
End Sub
FORM5
Private Sub Command1_Click()
Unload Me
End Sub
Then s1 = “delete from reservation where PNR_NO = “ & Text1.Text & “ “ Connect
(s1)
Temp5 = Text1.Text
n7 = Text2.Text
Unload Me
Load Form7
Form7.Show
End If
End Sub
Private Sub Text1_Change()
s = “select * from reservation where PNR_NO = “ & Text1.Text &
“ “ Connect (s)
Set DataGrid1.DataSource = rs
Text2.Text = rs.RecordCount
End Sub
FORM6
Private Sub Command1_Click()
Unload Me
End Sub
FORM7
SCREENSHOTS:
A.133
A.134-A
A.135
A.136-A
RESULT:
Thus the project for railway reservation system was designed in Visual Basic
using Microsoft Access as backend and successfully execute