You are on page 1of 40

DR.

AKHILESH DAS GUPTA INSTITUTE OF


PROFESSIONAL STUDIES
(Affiliated to Guru Gobind Singh Indraprastha University, Dwarka, New Delhi)

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

PRACTICAL FILE

ADVANCE DBMS LAB


(ETCS 457)

Submitted to : Submitted by:

Dr. Jyoti Parashar Name- Sheikh Aamir


(Assistant Professor) Roll No. - 11915602720
CSE Department Section- F3
INDEX

Exp. Experiment Name Date of Date of Remarks Marks


no performa checkin
nce g
1. To implement the DDL
commands in RDBMS

2. To implement the DML


commands in RDBMS.
3. To implement the DCL
commands in RDBMS.
4. To implement the
Relational Algebra
operations in RDBMS.
5. To implement aggregate
functions in RDBMS.
6. To implement the
Nested Queries in
RDBMS.
7. To implement the View
in RDBMS.

8. To create PL/SQL
programs to implement
various types of control
structure.
9. To study and execute
the procedure and
functions in PL/SQL.
10. To study and execute
triggers in RDBMS.
EXPERIMENT-1

Data Definition Language (DDL) Commands In RDBMS

AIM:-
To implement the DDL commands in RDBMS.

SYNTAX AND DEFINITION:-


DDL commands:
1. Command: CREATE TABLE
Syntax:
CREATE TABLE table_name
(
Column_name [ owner_name ] table_name
);

2. Command: ALTER TABLE


Syntax:
ALTER TABLE[ owner_name ] table_name
[ADD column_name datatype attributes]
[MODIFY{column_name datatype | column_constraint}]

3. Command: TRUNCATE TABLE


Syntax:
TRUNCATE TABLE table name;

4. Command: DROP TABLE


Syntax:
DROP TABLE table name;

QUERIES:-
1. CREATE table HARSH73
(
name CHAR(20),
regn NUMBER(10),
dob DATE,
class CHAR(10)
);
DESC HARSH73;

2. ALTER table HARSH73


MODIFY class CHAR(5);
3. TRUNCATE table HARSH73;

4. DROP table HARSH73;

OUTPUT:-
EXPERIMENT-2

Data Manipulation Language (DML) in RDBMS

AIM:-
To implement the DML commands in RDBMS.

SYNTAX AND DEFINITION:-


DML commands:
1. Command: SELECT
Syntax:
SELECT[ALL | DISTINCT] select list
FROM table_name1[,…table_nameN]
[JOIN join_condition]
[WHERE search_condition]

2. Command: INSERT
Syntax:
INSERT INTO[[database_name]owner]{table_name|view_name}
[(column_list)]{[DEFAULT]VALUES|VALUES(value[…])|SELECT
Statement}

3. Command: UPDATE
Syntax:
UPDATE table_name
SET column_name=expression[,…n]
WHERE search_condition

4. Command: DELETE
Syntax:
DELETE[FROM] table_name WHERE search_condition]

QUERIES:-
DML queries:
1. INSERT INTO HARSH73 VALUES('HARSH',1031030073,'16 FEBRUARY
1992’,’CSE B’);

INSERT INTO HARSH73 VALUES('&name',&regn,'&dob','&class');

2. SELECT * FROM HARSH73;

SELECT name FROM HARSH73;

3. UPDATE HARSH73
SET class = 'CSE B'
WHERE name = ‘JATIN’;

4. DELETE FROM HARSH73


WHERE name = ‘AKASH’;

OUTPUT:-
DML output:
EXPERIMENT-3

Data Control Language (DCL) Commands in RDBMS

AIM:-
To implement the DCL commands in RDBMS.

SYNTAX AND DEFINITION:-


DCL commands:
1. Command: GRANT
Syntax:
GRANT permissions ON objects TO account
2. Command: REVOKE
Syntax:
REVOKE permissions ON object FROM account

DCL queries:
1. GRANT SELECT, UPDATE ON HARSH73 TO sys;

2. REVOKE SELECT, UPDATE ON HARSH73 FROM sys;

DCL output:
EXPERIMENT-4

Relational Algebra

AIM:-
To implement the Relational Algebra operations in RDBMS.

SYNTAX AND DEFINITION:-


Relational Algebra:
A query language is a language in which user requests information from the database. It can be
categorized as either procedural or nonprocedural. In a procedural language the user instructs the system
to do a sequence of operations on database to compute the desired result. In nonprocedural language the
user describes the desired information without giving a specific procedure for obtaining that information.
The relational algebra is a procedural query language. It consists of a set of operations that take one or
two relations as input and produces a new relation as output.

