0% found this document useful (0 votes)
47 views34 pages

DBMS Lab Manual

This lab manual for the Master of Computer Applications program at Sri Venkateswara College of Engineering outlines the objectives and outcomes of the Database Management Systems course. It includes detailed instructions for various SQL operations such as creating, altering, and querying tables, as well as using PL/SQL for database management. The manual serves as a comprehensive guide for students to practice and implement database concepts effectively.

Uploaded by

abhisathvika2417
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views34 pages

DBMS Lab Manual

This lab manual for the Master of Computer Applications program at Sri Venkateswara College of Engineering outlines the objectives and outcomes of the Database Management Systems course. It includes detailed instructions for various SQL operations such as creating, altering, and querying tables, as well as using PL/SQL for database management. The manual serves as a comprehensive guide for students to practice and implement database concepts effectively.

Uploaded by

abhisathvika2417
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

MASTER OF COMPUTER APPLICATIONS

DATABASE MANAGEMENT SYSTEMS LAB MANUAL


(CA24FPC103)

DEPARTMENT OF COMPUTER APPLICATIONS


SRI VENKATESWARA COLLEGE OF ENGINEERING
(AUTONOMOUS)
(Affiliated to J.N.T. University Anantapur, Ananthapuramu, Accredited by NBA & NAAC “A”)
Karakambadi Road, Tirupati - 517 507

Prepared by: B. Sai Hemanth,


Assistant Professor, Dept. Of MCA
DATABASE MANAGEMENT SYSTEMS
(CA24FPC106)
Lab Manual
COURSE OBJECTIVES:
The objective of this course are to:
 To implement the basic knowledge of SQL queries and relational
algebra.
 To construct database models for different database applications.
 To apply normalization techniques for refining of databases.
 To practice various triggers, procedures, and cursors using
PL/SQL.
 To design and implementation of a database for an organization.

COURSE OUTCOMES:
After successful completion of the course, students will be able to:

 C01: Design database for any real-world problem


 CO2: Implement PL/SQL programs
 CO3: Define SQL queries
 CO4: Decide the constraints
 CO5: Investigate for data inconsistency

INDEX

Prepared by: B. Sai Hemanth,


Assistant Professor, Dept. Of MCA
S.No Title Pg. No.
1 Creation of Table 1

2 Alteration of Table 2-3

3 Insertion of Records 4

4 Updation of Records 5

5 Retrieving Data Using Select Query 6

6 Advanced Select Query 7

7 Working with Clauses 8-9

8 Set Operations 10-11

9 Working with Functions in SQL 12-15

10 Subqueries 16

11 Joins 17-19

12 Integrity Constraints 20-21

13 Control Statements Using PL/SQL 22-24

14 Performing Arithmetic Operations Using PL/SQL Functions 25-27

15 Stored Procedures 28-29

16 Creating a Procedure Using Parameters 30

17 Cursors 31

18 Triggers 31

Prepared by: B. Sai Hemanth,


Assistant Professor, Dept. Of MCA
01. Aim: To create a student table by using Data Definition Language.

Create: (Command to define the table structure)


Syntax:
CREATE TABLE table_name ( ColumnName1 DataType1, ColumnName2 DataType2,
...
ColumnNamen DataTypeN
);

Example:
CREATE TABLE student ( sno NUMBER,
Sname VARCHAR2(20), fee NUMBER,
Saddress VARCHAR2(20)
);

Describe: (Command to view table structure)


Syntax:
DESCRIBE table_name;

Example:
DESCRIBE student;

Expected Output of DESCRIBE student:

Field Names Data Types


Sno NUMBER
Sname VARCHAR2(20)
Fee NUMBER
Saddress VARCHAR2(20)

02. Aim: To Alter a table using Data Definition Language.

Alter: Alter with add Syntax:


ALTER TABLE table_name
ADD (column_name datatype);

Example:
1
Prepared by: B. Sai Hemanth,
Assistant Professor, Dept. Of MCA
ALTER TABLE Student ADD (phno NUMBER);
Describe:
Syntax:
DESCRIBE table_name;

