PLSQL
PLSQL
PLSQL
Create a PL/SQL block to insert a new record into the Department table.
Fetch the maximum department id from the Department table and add 10 to it;
take this value for department id; 'TESTING' is the value for department name and
CHN-102 is the value for Location ID.
Column name
Data type
Constraints
DEPARTMENT_ID
NUMBER(5)
PK
DEPARTMENT_NAME
VARCHAR2(25)
NOT NULL
LOCATION_ID
VARCHAR2(15)
Sample Output:
DEPARTMENT_ID DEPARTMENT_NAME LOCATION_ID
1. Answer
DECLARE
l_department_id department.department_id%TYPE;
BEGIN
END;
2. Update Location
Create a PL/SQL block to update the location ID for an existing department, which
has location ID preceded with 'HQ' as
'HQ-BLR-101'.
Column name
Data type
Constraints
DEPARTMENT_ID
NUMBER(5)
PK
DEPARTMENT_NAME
VARCHAR2(25)
NOT NULL
LOCATION_ID
VARCHAR2(15)
Sample Output:
DEPARTMENT_ID DEPARTMENT_NAME LOCATION_ID
2.Answer
BEGIN
update department
set LOCATION_ID='HQ-BLR-101'
END;
3. Area of a Circle
Write a PL/SQL block to calculate the area of a circle for the radius ranging from
3 to 7 .
Store the radius and corresponding area into the Circle table.
Circle :
Radius Number(5)
Area Number(7,2)
3. Answer
DECLARE
r number(5);
area number(14,2);
pi constant number (4,2):=3.14;
BEGIN
r:=3;
while r<=7
loop
area:=pi*power(r,2);
insert into areas values(r,area );
r:=r+1;
end loop;
END;
Hints: Procedure name: insert_credit Input parameter : credit_id with data type
as number,credit_card_number with data type as
varchar,credit_card_expire with data type as varchar,holder_name with data type as
varchar
and card_type with data type as varchar Table used: credit_card
4.Answer
credit_id IN credit_card.id%TYPE,
credit_card_number IN credit_card.card_number%TYPE,
credit_card_expire IN credit_card.card_expire%TYPE,
holder_name IN credit_card.name%TYPE,
card_type IN credit_card.cc_type%TYPE) AS
BEGIN
END ;
Create a procedure named 'select_city' which accepts one input parameter user_id of
type number
and one output parameter city_details of type varchar.
This procedure is used to display the city_details of user.
If the user is from Bangalore then display the city_details as 'User is from
Bangalore',or if the user is from Chennai then
display the city_details as 'User is from Chennai', else display the city_details
as 'User is from other cities'.
5. Answer
BEGIN
if(user_id=1) then
city_details:='User is from Bangalore';
dbms_output.put_line(city_details);
elsif(user_id=2) then
city_details:='User is from Chennai';
dbms_output.put_line(city_details);
else
city_details:='User is from other city';
dbms_output.put_line(city_details);
end if;
end select_city;
DECLARE
user_ids number;
begin
user_ids:= &user_ids;
select_city(user_ids);
END;
Create a PL/SQL Trigger to display the message “NEW EMPLOYEE DETAILS INSERTED”,
whenever a new record is inserted into Employee table.
Column name
Data type
Constraints
EMP_ID
NUMBER(5)
PK
EMP_NAME
VARCHAR2(25)
NOT NULL
SALARY
NUMBER(10,2)
Note: Use '/' to terminate your query before compilation and evaluation
6. Answer
BEGIN
END;
Create a PL/SQL Package with Procedure in it. Procedure will take designation and
incentive as input and
update the employee salary by adding the incentive for the given designation.
Display the number of employee records that have got updated, e.g. ‘3 employee
record(s) are updated’.
Employee:
Column name
Data type
Constraints
EMP_ID
NUMBER(5)
PK
EMP_NAME
VARCHAR2(25)
NOT NULL
SALARY
NUMBER(10,2)
DESIGNATION
VARCHAR2(25)
Functional Requirements:
Package name as EMP_DESIGNATION, and
Procedure signature:
EMP_DETAILS(design employee.designation%TYPE, incentive number);
7 . Answer
PACKAGE SPECIFICATION
PACKAGE BODY
updatedEmpCount number;
pemp_id employee.emp_id%type;
pempSal employee.salary%type;
cursor c1 is select emp_id, salary from employee where designation = design;
BEGIN
open c1;
updatedEmpCount := 0;
loop
fetch c1 into pemp_id, pempSal;
exit when c1%notfound;
pempSal := pempSal + incentive;
update employee set salary = pempSal where emp_id = pemp_id;
updatedEmpCount := updatedEmpCount + 1;
end loop;
close c1;
dbms_output.put_line(updatedEmpCount || ' record(s) are updated');
END;
Create a PL/SQL block to display all the department names from the Department table
using cursors.
The department names should be displayed in ascending order.
Column name
Data type
Constraints
DEPARTMENT_ID
NUMBER(5)
PK
DEPARTMENT_NAME
VARCHAR2(25)
NOT NULL
LOCATION_ID
VARCHAR2(15)
Sample Output:
Department Names are :
ADMIN
DEVELOPMENT
8 . Answer
DECLARE
BEGIN
open c1;
loop
fetch c1 into pdepname;
exit when c1%notfound;
dbms_output.put_line(pdepname);
end loop;
close c1;
END ;
Create a PL/SQL Procedure to insert employee details into Employee table. Before
inserting, check whether the employee age is eligible or not. Employee age should
be 18 or greater. Values are passed as argument to the procedure.
If age valid, insert employee record into table and print the message "Age valid -
Record inserted", else print the message "Age invalid - Record not inserted" by
raising an exception.
Table: EMPLOYEE
Column name
Data type
Constraints
EMP_ID
NUMBER(5)
PK
EMP_NAME
VARCHAR2(25)
NOT NULL
AGE
NUMBER(3)
Functional Requirement:
PROCEDURE CHECK_AGE_ELIGIBILITY(
v_id IN EMPLOYEE.EMPID%TYPE,
v_name IN EMPLOYEE.EMPNAME%TYPE,
v_age IN EMPLOYEE.AGE%TYPE)
Sample Input 1 :
CHECK_AGE_ELIGIBILITY(103, 'Robert', 24 ) ;
Sample Output 1:
Sample Input 2:
CHECK_AGE_ELIGIBILITY(104,'Riya', 4 );
Sample Output 2:
9. Answer
BEGIN