0% found this document useful (0 votes)
46 views

AD3391 LAB EXERCISE

Uploaded by

hamsadineshkumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views

AD3391 LAB EXERCISE

Uploaded by

hamsadineshkumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

EX.

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 user-name: system

Enter password:

Connected.

Creating a table:

SQL> create table employeedetails(empid number(10),fname varchar(30),lname varchar(30),designation


varchar(30),salary number(15));

Table created.

Insertion:

SQL> insert into


employeedetails(empid,fname,lname,designation,salary)values(100,'arjun','sankar','manager',50000);

1 row created.

SQL> insert into


employeedetails(empid,fname,lname,designation,salary)values(200,'arun','kumar','professor',40000);

1 row created.

SQL> insert into


employeedetails(empid,fname,lname,designation,salary)values(300,'deva','raj','cashier',30000);

1 row created.

SQL> insert into


employeedetails(empid,fname,lname,designation,salary)values(400,'hari','vishnu','helper',25000);

1 row created.

SQL> insert into


employeedetails(empid,fname,lname,designation,salary)values(500,'lokesh','kumar','director',45000);

1 row created.
To view contents of table:

select * from employeedetails;

EMPID FNAME LNAME DESIGNATION SALARY

100 arjun sankar manager 50000

200 arun kumar professor 40000

300 deva raj cashier 30000

400 hari Vishnu helper 25000

500 lokesh kumar director 45000

Updation:

SQL> update employeedetails set salary=20000 where designation='helper';

1 row updated.

SQL> update employeedetails set designation='contract employee' where empid=500;

1 row updated.

SQL> select * from employeedetails;

EMPID FNAME LNAME DESIGNATION SALARY

100 arjun sankar manager 50000

200 arun kumar professor 40000

300 deva raj cashier 30000

400 hari Vishnu helper 20000

500 lokesh kumar contract employee 45000

Deletion:

#deleting a specific record from the table#

delete from employeedetails where empid=005;

0 rows deleted.

SQL> delete from employeedetails where empid=500;

1 row deleted.

SQL> select *from employeedetails;


EMPID FNAME LNAME DESIGNATION SALARY

100 arjun sankar manager 50000

200 arun kumar professor 40000

300 deva raj cashier 30000

400 hari Vishnu helper 20000

#deleting the entire table#

SQL> create table tempdata(name varchar(10),regno number(10));

Table created.

SQL> insert into tempdata(name,regno)values('aaa',101);

1 row created.

SQL> insert into tempdata(name,regno)values('bbb',102);

1 row created.

SQL> select * from tempdata;

NAME REGNO

aaa 101

bbb 102

SQL> drop table tempdata;

Table dropped.

SQL> select * from tempdata;

select * from tempdata

ERROR at line 1:

ORA-00942: table or view does not exist

Alter table:

SQL> alter table employeedetails add joining_date date;

Table altered.

SQL> select * from employeedetails;


EMPID FNAME LNAME DESIGNATION SALARY JOINING_DATE

100 arjun sankar manager 50000

200 arun kumar professor 40000

300 deva raj cashier 30000

400 hari Vishnu helper 20000

selecting a specific record from table:

SQL> select fname,joining_date from employeedetails;

FNAME JOINING_DATE

arjun

arun

deva

hari

SQL> select fname,designation,salary from employeedetails where salary>=25000;

FNAME DESIGNATION SALARY

arjun manager 50000

arun professor 40000

deva cashier 30000

Creating a view from a table:

SQL> create view employeeview as select fname,lname,designation from employeedetails;

View created.

SQL> select * from employeeview;

FNAME LNAME DESIGNATION

arjun sankar manager

arun kumar professor

deva raj cashier

hari Vishnu helper

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:

CREATE TABLE employees1 ( employee_id NUMBER PRIMARY KEY, employee_name


VARCHAR2(100), salary NUMBER );

insert into employees1(employee_id,employee_name,salary)values(101,'abinesh',50000);

insert into employees1(employee_id,employee_name,salary)values(102,'bargavi',20000);

insert into employees1(employee_id,employee_name,salary)values(103,'chakkaravarthy',50000);

insert into employees1(employee_id,employee_name,salary)values(104,'daya',53000);

insert into employees1(employee_id,employee_name,salary)values(105,'ezhil',89000);