Example:
DESCRIBE student;

Expected Output (after adding phno):

Field Name Data Type


Sno NUMBER
Sname VARCHAR2(20)
Fee NUMBER
Saddress VARCHAR2(20)
Phno NUMBER

Alter with modify:


Syntax:
ALTER TABLE table_name
MODIFY (column_name datatype);

Example:
ALTER TABLE Student
MODIFY (Saddress VARCHAR2(30));

Describe:
Syntax:
DESCRIBE table_name;

Example:
DESCRIBE Student;

Expected Output (after modifying Saddress):

Field Name Data Type


Sno NUMBER
Sname VARCHAR2(20)
Fee NUMBER
Saddress VARCHAR2(30)
Phno NUMBER

Alter with drop:


Syntax:
ALTER TABLE table_name DROP COLUMN column_name;
Example:
ALTER TABLE Student DROP COLUMN phno;

Describe:
2
Prepared by: B. Sai Hemanth,
Assistant Professor, Dept. Of MCA
Syntax:
DESCRIBE table_name;

Example:
DESCRIBE Student;

Expected Output (after dropping phno):

Field Name Data Type


Sno NUMBER
Sname VARCHAR2(20)
Fee NUMBER
Saddress VARCHAR2(30)

03. Aim: To insert records into a table using Data Manipulation Language.

1. Inserting data into all columns:


Syntax:
INSERT INTO table_name
VALUES (value1, value2, ..., valueN);

3
Prepared by: B. Sai Hemanth,
Assistant Professor, Dept. Of MCA
Example:
INSERT INTO Student
VALUES (01, 'Koushik', 25000, 'CTR');

2. Inserting data into specific columns:


Syntax:
INSERT INTO table_name (column1, column2, ...) VALUES (value1, value2, ...);
Example:
INSERT INTO student (sno, sname, fee, saddress) VALUES (&sno, &sname, &fee, &saddress);
● Session 1:
Enter Sno: 02 Enter Sname: Vishnu Enter fee: 15000
Enter saddress: Kadapa
/ (Indicates end of input for this record)

● Session 2:
Enter Sno: 03 Enter Sname: Siddu Enter fee: 20000
Enter saddress: Kadiri
/ (Indicates end of input for this record)

● Session 3:
Enter Sno: 04 Enter Sname: Manju Enter fee: 10000 Enter saddress: MPL

Select: (To view inserted records)


Syntax:
SELECT * FROM table_name;

Example:
SELECT * FROM Student;

Expected Output (after all inserts):

Sno Sname Fee Saddress


01 Koushik 25000 CTR
02 Vishnu 15000 Kadapa
03 Siddu 20000 Kadiri
04 Manju 10000 MPL

04. Aim: To update the records by using the UPDATE command.

Program:

Update:
Syntax:
UPDATE table_name
4
Prepared by: B. Sai Hemanth,
Assistant Professor, Dept. Of MCA
SET column_name = value
WHERE condition;

Example:
UPDATE Student
SET Saddress = 'KDP'
WHERE Sno = 01;

Select: (To view updated records)


Syntax:
SELECT * FROM table_name;

Example:
SELECT * FROM Student;

Expected Output (after the UPDATE):

Sno Sname Fee Saddress


01 Koushik 25000 KDP
02 Vishnu 15000 Kadapa
03 Siddu 20000 Kadiri
04 Manju 10000 MPL

05. Aim: To retrieve the data from the database using SELECT statement.

Program:
1. Selecting all columns:
Syntax:
SELECT * FROM table_name;

Example:
SELECT * FROM Student;

2. Selecting specific columns:


Syntax:
SELECT column1, column2, ... FROM table_name;

5
Prepared by: B. Sai Hemanth,
Assistant Professor, Dept. Of MCA
Example:
SELECT Sno, Sname, Saddress FROM Student;

3. Selecting with a WHERE clause (filtering):


Syntax:
SELECT * FROM table_name
WHERE condition;

