0% found this document useful (0 votes)
4 views

NISANTH DBT LAB 24MCT12 FINAL

The document is a laboratory record for a student named G. Nisanth from Kongu Engineering College, detailing the experiments conducted in the Database Technologies Laboratory. It includes a list of exercises involving DDL, DML, TCL, and DCL commands, along with examples of SQL commands for creating and manipulating a student database. The document certifies the completion of practical work for the academic year 2024-2025.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

NISANTH DBT LAB 24MCT12 FINAL

The document is a laboratory record for a student named G. Nisanth from Kongu Engineering College, detailing the experiments conducted in the Database Technologies Laboratory. It includes a list of exercises involving DDL, DML, TCL, and DCL commands, along with examples of SQL commands for creating and manipulating a student database. The document certifies the completion of practical work for the academic year 2024-2025.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 79

KONGU ENGINEERING COLLEGE

(Autonomous)
Perundurai, Erode – 638 060

DEPARTMENT OF COMPUTER APPLICATIONS

22MCL12 – DATABASE TECHNOLOGIES LABORATORY

Name : G.NISANTH

Register Number : 24MCR074

Branch : Computer Applications

Semester :I

Certified that this is a bonafide record of work for the above student for

24MCL12 – DATABASE TECHNOLOGIES LABORATORY during the academic


year 2024- 2025.

Submitted for the Model Practical Examination held on

LAB IN-CHARGE Head of the Department

Examiner - 1 Examiner - 2
KONGU ENGINEERING COLLEGE
(Autonomous)
Perundurai, Erode – 638 060

DEPARTMENT OF COMPUTER APPLICATIONS

24MCL12- DATABASE TECHNOLOGIES LABORATORY

LIST OF EXPERIMENTS

.S.NO Date Exercise Name Page Marks Signature


No.

1. DDL and DML commands 03

2. DCL and TCL commands 13

3. Database Objects 20

4. Single and multi-row functions, Set 25


operations
5. Joins and Subqueries

6. Basic PL/SQL

7. Functions and Procedures

8. Cursor operations

9. Triggers and Packages

10. Exception Handling


Ex.no: 01

Date:

DDL, DML COMMANDS AND INTEGRITY CONSTRAINTS

AIM:

To perform DDL commands like create, alter, rename, truncate, drop and DML commands like
insert, update, select, delete and DDL with integrity constraints for University Database.

DDL COMMANDS:
1. Create

2. Alter

3. Rename

4. Truncate

5. Drop

1.CREATE

SQL> create table student (


sid int(10),
sname varchar(20),
dpmt varchar(5),
email varchar(25),
contactno numeric(10)
);

Table created.
SQL> describe student;

Name Null? Type

--------------------------------- -------- ------------------------

SID INTEGER (10)

SNAME VARCHAR(20)

DPMT VARCHAR(5)

EMAIL VARCHAR(25)

CONTACTNO NUMBER (10)

SQL> insert into student values


(101, 'Nisanth', 'MCA', '[email protected]', 9345292781),
(102, 'Sivakumar', 'MCA', '[email protected]', 8072363074),
(103, 'Sachin', 'MCA', '[email protected]', 8754681258),
(104, 'Siva', 'MCA', '[email protected]', 6380603146);

SQL> select * from student;

SID SNAME DPMT EMAIL CONTACTNO

101 Nisanth MCA [email protected] 9345292781

102 Sivakumar MCA [email protected] 8072363074

103 Sachin MCA [email protected] 8754681258

104 Siva MCA [email protected] 6380603146


2.ALTER

i) To add field name as AGE

SQL> alter table student add age int (3);


Table altered.
SQL> select * from student;

SID SNAME DPMT EMAIL CONTACTNO AGE

101 Nisanth MCA [email protected] 9345292781

102 Sivakumar MCA [email protected] 8072363074

103 Sachin MCA [email protected] 8754681258

104 Siva MCA [email protected] 6380603146

ii) To modify CONTACTNO field name:

SQL> alter table student modify contactno bigint;


Table altered.
SQL> describe student;
Name Null? Type

-------------------------------- ---------- -----------------------

SID NUMBER (10)

SNAME VARCHAR(20)

DPMT VARCHAR(5)

EMAIL VARCHAR(25)

CONTACTNO BIGINT

AGE INT (3)


iii) To rename field name Email to S_Email:

SQL> alter table student rename column Email to S_Email;


Table altered.
SQL> describe student;
Name Null? Type

-------------------------------- ---------- -----------------------

SID NUMBER (10)

SNAME VARCHAR(20)

DPMT VARCHAR(5)

S_ EMAIL VARCHAR(25)

CONTACTNO BIGINT

AGE INT (3)

iv) To drop field name:

SQL> alter table student drop column age;


Table altered.
SQL> describe student;
Name Null? Type

-------------------------------- ---------- -----------------------

SID NUMBER (10)

SNAME VARCHAR(20)

DPMT VARCHAR(5)

EMAIL VARCHAR(25)

CONTACTNO BIGINT
3.RENAME

SQL> alter table student rename to student_info;


Table renamed.

SQL>
describe student_info;
Name Null? Type

-------------------------------- ---------- -----------------------

SID NUMBER (10)

SNAME VARCHAR(20)

DPMT VARCHAR(5)

EMAIL VARCHAR(25)

CONTACTNO BIGINT

4.TRUNCATE

SQL>
truncate table student_info;
Table truncated.
SQL>
select * from student_info;
no rows selected
SQL> describe student_info;
Name Null? Type

-------------------------------- ---------- -----------------------

SID NUMBER (10)

SNAME VARCHAR(20)

DPMT VARCHAR(5)
EMAIL VARCHAR(25)

CONTACTNO BIGINT

5.DROP

SQL> drop table student_info;


Table dropped.
SQL>
describe student_info;
ERROR:
ORA-04043: object student_info does not exist

DML COMMANDS
1.Insert

2.Update

3.Select

4.Delete

Table Creation:

create table student (


sid int(10),
sname varchar(20),
dpmt varchar(5),
email varchar(25),
contactno numeric(10)
);

Table created.
SQL> describe student;

Name Null? Type

--------------------------------- -------- ------------------------

SID INTEGER (10)

SNAME VARCHAR(20)

DPMT VARCHAR(5)

EMAIL VARCHAR(25)

CONTACTNO NUMBER (10)

1.Insert command
SQL> insert into student values
(101, 'Nisanth', 'MCA', '[email protected]', 9345292781),
(102, 'Sivakumar', 'MCA', '[email protected]', 8072363074),
(103, 'Sachin', 'MCA', '[email protected]', 8754681258),
(104, 'Siva', 'MCA', '[email protected]', 6380603146);

2.Select Command

SQL> select * from student;

SID SNAME DPMT EMAIL CONTACTNO

101 Nisanth MCA [email protected] 9345292781

102 Sivakumar MCA [email protected] 8072363074

103 Sachin MCA [email protected] 8754681258

104 Siva MCA [email protected] 6380603146


3.Update Command

SQL>update student set sname = 'p. sivakumar' where sid = 102;

select*from student;

SID SNAME DPMT EMAIL CONTACTNO

101 Nisanth MCA [email protected] 9345292781

102 P.Sivakumar MCA [email protected] 8072363074

103 Sachin MCA [email protected] 8754681258

104 Siva MCA [email protected] 6380603146

4.Delete command

SQL> delete from student where sid = 104;

1 row deleted.

select*from student;

SID SNAME DPMT EMAIL CONTACTNO

101 Nisanth MCA [email protected] 9345292781

102 P.Sivakumar MCA [email protected] 8072363074

103 Sachin MCA [email protected] 8754681258


INTEGRITY CONSTRAINTS

1.Primary Key

2.Foreign Key

3.Unique Key

4.Check constraint

5.Not null constraint

6.Null constraint

Creating the Student Table with Integrity Constraints

