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

PLSQL

The document contains various PL/SQL scripts for calculating factorials, checking prime numbers, summing even numbers, formatting dates, finding the largest number, reversing strings, calculating areas, and updating employee salaries using explicit cursors. It also includes SQL commands for altering, updating, and deleting records in an employees table, along with expected results for each operation. Additionally, it emphasizes caution when using DELETE commands without WHERE clauses to prevent unintended data loss.

Uploaded by

ahmadayaan00
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

PLSQL

The document contains various PL/SQL scripts for calculating factorials, checking prime numbers, summing even numbers, formatting dates, finding the largest number, reversing strings, calculating areas, and updating employee salaries using explicit cursors. It also includes SQL commands for altering, updating, and deleting records in an employees table, along with expected results for each operation. Additionally, it emphasizes caution when using DELETE commands without WHERE clauses to prevent unintended data loss.

Uploaded by

ahmadayaan00
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

1.

Factorial
-- Factorial
DECLARE
n NUMBER;
fact NUMBER := 1;
i NUMBER ;

BEGIN

n := &n;

FOR i IN 1..n LOOP


fact := fact * i;
END LOOP;

DBMS_OUTPUT.PUT_LINE('Factorial is '||fact);

END;
/

2. Prime number
-- Prime

DECLARE
n NUMBER;
i NUMBER:=2;
flag NUMBER:=0;
BEGIN
n := &n;

WHILE i<=SQRT(n) LOOP

IF MOD(n,i) = 0 THEN
flag :=1;
EXIT;
END IF;

i:=i+1;

END LOOP;
IF flag = 0 THEN
DBMS_OUTPUT.PUT_LINE('Prime');

ELSE
DBMS_OUTPUT.PUT_LINE('Non Prime');
END IF;
END;
/

3. Sum of even numbers upto 100


-- Sum of even numbers

DECLARE
sum NUMBER :=0;
i NUMBER :=2;

BEGIN

LOOP
sum := sum + i;
i := i+2;

EXIT WHEN i>100;

END LOOP;

DBMS_OUTPUT.PUT_LINE('Result is: '||sum);

END;

4. Formatting date and time


DECLARE

BEGIN