Example:
SELECT * FROM Student
WHERE Fee > 15000;

Example Output for SELECT * FROM Student;

Sno Sname Fee Saddress


01 Koushik 25000 KDP
02 Vishnu 15000 Kadapa
03 Siddu 20000 Kadiri
04 Manju 10000 MPL

Example Output for SELECT Sno, Sname, Saddress FROM Student;

Sno Sname Saddress


01 Koushik KDP
02 Vishnu Kadapa
03 Siddu Kadiri
04 Manju MPL

Example Output for SELECT * FROM Student WHERE Fee > 15000;

Sno Sname Fee Saddress


01 Koushik 25000 KDP
03 Siddu 20000 Kadiri

06. Aim: Working with GROUP BY clause and HAVING clause.

Program: Employee Table

ENO ENAME AGE SALARY DEPTNO


1 Guna 24 35000 20401
2 Aritm 22 25000 20403
3 Hari 25 20000 20402
4 Arshad 28 30000 20401
5 Rehman 30 15000 20401

Group By Clause:
Syntax:
6
Prepared by: B. Sai Hemanth,
Assistant Professor, Dept. Of MCA
SELECT column_list
FROM table_name
WHERE <condition>
[GROUP BY <column>];

Example:
SELECT deptno, SUM(Salary)
FROM employee
GROUP BY deptno;

Output:

Deptno SUM(Salary)
20401 80000
20403 25000
20402 20000

Having Clause:
Syntax:
SELECT column, group_function(column)
FROM table_name
[WHERE <condition>]
[GROUP BY <column>]
[HAVING group_condition];

Example:
SELECT deptno, COUNT(deptno)
FROM employee
GROUP BY deptno
HAVING deptno > 20401;

Output:
Deptno COUNT(deptno)
20403 1
20402 1
07: From Querying the table using Clause like WHERE, ORDER BY, SAL, FWD, OR, NOT, IS
NULL

WHERE Clause:

Syntax: SELECT * from tablename WHERE condition

Example: SELECT * from employee WHERE salary > 2000

Output:

ENO ENAME SALARY COMMISSION


1 Amit 3000
2 Vivan 2500 500
7
Prepared by: B. Sai Hemanth,
Assistant Professor, Dept. Of MCA
ENO ENAME SALARY COMMISSION
4 Payal 4000
5 Vimal 5000 1000

ORDER BY Clause:

Syntax: SELECT [column1, column2, ...] from Table name [WHERE condition] [ORDER BY column
name [ASC/DESC]]

Example: SELECT * from employee ORDER BY Salary;

Output:

ENO ENAME SALARY COMMISSION


2 Vivan 2500 500
1 Amit 3000
4 Payal 4000
5 Vimal 5000 1000

Logical AND:

Syntax: SELECT * from tablename WHERE column1 = value1 AND column2 = value2;

Example: SELECT * from employee WHERE ename = 'Payal' AND salary > 3000;

Output:

ENO ENAME SALARY COMMISSION


4 Payal 4000

Logical OR:

Syntax: SELECT * from tablename WHERE column1 = value1 OR column2 = value2;

Example: SELECT * from employee WHERE ename = 'Amit' OR salary = 2500;

Output:

ENO ENAME SALARY COMMISSION


1 Amit 3000
2 Vivan 2500 500

IS NULL Clause:

Syntax: SELECT * from tablename WHERE column IS NULL;

Example: SELECT * from employee WHERE commission IS NULL;

Output:
8
Prepared by: B. Sai Hemanth,
Assistant Professor, Dept. Of MCA
ENO ENAME SALARY COMMISSION
1 Amit 3000
4 Payal 4000

Output: No rows selected

08 Aim: Working with Set of operators.

Program:

Table 1:

ID NAME
1 Abhi
2 Adam

Table 2:

ID NAME
2 Adam
3 Chester

Union:

Syntax:

Select * from tablename


Union
Select * from tablename
9
Prepared by: B. Sai Hemanth,
Assistant Professor, Dept. Of MCA
Example:

Select * from table1


Union
Select * from table2;

Output:

ID NAME
1 Abhi
2 Adam
3 Chester

Union All:

Syntax:
Select * from tablename
Union All
Select * from tablename;

Example:
Select * from table1
Union All
Select * from table2;

Output:

ID NAME
1 Abhi
2 Adam
2 Adam
3 Chester

Intersect:

Syntax:
Select * from tablename
Intersect
Select * from tablename;

Example:
Select * from table1
Intersect
Select * from table2;

Output:
ID NAME
2 Adam
10
Prepared by: B. Sai Hemanth,
Assistant Professor, Dept. Of MCA
Minus:

Syntax:
Select * from tablename
Minus
Select * from tablename;

Example:
Select * from table1
Minus
Select * from table2;

Output:
ID NAME
10 Abhi

09 Aim: Working with numbers, characters, dates & aggregate functions with in sql.
Programs on Character functions:
1.Lower()
Syntax: Lower(string);
Example: Select lower('HARI') from dual;
Output: hari
2.Upper()
Syntax: Upper(string);
Example: Select upper('Hari') from dual;
Output: HARI
3.Initcap()
Syntax: Initcap(string);
Example: Select initcap('hari') from dual;
Output: Hari
4.Concat()
Syntax: Concat(string1, string2);
Example: Select concat('Guna', 'shekar') from dual;
Output: Gunashekar
5.Length()
Syntax: Length(string);
Example: Select length('Hari') from dual;
Output: 4
11
Prepared by: B. Sai Hemanth,
Assistant Professor, Dept. Of MCA
6.Replace()
Syntax: Replace(string, old-string, new-string);
Example: Select replace('SV Academy', 'SV', 'Hari') from dual;
Output: Hari Academy
7.Translate()
Syntax: Translate(string, characters, translation);
Example: Select translate('cold', 'o', '1') from dual;
Output: cool
Number functions or mathematical functions:
1.Abs()
Syntax: Abs(n);
Example: Select abs(-10.8) from dual;
Output: 10.8
2.Sqrt()
Syntax: Sqrt(n);
Example: Select sqrt(4) from dual;
Output: 2
3.Mod()
Syntax: Mod(m, n);
Example: Select mod(4, 2) from dual;
Output: 0
4.Power()
Syntax: Power(x, y);
Example: Select power(2, 3) from dual;
Output: 8
5.Cos()
Syntax: Cos(n);
Example: Select cos(0) from dual;
Output: 1
6.Sin()
Syntax: Sin(n);
Example: Select sin(4) from dual;
Output: 1
7.Round()
Syntax: Round(n, d);
Example: Select round(16.791, 2) from dual;
Output: 16.79

8.Ceil()
Syntax: Ceil(n);
Example: Select ceil(15.20) from dual;
Output: 16
Date functions:
1.Sysdate:
Syntax: Sysdate;
Example: Select Sysdate from dual;
Output: (Current date and time)
12
Prepared by: B. Sai Hemanth,
Assistant Professor, Dept. Of MCA
2.Round (Sysdate):
Syntax: Round(Sysdate);
Example: Select round(Sysdate) from dual;
Output: (Current date rounded to the nearest day)
3.Add_months (Sysdate, n):
Syntax: Add_months(Sysdate, n);
Example: Select add_months(Sysdate, 2) from dual;
Output: (Date after adding n months)
4.Last_day (Sysdate):
Syntax: Last_day(Sysdate);
Example: Select last_day(Sysdate) from dual;
Output: (Last day of the current month)
5.Next_day (Sysdate, day):
Syntax: Next_day(Sysdate, day);
Example: Select next_day(Sysdate, 'Friday') from dual;
Output: (Date of the next specified day of the week)

Aggregate / Group Functions:


Employee Table:

EID EName Eaddress Salary

1 Arjun CTR 20000

2 Tej ATP 15000

3 Yuva DMD 30000

4 Nandhu MPL 40000