create table student (sid int (10) primary key, sname varchar (20) not null, dpmt varchar
(5) not null, email varchar (25) not null unique, contactno numeric (10) not null, age int
check (age >= 18), enrollment_date date default current_date);

Inserting Values into the Student Table


insert into student (sid, sname, dpmt, email, contactno, age)
values
(101, 'Nisanth', 'MCA', '[email protected]', 9345292781, 20),
(102, 'Sivakumar', 'MCA', '[email protected]', 8072363074, 21),
(103, 'Sachin', 'MCA', '[email protected]', 8754681258, 22),
(104, 'Siva', 'MCA', '[email protected]', 6380603146, 19);

select*from student;

SID SNAME DPMT EMAIL CONTACTNO AGE EN_DA

101 Nisanth MCA [email protected] 9345292781 22 6-10-24

102 Sivakumar MCA [email protected] 8072363074 21 6-10-24

103 Sachin MCA [email protected] 8754681258 20 6-10-24

104 Siva MCA [email protected] 6380603146 19 6-10-24


Creating the Courses Table with Foreign Key

create table courses (course_id int primary key, course_name varchar(100) not null,
credits int check (credits > 0), sid int,foreign key (sid) references student(sid));

Inserting Values into the Courses Table

insert into Courses (course_id, course_name, credits, sid)values (201, 'Database


Systems', 3, 101),(202, 'Data Structures', 4, 102), (203, 'Operating Systems', 3, 103);

select*from courses;

COURSE_ID COURSE_NAME CREDITS SID

201 Database System 3 101

202 Data Structures 4 102

203 Operating System 3 103

COE (30)

RECORD(20)

VIVA (10)

TOTAL (60)

RESULT:

Thus, the execution of DDL, DML commands and integrity constraints for University Database
system has been done successfully.
Ex.no: 02

Date:

TCL COMMANDS AND DCL COMMANDS

AIM:

To perform transaction control language commands like savepoint, rollback, commit and data
control language commands like grant, revoke for University Database system.

TCL COMMANDS:

1. Savepoint

2. Rollback

3. Commit

Table creation:

create table student (

sid int(10),
sname varchar(20),
dpmt varchar(5),
email varchar(25),
contactno numeric(10)
);

Table created.
SQL> insert into student values
(101, 'Nisanth', 'MCA', '[email protected]', 9345292781),
(102, 'Sivakumar', 'MCA', '[email protected]', 8072363074),
(103, 'Sachin', 'MCA', '[email protected]', 8754681258),
(104, 'Siva', 'MCA', '[email protected]', 6380603146);
SQL> select * from student;

SID SNAME DPMT EMAIL CONTACTNO

101 Nisanth MCA [email protected] 9345292781

102 Sivakumar MCA [email protected] 8072363074

103 Sachin MCA [email protected] 8754681258

104 Siva MCA [email protected] 6380603146

Creating a Savepoint:

1.Savepoint

SQL>SAVEPOINT s1;

Savepoint created.

SQL>insert into student values (105, 'sujith', 'MCA', '[email protected]', 7305207628);

1 row created.

SQL>SAVEPOINT s2;

Savepoint created.

SQL> insert into student values (106, 'Vasanth', 'MCA', '[email protected]',


9994175646);

1 row created.

SQL>Select *from student;


SID SNAME DPMT EMAIL CONTACTNO

101 Nisanth MCA [email protected] 9345292781

102 Sivakumar MCA [email protected] 8072363074

103 Sachin MCA [email protected] 8754681258

104 Siva MCA [email protected] 6380603146

105 Sujith MCA sujith,[email protected] 7305207628

106 Vasanth MCA [email protected] 9994175646

2.Rollback

SQL> rollback to s2;

Rollback complete.

SQL>Select *from student;

SID SNAME DPMT EMAIL CONTACTNO

101 Nisanth MCA [email protected] 9345292781

102 Sivakumar MCA [email protected] 8072363074

103 Sachin MCA [email protected] 8754681258

104 Siva MCA [email protected] 6380603146

105 Sujith MCA sujith,[email protected] 7305207628

Notice that Vasanth has been removed.


3.Commit

COMMIT;

Commit complete.

SQL> rollback to s1;

ORA-01086: savepoint 'S1' never established in this session or is invalid.

DCL COMMANDS:

1. Grant

2. Revoke

1.Creating Users:
SQL>create user nisanth identified by password_nisanth;
User created.
SQL>create user sivakumar identified by password_sivakumar;
User created.

2. Granting Privileges to Users:


SQL> grant select on student to nisanth;
Grant succeeded.
SQL> grant select on student to sivakumar;
Grant succeeded.

Granting INSERT Privilege:

SQL> grant insert on student to nisanth;


Grant succeeded
SQL> grant insert on student to sivakumar;
Grant succeeded.
3.Nisanth Logs In and SELECTs Data:
SQL> connect
Enter user-name:nisanth
Enter password: …….
Connected.
Select*from student;

SID SNAME DPMT EMAIL CONTACTNO

101 Nisanth MCA [email protected] 9345292781

102 Sivakumar MCA [email protected] 8072363074

103 Sachin MCA [email protected] 8754681258

104 Siva MCA [email protected] 6380603146

105 Sujith MCA sujith,[email protected] 7305207628

106 Vasanth MCA [email protected] 9994175646

Sivakumar Logs In and INSERTs Data:

SQL> connect
Enter user-name:sivakumar
Enter password: …….
Connected.

SQL> insert into student values (107, 'Santhosh', 'MCA', '[email protected]',


8870996541);

1 row inserted.

SQL> select * from student;


SID SNAME DPMT EMAIL CONTACTNO

101 Nisanth MCA [email protected] 9345292781

102 Sivakumar MCA [email protected] 8072363074

103 Sachin MCA [email protected] 8754681258

104 Siva MCA [email protected] 6380603146

105 Sujith MCA sujith,[email protected] 7305207628

106 Vasanth MCA [email protected] 9994175646

107 Santhosh MCA [email protected] 8870996541

4. Revoking Privileges:

SQL> revoke insert on student from nisanth;

Revoke succeeded.

Trying to INSERT after REVOKE:

SQL> connect
Enter user-name: nisanth
Enter password: …….
Connected.

SQL> insert into student values (108, 'Aswin', 'MCA', '[email protected]',


6382126990);

ERROR: ORA-01031: insufficient privileges


5. DROP USER

SQL> drop user nisanth;


User dropped.
SQL> connect
Enter user-name: nisanth
Enter password:
ERROR:
ORA-01017: invalid username/password; login denied

COE (30)

RECORD (20)

VIVA (10)

TOTAL (60)

RESULT:

Thus, the execution of TCL, DCL commands for University Database system has been done
successfully.
Ex.no: 03

Date:

DATABASE OBJECTS

AIM:

To perform database objects such as view, sequences, synonyms and indexes for University
Database System.

DATABASE OBJECTS:

1. View

2. Sequences

3. Synonyms

4. Indexes

1.View

Table creation:

create table student (

sid int(10),
sname varchar(20),
dpmt varchar(5),
email varchar(25),
contactno numeric(10)
);
Table created.
SQL> insert into student values
(101, 'Nisanth', 'MCA', '[email protected]', 9345292781),
(102, 'Sivakumar', 'MCA', '[email protected]', 8072363074),
(103, 'Sachin', 'MCA', '[email protected]', 8754681258),
(104, 'Siva', 'MCA', '[email protected]', 6380603146);

SQL> select * from student;

SID SNAME DPMT EMAIL CONTACTNO

101 Nisanth MCA [email protected] 9345292781

102 Sivakumar MCA [email protected] 8072363074

103 Sachin MCA [email protected] 8754681258

104 Siva MCA [email protected] 6380603146

1)Creating Read-Only Views:

SQL> create view v1 as select sid, sname from student with read only;

View created.

