Name: _____________________________________________________ ______________________________________________________ Batch: _______________ Roll no : ________ Department: __________________________________________ Subject:____________________________ SHRI G.P.M.DEGREE COLLEGE Mahatma Gandhi Rd ,Vile Parle East , Mumbai-400057, Tel.:8928387200. CERTIFICATE This is to certify that Mr/Ms _____________________________________ a student of F.Y.B.Sc. Roll No. _______ has completed the required number of practical in the subject of ___________________________________ as prescribed by the UNIVERSITY OF MUMBAI under my super vision during the academic year 2025-2026. Signatories: Subject In-charge (Name &Sign) : _________________ Principal Sign (Name & Sign) :_ ____________________ External Examiner (Name &Sign) : _________________ Date: ___________ ____________ College Stamp Professor Name : Ass.Prof Kanchan Sav Class : FY.BSC-CS Semester : Sem II (2024-2025) Course code : SEC Subject: Database Management Systems Using PL/SQL Sr. No. Date Index Page No. Sign 1 Using Variables and Expressions Write a PL/SQL program that declares two numeric variables, assigns values to them, calculates theirsum, and printsthe result. ............ ...... .. ............... ......... 2 Sequence Management Create a sequence named emp seq with the following specifications: ∙ Starts at 1000 ∙ Increments by 10 ∙ Has a maximum value of 2000 ∙ Cycles when the max value is reached Then, write a PL/SQL block to fetch and display the next 5 values of the sequence. ............ ...... .. ............... ......... 3 Stored Procedure - Calculate Factorial Write a stored procedure named calc_factorial that: ∙ Accepts a number (N) as input. ∙ Returnsthe factorial of that number using a loop. ∙ Print the result. ............ ...... .. ............... ......... 4 Collections - Using Associative Arrays Write a PL/SQL program that: ∙ Declares an associative array (index-by table). ∙ Stores the first five employee salaries in the array. ∙ Uses a loop to print all salaries from the array. ............ ...... .. ............... ......... 5 Error Handling - Handling User-Defined Exceptions Write a PL/SQL block that: ∙ Accepts a number as input. ∙ Raises a user-defined exception if the number is negative (e.g., "Negative Number Exception"). ∙ Handles the exception using EXCEPTION WHEN and prints an appropriate error message. ............ ...... .. ............... ......... 6 Cursor - Fetching Employee Details Write a PL/SQL program that: ∙ Declares an explicit cursor to fetch employee ID, Name, and Salary from the EMPLOYEES table. ∙ Uses a loop to fetch and display all records. ∙ Closes the cursor properly after execution. ............ ...... .. ............... ......... 7 Transaction Control - Handling Deadlocks and Concurrency Write a PL/SQL block that: ∙ Begins a transaction ∙ Updates the salary of an employee in the EMPLOYEES table. ∙ Introduces a delay ( DBMS_LOCK.SLEEP ) to simulate concurrency issues. ∙ Uses SAVEPOINT and ROLLBACK to handle deadlocks ............ ...... .. ............... ......... 8 Dynamic SQL - Preventing SQL Injection Write a PL/SQL block that: ∙ Accepts a table name and column name as input. ∙ Uses Native Dynamic SQL (EXECUTE IMMEDIATE) to fetch data dynamically. ∙ Prevents SQL Injection by using bind variables instead of concatenation. ............ ...... .. ............... ......... 9 Triggers - Enforcing Data Integrity on Insert Write a BEFORE INSERT trigger on the EMPLOYEES table that: ∙ Ensures that the salary is at least 5000 ∙ If a lower salary is inserted, automatically set it to 5000 ∙ Display a message when the trigger modifies the salary. ............ ...... .. ............... ......... 10 PL/SQL Package - Using Nested Tables Create a PL/SQL package named emp_package that: ∙ Contains a nested table to store employee salaries. ∙ Has procedures to insert, update, and display the salaries. ∙ Uses TABLEOF NUMBER to define the nested table. ∙ Calls the package procedures in a PL/SQL block. ............ ...... .. ............... ......... SHRIG.P.M. DEGREE COLLEGE OF SCIENCE & COMMERCE Department of Computer Affiliated to University of Mumbai Experiment 1: Using Variables and Expressions Aim: Write a PL/SQL program that declares two numeric variables, assigns values to them, calculates their sum, and prints the result. Tools Used: ∙ PL/SQL : Procedural Language extensions to SQL for Oracle databases. ∙ DBMS_OUTPUT.PUT_LINE : A PL/SQL built-in procedure used to print output to the console. Learning Objective: ∙ To understand how to declare variablesin PL/SQL. ∙ To practice performing arithmetic operations using numeric variables. ∙ To learn how to display output using the DBMS_OUTPUT.PUT_LINE procedure. Theory: PL/SQL allows you to declare variables, perform operations on them, and print results. Here’s a breakdown of the concepts used in this program: 1. Variables in PL/SQL : Variables store data temporarily. In the above program, we declared two numeric variables (num1 and num2) and assigned them initial values (10 and 20). 2. Expressions : An expression is a combination of variables, constants, operators, and functions that evaluates to a value. In this case, the expression num1 + num2 calculates the sum of num1 and num2. 3. DBMS_OUTPUT.PUT_LINE : This built-in procedure is used to display the output of the program on the console. Code & Output: SHRIG.P.M. DEGREE COLLEGE OF SCIENCE & COMMERCE Department of Computer Affiliated to University of Mumbai Learning Outcomes: By the end of this program, learners should be able to: ∙ Declare and initialize numeric variables in PL/SQL. ∙ Perform basic arithmetic operations (like addition) with variables. ∙ Output results to the console using DBMS_OUTPUT.PUT_LINE. Course Outcomes: ∙ Students will gain an understanding of how to manipulate data in PL/SQL. ∙ They will develop the ability to write simple programs that involve variable declarations and arithmetic operations. ∙ Students will learn to display the results of computations in PL/SQL applications. Conclusion: This PL/SQL program demonstrates the basic usage of variables, arithmetic operations, and output in PL/SQL. By declaring variables and performing a simple calculation (addition in this case), students can understand how variables interact within the PL/SQL environment. This is foundational knowledge for more complex PL/SQL programming. Viva Questions: 1. What is the purpose of the DECLARE section in PL/SQL? 2. Why do we use DBMS_OUTPUT.PUT_LINE in PL/SQL? 3. How do you perform arithmetic operations in PL/SQL? 4. What will happen if you don't use the / at the end of the PL/SQL block? 5. Can you assign a string value to a numeric variable in PL/SQL? Why or why not? 6. What will happen if we change the values of num1 and num2 ? 7. What are the different types of variables in PL/SQL? For Faculty use: Correction Parameters Formative Assessment[40%] Timely Completion of Practical[40%] Attendance Learning Attitude[20%] SHRIG.P.M. DEGREE COLLEGE OF SCIENCE & COMMERCE Department of Computer Affiliated to University of Mumbai Experiment 2: Sequence Management Aim: Create a sequence named emp seq with the following specifications: ∙ Starts at 1000 ∙ Increments by 10 ∙ Has a maximum value of 2000 ∙ Cycles when the max value is reached Then, write a PL/SQL block to fetch and display the next 5 values of the sequence. Tools Used: ∙ PL/SQL : The procedural extension to SQL, used to write and execute the program. ∙ DBMS_OUTPUT.PUT_LINE : A built-in PL/SQL procedure used to output data to the console. ∙ Oracle SQL : Used to create and configure the sequence in the database. Learning Objective: ∙ To learn how to create and configure sequences in PL/SQL. ∙ To understand how sequences are used to generate unique numbers for data processing. ∙ To practice fetching sequence values programmatically in a PL/SQL block. Theory: 1. Sequences in PL/SQL: o A sequence is a database object in Oracle that automatically generates a series of unique numbers. It is typically used for generating primary keys for database tables. o Sequence Syntax : The CREATE SEQUENCE statement allows you to define how the sequence behaves, including its start value, increment, maximum value, and whether it cycles back to the starting point. 2. Components of a Sequence: SHRIG.P.M. DEGREE COLLEGE OF SCIENCE & COMMERCE Department of Computer Affiliated to University of Mumbai o START WITH : Defines the first value in the sequence. o INCREMENT BY : Defines the step or difference between consecutive numbers. o MAXVALUE : Specifies the maximum value the sequence can reach. o CYCLE : If enabled, the sequence will restart from the starting value after reaching the maximum value. 3. Using Sequence in PL/SQL : o In a PL/SQL block, the sequence value can be fetched using sequence_name.NEXTVAL. o Sequences can be useful for generating primary key values, unique transaction IDs, etc. Code & Output: Learning Outcomes: ∙ Learners will understand how to create and configure a sequence in Oracle. ∙ Learners will be able to write a PL/SQL block to fetch multiple values from a sequence. SHRIG.P.M. DEGREE COLLEGE OF SCIENCE & COMMERCE Department of Computer Affiliated to University of Mumbai ∙ Learners will gain practical experience working with sequences in database applications. Course Outcomes: ∙ Students will become proficient in using Oracle database objects such as sequences. ∙ Students will be able to implement sequence management to generate unique identifiers for database records. ∙ Students will develop the ability to write and test PL/SQL blocks for practical database management tasks. Conclusion: This exercise demonstrated how to create a sequence in Oracle with specific specifications such as starting value, increment, maximum value, and cycling behavior. It also illustrated how to write a PL/SQL block to fetch and display multiple values from the sequence. Sequences are essential for generating unique, auto-incremented numbers in database applications, and understanding their management is crucial for effective database design and development Viva Questions: 1. What is a sequence in Oracle, and what is its purpose? 2. What is the purpose of the CYCLE option in a sequence? 3. What will happen if you try to fetch the next value from a sequence that has reached its maximum value and does not cycle? 4. What does the NEXTVAL function do in PL/SQL? 5. What is the difference between CURRVAL and NEXTVAL in sequence management? 6. How can you ensure a sequence value is always unique across different sessions? 7. What happens if the MAXVALUE of a sequence is reached without the CYCLE option? 8. Can you modify the properties of a sequence once it is created (e.g., change the increment or max value)? For Faculty use: Correction Parameters Formative Assessment[40%] Timely Completion of Practical[40%] Attendance Learning Attitude[20%] SHRIG.P.M. DEGREE COLLEGE OF SCIENCE & COMMERCE Department of Computer Affiliated to University of Mumbai Experiment 3: Stored Procedure - Calculate Factorial Aim: Write a stored procedure named calc_factorial that: ∙ Accepts a number (N) as input. ∙ Returns the factorial of that number using a loop. ∙ Print the result. Tools Used: ∙ PL/SQL : Oracle's procedural language extension to SQL used to write the procedure. ∙ DBMS_OUTPUT.PUT_LINE : A built-in procedure to display output in theOracle console. ∙ Oracle Database : The underlying database system to run and execute the procedure. Learning Objective: ∙ To understand how to create and work with stored proceduresin PL/SQL. ∙ To learn how to perform iterative tasks (such as factorial calculation) using loops in PL/SQL. ∙ To practice using output functions in PL/SQL to display results. Theory: 1. Stored Procedures in PL/SQL : o A stored procedure is a named PL/SQL block that can be executed by calling it explicitly. It can accept parameters, perform operations, and return results. o The procedure in this example calculates the factorial of a number. 2. Factorial Calculation : o The factorial of a number N (denoted as N!) is the product of all positive integers from 1 to N. For example, 4! = 4 * 3 * 2 * 1 = 24. o Factorial calculations are a common use case for loops in programming. 3. PL/SQL Loops : SHRIG.P.M. DEGREE COLLEGE OF SCIENCE & COMMERCE Department of Computer Affiliated to University of Mumbai o A FOR loop in PL/SQL is used to iterate over a range of values. In this case, we iterate from 1 to N to multiply the values and compute the factorial. 4. DBMS_OUTPUT.PUT_LINE : o This procedure is used to display the output in Oracle SQL*Plus or other PL/SQL tools. It's a useful tool for debugging and displaying results during PL/SQL program execution. Code & Output: Learning Outcomes: ∙ Learners will be able to create stored procedures in PL/SQL to automate tasks. ∙ Learners will gain hands-on experience using loops in PL/SQL to perform calculations. ∙ Learners will understand how to display output in PL/SQL using the DBMS_OUTPUT.PUT_LINE procedure. Course Outcomes: ∙ Technical Skills : Students will develop the ability to work with stored procedures in PL/SQL. SHRIG.P.M. DEGREE COLLEGE OF SCIENCE & COMMERCE Department of Computer Affiliated to University of Mumbai ∙ Problem-Solving Skills : Students will be able to break down problems (such as calculating factorials) and implement them in PL/SQL. ∙ Database Programming : Learners will gain proficiency in using PL/SQL for automating repetitive tasks like calculations, data manipulation, and reporting. Conclusion: This stored procedure illustrates a common use case for loops in PL/SQL—calculating the factorial of a given number. By creating and running this procedure, learners will understand how to work with PL/SQL blocks, handle input parameters, use loops for iteration, and print output to the console. This fundamental skill can be extended to more complex calculations and applications in database programming. Viva Questions: 1. What is the purpose of the CREATE OR REPLACE PROCEDURE statement in PL/SQL? 2. How does the factorial calculation work in this procedure? 3. Why are we using a FOR loop in this procedure? 4. How does PL/SQL handle input parameters in stored procedures? 5. What will happen if the input number N is 0? 6. Can this procedure handle negative numbers? 7. How can you modify the procedure to return the factorial instead of printing it? 8. What is the difference between a stored procedure and a function in PL/SQL? 9. Can you call this stored procedure from another PL/SQL block? For Faculty use: Correction Parameters Formative Assessment[40%] Timely Completion of Practical[40%] Attendance Learning Attitude[20%] SHRIG.P.M. DEGREE COLLEGE OF SCIENCE & COMMERCE Department of Computer Affiliated to University of Mumbai Experiment 4: Collections - Using Associative Arrays Aim: Write a PL/SQL program that: ∙ Declares an associative array (index-by table). ∙ Stores the first five employee salaries in the array. ∙ Uses a loop to print allsalaries from the array. Tools Used: ∙ PL/SQL : Oracle’s procedural language extension to SQL, used to write the program. ∙ DBMS_OUTPUT.PUT_LINE : A built-in PL/SQL procedure used to print output to the console for debugging and displaying results. Learning Objective: ∙ To learn how to declare and use associative arrays(also called index-by tables) in PL/SQL. ∙ To practice storing valuesin arrays and retrieving them using a loop. ∙ To understand how to display results using the DBMS_OUTPUT.PUT_LINE procedure. Theory: 1. Associative Arrays (Index-by Tables): o Associative arrays are one of the three types of PL/SQL collections(the others are nested tables and varrays). o An associative array allows you to use any scalar type (e.g., VARCHAR2, NUMBER) as the index or key. These arrays can be used when you need to index elements by non-sequential keys. o In this program, employee names(like Employee1, Employee2) are used as the indexes of the associative array, and salaries are stored as the values. 2. Array Operations in PL/SQL: o The values in an associative array are accessed by referencing the index. For example, employee_salaries('Employee1') gives the salary of the employee named "Employee1". o The INDEX BY clause specifies the type of the index for the array. SHRIG.P.M. DEGREE COLLEGE OF SCIENCE & COMMERCE Department of Computer Affiliated to University of Mumbai 3. DBMS_OUTPUT.PUT_LINE: o The DBMS_OUTPUT.PUT_LINE procedure is used to print output in PL/SQL, which is particularly useful for displaying the results of the array processing. 4. Using Loops in PL/SQL: o A FOR loop is employed here to iterate over a predefined set of employee names and print their corresponding salaries from the associative array. o The FOR loop variable employee dynamically generates the employee names (Employee1, Employee2, etc.). Code & Output: Learning Outcomes: By the end of this program, learners will: ∙ Understand how to declare and work with associative arrays in PL/SQL. ∙ Be able to store data in an associative array and retrieve it using a loop. SHRIG.P.M. DEGREE COLLEGE OF SCIENCE & COMMERCE Department of Computer Affiliated to University of Mumbai ∙ Know how to print the contents of collections using the DBMS_OUTPUT.PUT_LINE procedure. Course Outcomes: ∙ Students will acquire the skills necessary to manage collections in PL/SQL. ∙ Students will learn how to store, retrieve, and manipulate data in associative arrays. ∙ Students will develop a deeper understanding of loops and output mechanisms in PL/SQL programming. Conclusion: This PL/SQL program demonstrates the power of associative arrays for managing non sequential data,such as storing employee salaries indexed by employee names. By using a loop to retrieve and print values, this example illustrates how collections can be leveraged for practical tasks in database programming. The knowledge gained from this example is fundamental to managing and processing complex data sets in PL/SQL. Viva Questions: 1. What is an associative array in PL/SQL? 2. How does the INDEX BY clause work in PL/SQL? 3. What are the advantages of using associative arrays over regular arrays? 4. What is the output of the program if you change the salary of Employee2 to 7500? 5. How does the loop work to print all the salaries in the array? 6. Can associative arrays store multiple values with the same index? 7. How would you modify this program to store and print the names and salaries of employees? For Faculty use: Correction Parameters Formative Assessment[40%] Timely Completion of Practical[40%] Attendance Learning Attitude[20%] SHRIG.P.M. DEGREE COLLEGE OF SCIENCE & COMMERCE Department of Computer Affiliated to University of Mumbai Experiment 5: Error Handling - Handling User-Defined Exceptions Aim: Write a PL/SQL block that: ∙ Accepts a number as input. ∙ Raises a user-defined exception if the number is negative (e.g., "NegativeNumberException"). ∙ Handlesthe exception using EXCEPTION WHEN and prints an appropriate error message. Tools Used: ∙ PL/SQL : The procedural extension to SQL used for writing the program logic. ∙ RAISE statement : Used to raise user-defined exceptions. ∙ EXCEPTION block : Used to handle exceptions and errors in PL/SQL. ∙ DBMS_OUTPUT.PUT_LINE : A procedure to print output to the console. Learning Objective: ∙ To understand how to declare and raise user-defined exceptionsin PL/SQL. ∙ To learn how to handle user-defined exceptions using the EXCEPTION block in PL/SQL. ∙ To practice error handling in PL/SQL by printing appropriate error messages based on specific conditions. Theory: 1. User-Defined Exceptions in PL/SQL: o A user-defined exception is created when you want to handle a specific condition that is not already covered by built-in exceptions (e.g., NO_DATA_FOUND, TOO_MANY_ROWS). o To create a user-defined exception, you declare it using the EXCEPTION keyword and raise it using the RAISE statement when a specific condition occurs. 2. RAISE Statement: o The RAISE statement is used to explicitly trigger an exception. You can raise either predefined exceptions or user-defined exceptions. o Example: RAISE NegativeNumberException; SHRIG.P.M. DEGREE COLLEGE OF SCIENCE & COMMERCE Department of Computer Affiliated to University of Mumbai 3. EXCEPTION Block: o The EXCEPTION block in PL/SQL is used to handle errors and exceptions that occur during the execution of the PL/SQL code. o Specific exceptions are handled first, and the OTHERS handler acts as a catch all for any other unhandled errors. 4. Built-in vs. User-Defined Exceptions: o PL/SQL provides a set of predefined exceptions (e.g., ZERO_DIVIDE, NO_DATA_FOUND, etc.). o For conditions specific to your program's logic, user-defined exceptions allow you to raise and handle custom errors. Code & Output: Learning Outcomes: ∙ Learners will be able to declare, raise, and handle user-defined exceptions in PL/SQL. ∙ Learners will understand the structure of the EXCEPTION block and how to handle errors programmatically. ∙ Learners will be able to use error-handling mechanisms to improve the robustness of PL/SQL programs. Course Outcomes: ∙ Students will gain an understanding of error handling techniques in PL/SQL and learn to handle specific error conditions using user-defined exceptions. ∙ Students will develop problem-solving skills by incorporating exception handling in their PL/SQL programs. SHRIG.P.M. DEGREE COLLEGE OF SCIENCE & COMMERCE Department of Computer Affiliated to University of Mumbai ∙ Students will learn how to ensure smooth execution of PL/SQL code by managing potential errors and providing user-friendly error messages. Conclusion: This PL/SQL block demonstrates the use of user-defined exceptions for error handling. By raising and catching custom exceptions,such as handling negative numbers, the program ensures that only valid data is processed and appropriate messages are displayed for erroneous input. Exception handling is an essential skill in PL/SQL programming, and this example provides a foundation for managing errors and improving the reliability of database applications. Viva Questions: 1. What is a user-defined exception in PL/SQL, and why would you use it? 2. How do you raise a user-defined exception in PL/SQL? 3. What is the purpose of the EXCEPTION block in PL/SQL? 4. What happens if the input number is positive in this program? 5. What will happen if a negative number is entered? 6. What is the purpose of the OTHERS exception handler? 7. What are the advantages of using exception handling in PL/SQL? 8. Can you use a user-defined exception in a WHEN OTHERS block? For Faculty use: Correction Parameters Formative Assessment[40%] Timely Completion of Practical[40%] Attendance Learning Attitude[20%] SHRIG.P.M. DEGREE COLLEGE OF SCIENCE & COMMERCE Department of Computer Affiliated to University of Mumbai Experiment 6: Cursor - Fetching Employee Details Aim: Write a PL/SQL program that: ∙ Declares an explicit cursor to fetch employee ID, Name, and Salary from the EMPLOYEES table. ∙ Uses a loop to fetch and display all records. ∙ Closes the cursor properly after execution. Tools Used: ∙ PL/SQL : Oracle’s procedural language extension to SQL, used to write the program. ∙ Cursor : An explicit cursor is used to fetch data from the EMPLOYEES table. ∙ DBMS_OUTPUT.PUT_LINE : A procedure used to print output in Oracle SQL*Plus or PL/SQL Developer. Learning Objective: ∙ To understand how to declare and use explicit cursors in PL/SQL. ∙ To learn how to loop through records fetched by a cursor. ∙ To gain knowledge of how to close the cursor properly to free resources. Theory: 1. Explicit Cursors: o An explicit cursor is a named pointer to a context area in the database. It allows for the retrieval of rows one at a time from a SQL query. o A cursor is explicitly declared, opened, fetched, and closed. o The general flow for using a cursor involves: ▪ Declaring the cursor. ▪ Opening the cursor to execute the SQL query. ▪ Fetching records into variables. ▪ Closing the cursor after all records have been processed. SHRIG.P.M. DEGREE COLLEGE OF SCIENCE & COMMERCE Department of Computer Affiliated to University of Mumbai 2. Cursor Operations: o OPEN : Initializes the cursor and associates it with the SQL query. o FETCH : Retrieves the next row from the cursor and stores it in variables. o CLOSE : Releases the cursor resources after all data has been processed. 3. DBMS_OUTPUT.PUT_LINE: o This procedure is used for debugging and displaying output in Oracle’s SQL*Plus or PL/SQL Developer. o In this program, it is used to display employee information on the screen. Code & Output: