CAP 301 RS1006B44

Embed Size (px)

Citation preview

  • 8/7/2019 CAP 301 RS1006B44

    1/9

    ASSIGNMENT OF

    DBMS

    Submitted to: Submitted by:

    Ms. Pency Juneja Rohit Nagpal

    Roll no.

    B44

  • 8/7/2019 CAP 301 RS1006B44

    2/9

    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:

  • 8/7/2019 CAP 301 RS1006B44

    3/9

  • 8/7/2019 CAP 301 RS1006B44

    4/9

    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 whileloops 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

    LOOP

    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:-

  • 8/7/2019 CAP 301 RS1006B44

    5/9

    WHILE monthly_value

  • 8/7/2019 CAP 301 RS1006B44

    6/9

    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 THEN

    ELSE IF THEN

    ELSE

    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;

  • 8/7/2019 CAP 301 RS1006B44

    7/9

    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.

  • 8/7/2019 CAP 301 RS1006B44

    8/9

    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, althoughassociative 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.

  • 8/7/2019 CAP 301 RS1006B44

    9/9

    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 withstring 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 (*/).