SQL> select * from v1;


SID SNAME

101 Nisanth

102 Sivakumar

103 Sachin

104 Siva

SQL> insert into v1 values(105, 'Kavi');

ERROR at line 1: ORA-01733:

virtual column not allowed here


2. Creating Updateable Views:

SQL> create view v2 as select * from student;

View created.

SQL> insert into v2 values (105, 'Sujith', 'MCA', '[email protected]',


7305207628);

1 row created.

SQL> select * from v2;

SID SNAME DPMT EMAIL CONTACTNO

101 Nisanth MCA [email protected] 9345292781

102 Sivakumar MCA [email protected] 8072363074

103 Sachin MCA [email protected] 8754681258

104 Siva MCA [email protected] 6380603146

105 Sujith MCA [email protected] 7305207628

3. Working with Sequences:

SQL> create sequence seq1 increment by 1 start with 6;

Sequence created.

SQL> insert into student values (seq1.nextval, 'Vasanth', 'MCA',


'[email protected]', 9994175646);

1 row created.

SQL> select * from student;


SID SNAME DPMT EMAIL CONTACTNO

101 Nisanth MCA [email protected] 9345292781

102 Sivakumar MCA [email protected] 8072363074

103 Sachin MCA [email protected] 8754681258

104 Siva MCA [email protected] 6380603146

105 Sujith MCA sujith,[email protected] 7305207628

106 Vasanth MCA [email protected] 9994175646

4. Synonyms:

SQL> create synonym stud_syn for student;

Synonym created.

SQL> select * from stud_syn;

SID SNAME DPMT EMAIL CONTACTNO

101 Nisanth MCA [email protected] 9345292781

102 Sivakumar MCA [email protected] 8072363074

103 Sachin MCA [email protected] 8754681258

104 Siva MCA [email protected] 6380603146


5. Indexes:
SQL> create index idx_sid on student(sid);
Index created.
This creates an index named idx_sid on the sid column of the student table. Indexes help to
speed up the retrieval of rows by providing quick lookups on specific columns.

SQL> drop index idx_sid;


Index dropped.

This removes the index idx_sid from the student table, meaning the database will no longer
use this index for queries involving the sid column.

COE (30)

RECORD (20)

VIVA (10)

TOTAL (60)

RESULT:

Thus, the execution of database objects for University Database System has been done
successfully.
Ex.no: 04

Date:

SINGLE ROW FUNCTIONS, AGGREGATE FUNCTIONS

AND SET OPERATIONS

AIM:

To implement single row functions, aggregate functions and set operations for University
Database system.

SINGLE ROW FUNCTIONS:

1. General functions

2. Case conversion functions

3. Character functions

4. Date functions

5. Number functions

Table creation:

create table student (

sid int(10),
sname varchar(20),
dpmt varchar(5),
email varchar(25),
contactno numeric(10)
);
Table created.
SQL> insert into student values
(101, 'Nisanth', 'MCA', '[email protected]', 9345292781),
(102, 'Sivakumar', 'MCA', '[email protected]', 8072363074),
(103, 'Sachin', 'MCA', '[email protected]', 8754681258),
(104, 'Siva', 'MCA', '[email protected]', 6380603146);

SQL> select * from student;

SID SNAME DPMT EMAIL CONTACTNO

101 Nisanth MCA [email protected] 9345292781

102 Sivakumar MCA [email protected] 8072363074

103 Sachin MCA [email protected] 8754681258

104 Siva MCA [email protected] 6380603146

1. General Functions

a) NVL () - Replace NULL with a default value:

SQL> select sid, nvl (contactno, 'no contact') as contact from student;

SID CONTACTNO

101 9345292781

102 8072363074

103 8754681258

104 6380603146
Insert with NULL for Contact Number:
SQL> insert into student (sid, sname, dpmt, email, contactno) values (105, 'vasanth',
'mca', '[email protected]', null);
Query to Display Contact with NVL():
SQL> select sid, sname, nvl(contactno, 'no contact') as contact
from student;

SID SNAME CONTACTNO

101 Nisanth 9345292781

102 Sivakumar 8072363074

103 Sachin 8754681258

104 Siva 6380603146

105 Vasanth No contact

b) DECODE () - Conditional replacement:

SQL> select sid, sname, decode(dpmt, 'mca', 'master of computer applications',


'unknown') as department from student;

SID SNAME DPMT

101 Nisanth Master of ComputerApplication

102 Sivakumar Master of Computer Application

103 Sachin Master of Computer Application

104 Siva Master of Computer Application


2. Case Conversion Functions

a) UPPER () - Convert to uppercase:

SQL>select sid, upper(sname) as sname from student;

SID SNAME

101 NISANTH

102 SIVAKUMAR

103 SACHIN

104 SIVA

b) LOWER () - Convert to lowercase:

SQL> select sid, lower(dpmt) as dpmt from student;

SID DPMT

101 mca

102 mca

103 mca

104 mca

c) INITCAP () - Convert first letter of each word to uppercase:

SQL> select sid, initcap(dpmt) as dpmt from student;


SID DPMT

101 Mca

102 Mca

103 Mca

104 Mca

3. Character Functions
a) SUBSTR () - Extract substring:
SQL> select sid, substr(email, 1, 10) as email from student;

SID EMAIL

101 nisanthg.2

102 sivakumar

103 sachins.24

104 sivak.24mc

b) LENGTH() - Get the length of a string:


SQL>select sid, length(sname) as sname from student;

SID SNAME

101 7

102 9

103 6

104 4
4. Date Functions

1. SYSDATE: Returns the current date and time.

SQL>select sysdate as current_date from dual;

CURRENT_DATE
-------------------------
2024-10-06 12:34:56

2. TO_DATE: Converts a string to a date.

SQL>select to_date('2024-10-06', 'yyyy-mm-dd') as converted_date from dual;

CONVERTED_DATE
-------------------
2024-10-06 00:00:00

3. TO_CHAR: Converts a date to a string in a specified format

SQL> select to_char(sysdate, 'dd-mon-yyyy') as formatted_date from dual;

FORMATTED_DATE
-------------------
06-OCT-20254

5. Number Functions

1. MOD: Returns the remainder of a division operation.

SQL>SELECT MOD(10, 3) as modulus_value FROM dual;

MODULUS_VALUE
--------------
1
2. POWER: Returns a number raised to the power of another number.

SQL>SELECT POWER(2, 3) AS power_value FROM dual;

POWER_VALUE

------------

3. SQRT: Returns the square root of a number.

SQL>SELECT SQRT(16) AS square_root FROM dual;

SQUARE_ROOT

------------

4. ROUND: Rounds a number to a specified number of decimal places.

SQL>SELECT ROUND(123.4567, 2) AS rounded_value FROM dual;

ROUNDED_VALUE

--------------

123.46

AGGREGATE FUNCTIONS:

1. Average

2. Count

3. Max

4. Min

5. Sum

Table creation:

create table student (

sid int(10),
sname varchar(20),
dpmt varchar(5),
email varchar(25),
contactno numeric(10)
);
Table created.
SQL> insert into student values
(101, 'Nisanth', 'MCA', '[email protected]', 9345292781),
(102, 'Sivakumar', 'MCA', '[email protected]', 8072363074),
(103, 'Sachin', 'MCA', '[email protected]', 8754681258),
(104, 'Siva', 'MCA', '[email protected]', 6380603146);

SQL> select * from student;

SID SNAME DPMT EMAIL CONTACTNO

101 Nisanth MCA [email protected] 9345292781

102 Sivakumar MCA [email protected] 8072363074

103 Sachin MCA [email protected] 8754681258

104 Siva MCA [email protected] 6380603146

1. Minimum (MIN): Find the minimum contactno.

SQL> SELECT MIN (contactno) FROM student;

MIN(CONTACTNO)
--------------
6380603146

2. Maximum (MAX): Find the maximum contactno.

