You are on page 1of 9

ASSIGNMENT OF

DBMS

Submitted to: Submitted by:

Ms. Pency Juneja Rohit Nagpal


Roll no.
B44
Part A

Q1 - Explain explicit cursor handling with diagram?

ANS: When in dividend records in a table have to be processed in a side a PL/SQL code block
cursor icon used. This cursor will be declared and mapped to in SQL query in the declare section
at the PL/SQL block and used within the executable section. A curser thus created and used is
known as explicit cursor.

CURSOR DECLERATION:-

SYNTAX: Cursor cursor_name IS SQL SELECT statement;

OPENING A CURSOR

SYNTAX: Open cursor_name;

FETCHING RECORD FORM A CURSOR

SYNTAX: Fetch cursor_name INTO var1;

CLOSING A CURSOR

SYNTAX: Close cursor_name;

DIAGRAM:
Q2- Difference between procedure and functions?

ANS: A Function can be used in the Sql Queries while a procedure cannot be used in sql queries
that cause a major difference b/w function and procedures.

A Function can return a single value using return statement whereas a Procedure cannot return
using return statement but can return using parameters i.e. a procedure can return one or more
values. A Function can be called inside the select statement whereas the Procedures cannot call
inside the select statement procedures are standalone.

Q3- Use of database triggers?

ANS: - A trigger is used to ensure that certain jobs are automatically done when a predefined
event occurs. Both the event and the resultant action typically happen within the database.

There are four types of database triggers:

• Table-level triggers can initiate activity before or after an INSERT, UPDATE, or DELETE event.

• View-level triggers define what can be done to the view.

• Database-level triggers can be activated at startup and shutdown of a database.

• Session-level triggers can be used to store specific information

To design a trigger mechanism, we must:

• Specify the conditions under which the trigger is to be executed.

• Specify the actions to be taken when the trigger executes.

Triggers are commonly used to:

• Automatically generate derived column values.

• Prevent invalid transactions.

• Enforce complex security authorizations.

• Enforce referential integrity across nodes in a distributed database.


• Enforce complex business rules.

• Provide transparent event logging.

• Provide sophisticated auditing.

• Maintain synchronous table replicates.

• Gather statistics on table access.

Part B

Q4 -Explain syntax with example for loops, while loop?

ANS-: A while loop is a control flow statement that allows code to be executed repeatedly based
on a given Boolean condition. The while loop can be thought of as a repeating if statement.

The while construct consists of a block of code and a condition. The condition is evaluated, and
if the condition is true, the code within the block is executed. This repeats until the condition
becomes false. Because while loops check the condition before the block is executed, the control
structure is often also known as a pre-test loop. Compare with the do while loop, which tests the
condition after the loop has executed.

Syntax for the WHILE Loop is:

WHILE <condition>
LOOP<action>
END LOOP;

You would use a WHILE Loop when you are not sure how many times you will execute the loop
body. Since the WHILE condition is evaluated before entering the loop, it is possible that the
loop body may not execute even once.

EXAMPLE:-
WHILE monthly_value <= 50000
LOOP
monthly_value:= daily_value * 31;
END LOOP;

In this example, the WHILE Loop would terminate once the monthly_value exceeded 50000.

For loop is a programming language statement which allows code to be repeatedly executed A
For loop is classified as an iteration statement.

Unlike many other kinds of loops, such as the while loop, For loop is often distinguished by an
explicit loop counter or loop variable. This allows the body of the For loop to know about the
sequencing of each iteration. For loops are also typically used when the number of iterations is
known before entering the loop.

Syntax for the FOR Loop is:

For variable IN [REVERSE]

Start………….end

LOOP
{action}
END LOOP;

You would use a FOR Loop when you want to fetch and process every record in a cursor. The
FOR Loop will terminate when all of the records in the cursor have been fetched.

EXAMPLE:-

Write a PL/SQL code for inverting a no. & the no. is 5639 to 9365.

Declare
given_ number varchar 2(5):=(‘5639’);
str_length number (2);
invented _ number varchar 2 (5);
Begin
str_length:=length (given_number);
for enter IN REVERSE 1..str_length

LOOP Inverted_number:=inverted_number|| substr (given_number, enter 1);

END LOOP;

DBMS_output.put_line (‘the given_number is’||given_number);


DBMS_output.put_line (‘the inverted number is’||inverted_number);