1. Avg():
Syntax: Select Avg(Columnname) from tablename;
Example: Select avg(salary) from employee;
Output: 26250
2. Count():
Syntax: Select Count(Columnname) from tablename;
Example: Select Count(salary) from employee;
Output: 4
3. Max():
Syntax: Select max(columname) from tablename;
Example: Select max(salary) from employee;
Output: 40000

13
Prepared by: B. Sai Hemanth,
Assistant Professor, Dept. Of MCA
4. Min():
Syntax: Select min(columnmame) from tablename;
Example: Select min(salary) from employee;
Output: 15000
5. Sum():
Syntax: Select Sum(columnname) from tablename;
Example: Select Sum(salary) from employee;
Output: 105000

10 Aim: Creating a program on Subqueries.

Program:
Subquery Syntax:
Select ... where ... (Select ... where ...);
↓ ↓
Outer Query Inner Query

Select * from Employee;

Employee Table:

ENO ENAME DEPT SALARY


01 Joseph IT 15000
02 Jenny Marketing 10000
03 George Sales 12000
04 Francis Production 13000

SQL Query:
Select dept from employee
where Salary = (select max(salary) from employee);

Output:

DEPT
IT

14
Prepared by: B. Sai Hemanth,
Assistant Professor, Dept. Of MCA
11 Aim: Working with joins in SQL

Program:

Employee 1 Table:

eno ename job salary


01 Anand Admin 15000
02 Madhu Marketing 12000
03 Srinivas Production 13000
04 Anjan HR 16000
05 Koushik Admin 15000
06 Prudhvi Marketing 12000
07 Yuva HR 16000

Employee 2 Table:

eid phno address


01 1234 CTR
02 4321 TPT
03 3421 Bang
04 4213 Chennai
05 1342 Mumbai
06 2431 Delhi
09 1432 Kerala

Inner Join:

Syntax: Select table1.Column1, table2.Column1, table2.Column2 from table1, table2 where


table1.matching_column = table2.matching_column;
Example: Select ename, job, salary, phno, address from employee1, employee2 where employee1.eno =
employee2.eid;

Output:

15
Prepared by: B. Sai Hemanth,
Assistant Professor, Dept. Of MCA
ename job salary phno address
Anand Admin 15000 1234 CTR
Madhu Marketing 12000 4321 TPT
Srinivas Production 13000 3421 Bang
Anjan HR 16000 4213 Chennai
Koushik Admin 15000 1342 Mumbai
Prudhvi Marketing 12000 2431 Delhi
Yuva HR 16000 1432 Kerala

Left Outer Join:

Syntax: Select table1.Column1, table2.Column1, table2.Column2 from table1, table2 where


table1.matching_column = table2.matching_column(+);
Example: Select ename, job, salary, phno, address from employee1, employee2 where employee1.eno =
employee2.eid(+);

Output:

ename job salary phno address


Anand Admin 15000 1234 CTR
Madhu Marketing 12000 4321 TPT
Srinivas Production 13000 3421 Bang
Anjan HR 16000 4213 Chennai
Koushik Admin 15000 1342 Mumbai
Prudhvi Marketing 12000 2431 Delhi
Yuva HR 16000 1432 Kerala

Right Outer Join:

Syntax: Select table1.Column1, table1.Column2, table2.Column1, table2.Column2 from table1, table2


where table1.matching_column(+) = table2.matching_column;
Example: Select ename, job, salary, phno, address from employee1, employee2 where employee1.eno(+)
= employee2.eid;

Output:

ename job salary phno address


Anand Admin 15000 1234 CTR
Madhu Marketing 12000 4321 TPT
Srinivas Production 13000 3421 Bang
Anjan HR 16000 4213 Chennai
Koushik Admin 15000 1342 Mumbai
Prudhvi Marketing 12000 2431 Delhi

16
Prepared by: B. Sai Hemanth,
Assistant Professor, Dept. Of MCA
ename job salary phno address
Yuva HR 16000 1432 Kerala