SQL> SELECT MAX (contactno) FROM student;


MAX(CONTACTNO)
---------------
9345292781

3. Count (COUNT): Count the total number of students.

SQL> SELECT COUNT (sid) FROM student;

COUNT(SID)
-----------
4

4. Sum (SUM): Calculate the total sum of SID.

SQL> SELECT SUM (sid) FROM student;

SUM(SID)
--------
410

5. Average (AVG): Calculate the average of SID.

SQL> SELECT AVG (sid) FROM student;

AVG(SID)
---------
102.5

SET OPERATIONS

1. Union
2. Union all
3. Intersect
4. Minus
Table creation:

create table student (

sid int(10),
sname varchar(20),
dpmt varchar(5),
email varchar(25),
contactno numeric(10)
);
Table created.
SQL> insert into student values
(101, 'Nisanth', 'MCA', '[email protected]', 9345292781),
(102, 'Sivakumar', 'MCA', '[email protected]', 8072363074),
(103, 'Sachin', 'MCA', '[email protected]', 8754681258),
(104, 'Siva', 'MCA', '[email protected]', 6380603146);

SQL> select * from student;

SID SNAME DPMT EMAIL CONTACTNO

101 Nisanth MCA [email protected] 9345292781

102 Sivakumar MCA [email protected] 8072363074

103 Sachin MCA [email protected] 8754681258

104 Siva MCA [email protected] 6380603146

create table course (


course_id int(10) primary key,
course_name varchar(50) not null,
department varchar(30),
credits int(3) );
Table created.
insert into course values
(1, 'DBMS', 'CS', 3),
(2, 'ML', 'CS', 4),
(3, 'DM', 'Business', 3),
(4, 'DSA', 'CS', 4),
(5, 'ML', 'CS', 4),
(6, 'DSA', 'CS', 4),

COURSE_ID COURSE_NAME DEPARTMENT CREDITS

1 DBMS CS 3
2 ML CS 4
3 DM BUSINESS 3
4 DSA CS 4
5 ML CS 4
6 DSA CS 4

1. UNION

SQL>select sname as name from student


union
select course_name as name from course;

NAME

Nisanth

Sivakumar

Sachin

Siva
DBMS

ML

DM

DSA

2. UNION ALL

SQL>select sname as name from student


union all
select course_name as name from course;

NAME

Nisanth

Sivakumar

Sachin

Siva

DBMS

ML

DM

DSA

ML

DSA

3. INTERSECT

SQL> select sname as name from student


intersect
select course_name as name from course;
NAME
(empty, as there are no common names)

4. MINUS

SQL>select sname as name from student


minus
select course_name as name from course;

NAME
Nisanth
Sivakumar
Sachin
Siva

COE (30)

RECORD (20)

VIVA (10)

TOTAL (60)

RESULT:

Thus, the execution of single row functions, aggregate functions and set operations for
University Database system has been done successfully.
Ex.no: 05

Date:

JOIN OPERATIONS AND SUBQUERIES

AIM:

To perform join operation and subqueries for university database system

Types of Joins :

1. Inner Join
2. Left Join (Or Left Outer Join)
3. Right Join (Or Right Outer Join)
4. Full Outer Join
5. Cross Join
6. Self-Join

Table creation:

create table student (

sid int(10),
sname varchar(20),
dpmt varchar(5),
email varchar(25),
contactno numeric(10)
);

Table created.
SQL> insert into student values
(101, 'Nisanth', 'AM', '[email protected]', 9345292781),
(102, 'Sivakumar', 'DBT', '[email protected]', 8072363074),
(103, 'Sachin', 'C', '[email protected]', 8754681258),
(104, 'Siva', 'IOT', '[email protected]', 6380603146);

SQL> select * from student;

SID SNAME DPMT EMAIL CONTACTNO

101 Nisanth AM [email protected] 9345292781

102 Sivakumar DBT [email protected] 8072363074

103 Sachin C [email protected] 8754681258

104 Siva IOT [email protected] 6380603146

CREATE TABLE department (


did int(10),
dname varchar(30),
head varchar(30)
);
Table created

INSERT INTO department VALUES


(101, 'AM', 'Dr. Smith'),
(102, 'DBT', 'Dr. Johnson'),
(103, 'DS', 'Dr. Lee'),
(104, 'C', 'Dr. Kim'),
(105, 'IoT', 'Dr. Patel'),
(106, 'Blockchain', 'Dr. Brown');

SQL> select * from department;

DID DNAME HEAD


101 AM Dr. Smith
102 DBT Dr. Johnson
103 DS Dr. Lee
104 C Dr. Kim
105 IOT Dr. Patel
106 BLOCKCHAIN Dr. Brown
1. Inner Join

An inner join returns only the rows where there is a match in both tables based on the dpmt
field in student and dname field in department.

SQL>SELECT student.sid, student.sname, student.dpmt, student.email, student.contactno,


department.did, department.dname, department.head
FROM student
INNER JOIN department ON student.dpmt = department.dname;

SID SNAME DPMT EMAIL CONTACTNO DID DNAME HEAD

101 Nisanth AM [email protected] 9345292781 101 AM Dr.


Smith

102 Sivakumar DBT [email protected] 8072363074 102 DBT Dr.


Johnson

103 Sachin C [email protected] 8754681258 104 C Dr.


Kim

104 Siva IOT [email protected] 6380603146 105 IoT Dr.


Patel

2.Left Join (Left Outer Join)

A left join returns all rows from the student table and matched rows from the department
table. If there is no match, NULL values are returned.

SQL>SELECT student.sid, student.sname, student.dpmt, student.email, student.contactno,


department.did, department.dname, department.head
FROM student
LEFT JOIN department ON student.dpmt = department.dname;
SID SNAME DPMT EMAIL CONTACTNO DID DNAME HEAD

101 Nisanth AM [email protected] 9345292781 101 AM Dr.


Smith

102 Sivakumar DBT [email protected] 8072363074 102 DBT Dr.


Johnson

103 Sachin C [email protected] 8754681258 104 C Dr.


Kim

104 Siva IOT [email protected] 6380603146 105 IoT Dr.


Patel

3. Right Join (Right Outer Join)


A right join returns all rows from the department table and matched rows from the student
table. If there is no match, NULL values are returned.

SQL>SELECT student.sid, student.sname, student.dpmt, student.email, student.contactno,


department.did, department.dname, department.head
FROM student
RIGHT JOIN department ON student.dpmt = department.dname;

SID SNAME DPMT EMAIL CONTACTNO DID DNAME HEAD

101 Nisanth AM [email protected] 9345292781 101 AM Dr.


Smith

102 Sivakumar DBT [email protected] 8072363074 102 DBT Dr.


Johnson

103 Sachin C [email protected] 8754681258 104 C Dr.


Kim

104 Siva IOT [email protected] 6380603146 105 IoT Dr.


Patel
NULL NULL NULL NULL NULL 103 DS Dr. Lee

NULL NULL NULL NULL NULL 106 Blockchain Dr.


Brown

4. Full Outer Join


A full outer join returns all rows where there is a match in either table, or NULL where no
match exists in either table.
SQL>department.did, department.dname, department.head
FROM student
FULL OUTER JOIN department ON student.dpmt = department.dname;

SID SNAME DPMT EMAIL CONTACTNO DID DNAME HEAD

101 Nisanth AM [email protected] 9345292781 101 AM Dr.


Smith

102 Sivakumar DBT [email protected] 8072363074 102 DBT Dr.


Johnson

103 Sachin C [email protected] 8754681258 104 C Dr.


Kim

104 Siva IOT [email protected] 6380603146 105 IoT Dr.


Patel

NULL NULL NULL NULL NULL 103 DS Dr. Lee

NULL NULL NULL NULL NULL 106 Blockchain Dr.