END;

total_val:= total_val + employee_rec.monthly_income;


END LOOP;
RETURN total_val;

END;

Q5 -Explain syntax with example if and then loop?

ANS: SYNTAX: - IF

IF <cond> THEN

<Action>

ELSE IF <cond> THEN

<Action>

ELSE

<Action>

END IF;

EXAMPLE:-

Write a PL/SQL code block that will accept an account no. from the user & debit an amount of
rupees 2000 from the account if the account has a minimum of rs 500 after the amount is debited
the process is to be fired on the account table.

DECLARE

Account_balance number (11,2);

Account_number varchar 2 (0);

Debit_amount_number (5):=2000;

Min_balance constant number (5, 2):=500.00

BEGIN

Account_number:=&account number;
SELECT balance into account_balance from accounts where
account_id=account_number;

Account_balance:=account_balance – debit_amount;

If account_balance>= min_balance Then

Update account set balance= balance – debit_amount

Where account = id = account_number;

END IF;

END;

Q6 -Explain the PL/SQL languages elements?

ANS: PL/SQL Language has total 52 Elements. Some of the Pl/SQL elements are:

Assignment Statement

An assignment statement sets the current value of a variable, field, parameter, or element. The
statement consists of an assignment target followed by the assignment operator and an
expression. When the statement is executed, the expression is evaluated and the resulting value is
stored in the target.

AUTONOMOUS_TRANSACTION Pragma

The AUTONOMOUS_TRANSACTION pragma changes the way a subprogram works within a


transaction. A subprogram marked with this pragma can do SQL operations and commit or roll
back those operations, without committing or rolling back the data in the main transaction.

Block Declaration

The basic program unit in PL/SQL is the block. A PL/SQL block is defined by the keywords
DECLARE, BEGIN, EXCEPTION, and END. These keywords partition the block into a
declarative part, an executable part, and an exception-handling part. Only the executable part is
required. You can nest a block within another block wherever you can place an executable
statement.
CASE Statement

The CASE statement chooses from a sequence of conditions, and executes a corresponding
statement. The CASE statement evaluates a single expression and compares it against several
potential values, or evaluates multiple Boolean expressions and chooses the first one that is
TRUE.

CLOSE Statement

The CLOSE statement indicates that you are finished fetching from a cursor or cursor variable,
and that the resources held by the cursor can be reused.

Collection Definition

A collection is an ordered group of elements, all of the same type. For example, the grades for a
class of students. Each element has a unique subscript that determines its position in the
collection. PL/SQL offers three kinds of collections: associative arrays, nested tables, and
varrays (short for variable-size arrays). Nested tables extend the functionality of associative
arrays (formerly called PL/SQL tables or index-by tables).

Collections work like the arrays found in most third-generation programming languages.
Collections can have only one dimension. Most collections are indexed by integers, although
associative arrays can also be indexed by strings. To model multi-dimensional arrays, you can
declare collections whose items are other collections.

Nested tables and varrays can store instances of an object type and, conversely, can be attributes
of an object type. Collections can also be passed as parameters. You can use them to move
columns of data into and out of database tables or between client-side applications and stored
subprograms.

Collection Methods

A collection method is a built-in function or procedure that operates on collections and is called
using dot notation. You can use the methods EXISTS, COUNT, LIMIT, FIRST, LAST, PRIOR,
NEXT, EXTEND, TRIM, and DELETE to manage collections whose size is unknown or varies.
EXISTS, COUNT, LIMIT, FIRST, LAST, PRIOR, and NEXT are functions that check the
properties of a collection or individual collection elements. EXTEND, TRIM, and DELETE are
procedures that modify a collection.

EXISTS, PRIOR, NEXT, TRIM, EXTEND, and DELETE take integer parameters. EXISTS,
PRIOR, NEXT, and DELETE can also take VARCHAR2 parameters for associative arrays with
string keys. EXTEND and TRIM cannot be used with index-by tables.

Comments

Comments let you include arbitrary text within your code to explain what the code does. You can
also disable obsolete or unfinished pieces of code by turning them into comments.

PL/SQL supports two comment styles: single-line and multi-line. A double hyphen (- -)
anywhere on a line (except within a character literal) turns the rest of the line into a comment.
Multi-line comments begin with a slash-asterisk (/*) and end with an asterisk-slash (*/).

You might also like