Lab Record
of
Advanced DBMS
(CS441)
Submitted to: Submitted by:
Mr. Deep Kumar Aditya Mishra
CSE-B
180102114
Session 2021-22
Index
[Link] Title of Experiment/Objective Date of Signature of
Conduction Faculty
1 Use of DDL, DML and DCL commands for table creation in database. 12.08.2021
2 Use of where, rename, tuple variables and string operations on tables. 19.08.2021
3 Use of nested queries, set operations and join operations on tables. 26.08.2021
4 Use of PL/SQL variables, reserved words, identifiers and anchored data 02.09.2021
types.
5 Use of block, nested blocks and labels in PL/SQL 09.09.2021
6 Use of DML and SAVEPOINT in PL/SQL 16.09.2021
7 Use of IF, ELSEIF, NESTED IF and Case statement in PL/SQL 23.09.2021
8 Use of Error handling and Build-in-Exceptions in PL/SQL 30.09.2021
9 Use a cursor for loop and process nested cursors in PL/SQL 07.10.2021
10 Use of Procedures and Functions in PL/SQL 14.10.2021
11 Use of row and statement triggers in PL/SQL 14.10.2021
Experiment – 1
Objective:Use of DDL, DML and DCL commands for table creation in database.
Ouput:
Experiment – 2
Objective:Use of where, rename, tuple variables and string operations on tables.
Where command
Tuple variations
Rename command
String operations
Experiment – 3
Objective:Use of nested queries, set operations and join operations on tables.
Nested queries :
Employee1 Employee2
Query : select name, city from employee1 where name in (select name from employee4 where
designation = 'shareholder');
Set operations :
First second
Query1 (union): select * from first union select * from second;
Query2 (union all): select * from first union all select * from second;
Query3 (intersect): select * from first intersect select * from
second;
Query4 (minus): select * from first minus select * from second;
Join operations :
supplier_info
Order_info
Query1 : select supplier_info.supplier_id, supplier_info.supplier_name, order_info.city,
order_info.quantity from supplier_infofull outer join order_info on
supplier_info.supplier_id=order_info.supplier_id
Query2 : select supplier_info.supplier_id, supplier_info.supplier_name, order_info.city,
order_info.quantity from supplier_inforight outer join order_info on
supplier_info.supplier_id=order_info.supplier_id
Query3 : select supplier_info.supplier_id, supplier_info.supplier_name, order_info.city,
order_info.quantity from supplier_infoleft outer join order_info on
supplier_info.supplier_id=order_info.supplier_id
Experiment – 4
Objective:Use of PL/SQL variables, reserved words, identifiers and anchored data types.
PL/SQL Variables: A variable is a meaningful name which facilitates a programmer to store
data temporarily during the execution of code. It helps you to manipulate data in PL/SQL
programs. It is nothing except a name given to a storage area. Each variable in the PL/SQL
has a specific data type which defines the size and layout of the variable's memory.
A variable should not exceed 30 characters. Its letter optionally followed by more letters,
dollar signs, numerals, underscore etc.
1. It needs to declare the variable first in the declaration section of a PL/SQL block
before using it.
2. By default, variable names are not case sensitive. A reserved PL/SQL keyword cannot
be used as a variable name.
The PL/SQL Identifiers
PL/SQL identifiers are constants, variables, exceptions, procedures, cursors, and reserved
words. The identifiers consist of a letter optionally followed by more letters, numerals, dollar
signs, underscores, and number signs and should not exceed 30 characters.
By default, identifiers are not case-sensitive. So, you can use integer or INTEGER to
represent a numeric value. You cannot use a reserved keyword as an identifier.
Anchor data types: use of the %TYPE declaration attribute to anchor the datatype of one
variable to another data structure, such as a PL/SQL variable or a column in a table. When
you anchor a datatype, you tell PL/SQL to set the datatype of one variable from the datatype
of another element.
The %TYPE attribute lets you declare a constant, variable, field, or parameter to be of the
same data type a previously declared variable, field, record, nested table, or database column.
If the referenced item changes, your declaration is automatically updated.
An item declared with %TYPE (the referencing item) always inherits the data type of the
referenced item. The referencing item inherits the constraints only if the referenced item is
not a database column. The referencing item inherits the default value only if the referencing
item is not a database column and does not have the NOT NULL constraint.
Experiment – 5
Objective:Use of block, nested blocks and labels
in PL/SQL.
Use of Block:
Running a simple block of command.
Use of Nested Block:
Labels in PL/SQL
Experiment – 7
Objective:Use of IF, ELSEIF, NESTED IF and Case statement in PL/SQL
IF THEN :-
DECLARE
a number(2) := 10;
BEGIN
IF(a < 20) THEN dbms_output.put_line (‘Value of a
is less than 20’);
END IF;
dbms_output.put_line (‘Value of a is : ‘ || a’);
END:
ELSEIF :-
DECLARE
a number (3) := 100;
BEGIN
IF(a = 10) THEN
dbms_output.put_line (‘Value of a is 10 ’);
ELSEIF (a = 20) THEN
dbms_output.put_line(‘Value of a is 20’);
ELSEIF (a = 30) THEN
dbms_output.put_line (‘Value of a
is 30 ’);
ELSE
dbms_output.put_line (‘None of the values is matching ’); END
IF;
dbms_output.put_line (‘Exact value of a is : ‘ || a’);
END;
NESTED IF:-
DECLARE
a number(3) := 100; b
number(3) := 200;
BEGIN
IF(a = 100) THEN
IF(b = 200) THEN dbms_output.put_line(‘Value of a is
100 and b is 200’);
END IF;
END IF;
dbms_output.put_line(‘Exact value of a is : ‘ || a);
dbms_output.put_line(‘Exact value of b is : ‘ || b);
END;
CASE STATMENT :-
DECLARE
grade char(1) := ‘A’;
BEGIN
CASE grade
when ‘A’ then dbms_output.put_line(‘Excellent’); when
‘B’ then dbms_output.put_line(‘Very good’); when ‘C’
then dbms_output.put_line(‘Well done’); when ‘D’ then
dbms_output.put_line(‘You passed’); when ‘E’ then
dbms_output.put_line(‘Better try again ’); when ‘F’ then
dbms_output.put_line(‘No such grade’);
END CASE;
END;
Experiment – 9
Objective:Use a cursor for loop and process nested cursors in PL/SQL.
Create table Employee(EID NUMBER,ENAME VARCHAR2(30),AGE
NUMBER,ADDRESS VARCHAR2(30),SALARY NUMBER)
INSERT INTO
Employee(EID,ENAME,AGE,ADDRESS,SALARY)VALUES(1,'SHUBHAM',21,'HARID
WAR',30000);
INSERT INTO
Employee(EID,ENAME,AGE,ADDRESS,SALARY)VALUES(2,'KUNAL',21,'JAIPUR',350
00);
INSERT INTO
Employee(EID,ENAME,AGE,ADDRESS,SALARY)VALUES(3,'SHRESTH',21,'MEERUT'
,20000);
INSERT INTO
Employee(EID,ENAME,AGE,ADDRESS,SALARY)VALUES(4,'RAKSHIT',22,'DEHRAD
UN',30000);
INSERT INTO
Employee(EID,ENAME,AGE,ADDRESS,SALARY)VALUES(5,'SHUBHANG',22,'RISHIK
ESH',25000);
SELECT * FROM Employee;
-- IMPLICIT CURSOR DECLARE
total_rows number(2);
BEGIN
UPDATE Employee
SET SALARY = SALARY + 5000;
IF sql%notfound THEN
dbms_output.put_line('No EMPLOYEE Updated');
ELSIF sql%found THEN total_rows := sql%rowcount;
dbms_output.put_line( total_rows || ' Employee Updated ');
END IF;
END;
/
-- EXPLICIT CURSOR
DECLARE
E_ID [Link]%type;
E_NAME [Link]%type;
E_ADDR [Link]%type;
CURSOR E_Employee is
SELECT EID, ENAME, ADDRESS FROM Employee;
BEGIN
OPEN E_Employee;
LOOP
FETCH E_Employee into E_ID, E_NAME, E_ADDR;
EXIT WHEN E_Employee%notfound;
dbms_output.put_line(E_ID || ' ' || E_NAME || ' ' || E_ADDR);
END LOOP;
CLOSE E_Employee;
END;
/
Experiment –10
Objective:Use of Procedures and Functions in PL/SQL.
create table Student_details(student_id number(5), student_namevarchar(50), student_cityvarchar(50),
marks number(3));
Insert All
into Student_details values(1, 'Abhinav', 'Delhi', 75)
into Student_details values(2, 'Anurag', 'Dehradun',
85) into Student_details values(3, 'Anshuman',
'Shimla', 95) into Student_details values(4, 'Anirudh',
'Noida', 69) into Student_details values(5, 'Abhay',
'Gurugram', 78)
Select * from dual;
Create or replace function getNumberOfStudents
return number is total number(10) := 0;
Begin
Select count(*) into total from Student_details;
return total;
End;
create or replace procedure find_name(i_id in number, o_name out varchar2, o_city out varchar2) As Begin
Select student_name, student_city into o_name, o_city from Student_details
where student_id = i_id;
Exception when others then
dbms_output.put_line('No such record with student_id ' || i_id || '
exist'); End find_name;
Declare
numberOfStudents number(10) := 0;
o_namevarchar(50);
o_cityvarchar(50);
Begin
numberOfStudents := getNumberOfStudents();
dbms_output.put_line('There are ' || numberOfStudents || ' records in Student Schema');
find_name(1, o_name, o_city);
dbms_output.put_line('Name: ' || o_name);
dbms_output.put_line('City: ' || o_city);
End;
Experiment 11
OBJECTIVE: Use of row and statement trigger in PL/SQL.
We have this customer table on which we will be creating row and statement trigger.
CREATE OR REPLACE TRIGGER display_salary_changes
BEFORE DELETE OR INSERT OR UPDATE ON customers
FOR EACH ROW
WHEN ([Link] > 0)
DECLARE
Sal_diff number;
BEGIN
Sal_diff := :[Link] - :[Link]; dbms_output.put_line
(‘Old salary : ‘ || :[Link] ’); dbms_output.put_line (‘New
salary : ‘ || : [Link]’); dbms_output.put_line (‘Salary
difference : ‘ || sal_diff ’);
END;
Query
INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY)
VALUES (7, 'Kriti', 22, 'HP', 7500.00 );
Query
UPDATE customers
SET salary = salary + 500
WHERE id = 2;