Brown
5. Cross Join
A cross join returns the Cartesian product of the two tables, combining each row of the
student table with each row of the department table.

SQL> SELECT s.sname, d.dname


FROM student s
CROSS JOIN department d;
SNAME DNAME
Nisanth AM
Nisanth DBT
Nisanth DS
Nisanth C
Nisanth IoT
Nisanth Blockchain
Sivakumar AM
Sivakumar DBT
Sivakumar DS
Sivakumar C
Sivakumar IoT
Sivakumar Blockchain
Sachin AM
Sachin DBT
Sachin DS
Sachin C
Sachin IoT
Sachin Blockchain
Siva AM
Siva DBT
Siva DS
Siva C
Siva IoT
Siva Blockchain
6. Self Join
A self-join combines rows within the same table

1. Self-Join on the student Table to Find Students in the Same Department

SQL>SELECT student.sid AS student1_id, student.sname AS student1_name,

student2.sid AS student2_id, student2.sname AS student2_name,

student.dpmt

FROM student

JOIN student AS student2 ON student.dpmt = student2.dpmt AND student.sid < student2.sid;

Expected Output:

Based on the current data, no two students share the same department. Therefore, the result of

this query would be an empty set.

2. Self-Join on the department Table to Find Departments with the Same Head

SQL>SELECT department.did AS department1_id, department.dname AS

department1_name, department2.did AS department2_id, department2.dname AS

department2_name, department.head

FROM department

JOIN department AS department2 ON department.head = department2.head AND

department.did < department2.did;

Expected Output:

Each department has a unique head in the current data, so this query would also return an

empty set.

GROUP BY, ORDER BY, and HAVING clauses

1. Count Students in Each Department

SQL>SELECT dpmt, COUNT(*) AS student_count

FROM student

GROUP BY dpmt
ORDER BY dpmt;

Explanation: This query counts the number of students in each department and orders the

results alphabetically by the department code (dpmt).

Expected Output:

DPMT STUDENT_COUNT

AM 1

C 1

DBT 1

IOT 1

2.List All Departments with Their Heads

SQL>SELECT dname, head

FROM department

ORDER BY dname;

Explanation: This retrieves all departments along with their heads and orders the results

alphabetically by department name (dname).

Expected Output:

DNAME HEAD

AM Dr. Smith

Blockchain Dr. Brown

C Dr. Kim

DBT Dr. Johnson

DS Dr. Lee

IoT Dr. Patel


3.Finding Departments with HAVING Clause

SQL>SELECT dname, head

FROM department

HAVING head LIKE 'Dr.%';

Explanation: This retrieves all departments where the head's name starts with "Dr.".

Expected Output:

DNAME HEAD

AM Dr. Smith

DBT Dr. Johnson

DS Dr. Lee

C Dr. Kim

IoT Dr. Patel

Blockchain Dr. Brown

COE (30)

RECORD (20)

VIVA (10)

TOTAL (60)

RESULT:

The execution of join operations, subqueries, and the use of aggregate functions has
been performed successfully on the university database system
Ex No: 06

Date:

PL/SQL

AIM:

To create and execute PL/SQL functions for fo University Database Management System.

QUERIES

1.Write a PL/SQL code block to declare a basic variable, assign it a value, and then

display that value using `DBMS_OUTPUT.PUT_LINE`.

SQL> set serveroutput on;

SQL> declare

2 var varchar2(50);

3 begin

4 var:='hello world';

5 dbms_output.put_line(var);

6 end;

7 /

O/P

Database Technologies

PL/SQL procedure successfully completed.

2.Write a PL/SQL code block that takes two numbers as input from the user, calculates

their sum, and displays the result. Use DBMS_OUTPUT.PUT_LINE to print the output

in the following format:

The sum of <a> and <b> is <sum>


DECLARE

a NUMBER := &a;

b NUMBER := &b;

sum NUMBER;

BEGIN

sum := a + b;

dbms_output.put_line('The sum of ' || a || ' and ' || b || ' is ' || sum);

END;

O/P

The sum of 5 and 10 is 15

PL/SQL procedure successfully completed.

3.Write a SQL script to create a table named student with the following columns: sid (a

number for student ID), sname (a string for student name), and sdept (a string for

student department). Then, insert the following records into the table:

1. Student ID: 5, Name: Nisanth, Department: MCA

2. Student ID: 6, Name: Sivakumar, Department: MCA

3. Student ID: 7, Name: Sachin, Department: MCA

4. Student ID: 8, Name: Siva, Department: MCA

After inserting the records, display the contents of the student table.
CREATE TABLE student (

sid NUMBER(5),

sname VARCHAR2(10),

sdept VARCHAR2(5)

);

INSERT INTO student VALUES (5, 'Nisanth', 'MCA');

INSERT INTO student VALUES (6, 'Sivakumar', 'MCA');

INSERT INTO student VALUES (7, 'Sachin', 'MCA');

INSERT INTO student VALUES (8, 'Siva', 'MCA');

O/P

SELECT * FROM student;

SID SNAME SDEPT

5 Nisanth MCA

6 Sivakumar MCA

7 Sachin MCA

8 Siva MCA

4. Write a PL/SQL block that retrieves and displays the name and department of a student

from the student table based on a specified student ID. Use the following details:

• Specify a student ID to search for (e.g., 1).

• Retrieve the student's name (sname) and department (sdept).

• Use DBMS_OUTPUT.PUT_LINE to print the output in the following format:

Name: <name>, Department: <department>


DECLARE

id NUMBER := 5; -- specify student ID to search

name VARCHAR2(10);

dept VARCHAR2(5);

BEGIN

SELECT sname, sdept INTO name, dept FROM student WHERE sid = id;

dbms_output.put_line('Name: ' || name || ', Department: ' || dept);

END;

O/P

Name: Alice, Department: CS

PL/SQL procedure successfully completed.

5.Write a PL/SQL block that takes a number as input from the user and determines

whether the number is even or odd. Use a substitution variable (&num) to prompt the

user for the input. The block should use the MOD function to check if the number is

even or odd and display the result using DBMS_OUTPUT.PUT_LINE in the following

format: <num> is even

DECLARE

num NUMBER := &num;

BEGIN

IF MOD(num, 2) = 0 THEN

dbms_output.put_line(num || ' is even');

ELSE
dbms_output.put_line(num || ' is odd');

END IF;

END;

input num = 7:

O/P

7 is odd

PL/SQL procedure successfully completed.

6. Write a PL/SQL block that takes two numbers as input from the user and determines

which number is greater. Use substitution variables (&a and &b) to prompt the user for

the two input values. The block should compare the numbers using an IF statement and

display the result using DBMS_OUTPUT.PUT_LINE in the following format:

<greater_number> is greater

DECLARE

a NUMBER := &a;

b NUMBER := &b;

BEGIN

IF a > b THEN

dbms_output.put_line(a || ' is greater');

ELSE

dbms_output.put_line(b || ' is greater');

END IF;

END;

inputs a = 15 and b = 10
O/P

15 is greater

PL/SQL procedure successfully completed.

7. Write a PL/SQL block that checks whether a given number is divisible by 3. Use a

substitution variable (&a) to prompt the user for the input number. The block should

perform the following tasks:

1. Take a number as input from the user.

2. Use the MOD function to determine if the number is divisible by 3.

3. Display a message using DBMS_OUTPUT.PUT_LINE indicating whether the

number is divisible by 3 or not. The output should be in the following format:

<number> is divisible by 3

DECLARE

a NUMBER := &a; -- Take input from the user

BEGIN

IF MOD(a, 3) = 0 THEN

dbms_output.put_line(a || ' is divisible by 3');

ELSE

dbms_output.put_line(a || ' is not divisible by 3');

END IF;

END;

O/p

If the user inputs 9, the output will be: 9 is divisible by 3

If the user inputs 10, the output will be: 10 is not divisible by 3
8. Write a PL/SQL block that uses a loop to print the numbers from 1 to 10. The block

should perform the following tasks:

1. Initialize a variable i with a value of 1.

2. Use a LOOP to repeatedly execute the block of code.

3. Within the loop, use DBMS_OUTPUT.PUT_LINE to display the current value

of i.

4. Increment the value of i by 1 in each iteration.

5. Include an EXIT condition to terminate the loop when i exceeds 10.

DECLARE

i NUMBER := 1;

BEGIN

LOOP

EXIT WHEN i > 10;

dbms_output.put_line(i);

i := i + 1;

END LOOP;

END;

O/P

5
6

10

PL/SQL procedure successfully completed.

9. Write a PL/SQL block to generate and display the Fibonacci series up to a user-

specified number of terms. Use a substitution variable (`&n`) to take the input for the

number of terms and print each Fibonacci number

using`DBMS_OUTPUT.PUT_LINE`.

SET SERVEROUTPUT ON;

DECLARE

n NUMBER;

a NUMBER := 0;

b NUMBER := 1;

temp NUMBER;

BEGIN

n := &n;

dbms_output.put_line('Fibonacci Series:');

FOR i IN 1..n LOOP

dbms_output.put_line(a);

temp := a + b;

a := b;

b := temp;
END LOOP;

END;

O/P

If the user enters 7, the output will be:

Fibonacci Series:

10.What does the following PL/SQL code produce, and how does it create a pattern

based on user input? Describe the roles of the variables n, i, and j in the nested loops.

DECLARE

n NUMBER := &n; -- Take input from the user for the number of rows

i NUMBER;

j NUMBER;

BEGIN

FOR i IN 1..n LOOP -- Loop through rows

FOR j IN 1..i LOOP -- Loop through columns

dbms_output.put(j); -- Print the current row number

END LOOP;

dbms_output.new_line; -- Move to the next line after each row


END LOOP;

END;

O/P

22

333

4444

55555

COE (30)

RECORD (20)

VIVA (10)

TOTAL (60)

RESULT:

Thus, the execution of basic PL/SQL queries for University Database Management System.

has been done successfully.


Ex No: 07

Date:

FUNCTIONS AND PROCEDURES

AIM:

To create and execute PL/SQL functions and procedures to manage the payroll process,
including calculating gross salaries, displaying payroll details, incrementing salaries, and
generating payroll reports.

Table creation:

1.CREATE TABLE employee (

emp_id NUMBER(5) PRIMARY KEY,


emp_name VARCHAR2(50),
emp_designation VARCHAR2(50),
email VARCHAR2(100),
contactno VARCHAR2(15),
basic_salary NUMBER(10, 2), --
hra NUMBER(10, 2),
allowances NUMBER(10, 2)
);
Table created.

2.Insert Values for above created table

INSERT INTO employee VALUES (101, 'Nisanth', 'AM', '[email protected]',


9345292781, 40000.00, 10000, 5000);
INSERT INTO employee VALUES (102, 'Sivakumar', 'DBT',
'[email protected]', 8072363074, 45000.00, 12000, 7000);
INSERT INTO employee VALUES (103, 'Sachin', 'C', '[email protected]',
8754681258, 38000.00, 9000, 4000);
INSERT INTO employee VALUES (104, 'Siva', 'IOT', '[email protected]',
6380603146, 42000.00, 11000, 6000);
COMMIT;

3.select * from employee;

EMP_ID EMP_NAME EMP_DES EMAIL CONTACTNO

101 Nisanth AM [email protected] 9345292781

102 Sivakumar DBT [email protected] 8072363074

103 Sachin C [email protected] 8754681258

104 Siva IOT [email protected] 6380603146

BASIC_SALARY HRA ALLOWANCES


40000.00 10000 5000
45000.00 12000 7000
38000.00 9000 4000
42000.00 11000 6000

4. Stored Function for Calculating Gross Salary


Gross Salary = Basic Salary + HRA + Allowances

CREATE OR REPLACE FUNCTION calculate_gross_salary(emp_id IN NUMBER)


RETURN NUMBER
IS
gross_salary NUMBER;
BEGIN
SELECT basic_salary + hra + allowances
INTO gross_salary
FROM employee
WHERE emp_id = emp_id;

RETURN gross_salary;
END;
Test the Function

SELECT calculate_gross_salary(101) AS gross_salary FROM DUAL;

O/P
GROSS_SALARY

55000.00

5. Stored Procedure for Displaying Employee Payroll


This procedure fetches and displays the payroll details for an employee based on their ID

CREATE OR REPLACE PROCEDURE display_payroll(emp_id IN NUMBER)


IS
emp_name VARCHAR2(50);
gross_salary NUMBER;
BEGIN
-- Calculate Gross Salary
gross_salary := calculate_gross_salary(emp_id);
-- Fetch Employee Name
SELECT emp_name INTO emp_name FROM employee WHERE emp_id = emp_id;
-- Display Payroll Details
DBMS_OUTPUT.PUT_LINE('Payroll Details:');
DBMS_OUTPUT.PUT_LINE('Employee Name: ' || emp_name);
DBMS_OUTPUT.PUT_LINE('Gross Salary: ' || gross_salary);
END;
Test the Procedure
BEGIN
display_payroll(101);
END;
O/P

Payroll Details:
Employee Name: Nisanth
Gross Salary: 55000

6. Stored Procedure for Incrementing Salaries

This procedure increments the basic salary of employees based on their designation.

CREATE OR REPLACE PROCEDURE increment_salary(designation IN VARCHAR2,


increment_amount IN NUMBER)
IS
BEGIN
UPDATE employee
SET basic_salary = basic_salary + increment_amount
WHERE emp_designation = designation;

DBMS_OUTPUT.PUT_LINE('Salary incremented for designation: ' || designation);


END;

Test the Procedure

BEGIN
increment_salary('AM', 5000); -- Increment for Assistant Manager
END;

Verify the Updated Salary:

SELECT emp_id, emp_name, basic_salary FROM employee WHERE emp_designation =


'AM';

O/P

EMP_ID EMP_NAME EMP_SALARY


101 Nisanth 45000.00

7. Generate a Payroll Report

This report displays all employees’ gross salaries.


SELECT emp_id, emp_name, calculate_gross_salary(emp_id) AS gross_salary
FROM employee;

O/P

EMP_ID EMP_NAME GROSS_SALARY


101 Nisanth 60000.00
102 Sivakumar 64000.00
103 Sachin 51000.00
104 Siva 59000.00

COE (30)

RECORD (20)

VIVA (10)

TOTAL (60)

RESULT:

PL/SQL functions and procedures were effectively implemented to manage the payroll process.
Ex No: 08

Date:

PL/SQL: CURSOR OPERATIONS

AIM:

To perform implicit and explicit cursor operations on an employee table to iterate through and
display employee details.

1.Table Creation

CREATE TABLE employee (

emp_id NUMBER(5) PRIMARY KEY,

emp_name VARCHAR2(50),

emp_designation VARCHAR2(50),

email VARCHAR2(100),

contactno VARCHAR2(15),

basic_salary NUMBER(10, 2),

hra NUMBER(10, 2),

allowances NUMBER(10, 2)

);

Table created
2.Insert Values

INSERT INTO employee VALUES (101, 'Nisanth', 'AM', '[email protected]',


'9345292781', 40000.00, 10000, 5000);

INSERT INTO employee VALUES (102, 'Sivakumar', 'DBT',


'[email protected]', '8072363074', 45000.00, 12000, 7000);

INSERT INTO employee VALUES (103, 'Sachin', 'C', '[email protected]',


'8754681258', 38000.00, 9000, 4000);

INSERT INTO employee VALUES (104, 'Siva', 'IOT', '[email protected]',


'6380603146', 42000.00, 11000, 6000);

COMMIT;

3.select * from employee;

EMP_ID EMP_NAME EMP_DES EMAIL CONTACTNO

101 Nisanth AM [email protected] 9345292781

102 Sivakumar DBT [email protected] 8072363074

103 Sachin C [email protected] 8754681258

104 Siva IOT [email protected] 6380603146

BASIC_SALARY HRA ALLOWANCES


40000.00 10000 5000
45000.00 12000 7000
38000.00 9000 4000
42000.00 11000 6000
4. Implicit Cursor

To calculate and display the total number of employees in the table.

DECLARE
total_employees NUMBER;
BEGIN
-- Implicit Cursor Usage
SELECT COUNT(*) INTO total_employees FROM employee;
DBMS_OUTPUT.PUT_LINE('Total Number of Employees: ' || total_employees);
END;

O/P

Total Number of Employees: 4

5. Explicit Cursor

To iterate through all employees and display their details.

DECLARE
-- Declare a cursor
CURSOR employee_cursor IS
SELECT emp_id, emp_name, emp_designation, basic_salary FROM employee;

-- Variables to hold fetched data


v_emp_id employee.emp_id%TYPE;
v_emp_name employee.emp_name%TYPE;
v_emp_designation employee.emp_designation%TYPE;
v_basic_salary employee.basic_salary%TYPE;
BEGIN
-- Open the cursor
OPEN employee_cursor;
DBMS_OUTPUT.PUT_LINE('Employee Details:');
DBMS_OUTPUT.PUT_LINE('----------------------------');
-- Fetch each row
LOOP
FETCH employee_cursor INTO v_emp_id, v_emp_name, v_emp_designation,
v_basic_salary
-- Exit loop when no more rows
EXIT WHEN employee_cursor%NOTFOUND;
-- Display employee details
DBMS_OUTPUT.PUT_LINE('Emp_ID: ' || v_emp_id || ', Name: ' || v_emp_name ||
', Designation: ' || v_emp_designation ||
', Basic Salary: ' || v_basic_salary);
END LOOP;

-- Close the cursor


CLOSE employee_cursor;
END;

O/P

Employee Details:
----------------------------
Emp_ID: 101, Name: Nisanth, Designation: AM, Basic Salary: 40000
Emp_ID: 102, Name: Sivakumar, Designation: DBT, Basic Salary: 45000
Emp_ID: 103, Name: Sachin, Designation: C, Basic Salary: 38000
Emp_ID: 104, Name: Siva, Designation: IOT, Basic Salary: 42000

6. Explicit Cursor with Parameters

To filter and display employees based on their designation.


DECLARE
-- Declare a parameterized cursor
CURSORemployee_designation_cursor(p_designation
employee.emp_designation%TYPE) IS
SELECT emp_id, emp_name, basic_salary FROM employee WHERE emp_designation
= p_designation;

-- Variables to hold fetched data


v_emp_id employee.emp_id%TYPE;
v_emp_name employee.emp_name%TYPE;
v_basic_salary employee.basic_salary%TYPE;
BEGIN
-- Open the cursor for a specific designation
OPEN employee_designation_cursor('AM');

DBMS_OUTPUT.PUT_LINE('Employees with Designation: AM');


DBMS_OUTPUT.PUT_LINE('--------------------------------');

-- Fetch each row


LOOP
FETCH employee_designation_cursor INTO v_emp_id, v_emp_name, v_basic_salary;

-- Exit loop when no more rows


EXIT WHEN employee_designation_cursor%NOTFOUND;

-- Display employee details


DBMS_OUTPUT.PUT_LINE('Emp_ID: ' || v_emp_id || ', Name: ' || v_emp_name ||
', Basic Salary: ' || v_basic_salary);
END LOOP;

-- Close the cursor


CLOSE employee_designation_cursor;
END;
O/P
Employees with Designation: AM
--------------------------------
Emp_ID: 101, Name: Nisanth, Basic Salary: 40000

COE (30)

RECORD (20)

VIVA (10)

TOTAL (60)

Result

The PL/SQL programs to perform implicit and explicit cursor operations on the employee table
were executed successfully, and the employee details were displayed as expected.
Ex. No.: 09
Date:
TRIGGERS AND PACKAGES
AIM:
To implement PL/SQL triggers and packages for the university database system
Table Structure (student):
CREATE TABLE student (
sid INT(10),
sname VARCHAR(20),
dpmt VARCHAR(5),
email VARCHAR(25),
contactno NUMERIC(10)
);
Inserting Records:
INSERT INTO student VALUES
(101, 'Nisanth', 'AM', '[email protected]', 9345292781),
(102, 'Sivakumar', 'DBT', '[email protected]', 8072363074),
(103, 'Sachin', 'C', '[email protected]', 8754681258),
(104, 'Siva', 'IOT', '[email protected]', 6380603146);
Select *from student;

SID SNAME DPMT EMAIL CONTACTNO


101 Nisanth AM [email protected] 9345292781
102 Sivakumar DBT [email protected] 8072363074
103 Sachin C [email protected] 8754681258
104 Siva IOT [email protected] 6380603146

1.Trigger t1: After Insert Trigger


This trigger prints a message after inserting a new record into the student table.
CREATE OR REPLACE TRIGGER t1
AFTER INSERT ON student
BEGIN
DBMS_OUTPUT.PUT_LINE('Data inserted into student table.');
END;
/
O/P
Insert Records:
INSERT INTO student VALUES (105, 'Anand', 'AI', '[email protected]',
9876543210);
Select*from student;

SID SNAME DPMT EMAIL CONTACTNO


101 Nisanth AM [email protected] 9345292781
102 Sivakumar DBT [email protected] 8072363074
103 Sachin C [email protected] 8754681258
104 Siva IOT [email protected] 6380603146
105 Anand AI [email protected] 9876543210

2.Trigger t2: After Insert or Update of sname


This trigger displays the old and new sname values after an update.
CREATE OR REPLACE TRIGGER t2
AFTER INSERT OR UPDATE OF sname ON student
FOR EACH ROW
BEGIN
IF INSERTING THEN
DBMS_OUTPUT.PUT_LINE('New student inserted: ' || :NEW.sname);
ELSE
DBMS_OUTPUT.PUT_LINE('Student name updated');
DBMS_OUTPUT.PUT_LINE('New Name: ' || :NEW.sname || ' | Old Name: ' ||
:OLD.sname);
END IF;
END;
/
O/P
Update Records:
SQL> UPDATE student SET sname = 'Nisanth G' WHERE sid = 101;
Student name updated
New Name: Nisanth G | Old Name: Nisanth
1 row updated.
SQL> UPDATE student SET sname = 'Sivakumar P' WHERE sid = 102;
Student name updated
New Name: Sivakumar P | Old Name: Sivakumar
1 row updated.

SID SNAME DPMT EMAIL CONTACTNO


101 Nisanth G AM [email protected] 9345292781
102 Sivakumar DBT [email protected] 8072363074
P
103 Sachin C [email protected] 8754681258
104 Siva IOT [email protected] 6380603146
105 Anand AI [email protected] 9876543210

3.Trigger t3: Before Insert Trigger (Raise Error)


This trigger raises an error before inserting a new record.
CREATE OR REPLACE TRIGGER t3
BEFORE INSERT ON student
BEGIN
RAISE_APPLICATION_ERROR(-20001, 'Insert not allowed in student table.');
END;
O/P
Insert Record (Error):
SQL> INSERT INTO student VALUES (106, 'Ravi', 'AI', '[email protected]', 9876543210);
ERROR at line 1:
ORA-20001: Insert not allowed in student table.
ORA-06512: at "SYSTEM.T3", line 2
ORA-04088: error during execution of trigger 'SYSTEM.T3'
4. Trigger news: Before Update Trigger
This trigger stores the old values in a backup table stu_new before updating the student table.
Backup Table (stu_new):
CREATE TABLE stu_new (
sid INT(10),
sname VARCHAR(20),
dpmt VARCHAR(5),
email VARCHAR(25),
contactno NUMERIC(10)
);
Trigger Code:
CREATE OR REPLACE TRIGGER news
BEFORE UPDATE OF sname
ON student
FOR EACH ROW
BEGIN
INSERT INTO stu_new (sid, sname, dpmt, email, contactno)
VALUES (:OLD.sid, :OLD.sname, :OLD.dpmt, :OLD.email, :OLD.contactno);
END;
/
O/P
Update Records:
SQL> UPDATE student SET sname = 'Siva Kumar' WHERE sid = 104;
1 row updated.