insert into employees1(employee_id,employee_name,salary)values(106,'fazil',65000);

insert into employees1(employee_id,employee_name,salary)values(107,'gayathri',56000);

insert into employees1(employee_id,employee_name,salary)values(108,'hari',10000);

insert into employees1(employee_id,employee_name,salary)values(109,'iniyan',15000);

DECLARE

-- Define a cursor to fetch the top 5 highest-paid employees

CURSOR top_employees_cursor IS

SELECT employee_id, employee_name, salary

FROM (

SELECT employee_id, employee_name, salary

FROM employees1

ORDER BY salary DESC

WHERE ROWNUM <= 5;


-- Define variables to hold employee data

v_employee_id employees1.employee_id%TYPE;

v_employee_name employees1.employee_name%TYPE;

v_salary employees1.salary%TYPE;

BEGIN

-- Open the cursor

OPEN top_employees_cursor;

DBMS_OUTPUT.PUT_LINE('TOP FIVE PAID EMPLOYEES');

-- Loop through the cursor and print the results

LOOP

FETCH top_employees_cursor INTO v_employee_id, v_employee_name, v_salary;

EXIT WHEN top_employees_cursor%NOTFOUND;

-- Output employee details

DBMS_OUTPUT.PUT_LINE('Employee ID: ' || v_employee_id || ', Name: ' || v_employee_name ||


', Salary: ' || v_salary);

END LOOP;

-- Close the cursor

CLOSE top_employees_cursor;

EXCEPTION

WHEN OTHERS THEN

-- Handle any exceptions

DBMS_OUTPUT.PUT_LINE('An error has occurred: ' || SQLERRM);

IF top_employees_cursor%ISOPEN THEN

CLOSE top_employees_cursor;

END IF;

END;

/
OUTPUT:

TOP FIVE PAID EMPLOYEES

Employee ID: 105, Name: ezhil, Salary: 89000


Employee ID: 106, Name: fazil, Salary: 65000
Employee ID: 107, Name: gayathri, Salary: 56000
Employee ID: 104, Name: daya, Salary: 53000
Employee ID: 101, Name: abinesh, Salary: 50000

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:

To write a PLSQL program to find sum of N numbers.

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:

1. Temporary Table Creation:


o temp_numbers is a global temporary table created for holding numbers. It’s created using
dynamic SQL (EXECUTE IMMEDIATE).
2. Insert Numbers:
o Numbers are inserted into the temp_numbers table. Modify these INSERT statements to add
your own numbers or data as needed.
3. Cursor for Data Fetching:
o num_cursor selects numbers from the temporary table. A loop is used to fetch and sum these
numbers.
4. Sum Calculation:
o The v_sum variable accumulates the total sum of numbers.
5. Exception Handling:
o Basic exception handling is included to handle cases like no data found or other errors.
6. Output:
o The result is displayed using DBMS_OUTPUT.PUT_LINE.
Program:

CREATE TABLE temp_numbers(tempvalue number);

INSERT INTO temp_numbers (tempvalue) VALUES (10);

INSERT INTO temp_numbers (tempvalue) VALUES (20);

INSERT INTO temp_numbers (tempvalue) VALUES (30);

INSERT INTO temp_numbers (tempvalue) VALUES (40);


INSERT INTO temp_numbers (tempvalue) VALUES (50);

DECLARE

-- Variables

v_sum NUMBER := 0; -- To store the sum of numbers

v_num_count NUMBER := 0; -- To store the count of numbers

-- Exception for empty list or other issues

no_data_found EXCEPTION;

-- Cursor to fetch numbers from the table

CURSOR num_cursor IS

SELECT tempvalue FROM temp_numbers;

BEGIN

OPEN num_cursor; -- Open cursor

-- Fetch and sum the numbers

LOOP

FETCH num_cursor INTO v_num_count;

EXIT WHEN num_cursor%NOTFOUND;

v_sum := v_sum + v_num_count;

END LOOP;

CLOSE num_cursor;

DBMS_OUTPUT.PUT_LINE('The sum of the numbers is: ' || v_sum);

EXCEPTION

WHEN no_data_found THEN

DBMS_OUTPUT.PUT_LINE('No data found in the temporary table.');

WHEN OTHERS THEN

DBMS_OUTPUT.PUT_LINE('An error has occurred: ' || SQLERRM);

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.

You might also like