Fundamental Operations:
● SELECT
● UNION
● UNION ALL
● MINUS
Union, set difference, Cartesian product and rename operations are binary operations as they operate on
pairs of relations.

Other Operations:
● INTERSECT
● NATURAL JOIN
● EQUIJOIN
● OUTER JOIN

1. Command: UNION COMMAND


Syntax:
SELECT column1, column2 from t1
UNION
SELECT column1, column2 from t2;

2. Command: UNION ALL


Syntax:
SELECT column1, column2 from t1
UNION ALL
SELECT column1, column2 from t2;

3. Command: INTERSECT
Syntax:
SELECT column1, column2 from t1
INTERSECT
SELECT column1, column2 from t2;

4. Command: MINUS
Syntax:
SELECT column1, column2 from t1
MINUS
SELECT column1, column2 from t2;

5. Operation: EQUIJOIN
Syntax:
SELECT t1.column, t2.column FROM t1,t2
WHERE t1.column=t2.column;

6. Operation: LEFT OUTER JOIN


Syntax:
SELECT column1, column2 FROM t1 LEFT OUTER JOIN t2
ON t1.column1=t2.column1;
(or)
SELECT t1.column1, t2.column2 FROM t1,t2
WHERE t1.column1=t2.column1(+);

7. Operation: RIGHT OUTER JOIN


Syntax:
SELECT column1, column2 FROM t1 RIGHT OUTER JOIN t2
ON t1. column1=t2.column1;
(or)
SELECT t1.column1, t2.column2 FROM t1,t2
WHERE t1.column1(+)=t2.column1;

8. Operation: FULL OUTER JOIN


Syntax:
SELECT column1, column2 FROM t1 FULL OUTER JOIN t2
ON t1.column1=t2.column1;
(or)
SELECT t1.column1, t2.column2 FROM t1,t2
WHERE t1.column1=t2.column1(+)
UNION
SELECT t1.column, t2.column FROM t1,t2
WHERE t1.column1(+)=t2.column1;

QUERIES:-

SELECT * FROM HARSH73;


SELECT * FROM ;

1. SELECT NAME,REGNO FROM HARSH73


UNION
SELECT NAME,REGNO FROM HARSH273;

2. SELECT NAME,REGNO FROM HARSH73


UNION ALL
SELECT NAME,REGNO FROM HARSH273;

3. SELECT NAME FROM HARSH73


INTERSECT
SELECT NAME FROM HARSH273;

4. SELECT NAME FROM HARSH73


MINUS
SELECT NAME FROM HARSH273;

5. SELECT * FROM HARSH73,HARSH273


WHERE HARSH73.REGNO=HARSH273.REGNO;

6. SELECT * FROM HARSH73,HARSH273


WHERE HARSH73.REGNO=HARSH273.REGNO(+);

7. SELECT * FROM HARSH73,HARSH273


WHERE HARSH73.REGNO(+)=HARSH273.REGNO;

8. SELECT * FROM HARSH73,HARSH273


WHERE HARSH73.REGNO=HARSH273.REGNO(+)
UNION
SELECT * FROM HARSH73,HARSH273
WHERE HARSH73.REGNO(+)=HARSH273.REGNO;

OUTPUT:-
EXPERIMENT- 5

Functions

AIM:-
To implement aggregate functions in RDBMS.

SYNTAX AND DEFINITION:-


1. Function: AVG()
Syntax:
SELECT AVG(column_name) FROM table_name;

2. Function: COUNT()
Syntax:
SELECT COUNT(column_name) FROM table_name;
(or)
SELECT COUNT(distinct column_name) FROM table_name;

3. Function: FIRST()
Syntax:
SELECT FIRST(column_name) FROM table_name;

4. Function: LAST()
Syntax:
SELECT LAST(column_name) FROM table_name;

5. Function: MAX()
Syntax:
SELECT MAX(column_name) FROM table_name;

6. Function: MIN()
Syntax:
SELECT MIN(column_name) FROM table_name;

7. Function: SUM()

Syntax:
SELECT SUM(column_name) FROM table_name;

QUERIES:-
1. SELECT AVG(REGN) FROM HARSH73;
2. SELECT COUNT(NAME) FROM HARSH73;

SELECT COUNT(DISTINCT CLASS) FROM HARSH73;