Full Outer Join:

Syntax: Select table1.Column1, table1.Column2, table2.Column1, table2.Column2 from table1, table2


where table1.matching_column = table2.matching_column(+);
OR
Select table1.Column1, table1.Column2, table2.Column1, table2.Column2 from table1, table2 where
table1.matching_column(+) = table2.matching_column;

Union:

Syntax: Select ename, job, salary, phno, address from employee1, employee2 where employee1.eno(+) =
employee2.eid;
Union
Select ename, job, salary, phno, address from employee1, employee2 where employee1.eno =
employee2.eid(+);

Output:

ename job salary phno address


Anand Admin 15000 1234 CTR
Madhu Marketing 12000 4321 TPT
Srinivas Production 13000 3421 Bang
Anjan HR 16000 4213 Chennai
Koushik Admin 15000 1342 Mumbai
Prudhvi Marketing 12000 2431 Delhi
Yuva HR 16000 1432 Kerala

17
Prepared by: B. Sai Hemanth,
Assistant Professor, Dept. Of MCA
12.Aim: Program on Integrity Constraints

1. NOT NULL

Syntax: CREATE TABLE tablename (columnname datatype NOT NULL, ...);

Example:
CREATE TABLE employee (
emp_id NUMBER NOT NULL,
emp_name VARCHAR(30) NOT NULL,
caddress VARCHAR(40),
ephone NUMBER UNIQUE
);

INSERT INTO employee VALUES (1, 'Koushik', 'Madhupapal', 997321456);

2. UNIQUE

Syntax: CREATE TABLE tablename (columnname datatype UNIQUE, ...);

Example:

CREATE TABLE emps (


emp_id NUMBER NOT NULL,
emp_name VARCHAR(30) NOT NULL,
caddress VARCHAR(40),
ephone NUMBER UNIQUE
);

INSERT INTO emps VALUES (1010, 'Thyesh', 'Chittoor', 99852165);

3. PRIMARY KEY

Syntax: CREATE TABLE tablename (columnname datatype PRIMARY KEY, ...);

Example:

CREATE TABLE emp3s (


emp_id NUMBER PRIMARY KEY,
emp_name VARCHAR(20) NOT NULL,
paddress VARCHAR(40),
ephone NUMBER UNIQUE
);

INSERT INTO emp3s VALUES (1, 'Anjan', 'B', 9962314516);

18
Prepared by: B. Sai Hemanth,
Assistant Professor, Dept. Of MCA
4. CHECK

Syntax: CREATE TABLE tablename (columnname datatype CHECK (condition), ...);

Example:

CREATE TABLE Voter (


voter_name VARCHAR(20),
voter_address VARCHAR(20),
voter_age NUMBER CHECK (voter_age > 18)
);

INSERT INTO Voter VALUES ('Kumar', 'K', 10); -- This will violate the CHECK constraint

5. FOREIGN KEY

Syntax: CREATE TABLE tablename (columnname datatype PRIMARY KEY, ...);

Syntax: CREATE TABLE tablename (columnname datatype, FOREIGN KEY (columnname)


REFERENCES tablename(columnname));

Example:

CREATE TABLE dept1 (


dept_id NUMBER PRIMARY KEY,
dept_name VARCHAR(20),
location VARCHAR(10)
);

CREATE TABLE emp1 (


emp_id NUMBER PRIMARY KEY,
emp_name VARCHAR(20),
department_id NUMBER,
FOREIGN KEY (department_id) REFERENCES dept1(dept_id)
);

INSERT INTO dept1 VALUES (1, 'IT', 'New York');

19
Prepared by: B. Sai Hemanth,
Assistant Professor, Dept. Of MCA
13.Aim: Implementation of Control Statement Using PL/SQL Decision making Statement.

Program: Decision making Statement

1. IF...THEN
Syntax:
IF condition THEN
Statement(s);
END IF;

Example:
DECLARE
n NUMBER := 10;
BEGIN
IF n > 0 THEN
DBMS_OUTPUT.PUT_LINE('Positive number');
END IF;
END;
/

