AD3391 LAB EXERCISE
AD3391 LAB EXERCISE
NO:5
DATE:
Basic SQL commands(DDL&DML)
Introduction:
DDL (Data Definition Language) provides the ability to define, create and modify database objects such as
tables, views, indexes, and users. DML (Data Manipulation Language) allows for manipulating data in a
database, such as inserting, updating, and deleting records.
Connecting to a database:
SQL> connect
Enter password:
Connected.
Creating a table:
Table created.
Insertion:
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
To view contents of table:
Updation:
1 row updated.
1 row updated.
Deletion:
0 rows deleted.
1 row deleted.
Table created.
1 row created.
1 row created.
NAME REGNO
aaa 101
bbb 102
Table dropped.
ERROR at line 1:
Alter table:
Table altered.
FNAME JOINING_DATE
arjun
arun
deva
hari
View created.
Result:
Thus, the basic DDL and DML operations are performed successfully.
EX.NO:6
PLSQL PROGRAM TO FIND HIGHEST PAID EMPLOYEES
DATE:
Aim:
To write a PLSQL program to find the highest paid top five employees in an organization.
Algorithm:
1. Define the Data Structure:
o Ensure you have a table named employees (or similar) with relevant columns such as
employee_id, employee_name, and salary.
2. Create a Cursor:
o Define a cursor to select the top 5 highest-paid employees. This involves:
Ordering the employees by salary in descending order.
Limiting the results to the top 5 rows.
3. Open the Cursor:
o Initialize the cursor for processing.
4. Fetch Records:
o Loop through the cursor to fetch records one by one.
5. Process Each Record:
o For each fetched record, retrieve the employee details.
6. Handle Exceptions:
o Include error handling to manage any exceptions that might occur.
7. Close the Cursor:
o Ensure that the cursor is closed after processing to release resources.
8. Display Results:
o Use DBMS_OUTPUT.PUT_LINE to print the results.
Explanation:
Cursor Declaration:
top_employees_cursor selects the top 5 highest-paid employees. It uses an inner query to sort the
employees by salary in descending order and then applies ROWNUM to limit the result to the top 5.
Variables:
Variables v_employee_id, v_employee_name, and v_salary are used to store each employee's
details fetched from the cursor.
Cursor Processing:
Open the cursor and loop through the result set. For each record, fetch the employee details into the
defined variables and print them using DBMS_OUTPUT.PUT_LINE.
Exception Handling:
Basic exception handling is included to catch and report any errors that occur during the execution
of the PL/SQL block. It also ensures the cursor is closed if an error occurs.
Execution:
The / at the end of the block is used in SQL*Plus or Oracle SQL Developer to execute the PL/SQL
block.
PROGRAM:
DECLARE
CURSOR top_employees_cursor IS
FROM (
FROM employees1
v_employee_id employees1.employee_id%TYPE;
v_employee_name employees1.employee_name%TYPE;
v_salary employees1.salary%TYPE;
BEGIN
OPEN top_employees_cursor;
LOOP
END LOOP;
CLOSE top_employees_cursor;
EXCEPTION
IF top_employees_cursor%ISOPEN THEN
CLOSE top_employees_cursor;
END IF;
END;
/
OUTPUT:
Result:
Thus,the PLSQL program to find the highest paid top five employees in an organization is written
and executed successfully.
EX.NO:7
PLSQL PROGRAM TO FIND SUM OF N NUMBERS
DATE:
Aim:
Algorithm:
1. Create a Temporary Table: This will hold the numbers for which you want to find the sum.
2. Insert Numbers into the Table: Populate the table with the numbers.
3. Calculate the Sum: Compute the sum using an SQL query.
4. Display the Result: Output the sum using DBMS_OUTPUT.
Explanation:
DECLARE
-- Variables
no_data_found EXCEPTION;
CURSOR num_cursor IS
BEGIN
LOOP
END LOOP;
CLOSE num_cursor;
EXCEPTION
END;/
Output:
The sum of the numbers is: 150
Result:
Thus, the PLSQL program to find sum of N numbers is written and executed successfully.