3. SELECT FIRST(NAME) FROM HARSH73;

4. SELECT LAST(DATE) FROM HARSH73;

5. SELECT MAX(REGN) FROM HARSH73;

6. SELECT MIN(REGN) FROM HARSH73;

OUTPUT:-
EXPERIMENT- 6

Nested Queries

AIM:-
To implement the Nested Queries in RDBMS.

SYNTAX AND DEFINITION:-


Subquery or Inner query or Nested query is a query in a query. A subquery is usually
added in the WHERE Clause of the sql statement. Most of the time, a subquery is used when you know
how to search for a value using a SELECT statement, but do not know the exact value.
Subqueries are an alternate way of returning data from multiple tables.
Subqueries can be used with the following sql statements along with the comparison operators like =, <,
>, >=, <= etc.
● SELECT
● INSERT
● UPDATE
● DELETE

1. Subqueries with the INSERT statement:


Syntax:
INSERT INTO table_name [(column1 [,column2])]
SELECT [*|column1 [,column2]]
FROM table1 [,table2]
[WHERE VALUE OPERATOR];

2. Subqueries with the SELECT statement:


Syntax:
SELECT column_name [,column_name]
FROM table1 [,table2]
WHERE column_name OPERATOR
( SELECT column_name [,column_name]
FROM table1 [,table2]
[WHERE condition]
);

3. Subqueries with the UPDATE statement:


Syntax:
UPDATE table_name1
SET column_name=new_value
[WHERE OPERATOR [VALUE]
( SELECT column_name FROM table_name2
[WHERE condition]
)];

4. Subqueries with the DELETE statement:


Syntax:
DELETE FROM table1
[WHERE OPERATOR [VALUE]
( SELECT column_name FROM table2
[WHERE condition]
)];

QUERIES:-
SELECT * FROM HARSH73;
SELECT * FROM HARSH273;

1. INSERT INTO HARSH273 (name,regn)


SELECT name,regn FROM HARSH73
WHERE name=’MOHIT’;

SELECT * FROM HARSH273;

2. SELECT * FROM HARSH73


WHERE regn IN
( SELECT regn FROM HARSH273 WHERE regn>1031031060 );

3. UPDATE HARSH273
SET regn=regn+5
WHERE name
IN ( SELECT name FROM HARSH73);

SELECT * FROM HARSH273;

4. DELETE FROM HARSH273


WHERE regn
IN ( SELECT regn FROM HARSH73 WHERE regn<1031030099 );

SELECT * FROM HARSH273;

OUTPUT:-
EXPERIMENT- 7

View

AIM:-
To implement the View in RDBMS.

Implement View
It is not desirable for all users to see the entire logical model. Security considerations may
require that certain data be hidden from users. Consider a clerk who needs to know an
instructor’s ID, name and department name, but does not have authorization to see the
instructor’s salary amount. Aside from security concerns, we may wish to create a personalized
collection of relations that is better matched to a certain user’s intuition than is the logical model.

View Definition
We define a view in SQL by using the create view command. To define a view, we must give the
view a name and must state the query that computes the view. The form of the create view
command is:

create view v as <query expression>;

where <query expression> is any legal query expression. The view name is represented by v.

Create a customer table


create table customer(ID varchar(5) primary key,Name varchar(20)
not null,Age int, State varchar(20),Salary int)
Use the above table to perform the following queries:
1. Create table customers by using the information given above.
2. Insert 7 records in table customers.
3. Create view using customer name and age.
4. Update the view and see the table.
5. Delete the view and see the table.

MariaDB [shop]> create table customer(ID varchar(5) primary key,Name varchar(20)


not null,Age int, State varchar(20),Salary int);
Query OK, 0 rows affected (0.43 sec)

MariaDB [shop]> insert into customer values('1','Ramesh',32,'Ahmedabad','20000')


;
Query OK, 1 row affected (0.08 sec)

MariaDB [shop]> insert into customer values('2','Khilan',25,'Delhi','15000');


Query OK, 1 row affected (0.06 sec)

MariaDB [shop]> insert into customer values('3','Kaushik',23,'Kota','10000');


Query OK, 1 row affected (0.05 sec)

MariaDB [shop]> insert into customer values('4','Chaitali',25,'Mumbai','6500');


Query OK, 1 row affected (0.07 sec)

MariaDB [shop]> insert into customer values('5','Hardik',27,'Bhopal','8500');