SID SNAME DPMT EMAIL CONTACTNO


101 Nisanth AM [email protected] 9345292781
102 Sivakumar DBT [email protected] 8072363074
103 Sachin C [email protected] 8754681258
104 SivaKumar IOT [email protected] 6380603146
105 Anand AI [email protected] 9876543210
5.Trigger t4: Before Delete Trigger
This trigger prevents deletion of records from the student table.
CREATE OR REPLACE TRIGGER t4
BEFORE DELETE ON student
BEGIN
RAISE_APPLICATION_ERROR(-20002, 'Deletion not allowed in student table.');
END;
/
O/P
Delete Record (Error):
SQL> DELETE FROM student WHERE sid = 103;
ERROR at line 1:
ORA-20002: Deletion not allowed in student table.
ORA-06512: at "SYSTEM.T4", line 2
ORA-04088: error during execution of trigger 'SYSTEM.T4'
Dropping a Trigger:
DROP TRIGGER t4;

PACKAGES

1. Package Specification (p1):


The package contains a procedure find_contact to display the contact number of a student by
sid.
SQL> CREATE OR REPLACE PACKAGE p1 AS
PROCEDURE find_s(id student.sid%TYPE);
END p1;
/O/P
Package created.

2. Package Body:
SQL> CREATE OR REPLACE PACKAGE BODY p1 AS
PROCEDURE find_s(id student.sid%TYPE) IS
stu_name student.sname%TYPE;
dept student.dpmt%TYPE;
contact student.contactno%TYPE;
BEGIN
SELECT sname, dpmt, contactno
INTO stu_name, dept, contact
FROM student
WHERE sid = id;

DBMS_OUTPUT.PUT_LINE('Student Name: ' || stu_name);


DBMS_OUTPUT.PUT_LINE('Department: ' || dept);
DBMS_OUTPUT.PUT_LINE('Contact No: ' || contact);
END find_s;
END p1;
O/P
Package body created.
3. Executing the Package:
Retrieve Details for Student with SID = 101
SQL> DECLARE
code student.sid%TYPE := 101;
BEGIN
p1.find_s(code);
END;
O/P
Student Name: Nisanth
Department: AM
Contact No: 9345292781
PL/SQL procedure successfully completed.

COE (30)

RECORD (20)

VIVA (10)

TOTAL (60)

Result:
Thus, the execution of PL/SQL triggers and packages for the university database system has
been successfully implemented and verified with appropriate outputs.
Ex. No.: 10
Date:
EXCEPTION HANDLING

AIM:
To implement PL/SQL programs for performing exception handling in a university database
system.
Table Structure (student):
CREATE TABLE student (
sid INT(10),
sname VARCHAR(20),
dpmt VARCHAR(5),
email VARCHAR(25),
contactno NUMERIC(10)
);
Inserting Records:
INSERT INTO student VALUES
(101, 'Nisanth', 'AM', '[email protected]', 9345292781),
(102, 'Sivakumar', 'DBT', '[email protected]', 8072363074),
(103, 'Sachin', 'C', '[email protected]', 8754681258),
(104, 'Siva', 'IOT', '[email protected]', 6380603146);
Select *from student;

SID SNAME DPMT EMAIL CONTACTNO


101 Nisanth AM [email protected] 9345292781
102 Sivakumar DBT [email protected] 8072363074
103 Sachin C [email protected] 8754681258
104 Siva IOT [email protected] 6380603146

1. Exception Handling for No Data Found


DECLARE
sid_to_find INT := 105; -- Non-existent student ID
student_name VARCHAR2(20);
BEGIN
SELECT sname INTO student_name FROM university_student WHERE sid = sid_to_find;
DBMS_OUTPUT.PUT_LINE('Student Name: ' || student_name);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No student found with ID: ' || sid_to_find);
END;

Output:
No student found with ID: 105
PL/SQL procedure successfully completed.

2. Dynamic Query to Fetch Student Name


DECLARE
sid_to_find INT := &sid_to_find; -- Accept input for SID
student_name VARCHAR2(20);
BEGIN
SELECT sname INTO student_name FROM university_student WHERE sid = sid_to_find;
DBMS_OUTPUT.PUT_LINE('Student Name: ' || student_name);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No student found with ID: ' || sid_to_find);
END;

Input and Output:


1. Valid SID:
Enter value for sid_to_find: 101
Student Name: Nisanth
PL/SQL procedure successfully completed.
2. Invalid SID:
Enter value for sid_to_find: 110
No student found with ID:110
PL/SQL procedure successfully completed.

3. Handling Too Many Rows Exception


DECLARE
sid INT;
student_name university_student.sname%TYPE;
BEGIN
SELECT sid, sname INTO sid, student_name FROM university_student WHERE dpmT =
'CS';
DBMS_OUTPUT.PUT_LINE('Student: ' || sid || ' - ' || student_name);
EXCEPTION
WHEN TOO_MANY_ROWS THEN
DBMS_OUTPUT.PUT_LINE('Error: Query returned more than one row.');
END;

Output:
Error: Query returned more than one row.
PL/SQL procedure successfully completed.

4. Invalid Cursor Handling


DECLARE
student_record university_student%ROWTYPE;
CURSOR student_cursor IS
SELECT * FROM university_student WHERE dpmT = 'XYZ'; -- Non-existent
department
BEGIN
FETCH student_cursor INTO student_record;
EXCEPTION
WHEN INVALID_CURSOR THEN
DBMS_OUTPUT.PUT_LINE('Cursor is invalid or not opened.');
END;
Output:
Cursor is invalid or not opened.
PL/SQL procedure successfully completed.

5. Out of Range Exception Handling


DECLARE
student_email VARCHAR2(50);
invalid_sid EXCEPTION;
sid_to_check INT := 5000; -- Out of range SID
BEGIN
IF sid_to_check NOT BETWEEN 201 AND 204 THEN
RAISE invalid_sid;
ELSE
SELECT email INTO student_email FROM university_student WHERE sid =
sid_to_check;
DBMS_OUTPUT.PUT_LINE('Student Email: ' || student_email);
END IF;
EXCEPTION
WHEN invalid_sid THEN
DBMS_OUTPUT.PUT_LINE('Student ID ' || sid_to_check || ' is out of range.');
END;

Output:
Student ID 5000 is out of range.
PL/SQL procedure successfully completed.

6. Arithmetic Exception Handling


DECLARE
marks1 INT := &marks1;
marks2 INT := &marks2;
average FLOAT;
BEGIN
average := (marks1 + marks2) / 2;
DBMS_OUTPUT.PUT_LINE('Average Marks: ' || average);
EXCEPTION
WHEN ZERO_DIVIDE THEN
DBMS_OUTPUT.PUT_LINE('Error: Division by zero encountered.');
END;

Input and Output:


1. Valid Division:
Enter value for marks1: 85
Enter value for marks2: 90
Average Marks: 87.5
PL/SQL procedure successfully completed.
2. Valid Division:
Enter value for marks1: 50
Enter value for marks2: 100
Average Marks: 75
PL/SQL procedure successfully completed.
RESULT:
Thus, exception handling for a university database system has been successfully
implemented.

COE (30)

RECORD (20)

VIVA (10)

TOTAL (60)

You might also like