Output:
Positive number
PL/SQL procedure successfully executed.

2. IF...THEN...ELSE
Syntax:
IF condition THEN
Statement(s) 1;
ELSE
Statement(s) 2;
END IF;

Example:
DECLARE
n NUMBER := -5;
BEGIN
IF n > 0 THEN
DBMS_OUTPUT.PUT_LINE('Positive number');
ELSE
DBMS_OUTPUT.PUT_LINE('Negative number');
END IF;
END;
/

Output:
Negative number
PL/SQL procedure successfully executed.
20
Prepared by: B. Sai Hemanth,
Assistant Professor, Dept. Of MCA
Another Example (Checking for even/odd):
DECLARE
n NUMBER := 7;
BEGIN
IF MOD(n, 2) = 0 THEN
DBMS_OUTPUT.PUT_LINE(n || ' is an even number.');
ELSE
DBMS_OUTPUT.PUT_LINE(n || ' is an odd number.');
END IF;
END;
/

Output:
7 is an odd number.
PL/SQL procedure successfully executed.

3. IF...THEN...ELSE …IF

Syntax:
IF condition 1 THEN
Statement(s) 1;
ELSIF condition 2 THEN
Statement(s) 2;
[ELSIF condition 3 THEN
Statement(s) 3;] -- Optional additional ELSIF clauses
...
ELSE
Statement(s) n; -- Optional ELSE clause
END IF;

Example:
DECLARE
S1 NUMBER;
S2 NUMBER;
S3 NUMBER;
S4 NUMBER;
BEGIN
S1 := &S1;
S2 := &S2;
S3 := &S3;
S4 := &S4;

IF S1 >= 35 AND S2 >= 35 AND S3 >= 35 AND S4 >= 35 THEN


DBMS_OUTPUT.PUT_LINE('Pass and promoted');
ELSIF S1 < 35 AND S2 < 35 AND S3 < 35 AND S4 < 35 THEN
DBMS_OUTPUT.PUT_LINE('Fail');
ELSE
DBMS_OUTPUT.PUT_LINE('Promoted');
END IF;
END;
21
Prepared by: B. Sai Hemanth,
Assistant Professor, Dept. Of MCA
/

Output:
Enter value for S1: 45
Enter value for S2: 54
Enter value for S3: 34
Enter value for S4: 85
Promoted
PL/SQL procedure successfully executed.

22
Prepared by: B. Sai Hemanth,
Assistant Professor, Dept. Of MCA
14.Aim: Implementation of Control Statement Using PL/SQL Looping Statements.

Decision Looping Statements:

● Basic loop
● FOR loop
● WHILE loop

1. Basic Loop:

Syntax:

LOOP
Statement(s);
EXIT WHEN condition;
END LOOP;

Example:

DECLARE
A NUMBER;
BEGIN
A := 4;
LOOP
DBMS_OUTPUT.PUT_LINE(A);
A := A + 1;
EXIT WHEN A > 10;
END LOOP;
END;
/

Output:
4
5
6
7
8
9
10
PL/SQL procedure successfully executed.

2. WHILE Loop:

Syntax:

WHILE condition LOOP


Statement(s);
23
Prepared by: B. Sai Hemanth,
Assistant Professor, Dept. Of MCA
END LOOP;

Example:

DECLARE
A NUMBER;
BEGIN
A := 2;
WHILE A <= 20 LOOP
DBMS_OUTPUT.PUT_LINE(A);
A := A + 2;
END LOOP;
END;
/

Output:
2
4
6
8
10
12
14
16
18
20
PL/SQL procedure successfully executed.

.
4. FOR Loop:

Syntax:

FOR loop_variable IN [REVERSE] lower_bound .. upper_bound LOOP


Statement(s);
END LOOP;

Example:

DECLARE
A NUMBER;
BEGIN
A := 2; -- Although A is initialized here, the FOR loop re-declares and uses its own loop variable A.
FOR A IN 2..20 LOOP -- Loop from 2 to 20
DBMS_OUTPUT.PUT_LINE(A);
END LOOP;
END;
/

