Assignment - 2: Nuzaiff Ather 27181 Bscs - 4 C

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 9

ASSIGNMENT – 2

Data Base Management System

NUZAIFF ATHER
27181
BSCS – 4 C

SUBMITTED TO:

SIR TANVEER KHAN


Q.) What is the difference between Implicit and Explicit Cursors? Explain it with
PL/SQL examples using %found, %notfound and %rowcount cursor attributes.

Ans) PL/SQL utilizes verifiable and unequivocal cursors. PL/SQL pronounces a


cursor certainly for all SQL information control proclamations, including questions
that arrival one and only line. Understood cursors are called SQL cursors. In the
event that you need exact control over question preparing, you can pronounce an
express cursor in the decisive part of any PL/SQL piece, subprogram, or bundle.
You should proclaim express cursors for questions that arrival more than one
column.

Implicit Cursors:

SQL cursors are managed automatically by PL/SQL. You need not write code to
handle these cursors. However, you can track information about the execution of
an SQL cursor through its attributes.

Using SQL%FOUND

CREATE TABLE dept_temp AS SELECT * FROM departments;

DECLARE

dept_no NUMBER(4) := 270;

BEGIN

DELETE FROM dept_temp WHERE department_id = dept_no;

IF SQL%FOUND THEN -- delete succeeded

INSERT INTO dept_temp VALUES (270, 'Personnel', 200, 1700);

END IF;

END;

/
Using SQL%ROWCOUNT

CREATE TABLE employees_temp AS SELECT * FROM employees;

DECLARE

mgr_no NUMBER(6) := 122;

BEGIN

DELETE FROM employees_temp WHERE manager_id = mgr_no;

DBMS_OUTPUT.PUT_LINE

('Number of employees deleted: ' || TO_CHAR(SQL%ROWCOUNT));

END;

Using SQL%NOTFOUND

The %NOTFOUND attribute is not useful in combination with


the SELECT INTO statement:

A SELECT INTO statement that invokes a SQL aggregate function always returns


a value or a null. After such a statement, the %NOTFOUND attribute is
always FALSE, so checking it is unnecessary.
EXPLICIT CURSOR:

When you require exact control over question handling, you can expressly announce a
cursor in the revelatory part of any PL/SQL piece, subprogram, or bundle.

Using SQL%FOUND

DECLARE

CURSOR c1 IS SELECT last_name, salary FROM employees WHERE ROWNUM < 11;

my_ename employees.last_name%TYPE;

my_salary employees.salary%TYPE;

BEGIN

OPEN c1;

LOOP

FETCH c1 INTO my_ename, my_salary;

IF c1%FOUND THEN -- fetch succeeded

DBMS_OUTPUT.PUT_LINE('Name = ' || my_ename || ', salary = ' ||


my_salary);

ELSE -- fetch failed, so exit loop

EXIT;

END IF;

END LOOP;

END;

If a cursor or cursor variable is not open, referencing it with %FOUND raises the


predefined exception INVALID_CURSOR.
Using SQL%ROWCOUNT

Declare

CURSOR c1 IS SELECT last_name FROM employees WHERE ROWNUM < 11;

name employees.last_name%TYPE;

BEGIN

OPEN c1;

LOOP

FETCH c1 INTO name;

EXIT WHEN c1%NOTFOUND OR c1%NOTFOUND IS NULL;

DBMS_OUTPUT.PUT_LINE(c1%ROWCOUNT || '. ' || name);

IF c1%ROWCOUNT = 5 THEN

DBMS_OUTPUT.PUT_LINE('--- Fetched 5th record ---');

END IF;

END LOOP;

CLOSE c1;

END;

If a cursor or cursor variable is not open, referencing it


with %ROWCOUNT raises INVALID_CURSOR.

Using %NOTFOUND

DECLARE

CURSOR c1 IS SELECT last_name, salary

FROM employees

WHERE ROWNUM < 11;

my_ename employees.last_name%TYPE;

my_salary employees.salary%TYPE;
BEGIN

OPEN c1;

LOOP

FETCH c1 INTO my_ename, my_salary;

IF c1%NOTFOUND THEN -- fetch failed, so exit loop

-- Another form of this test is

-- "EXIT WHEN c1%NOTFOUND OR c1%NOTFOUND IS NULL;"

EXIT;

ELSE -- fetch succeeded

DBMS_OUTPUT.PUT_LINE

('Name = ' || my_ename || ', salary = ' || my_salary);

END IF;

END LOOP;

END;

Before the first fetch, %NOTFOUND returns NULL. If FETCH never executes successfully,


the loop is never exited, because theEXIT WHEN statement executes only if
its WHEN condition is true. To be safe, you might want to use the
following EXITstatement instead:

EXIT WHEN c1%NOTFOUND OR c1%NOTFOUND IS NULL;

If a cursor or cursor variable is not open, referencing it with %NOTFOUND raises


an INVALID_CURSOR exception.
Q.2) Create a trigger which restricts the user from any DML operation on EMP table
under the following conditions:

a. User cannot perform any DML operation if it is SATURDAY or SUNDAY.

b. User can only perform DML operations between 9:00AM and 5:00PM .
Q) What is the difference between views and materialized views? Explain with SQL
exmaples.

VIEWS:

View is a logical or virtual memory which is based on select query and the simple view is
the view in which we cannot make DML command if the View is created by multiple
tables.

CREATE VIEW CUSTOMERS_VIEW AS

SELECT name, age

FROM CUSTOMERS;

Now, you can query CUSTOMERS_VIEW in similar way as you query an actual table. Following is the
example:

SQL > SELECT * FROM CUSTOMERS_VIEW;

This would produce the following result:

+----------+-----+

| name | age |

+----------+-----+

| Ramesh | 32 |

| Khilan | 25 |

| kaushik | 23 |

| Chaitali | 25 |

| Hardik | 27 |

| Komal | 22 |

| Muffy | 24 |

+----------+-----+

Materialized VIEWS:
Materialized view is a concept mainly used in Detaware housing. These views contain
the data itself .Reason being it is easier/faster to access the data. The main purpose of
Materialized view is to do calculations and display data from multiple tables using Joins .
CREATE MATERIALIZED VIEW MV_Test
NOLOGGING
CACHE
BUILD IMMEDIATE
REFRESH FAST ON COMMIT
AS
SELECT V.*, P.*, V.ROWID as V_ROWID, P.ROWID as P_ROWID, (2+1) as Calc1,
'Constant1' as Const1
FROM TPM_PROJECTVERSION V
INNER JOIN TPM_PROJECT P ON P.PROJECTID = V.PROJECTID

Now instead of running this same query everytime you can just run this simpler query against your
new view which will run faster. The really cool thing is that you can also add derived and calculated
columns too.

SELECT * FROM MV_Test WHERE ...

You might also like