DBMS_OUTPUT.PUT_LINE(TO_CHAR(SYSDATE,'DD-MON-YYYY')||'
'||TO_CHAR(SYSTIMESTAMP,'DD-MM-YY HH24:MM:SS:FF'));

END;
/

5. Largest
DECLARE
largest NUMBER := -1 * POWER(10, 25); -- Initialize largest to a very
small number
choice NUMBER := 1; -- Variable to control the loop
(1 to continue, 0 to stop)
n NUMBER; -- Variable to store input number
BEGIN

-- Loop continues as long as choice is 1


WHILE choice = 1 LOOP

-- Prompt the user for the number input


n := &n; -- User input for the number

-- Check if the current number is larger than the largest so far


IF n > largest THEN
largest := n;
END IF;

-- Prompt the user for choice (whether to continue or not)


choice := &choice; -- User input for choice (1 to continue, 0 to
stop)

END LOOP;

-- Output the largest number found


DBMS_OUTPUT.PUT_LINE('The largest number is: ' || largest);

END;
/

5. Reversing a String
DECLARE
str VARCHAR2(50);
rev VARCHAR2(50);
length NUMBER;
i NUMBER;

BEGIN
str := '&str';
length := 5;

FOR i in REVERSE 1..length LOOP


rev := rev || SUBSTR(str,i,1);
END LOOP;

DBMS_OUTPUT.PUT_LINE(rev);
end;
/

6. Calculate Area Function

CREATE OR REPLACE FUNCTION calculate_area(radius NUMBER)


RETURN NUMBER

IS
pi NUMBER:= 3.14159;

BEGIN
return pi*radius*radius;

END calculate_area;
/

BEGIN
DBMS_OUTPUT.PUT_LINE(calculate_area(100));
END;
/

7. Procedure to find sum and product

CREATE OR REPLACE PROCEDURE calculate_sum(x NUMBER, y NUMBER)


IS
sum NUMBER:=0;

BEGIN
DBMS_OUTPUT.PUT_LINE(x+y);
DBMS_OUTPUT.PUT_LINE(x*y);

END calculate_sum;
/

DECLARE
x NUMBER;
y NUMBER;
BEGIN
x:=&x;
y:=&y;
calculate_sum(x,y);
END;

8. Explicit cursor to update salary


-- -- Create the employees table
CREATE TABLE employees (
employee_id NUMBER PRIMARY KEY,
first_name VARCHAR2(50),
last_name VARCHAR2(50),
salary NUMBER,
department_id NUMBER
);

-- -- Insert data into the employees table


INSERT INTO employees (employee_id, first_name, last_name, salary,
department_id) VALUES (100, 'Steven', 'King', 24000, 90);
INSERT INTO employees (employee_id, first_name, last_name, salary,
department_id) VALUES (101, 'Neena', 'Kochhar', 17000, 90);
INSERT INTO employees (employee_id, first_name, last_name, salary,
department_id) VALUES (102, 'Lex', 'De Haan', 17000, 90);
INSERT INTO employees (employee_id, first_name, last_name, salary,
department_id) VALUES (103, 'Alexander', 'Hunold', 9000, 60);
INSERT INTO employees (employee_id, first_name, last_name, salary,
department_id) VALUES (104, 'Bruce', 'Ernst', 6000, 60);
INSERT INTO employees (employee_id, first_name, last_name, salary,
department_id) VALUES (105, 'David', 'Austin', 4800, 60);
INSERT INTO employees (employee_id, first_name, last_name, salary,
department_id) VALUES (106, 'Valli', 'Pataballa', 4800, 60);
INSERT INTO employees (employee_id, first_name, last_name, salary,
department_id) VALUES (107, 'Diana', 'Lorentz', 4200, 60);
INSERT INTO employees (employee_id, first_name, last_name, salary,
department_id) VALUES (108, 'Nancy', 'Greenberg', 12008, 100);
INSERT INTO employees (employee_id, first_name, last_name, salary,
department_id) VALUES (109, 'Daniel', 'Faviet', 9000, 100);
INSERT INTO employees (employee_id, first_name, last_name, salary,
department_id) VALUES (110, 'John', 'Chen', 8200, 100);
INSERT INTO employees (employee_id, first_name, last_name, salary,
department_id) VALUES (111, 'Ismael', 'Sciarra', 7700, 100);
INSERT INTO employees (employee_id, first_name, last_name, salary,
department_id) VALUES (112, 'Jose Manuel', 'Urman', 7800, 100);
INSERT INTO employees (employee_id, first_name, last_name, salary,
department_id) VALUES (113, 'Luis', 'Popp', 6900, 100);
INSERT INTO employees (employee_id, first_name, last_name, salary,
department_id) VALUES (114, 'Den', 'Raphaely', 11000, 30);
INSERT INTO employees (employee_id, first_name, last_name, salary,
department_id) VALUES (115, 'Alexander', 'Khoo', 3100, 30);
INSERT INTO employees (employee_id, first_name, last_name, salary,
department_id) VALUES (116, 'Shelli', 'Baida', 2900, 30);
INSERT INTO employees (employee_id, first_name, last_name, salary,
department_id) VALUES (117, 'Sigal', 'Tobias', 2800, 30);
INSERT INTO employees (employee_id, first_name, last_name, salary,
department_id) VALUES (118, 'Guy', 'Himuro', 2600, 30);

-- -- Now you can use this table and data for practicing cursors
DECLARE

CURSOR emp_cursor IS
SELECT * FROM employees;

emp_record emp_cursor%ROWTYPE;

BEGIN
OPEN emp_cursor;

LOOP
FETCH emp_cursor INTO emp_record;

EXIT WHEN emp_cursor%NOTFOUND;

UPDATE employees
SET salary = emp_record.salary*1.1
where
emp_record.employee_id=employee_id;

END LOOP;

CLOSE emp_cursor;
DBMS_OUTPUT.PUT_LINE('Salaries updated successfully.');

COMMIT;
END;
/

SELECT * FROM EMPLOYEES;

9. Cursor for employees earning more than avg


salary
-- Create the employees table
CREATE TABLE employees (
employee_id NUMBER PRIMARY KEY,
first_name VARCHAR2(50),
last_name VARCHAR2(50),
salary NUMBER,
department_id NUMBER
);

-- Insert data into the employees table


INSERT INTO employees (employee_id, first_name, last_name, salary,
department_id) VALUES (100, 'Steven', 'King', 24000, 90);
INSERT INTO employees (employee_id, first_name, last_name, salary,
department_id) VALUES (101, 'Neena', 'Kochhar', 17000, 90);
INSERT INTO employees (employee_id, first_name, last_name, salary,
department_id) VALUES (102, 'Lex', 'De Haan', 17000, 90);
INSERT INTO employees (employee_id, first_name, last_name, salary,
department_id) VALUES (103, 'Alexander', 'Hunold', 9000, 60);
INSERT INTO employees (employee_id, first_name, last_name, salary,
department_id) VALUES (104, 'Bruce', 'Ernst', 6000, 60);
INSERT INTO employees (employee_id, first_name, last_name, salary,
department_id) VALUES (105, 'David', 'Austin', 4800, 60);
INSERT INTO employees (employee_id, first_name, last_name, salary,
department_id) VALUES (106, 'Valli', 'Pataballa', 4800, 60);
INSERT INTO employees (employee_id, first_name, last_name, salary,
department_id) VALUES (107, 'Diana', 'Lorentz', 4200, 60);
INSERT INTO employees (employee_id, first_name, last_name, salary,
department_id) VALUES (108, 'Nancy', 'Greenberg', 12008, 100);
INSERT INTO employees (employee_id, first_name, last_name, salary,
department_id) VALUES (109, 'Daniel', 'Faviet', 9000, 100);
INSERT INTO employees (employee_id, first_name, last_name, salary,
department_id) VALUES (110, 'John', 'Chen', 8200, 100);
INSERT INTO employees (employee_id, first_name, last_name, salary,
department_id) VALUES (111, 'Ismael', 'Sciarra', 7700, 100);
INSERT INTO employees (employee_id, first_name, last_name, salary,
department_id) VALUES (112, 'Jose Manuel', 'Urman', 7800, 100);
INSERT INTO employees (employee_id, first_name, last_name, salary,
department_id) VALUES (113, 'Luis', 'Popp', 6900, 100);
INSERT INTO employees (employee_id, first_name, last_name, salary,
department_id) VALUES (114, 'Den', 'Raphaely', 11000, 30);
INSERT INTO employees (employee_id, first_name, last_name, salary,
department_id) VALUES (115, 'Alexander', 'Khoo', 3100, 30);
INSERT INTO employees (employee_id, first_name, last_name, salary,
department_id) VALUES (116, 'Shelli', 'Baida', 2900, 30);
INSERT INTO employees (employee_id, first_name, last_name, salary,
department_id) VALUES (117, 'Sigal', 'Tobias', 2800, 30);
INSERT INTO employees (employee_id, first_name, last_name, salary,
department_id) VALUES (118, 'Guy', 'Himuro', 2600, 30);
INSERT INTO employees (employee_id, first_name, last_name, salary,
department_id) VALUES (120, 'Karen', 'Colmenares', 2500, 30);

-- Create the departments table


CREATE TABLE departments (
department_id NUMBER PRIMARY KEY,
department_name VARCHAR2(50)
);

-- Insert data into the departments table


INSERT INTO departments (department_id, department_name) VALUES (30,
'Purchasing');
INSERT INTO departments (department_id, department_name) VALUES (60,
'IT');
INSERT INTO departments (department_id, department_name) VALUES (90,
'Executive');
INSERT INTO departments (department_id, department_name) VALUES (100,
'Finance');

DECLARE
CURSOR emp_cursor IS
SELECT * FROM employees;

emp_record emp_cursor%ROWTYPE;
avg_salary NUMBER;

BEGIN
OPEN emp_cursor;

LOOP
FETCH emp_cursor INTO emp_record;
EXIT WHEN emp_cursor%NOTFOUND;

SELECT
AVG(SALARY) INTO avg_salary
FROM
employees
INNER JOIN
departments
ON
emp_record.department_id = departments.department_id
GROUP BY
departments.department_id;

IF emp_record.salary>avg_salary THEN
DBMS_OUTPUT.PUT_LINE(emp_record.first_name || ' ' ||
emp_record.salary || ' ' || avg_salary);

END IF;
END LOOP;

CLOSE emp_cursor;

END;
/

10. Implicit Cursor for updating depts to same


value
DECLARE
avg_salary NUMBER;

BEGIN

UPDATE departments
SET
department_name='abc';

IF SQL%FOUND then
COMMIT;
DBMS_OUTPUT.PUT_LINE('COMMITTED');
END IF;

END;
/

SELECT * FROM departments;

1. ALTER Questions:
Question 1: Add a column hire_date to the employees table with
DATE type.

ALTER TABLE employees


ADD COLUMN hire_date DATE;

Question 2: Change the status column to have a default value of


'Probation'.
ALTER TABLE employees
ALTER COLUMN status SET DEFAULT 'Probation';

Question 3: Drop the salary column from the employees table.

ALTER TABLE employees


DROP COLUMN salary;

Question 4: Modify the name column to have a length of 100


characters.

ALTER TABLE employees


MODIFY COLUMN name VARCHAR(100);

2. UPDATE Questions:
Question 1: Increase the salary by 10% for all employees in the
'Engineering' department.

UPDATE employees
SET salary = salary * 1.1
WHERE department = 'Engineering';

Explanation: This updates the salary of all employees in the 'Engineering' department by
increasing it by 10%.

Question 2: Set the status to 'Resigned' for the employee with


employee_id 103.

UPDATE employees
SET status = 'Resigned'
WHERE employee_id = 103;

Question 3: Update the department of the employee with


employee_id 101 to 'Sales'.

UPDATE employees
SET department = 'Sales'
WHERE employee_id = 101;
Question 4: Set the status to 'Active' for all employees with a
salary greater than 50000.

UPDATE employees
SET status = 'Active'
WHERE salary > 50000;

3. DELETE Questions:
Question 1: Delete the record of the employee with employee_id
104.

DELETE FROM employees


WHERE employee_id = 104;

Question 2: Remove all employees in the 'Marketing' department.

DELETE FROM employees


WHERE department = 'Marketing';

Question 3: Delete all employees whose status is 'Inactive'.

DELETE FROM employees


WHERE status = 'Inactive';

Question 4: Delete all rows in the employees table.

DELETE FROM employees;

Expected Results After Running the Queries:


1. ALTER Results:
After running the ALTER commands:
The hire_date column will be added to the employees table.
The status column will have a default value of 'Probation' for any new rows
inserted without a value for status .
The salary column will be removed from the table.
The name column will have a maximum length of 100 characters.
2. UPDATE Results:
After Query 1: Employees in 'Engineering' will have their salary increased by
10%.
After Query 2: The status of employee employee_id 103 will change to
'Resigned'.
After Query 3: Employee employee_id 101's department will be changed to
'Sales'.
After Query 4: All employees with a salary greater than 50000 will have their
status updated to 'Active'.
3. DELETE Results:
After Query 1: Employee employee_id 104 will be deleted.
After Query 2: All employees in the 'Marketing' department will be removed.
After Query 3: All employees with the status 'Inactive' will be deleted.
After Query 4: The entire employees table will be emptied, deleting all rows.

Note:
Be careful when using DELETE without a WHERE clause, as it will remove all data from the
table. Always verify your WHERE conditions to avoid unintentional data loss.

You might also like