Query OK, 1 row affected (0.06 sec)

MariaDB [shop]> insert into customer values('6','Komal',22,'MP','4500');


Query OK, 1 row affected (0.05 sec)

MariaDB [shop]> insert into customer values('7','Muffy',24,'HP','10000');


Query OK, 1 row affected (0.05 sec)

MariaDB [shop]> select * from customer;


+----+----------+------+-----------+--------+
| ID | Name | Age | State | Salary |
+----+----------+------+-----------+--------+
| 1 | Ramesh | 32 | Ahmedabad | 20000 |
| 2 | Khilan | 25 | Delhi | 15000 |
| 3 | Kaushik | 23 | Kota | 10000 |
| 4 | Chaitali | 25 | Mumbai | 6500 |
| 5 | Hardik | 27 | Bhopal | 8500 |
| 6 | Komal | 22 | MP | 4500 |
| 7 | Muffy | 24 | HP | 10000 |
+----+----------+------+-----------+--------+
7 rows in set (0.00 sec)

Now, you can query buyer in a similar way as you query an actual table.

MariaDB [shop]> create view buyer as(select name,age from customer);


Query OK, 0 rows affected (0.05 sec)

MariaDB [shop]> select * from buyer;

+----------+------+
| name | age |
+----------+------+
| Ramesh | 32 |
| Khilan | 25 |
| Kaushik | 23 |
| Chaitali | 25 |
| Hardik | 27 |
| Komal | 22 |
| Muffy | 24 |
+----------+------+
7 rows in set (0.00 sec)

Updating a View
A view can be updated under certain conditions which are given below −
● The SELECT clause may not contain the keyword DISTINCT.
● The SELECT clause may not contain summary functions.
● The SELECT clause may not contain set functions.
● The SELECT clause may not contain set operators.
● The SELECT clause may not contain an ORDER BY clause.
● The FROM clause may not contain multiple tables.
● The WHERE clause may not contain subqueries.
● The query may not contain GROUP BY or HAVING.
● Calculated columns may not be updated.
● All NOT NULL columns from the base table must be included in the view in order for
the INSERT query to function.

So, if a view satisfies all the above-mentioned rules then you can update that view. The following
code block has an example to update the age of Ramesh.

MariaDB [shop]> update buyer set age=35 where name='Ramesh';


Query OK, 1 row affected (0.08 sec)
Rows matched: 1 Changed: 1 Warnings: 0

MariaDB [shop]> select * from customer;


+----+----------+------+-----------+--------+
| ID | Name | Age | State | Salary |
+----+----------+------+-----------+--------+
| 1 | Ramesh | 35 | Ahmedabad | 20000 |
| 2 | Khilan | 25 | Delhi | 15000 |
| 3 | Kaushik | 23 | Kota | 10000 |
| 4 | Chaitali | 25 | Mumbai | 6500 |
| 5 | Hardik | 27 | Bhopal | 8500 |
| 6 | Komal | 22 | MP | 4500 |
| 7 | Muffy | 24 | HP | 10000 |
+----+----------+------+-----------+--------+
7 rows in set (0.00 sec)

Deleting Rows into a View


Rows of data can be deleted from a view. The same rules that apply to the UPDATE and
INSERT commands apply to the DELETE command.
Following is an example to delete a record having AGE = 22.

MariaDB [shop]> delete from buyer where age=22;


Query OK, 1 row affected (0.07 sec)

This would ultimately delete a row from the base table CUSTOMERS and the same would
reflect in the view itself. Now, try to query the base table and the SELECT statement would
produce the following result.

MariaDB [shop]> select * from customer;


+----+----------+------+-----------+--------+
| ID | Name | Age | State | Salary |
+----+----------+------+-----------+--------+
| 1 | Ramesh | 35 | Ahmedabad | 20000 |
| 2 | Khilan | 25 | Delhi | 15000 |
| 3 | Kaushik | 23 | Kota | 10000 |
| 4 | Chaitali | 25 | Mumbai | 6500 |
| 5 | Hardik | 27 | Bhopal | 8500 |
| 7 | Muffy | 24 | HP | 10000 |
+----+----------+------+-----------+--------+
6 rows in set (0.00 sec)

MariaDB [shop]>

Create a employee table


create table employee(empno varchar(5) primary key, ename char(20) not null, designation
varchar(20),salary int, deptno varchar(5))

Use the above table to perform the following queries:


1. Create table employee by using the information given above.
2. Insert 7 records in table employee.
3. Create view using employee empno and name.
4. Update the view and see the table.
5. Delete the view and see the table.

MariaDB [mydb]> create table employee(empno varchar(5) primary key, ename char(20) not
null, designation varchar(20),salary int, deptno varchar(5));

Query OK, 0 rows affected (0.66 sec)

MariaDB [mydb]> insert into employee values('100','Vikas','AP-Sr.Grd',50000,'001


');
Query OK, 1 row affected (0.06 sec)

MariaDB [mydb]> insert into employee values('101','Jai','AP',45000,'001');


Query OK, 1 row affected (0.10 sec)

MariaDB [mydb]> insert into employee values('102','Yash','AP',55000,'001');


Query OK, 1 row affected (0.06 sec)

MariaDB [mydb]> insert into employee values('103','Nitin','AP',54000,'002');


Query OK, 1 row affected (0.05 sec)
MariaDB [mydb]> insert into employee values('104','Nitin','Lecture',25000,'003')
;
Query OK, 1 row affected (0.06 sec)

MariaDB [mydb]> insert into employee values('105','Prakash','AP',65000,'004');


Query OK, 1 row affected (0.05 sec)

MariaDB [mydb]> select * from employee;


+-------+---------+-------------+--------+--------+
| empno | ename | designation | salary | deptno |
+-------+---------+-------------+--------+--------+
| 100 | Vikas | AP-Sr.Grd | 50000 | 001 |
| 101 | Jai | AP | 45000 | 001 |
| 102 | Yash | AP | 55000 | 001 |
| 103 | Nitin | AP | 54000 | 002 |
| 104 | Nitin | Lecture | 25000 | 003 |
| 105 | Prakash | AP | 65000 | 004 |
| 110 | Yogi | NULL | 110000 | NULL |
+-------+---------+-------------+--------+--------+
7 rows in set (0.00 sec)

Create a view
MariaDB [mydb]> create view staff as(select empno,ename from employee);
Query OK, 0 rows affected (0.04 sec)

MariaDB [mydb]> select * from staff;


+-------+---------+
| empno | ename |
+-------+---------+
| 100 | Vikas |
| 101 | Jai |
| 102 | Yash |
| 103 | Nitin |
| 104 | Nitin |
| 105 | Prakash |
| 110 | Yogi |
+-------+---------+
7 rows in set (0.00 sec)

MariaDB [mydb]> update staff set ename='Jayant' where empno=100;

Query OK, 1 row affected (0.06 sec)


Rows matched: 1 Changed: 1 Warnings: 0

MariaDB [mydb]> select * from staff;


+-------+---------+
| empno | ename |
+-------+---------+
| 100 | Jayant |
| 101 | Jai |
| 102 | Yash |
| 103 | Nitin |
| 104 | Nitin |
| 105 | Prakash |
| 110 | Yogi |
+-------+---------+
7 rows in set (0.00 sec)

MariaDB [mydb]> select * from employee;


+-------+---------+-------------+--------+--------+
| empno | ename | designation | salary | deptno |
+-------+---------+-------------+--------+--------+
| 100 | Jayant | AP-Sr.Grd | 50000 | 001 |
| 101 | Jai | AP | 45000 | 001 |
| 102 | Yash | AP | 55000 | 001 |
| 103 | Nitin | AP | 54000 | 002 |
| 104 | Nitin | Lecture | 25000 | 003 |
| 105 | Prakash | AP | 65000 | 004 |
| 110 | Yogi | NULL | 110000 | NULL |
+-------+---------+-------------+--------+--------+
7 rows in set (0.00 sec)

Delete the data


MariaDB [mydb]> delete from staff where ename='yogi';
Query OK, 1 row affected (0.09 sec)

MariaDB [mydb]> select * from staff;


+-------+---------+
| empno | ename |
+-------+---------+
| 100 | Jayant |
| 101 | Jai |
| 102 | Yash |
| 103 | Nitin |
| 104 | Nitin |
| 105 | Prakash |
+-------+---------+
6 rows in set (0.00 sec)

MariaDB [mydb]> select * from employee;


+-------+---------+-------------+--------+--------+
| empno | ename | designation | salary | deptno |
+-------+---------+-------------+--------+--------+
| 100 | Jayant | AP-Sr.Grd | 50000 | 001 |
| 101 | Jai | AP | 45000 | 001 |
| 102 | Yash | AP | 55000 | 001 |
| 103 | Nitin | AP | 54000 | 002 |
| 104 | Nitin | Lecture | 25000 | 003 |
| 105 | Prakash | AP | 65000 | 004 |
+-------+---------+-------------+--------+--------+
6 rows in set (0.00 sec)
EXPERIMENT- 8

CONTROL STRUCTURE
AIM:-
To create PL/SQL programs to implement various types of control structure.
.
SYNTAX AND DEFINITION:-
CONTROL STRUCTRE:
PL/SQL can also process data using flow of statements. The flow of control
statements are classified into the following categories.
● Conditional control –Branching
● Iterative control – looping
● Sequential control - Selection
BRANCHING in PL/SQL:
Sequence of statements can be executed on satisfying certain condition. If
statements are being used and different forms of if are:
1. Simple IF 2. If then else 3. Else if 4. Nested if
SELECTION IN PL/SQL (Sequential Controls)
1. Simple case 2. Searched case
ITERATIONS IN PL/SQL
Sequence of statements can be executed any number of times using loop
construct. It is broadly classified into:
1. Simple Loop 2. For Loop 3. While Loop
SIMPLE IF:
Syntax:
IF condition
THEN statement1;
statement2;
END IF;
IF-THEN-ELSE STATEMENT:
Syntax:
IF condition
THEN statement1;
ELSE
statement
2; END
IF;
ELSIF STATEMENTS:
Syntax:
IF condition1
THEN statement1;
ELSIF condition2 THEN
statement2;
ELSIF condition3 THEN
statement3;
ELSE
statement;
END IF;
NESTED IF:
Syntax:
IF condition THEN
statement1;
ELSE
IF condition THEN
statement2;
ELSE
statement3;
END IF; END
IF; ELSE
statement3;
END IF;

SELECTION IN PL/SQL (Sequential Controls)


SIMPLE CASE
Syntax:
CASE SELECTOR
WHEN Expr1 THEN statement1;
WHEN Expr2 THEN statement2;
: ELSE
Statement n;
END CASE;
SEARCHED CASE:
Syntax:
CASE
WHEN searchcondition1 THEN statement1;
WHEN searchcondition2 THEN statement2;
:: ELSE
statementn;
END CASE;
ITERATIONS IN PL/SQL
SIMPLE LOOP
Syntax:
LOOP
statement1;
EXIT [ WHEN Condition];
END LOOP;
Example:
Declare
A number:=10; Begin
Loop
a := a+25;
exit when a=250; end loop;
dbms_output.put_line(to_char(a)); end;

WHILE LOOP
Syntax
WHILE condition LOOP
statement1;
statement2;
END LOOP;
Example:
Declare
i number:=0; j
number:=0;
begin
while i<=100 Loop j
:= j+i;
i := i+2;
end loop;
dbms_output.put_line(‘the value of j is’ ||j);
end;
/
FOR LOOP
Syntax:
FOR counter IN [REVERSE]
LowerBound..UpperBound
LOOP
statement1;
statement2;
END LOOP;
Example:
Begin
For I in 1..2
Loop
Update emp set field = value where
condition; End loop;
End;
/
Q1: write a pl/sql program to swap two numbers
b) Procedure for doing the experiment:

