Professional Documents
Culture Documents
PRACTICAL FILE
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
AIM:-
To implement the DDL commands in RDBMS.
QUERIES:-
1. CREATE table HARSH73
(
name CHAR(20),
regn NUMBER(10),
dob DATE,
class CHAR(10)
);
DESC HARSH73;
OUTPUT:-
EXPERIMENT-2
AIM:-
To implement the DML commands in RDBMS.
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’);
3. UPDATE HARSH73
SET class = 'CSE B'
WHERE name = ‘JATIN’;
OUTPUT:-
DML output:
EXPERIMENT-3
AIM:-
To implement the DCL commands in RDBMS.
DCL queries:
1. GRANT SELECT, UPDATE ON HARSH73 TO sys;
DCL output:
EXPERIMENT-4
Relational Algebra
AIM:-
To implement the Relational Algebra operations in RDBMS.
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
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;
QUERIES:-
OUTPUT:-
EXPERIMENT- 5
Functions
AIM:-
To implement aggregate functions in RDBMS.
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;
OUTPUT:-
EXPERIMENT- 6
Nested Queries
AIM:-
To implement the Nested Queries in RDBMS.
QUERIES:-
SELECT * FROM HARSH73;
SELECT * FROM HARSH273;
3. UPDATE HARSH273
SET regn=regn+5
WHERE name
IN ( SELECT name FROM HARSH73);
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:
where <query expression> is any legal query expression. The view name is represented by v.
Now, you can query buyer in a similar way as you query an actual table.
+----------+------+
| 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.
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]>
MariaDB [mydb]> create table employee(empno varchar(5) primary key, ename char(20) not
null, designation varchar(20),salary int, deptno varchar(5));
Create a view
MariaDB [mydb]> create view staff as(select empno,ename from employee);
Query OK, 0 rows affected (0.04 sec)
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;
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:
e) output:
SQL> @ grade.sql 31
/
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:
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 :
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);
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.
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.
QUERIES:-
1. SELECT * FROM production;
OUTPUT:-