DEPARTMENT OF COMPUTER APPLICATIONS
C. S. I. INSTITUTE OF TECHNOLOGY
THOVALAI - 629 302
PRACTICAL RECORD
NAME : ________________________________
REG.NUMBER : ________________________________
SUBJECT : Database Management System
SUB.CODE : BX 4004
DEPARTMENT OF COMPUTER APPLICATIONS
C. S. I. INSTITUTE OF TECHNOLOGY
THOVALAI - 629 302
CERTIFICATE
Certified that, this is a bonafide record work done
by_______________________ Reg. No______________________ in the MCA
Computer Laboratory for Database Management System (Sub.Code: BX
4004) at C. S. I. Institute of Technology, Thovalai during the year 2022-2023.
Staff-in-Charge Head of the Department
Submitted for the Anna University MCA Degree Practical Examination held at
C. S. I. INSTITUTE OF TECHNOLOGY, THOVALAI on _______________
Internal Examiner External Examiner
Sl. No CONTENTS Page No.
EXP. No. : 1 CREATION OF BASE TABLES VIEWS
DATE :
AIM:
To create database and apply Views, Sequences, Synonyms.
PROCEDURE
STEP 1: Start
STEP 2: Create the table with its essential attributes.
STEP 3: Insert attribute values into the table.
STEP 4: Create the view from the above created table.
STEP 5: Execute different Commands and extract information from the View.
STEP 6: Stop
SQL COMMANDS
1. COMMAND NAME: CREATE VIEW
COMMAND DESCRIPTION: CREATE VIEW command is used to define a view.
2. COMMAND NAME: INSERT IN VIEW
COMMAND DESCRIPTION: INSERT command is used to insert a new row into the view.
3. COMMAND NAME: DELETE IN VIEW
COMMAND DESCRIPTION: DELETE command is used to delete a row from the view.
4. COMMAND NAME: UPDATE OF VIEW
COMMAND DESCRIPTION: UPDATE command is used to change a value in a tuplewithout
changing all values in the tuple.
5. COMMAND NAME: DROP OF VIEW
COMMAND DESCRIPTION: DROP command is used to drop the view table
COMMANDS EXECUTION
CREATION OF TABLE
SQL> CREATE TABLE EMPLOYEE (EMPLOYEE_NAME VARCHAR(10),EMPLOYEE_NO
INT(8),DEPT_NAME VARCHAR(10),DEPT_NO INT (5),DATE_OF_JOIN DATE);
Table created.
TABLE DESCRIPTION
SQL> DESC EMPLOYEE;
NAME NULL? TYPE
------------------------------- -------- ------------------------
EMPLOYEE_NAME VARCHAR(10)
EMPLOYEE_NO INT (8)
DEPT_NAME VARCHAR(10)
DEPT_NO INT(5)
DATE_OF_JOIN DATE
SYNTAX FOR CREATION OF VIEW
SQL> CREATE <VIEW><VIEW NAME> AS SELECT<COLUMN_NAME_1>,
<COLUMN_NAME_2> FROM <TABLE NAME>;
CREATION OF VIEW
SQL> CREATE VIEW EMPVIEW AS SELECT
EMPLOYEE_NAME,EMPLOYEE_NO,DEPT_NAME,DEPT_NO,DATE_OF_JOIN FROM
EMPLOYEE;
VIEW CREATED.
DESCRIPTION OF VIEW
SQL> DESC EMPVIEW;
NAME NULL? TYPE
----------------------------------------- -------- ----------------------------
EMPLOYEE_NAME VARCHAR2(10)
EMPLOYEE_NO INT(8)
DEPT_NAME VARCHAR2(10)
DEPT_NO INT(5)
DISPLAY VIEW:
SQL> SELECT * FROM EMPVIEW;
EMPLOYEE EMPLOYEE_NO DEPT_NAME DEPT_NO
_N
RAVI 124 ECE 89
VIJAY 345 CSE 21
RAJ 98 IT 22
GIRI 100 CSE 67
INSERTION INTO VIEW
INSERT STATEMENT:
SYNTAX:
SQL> INSERT INTO <VIEW_NAME> (COLUMN NAME1,………)VALUES(VALUE1,….);
SQL> INSERT INTO EMPVIEW VALUES ('SRI', 120,'CSE', 67,'16-NOV-1981');
1 ROW CREATED.
SQL> SELECT * FROM EMPVIEW;
EMPLOYEE_N EMPLOYEE_NO DEPT_NAME DEPT_NO
---------- ----------- ---------- ----------
RAVI 124 ECE 89
VIJAY 345 CSE 21
RAJ 98 IT 22
GIRI 100 CSE 67
SRI 120 CSE 67
SQL> SELECT * FROM EMPLOYEE;
EMPLOYEE_N EMPLOYEE_NO DEPT_NAME DEPT_NO DATE_OF_J
---------- ----------- ---------- ---------- ---------
RAVI 124 ECE 89 15-JUN-05
VIJAY 345 CSE 21 21-JUN-06
RAJ 98 IT 22 30-SEP-06
GIRI 100 CSE 67 14-NOV-81
SRI 120 CSE 67 16-NOV-81
DELETION OF VIEW:
DELETE STATEMENT:
SYNTAX:
SQL> DELETE <VIEW_NMAE>WHERE <COLUMN NMAE> =’VALUE’;
SQL> DELETE FROM EMPVIEW WHERE EMPLOYEE_NAME='SRI';
1 ROW DELETED.
SQL> SELECT * FROM EMPVIEW;
EMPLOYEE_N EMPLOYEE_NO DEPT_NAME DEPT_NO
---------- ----------- - --------- ----------
RAVI 124 ECE 89
VIJAY 345 CSE 21
RAJ 98 IT 22
GIRI 100 CSE 67
UPDATE
STATEMENT:
SYNTAX:
AQL>UPDATE <VIEW_NAME> SET< COLUMN NAME> = <COLUMN NAME>
+<VIEW> WHERE <COLUMNNAME>=VALUE;
SQL> UPDATE EMPVIEW SET EMPLOYEE_NAME='KAVI'
WHERE EMPLOYEE_NAME='RAVI';
1 ROW UPDATED.
SQL> SELECT * FROM EMPKAVIVIEW;
EMPLOYEE_N EMPLOYEE_NO DEPT_NAME DEPT_NO
---------- ----------- ---------- ----------
KAVI 124 ECE 89
VIJAY 345 CSE 21
RAJ 98 IT 22
GIRI 100 CSE 67
DROP A
VIEW:
SYNTAX:
SQL> DROP VIEW <VIEW_NAME>
EXAMPLE
SQL>DROP VIEW
EMPVIEW; VIEW DROPED.
EXP. No. : 2 DATA MANIPULATION COMMANDS FOR INSERTION,
DELETION, UPDATION IN TABLES
AIM:
To create table and Execute Data Manipulation Commands for Insertion, Deletion,
Updation in tables
DML (DATA MANIPULATION LANGUAGE)
SELECT
INSERT
DELETE
UPDATE
PROCEDURE
STEP 1: Start
STEP 2: Create the table with its
essential attributes. STEP 3: Insert the
record into table
STEP 4: Update the existing records into the table
STEP 5: Delete the records in to the table
STEP 6: use save point if any changes occur in any portion of the record to undo its
original state. STEP 7: use rollback for completely undo the records
STEP 6: use commit for permanently save the records.
SQL COMMANDS
1. COMMAND NAME: INSERT
COMMAND DESCRIPTION: INSERT command is used to Insert objects in the
database.
2. COMMAND NAME: SELECT
COMMAND DESCRIPTION: SELECT command is used to SELECT the object from
the database.
3. COMMAND NAME: UPDATE
COMMAND DESCRIPTION: UPDATE command is used to UPDATE the records
from the table
4. COMMAND NAME: DELETE COMMAND DESCRIPTION: DELETE command
is used to
DELETE the Records form the table
5. COMMAND NAME: COMMIT
COMMAND DESCRIPTION: COMMIT command is used to save the Records.
6. COMMAND NAME: ROLLBACK
COMMAND DESCRIPTION: ROLL BACK command is used to undo the Records.
6. COMMAND NAME: SAVE POINT
COMMAND DESCRIPTION: SAVE POINT command is used to undo the Records
in a particular transaction.
INSERT
QUERY : 01
Q1. Write a query to insert the records in to employee.
Syntax for Insert Records in to a table:
SQL :> INSERT INTO <TABLE NAME> VALUES< VAL1, ‘VAL2’,…..);
QUERY: 01
INSERT A RECORD FROM AN EXISTING TABLE:
SQL>INSERT INTO EMP VALUES(101,'NAGARAJAN','LECTURER',15000);
1 row created.
SELECT
QUERY: 02
Q2. Write a query to display the records from employee.
Syntax for select Records
from the table: SQL>
SELECT * FROM
<TABLE NAME>;
QUERY: 02
DISPLAY THE EMP TABLE:
SQL> SELECT * FROM EMP;
EMPNO ENAME DESIGNATIN SALARY
---------- ------------ ---------- ----------
101 NAGARAJAN LECTURER 150
INSERT INTO EMP VALUES(102,'SARAVANAN','LECTURER','15000')
1 row created.
INSERT INTO EMP VALUES(103,'PANNER','ASST.PROF','20000')
1 row created.
INSERT INTO EMP VALUES(104,'CHINNI','HOD, PROF','45000')
1 row created.
SQL> SELECT * FROM EMP;
EMPNO ENAME DESIGNATIN SALARY
---------- ------------ ---------- ----------
101 NAGARAJAN LECTURER 15000
102 SARAVANAN LECTURER 15000
103 PANNER ASST. PROF 20000
104 CHINNI HOD, PROF 45000
QUERY: 03
Q3. Write a query to update the records from employee.
Syntax for update Records from the table:
SQL> UPDATE <<TABLE NAME> SET <COLUMNANE>=<VALUE>
WHERE<COLUMN NAME=<VALUE>;
QUERY: 03
SQL> UPDATE EMP SET SALARY=16000 WHERE EMPNO=101;
1 row updated.
SQL> SELECT * FROM EMP;
EMPNO ENAME DESIGNATION SALARY
---------- ------------ ---------- ----------
101 NAGARAJAN LECTURER 16000
102 SARAVANAN LECTURER 15000
103 PANNERSELVAM ASST. PROF 20000
104 CHINNI HOD, PROF 45000
UPDATE MULTIPLE COLUMNS
QUERY: 04
Q4. Write a query to update multiple records from employee.
Syntax for update multiple Records from the table:
SQL> UPDATE <<TABLE NAME> SET <COLUMNANE>=<VALUE>
WHERE<COLUMN NAME=<VALUE>;
QUERY: 04
SQL>UPDATE EMP SET SALARY = 16000, DESIGNATIN='ASST.
PROF' WHEREEMPNO=102;
1 row updated.
SQL> SELECT * FROM EMP;
EMPNO ENAME DESIGNATION SALARY
---------- ------------ ---------- ----------
101 NAGARAJAN LECTURER 16000
102 SARAVANAN ASST. PROF 16000
103 PANNERSELVAM ASST. PROF 20000
104 CHINNI HOD, PROF 45000
DELETE
QUERY: 05
Q5. Write a query to delete records from employee.
Syntax for delete Records from the table:
SQL> DELETE FROM <TABLE NAME> WHERE <COLUMN NAME>=<VALUE>;
SQL> DELETE FROM EMP WHERE EMPNO=103;
1 row deleted.
SQL> SELECT * FROM EMP;
EMPNO ENAME DESIGNATION SALARY
---------- ------------ ---------- ----------
101 NAGARAJAN LECTURER 16000
102 SARAVANAN ASST. PROF 16000
104 CHINNI HOD, PROF 45000
SAVEPOINT:
QUERY: 06
EXP. No. : 3 DATA CONTROL COMMANDS
AIM: To write Data control language commands and verify the same
1. Write a query to end your current transaction and make permanent all changes
performed in the transaction.
SQL> commit;
Commit complete.
2. Write a query to create a table goods with sno,itemcode,itemname,costnumber as its
attributes and assign primary key constraint for the column “itemcode”.
SQL> create table goods(sno number,itemcode number primary key,itemname varchar2(10),cost
number);
Table created.
SQL> insert into goods values(&sno,&itemcode,'&itemname',&cost);
Enter value for sno: 1
Enter value for itemcode: 1025
Enter value for itemname: dell moniters
Enter value for cost: 5000
old 1: insert into goods values(&sno,&itemcode,'&itemname',&cost)
new 1: insert into goods values(1,1025,'dell moniters',5000)
insert into goods values(1,1025,'dell moniters',5000)
*
ERROR at line 1:
ORA-01401: inserted value too large for column
SQL> insert into goods values(&sno,&itemcode,'&itemname',&cost);
Enter value for sno: 1
Enter value for itemcode: 1025
Enter value for itemname: moniter
Enter value for cost: 5000
old 1: insert into goods values(&sno,&itemcode,'&itemname',&cost)
new 1: insert into goods values(1,1025,'moniter',5000)
1 row created.
SQL> /
Enter value for sno: 2
Enter value for itemcode: 1026
Enter value for itemname: mouse
Enter value for cost: 250
old 1: insert into goods values(&sno,&itemcode,'&itemname',&cost)
new 1: insert into goods values(1026,1026,'mouse',250)
1 row created.
SQL> /
Enter value for sno: 3
Enter value for itemcode: 1027
Enter value for itemname: RAM
Enter value for cost: 1500
old 1: insert into goods values(&sno,&itemcode,'&itemname',&cost)
new 1: insert into goods values(3,1027,'RAM',1500)
1 row created.
SQL> /
Enter value for sno: 4
Enter value for itemcode: 1028
Enter value for itemname: webcam
Enter value for cost: 350
old 1: insert into goods values(&sno,&itemcode,'&itemname',&cost)
new 1: insert into goods values(4,1028,'webcam',350)
1 row created.
SQL> /
Enter value for sno: 5
Enter value for itemcode: 1029
Enter value for itemname: pendrive
Enter value for cost: 500
old 1: insert into goods values(&sno,&itemcode,'&itemname',&cost)
new 1: insert into goods values(5,1029,'pendrive',500)
1 row created.
SQL> select *from goods;
SNO ITEMCODE ITEMNAME COST
---------- -------------------- ------------------- ----------
1 1025 moniter 5000
2 1026 mouse 250
3 1027 RAM 1500
4 1028 webcam 350
5 1029 pendrive 500
SQL> commit; Commit complete.
3. Write a query to add the record into the table “goods” and set the Savepoint S1, S2 and S3
and verify it.
SQL> insert into goods values(6,1030,'keyboard',500);
1 row created.
SQL> savepoint s1;
Savepoint created.
SQL> insert into goods values(7,1031,'DVD drive',2500);
1 row created.
SQL> savepoint s2;
Savepoint created.
SQL> insert into goods values(8,1032,'UPS',3000);
1 row created.
SQL> insert into goods values(9,1033,'CPU',5000);
1 row created.
SQL> savepoint s3;
Savepoint created.
4. Write a query to Rollback to Savepoint S3 and verify it.
SQL> rollback to savepoint s3;
Rollback complete.
To Verify: SQL> select *from goods;
SNO ITEMCODE ITEMNAME COST
---------- -------------------- ------------------ ----------
1 1025 Monitor 5000
2 1026 mouse 250
3 1027 RAM 1500
4 1028 webcam 350
5 1029 pendrive 500
6 1030 keyboard 500
7 1031 DVD drive 2500
8 1032 UPS 3000
9 1033 CPU 5000
9 rows selected.
4. Write a query to Rollback to Savepoint S2 and verify it.
SQL> rollback to savepoint s2;
Rollback complete.
To Verify: SQL> select *from goods;
SNO ITEMCODE ITEMNAME COST
---------- --------------------- ------------------ ----------
1 1025 moniter 5000
2 1026 mouse 250
3 1027 RAM 1500
4 1028 webcam 350
5 1029 pendrive 500
6 1030 keyboard 500
7 1031 DVD drive 2500
7 rows selected.
5. Write a query to Rollback completely and verify it.
SQL> rollback;
Rollback complete.
To Verify: SQL> select *from goods;
SNO ITEMCODE ITEMNAME COST
---------- --------------------- ------------------ ----------
1 1025 moniter 5000
2 1026 mouse 250
3 1027 RAM 1500
4 1028 webcam 350
5 1029 pendrive 500
EXP. No. : 4 WRITE A PL/SQL PROGRAM TO PRINT
MULTIPLICATION TABLE
AIM:
To create a PL/SQL block for multiplication table.
PROCEDURE:
create or replace procedure
put(num number)
is
C number:=num; X
number:=1; BEGIN
LOOP
dbms_output.put_line(C||'X'||X||'='||C*x);
x:=x+1;
EXIT when x>10;
END LOOP;
END ;
Procedure created.
Put(8)
OUTPUT
N: 8
8X1=8
8X2=16
8X3=24
8X4=32
8X5=40
8X6=48
8X7=56
8X8=64
8X9=72
8X10=80
PL/SQL procedure successfully completed.
EXP. No. : 5 WRITE A PL/SQL PROGRAM TO CHECK WHETHER GIVEN
STRING IS PALINDROME OR NOT
AIM:
To create a PL/SQL block for check the given string is palindrome or not.
PROCEDURE
DECLARE
s VARCHAR2(10) := 'abccba';
l VARCHAR2(20);
t VARCHAR2(10);
BEGIN
FOR i IN REVERSE 1..Length(s) LOOP
l := Substr(s, i, 1);
t := t
||''
||l;
END LOOP;
IF t = s THEN
dbms_output.Put_line(t
||''
||' is palindrome');
ELSE
dbms_output.Put_line(t
||''
||' is not palindrome');
END IF;
END;
Output
abccba is palindrome
EXP. No. : 6 CURSORS, FUNCTIONS AND PROCEDURES
AIM:
To write a PL/SQL program for an application using cursor.
Cursor:
1.Declare the necessary variables with appropriate data types.
2.Define the cursor a, open it and select the value of m3 from the table where m3=51.
3. In the loop section, fetch the value of the cursor ‘a’ into sm3 and update (the
stud view) the sname to ‘aaa’ where m3=sm3.
4. Close the loop and print that table which has been updated.
5. Close the cursor and stop the execution.
Declare
eno employee.nn %type;
ename employee.nname % type;
esalary employee.nbp % type;
cursor employe is select nn,nname,nbp from employee where nn=&eno;
begin
open employe;
loop
fetch employe into eno,ename,esalary;
exit when employe%NOTFOUND;
dbms_output.put_line('Employee no:'||eno);
dbms_output.put_line('Employee name:'||ename);
dbms_output.put_line('Employee salary:'||esalary);
end loop;
close employe;
end;
OUTPUT:
Enter value for eno: 101
old 5: cursor employe is select nn,nname,nbp from employee where nn=&eno;
new 5: cursor employe is select nn,nname,nbp from employee where nn=101;
Employee no:101
Employee name:ram
Employee salary:1500
PL/SQL procedure successfully completed.
Creating a procedure to prepare reports for an application using functions.
Functions and Procedures
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.
create or replace function bill(N number)
return number is
ba number;
begin
if(N>3000) then
ba:=N*5;
else
ba:=N*3;
end if;
return(ba);
end;
declare
no eb.cno % type;
name eb.cname%type;
cr eb.cmr % type;
pr eb.pmr % type;
tr number;
ba number;
begin
select cno,cname,cmr,pmr into no,name,cr,pr from eb where cno=&no;
tr:=cr-pr;
ba:=bill(tr);
dbms_output.put_line('Customer No:'||no);
dbms_output.put_line('Customer Name:'||name);
dbms_output.put_line('PR:'||pr);
dbms_output.put_line('TR:'||tr);
dbms_output.put_line('BA:'||ba);
end;
OUTPUT:
Enter value for no: 111
old 9: select cno,cname,cmr,pmr into no,name,cr,pr from eb where cno=&no;
new 9: select cno,cname,cmr,pmr into no,name,cr,pr from eb where cno=111;
Customer No:111
Customer Name:Somu
PR:1000
TR:3000
BA:9000
Enter value for no: 100
old 9: select cno,cname,cmr,pmr into no,name,cr,pr from eb where cno=&no;
new 9: select cno,cname,cmr,pmr into no,name,cr,pr from eb where cno=100;
declare
*
EXP. No. : 7 TRIGGERS
AIM:
To create a PL/SQL block for transaction applications of a typical application
using triggers.
DESCRIPTION:
1. Create the triggers and the time / situation as to when it should be found or
triggered.
2. Select the item code from order mastertable where qtyhand < re level and put
or insert values into code.
3. Check if itemcode is in the ob and if so raise an application error.
4. Stop the process.
\
create or replace trigger resist before
insert or update on deposit for each row
begin
if:NEW.balance<1000 then
raise_application_error(-2002,'Minimum balance should be greater than 1000');
end if;
end;
OUTPUT:
SQL> insert into deposit values(103,'raman',100,900);
insert into deposit values(103,'raman',100,900)
*
ERROR at line 1:
ORA-21000: error number argument to raise_application_error of -2002 is out of range
ORA-06512: at "SCOTT.RESIST", line 3
ORA-04088: error during execution of trigger 'SCOTT.RESIST'
SQL> insert into deposit values(104,'rajr',1000,6000);
1 row created.
EXP. No. : 8
PL/SQL BLOCK THAT HANDLES EXCEPTIONS
AIM: To write PL/SQL program to handle exceptions.
PROGRAM - DIVIDE BY ZERO EXCEPTION
PL / SQL Query:
DECLARE
a NUMBER;
b NUMBER;
c NUMBER;
BEGIN
a:=:a;
b:=:b;
c:=a/b;
dbms_output.put_line('Result : '||c);
EXCEPTION
WHEN ZERO_DIVIDE THEN RAISE_APPLICATION_ERROR(-20003,'Invalid Input');
END; /
Output1:
Enter value for a: 10
Enter value for b: 5
Result : 2
Statement Processed.
Output2:
Enter value for a: 0
Enter value for b: 10
Result : 0
Statement Processed.
Output 3:
Enter value for a: 10
Enter value for b: 0
Invalid Input
Statement Processed.
Result
Thus the PL/SQL statement that handles exceptions were successfully executed and
verified.