24
Prepared by: B. Sai Hemanth,
Assistant Professor, Dept. Of MCA
Output:
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
PL/SQL procedure successfully executed.

25
Prepared by: B. Sai Hemanth,
Assistant Professor, Dept. Of MCA
15.Aim: Program on DDL Commands using PL/SQL

Program:

Insert:

CREATE OR REPLACE PROCEDURE add_student


IS
Sno NUMBER;
Sname VARCHAR2(20);
Saddress VARCHAR2(20);
BEGIN
Sno := 01;
Sname := 'Hari';
Saddress := 'Chittoor';

INSERT INTO Student (Sno, Sname, Saddress)


VALUES (Sno, Sname, Saddress);
END;
/

Procedure created.
SQL> execute add_student();

Select * from Student;

Output:
Sno Sname Saddress
01 Hari Chittoor
Update:

CREATE OR REPLACE PROCEDURE updt_student


IS
BEGIN
UPDATE Student
SET Saddress = 'Tirupathi'
WHERE Sno = 01;
END;
/

Procedure Created.
SQL> execute updt_student();

Select * from student;

Output:

26
Prepared by: B. Sai Hemanth,
Assistant Professor, Dept. Of MCA
Sno Sname Saddress
01 Hari Tirupathi

Delete:

CREATE OR REPLACE PROCEDURE del_student


IS
BEGIN
DELETE FROM Student
WHERE Sno = 01;
END;
/

Procedure Created.
SQL> execute del_student();

Select * from Student;

Output:
No rows selected.

27
Prepared by: B. Sai Hemanth,
Assistant Professor, Dept. Of MCA
16. Aim: Creating a procedure Using parameters.

Program:

CREATE OR REPLACE PROCEDURE Updt (


SA IN VARCHAR2(20),
SN IN NUMBER
)
IS
BEGIN
UPDATE Student
SET Saddress = SA
WHERE Sno = SN;
END;
/

Procedure Created.
SQL> execute Updt ('Tirupathi', 01);

Select * from student;

Output:

Sno Sname Saddress


01 Hari Tirupathi

28
Prepared by: B. Sai Hemanth,
Assistant Professor, Dept. Of MCA
17 Aim: Working with Cursors in PL/SQL

Program:

SQL> select * from employee;

Employee Table:

eno ename Dept Salary


01 Joseph IT 15000
02 Jenny Marketing 10000
03 George Sales 12000
04 Francis Production 13000

Declare
Cursor C is Select ename, sal from employee;
Vename employee.ename%type;
Vsal employee.sal%type;
begin
Open C;
loop
fetch c into Vename, Vsal;
exit when c%notfound;
dbms_output.put_line(Vename);
dbms_output.put_line(Vsal);
end loop;
Close C;
end;
/

Output:

Joseph
15000
Jenny
10000
George
12000
Francis
13000

29
Prepared by: B. Sai Hemanth,
Assistant Professor, Dept. Of MCA
18 Aim: Creating a program on Trigger

Program:

Syntax:

Create or replace trigger trigger_name


[before/after]
[insert /update/delete] on <tablename>
for each row [where condition]
begin
executable statements
end;
/

Example:

SQL> select * from Student;

Student Table:

Sno Sname Saddress


01 A chittoor
02 B Tirupathi
03 Cruston Bangalore

Declare
-- No declarations needed for this simple trigger example
begin
-- No executable statements in this simple trigger example
end;
/

Create or replace trigger trg_before_update_student


before update on Student
begin
dbms_output.put_line(‘Record is updated’);
end;
/

Trigger created.
SQL> update Student
Set Saddress = ‘Chennai’
where Sno=03;

30
Prepared by: B. Sai Hemanth,
Assistant Professor, Dept. Of MCA
Output:

Record is updated

31
Prepared by: B. Sai Hemanth,
Assistant Professor, Dept. Of MCA

You might also like