Step
Details of the step
no.
1 Declare three variables and read variables through a and b
2 Swap the values of a and b using temporary variables
3 Display the swapped results

c) Program:
SQL>edit swapping.sql
declare
a number(10);
b number(10);
c number(10);
begin
dbms_output.put_line('THE PREV VALUES OF A AND B WERE');
dbms_output.put_line(a);
dbms_output.put_line(b);
a:=&a;
b:=&b;
c:=a;
a:=b;
b:=c;
dbms_output.put_line('THE VALUES OF A AND B ARE');
dbms_output.put_line(a);
dbms_output.put_line(b);
end;
e)output:
SQL> @ swapping.sql
19 /
Enter value for a: 5
old 6: a:=&a;
new 6: a:=5;
Enter value for b: 3
old 7: b:=&b;
new 7: b:=3;
THE PREV VALUES OF A AND B WERE
53
THE VALUES OF A AND B ARE
35
PL/SQL procedure successfully completed.
Q2: Write a pl/sql program to find the largest of three numbers
c) Procedure for doing the experiment:
Step
Details of the step
no.
1 Read three numbers through a, b & c
2 Find the biggest among three using nested if statement
3 Display the biggest no as result
d) Program:
SQL>set server output on;
SQL>edit biggest.sql
declare
a number; b
number; c
number;
begin
a:=&a;
b:=&b;
c:=&c;
if a>b then if
a>c then
dbms_output.put_line ('biggest is:' ||to_char(a));
else
dbms_output.put_line('biggest is :' ||to_char(c));
end if;
elsif b>c then
dbms_output.put_line('biggest is :' ||to_char(b));
else
dbms_output.put_line('biggest is :' ||to_char(c));
end if;
end;
e) output:
SQL>@biggest.sql
/
Enter value for a: 5
old 6: a:=&a;
new 6: a:=5;
Enter value for b: 5
old 6: b:=&b;
new 6: b:=8;
Enter value for c: 8
old 6: c:=&c;
new 6: c:=4;
biggest is : 8
Q3: write a pl/sql program to find the total and average of 6 subjects and
display the grade
c) Procedure for doing the experiment:
Step
Details of the step
no.
1 Read six numbers and calculate total and average
2 Find whether the student is pass or fail using if statement
3 Find the grade using nested elseif statement
4 Display the Grade, Percentage and Total of the student
d) Program:

SQL> edit grade.sql


declare
java number(10);
dbms number(10);
co number(10);
se number(10); es
number(10); ppl
number(10); total
number(10); avgs
number(10); per
number(10); begin
dbms_output.put_line('ENTER THE MARKS');
java:=&java;
dbms:=&dbms;
co:=&co;
se:=&se;
es:=&es;
ppl:=&ppl;
total:=(java+dbms+co+se+es+ppl);
per:=(total/600)*100;
if java<50 or dbms<50 or co<50 or se<50 or es<50 or ppl<50 then
dbms_output.put_line('FAIL');
if per>75 then
dbms_output.put_line('GRADE A');
elsif per>65 and per<75 then
dbms_output.put_line('GRADE B');
elsif per>55 and per<65 then
dbms_output.put_line('GRADE C');
else
dbms_output.put_line('INVALID INPUT');
end if;
dbms_output.put_line('PERCENTAGE IS '||per);
dbms_output.put_line('TOTAL IS '||total);
end;

e) output:
SQL> @ grade.sql 31
/

Enter value for java: 80 old


12: java:=&java; new 12:
java:=80;
Enter value for dbms: 70
old 13: dbms:=&dbms;
new 13: dbms:=70; Enter
value for co: 89 old 14:
co:=&co;
new 14: co:=89; Enter
value for se: 72 old 15:
se:=&se;
new 15: se:=72; Enter
value for es: 76 old 16:
es:=&es;
new 16: es:=76;
Enter value for ppl: 71 old
17: ppl:=&ppl;
new 17: ppl:=71; GRADE
A PERCENTAGE IS 76
TOTAL IS 458
PL/SQL procedure successfully completed.
EXPERIMENT- 9

PROCEDURE AND FUNCTION


AIM:-
To study and execute the procedure and functions in PL/SQL.

PROCEDURE:-
1. Goto Start and click on programs.
2. Point to Oracle, then Ora81 and click on SQLPlus.
3. Then a pop-up menu will appear to authenticate you as user. Type 'scott' as
username, 'tiger' as password and 'orcl' as the host.
4. Now SQLPlus window will get opened for working with various SQL commands
in it.

SYNTAX AND DEFINITION:-


1. PROCEDURES:
A procedure is a subprogram that performs a specific action.

SYNTAX:

CREATE OR REPLACE PROCEDURE <proc_name>[parameter list] IS


[local declarations];
BEGIN
(executable statements)
[exception] (exception handlers)
END;

A procedure has two parts namely, specification and body. The procedure
specification begins with keyword procedure and ends with procedure name or parameter list. The
procedure body begins with keyword is and ends with the keyword end. It can also include declarative,
executable and exceptional parts with in the keywords are and end. Syntax to execute a procedure is given
below :

EXECUTE proc_name[parameter list];

2. FUNCTIONS:
A function is a subprogram that computes a value.

SYNTAX:
CREATE OR REPLACE FUNCTION<function_name> [argument]
RETURN datatype IS
(local declaration)
BEGIN
(executable statements)
[exception]
(exception handlers)
END;

Similar to a procedure, a function also has two parts namely, the function specification and the function
body. The function specification begins with the keyword function and ends with the return clause. The
function body begins with the keyword is and ends with the keyword end. A PL/SQL block can also be
included in a function body.

QUERIES:-
1. CREATE OR REPLACE PROCEDURE fibonacci(n NUMBER) IS
a NUMBER;
b NUMBER;
i NUMBER;
c NUMBER;
BEGIN
a := 0;
b := 1;
dbms_output.put_line(a);
dbms_output.put_line(b);
FOR i IN 3..n
LOOP
c := a+b;
dbms_output.put_line(c);
a := b;
b := c;
END LOOP;
END;
/

EXECUTE fibonacci(10);

2. CREATE OR REPLACE FUNCTION fibo(n Number)


RETURN NUMBER IS
a NUMBER;
b NUMBER;
i NUMBER;
c NUMBER;
BEGIN
a := 0;
b := 1;
dbms_output.put_line(a);
dbms_output.put_line(b);
FOR i IN 3..n
LOOP
c := a+b;
dbms_output.put_line(c);
a := b;
b := c;
END LOOP;
RETURN c;
END;
/

DECLARE
n NUMBER;
c NUMBER;
BEGIN
dbms_output.put_line('Enter the number of terms for fibonacci series : ');
n := &n;
c := fibo(n);
dbms_output.put_line('The last number in series is : '||c);
END;
/
OUTPUT:-
Procedure :
Function :
EXPERIMENT- 10

Triggers

AIM:-
To study and execute triggers in RDBMS.

SYNTAX AND DEFINITION:-


A database trigger is stored procedure that is fired when an insert, update or delete statement is issued
against the associated table. Database triggers can be used for the following purposes.
● To generate data automatically.
● To enforce complex integrity constraints.
● To customize complex security authorizations.
● To maintain replicate tables.
● To audit data modifications.

The triggers are classified into the following types based on when they are fired:
● Before
● After
● For each row
● For each statement (default)

Where,
CREATE [OR REPLACE] TRIGGER trigger_name : Creates or replace an existing trigger with the
trigger_name.

{BEFORE | AFTER | INSTEAD OF} : This specifies when the trigger would be executed. The
INSTEAD OF clause is used for creating trigger on a view.

{INSERT [OR] | UPDATE [OR] | DELETE}: This specifies the DML operation.

[OF col_name]: This specifies the column name that would be updated.

[ON table_name]: This specifies the name of the table associated with the trigger.

[REFERENCING OLD AS o NEW AS n]: This allows you to refer new and old values for various DML
statements, like INSERT, UPDATE, and DELETE.

[FOR EACH ROW]: This specifies a row level trigger, i.e., the trigger would be executed for each row
being affected. Otherwise the trigger will execute just once when the SQL statement is executed, which is
called a table level trigger.

WHEN (condition): This provides a condition for rows for which the trigger would fire. This clause is
valid only for row level triggers.

Syntax for creating triggers:


Create or Replace trigger<trigger name>
[before/after][insert/update/delete] on <table_name>
[for each statement/for each row]
[when <condition>]

QUERIES:-
1. SELECT * FROM production;

2. SELECT * FROM product;

3. CREATE TRIGGER productadd AFTER


INSERT ON product FOR EACH ROW
BEGIN
UPDATE production SET unitprice=0 WHERE prodid=1;
END;
/
4. CREATE TRIGGER productupd BEFORE
UPDATE ON product
FOR EACH ROW
BEGIN
DELETE FROM production WHERE supplier = ‘apple’;
END;
/
5. CREATE TRIGGER productdel AFTER
DELETE ON product FOR EACH ROW
BEGIN
INSERT INTO production VALUES (10,’iPhone’,’apple’,45500.00);
END;
/

6. INSERT INTO product VALUES (5,’trigger’,’afterinsert’,0);

7. SELECT * FROM production;

8. UPDATE product SET unitprice = 55000.00 WHERE prodid = 1;

9. SELECT * FROM production;

10. DELETE FROM product WHERE prodid = 1;

11. SELECT * FROM production;

12. CREATE TRIGGER productiodel BEFORE


DELETE ON production FOR EACH ROW
BEGIN
Dbms_output.put_line(‘One row deleted!!’);
END;
/

13. DELETE FROM production WHERE prodid = 1;

14. CREATE TRIGGER productioninsert


AFTER INSERT ON production FOR EACH ROW
BEGIN
dbms_output.put_line (‘The inserted information is’);
dbms_output.put_line (‘PRODUCT ID – ‘|| : new.prodid);
END;
/

15. INSERT INTO production VALUES (1,’xps laptop’,’dell’,0);

16. DROP TRIGGER productadd;

OUTPUT:-

You might also like