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

dbms-lab

The document contains various SQL and PL/SQL code snippets for tasks such as finding Armstrong numbers, counting vowels, checking prime numbers, and managing employee data in a database. It includes procedures for converting temperatures, swapping numbers, and calculating factorials, as well as creating and manipulating tables for employee, department, and project data. Additionally, it demonstrates how to retrieve and update employee information based on specific conditions.

Uploaded by

bhagyarajgarugu
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)
3 views

dbms-lab

The document contains various SQL and PL/SQL code snippets for tasks such as finding Armstrong numbers, counting vowels, checking prime numbers, and managing employee data in a database. It includes procedures for converting temperatures, swapping numbers, and calculating factorials, as well as creating and manipulating tables for employee, department, and project data. Additionally, it demonstrates how to retrieve and update employee information based on specific conditions.

Uploaded by

bhagyarajgarugu
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/ 4

1. Find Armstrong Numbers Between 1 and 1000 END; / DBMS_OUTPUT.

PUT_LINE(result);

DECLARE 4. Store First 10 Even Numbers in a Table and END; /


display
num NUMBER; temp NUMBER; 1. To Add Two Numbers
DECLARE
sum NUMBER; digit NUMBER; DECLARE
TYPE even_table IS TABLE OF NUMBER;
BEGIN num1 NUMBER := 10; num2 NUMBER := 20;
evens even_table := even_table();
FOR num IN 1..1000 LOOP sum NUMBER;
BEGIN
temp := num; sum := 0; BEGIN
FOR i IN 1..10 LOOP
FOR i IN 1..LENGTH(num) LOOP sum := num1 + num2;
evens.EXTEND;
digit := MOD(temp, 10); DBMS_OUTPUT.PUT_LINE('Sum: ' || sum);
evens(i) := i * 2; END LOOP;
sum := sum + POWER(digit, END; /
LENGTH(num)); FOR i IN 1..10 LOOP
2. Prime Number
temp := TRUNC(temp / 10); DBMS_OUTPUT.PUT_LINE(evens(i));
DECLARE
END LOOP; END LOOP; END; /
num NUMBER := 17;
IF sum = num THEN 5. Convert Temp (Celsius to Fahrenheit)
i NUMBER;
DBMS_OUTPUT.PUT_LINE(num); CREATE OR REPLACE PROCEDURE
Convert_Temp(cel NUMBER) IS is_prime BOOLEAN := TRUE;
END IF;
BEGIN BEGIN
END LOOP;
DBMS_OUTPUT.PUT_LINE(cel || '°C = ' || ((cel * IF num < 2 THEN
END; / 9/5) + 32) || '°F');
is_prime := FALSE;
2. Count the Number of Vowels END Convert_Temp; /
ELSE
DECLARE -- Call Procedure
FOR i IN 2 .. num/2 LOOP
str VARCHAR2(100) := 'Hello World'; BEGIN
IF MOD(num, i) = 0 THEN
count_vowel NUMBER := 0; Convert_Temp(25); END;
is_prime := FALSE;
BEGIN 6. Swap Two Numbers Without Using a 3 rd variable
EXIT;
FOR i IN 1..LENGTH(str) LOOP DECLARE
END IF;
IF INSTR('AEIOUaeiou', SUBSTR(str, i, 1)) > 0 a NUMBER := 10; b NUMBER := 20;
THEN END LOOP;
BEGIN
count_vowel := count_vowel + 1; END IF;
a := a + b; b := a - b; a := a - b;
END IF; IF is_prime THEN
DBMS_OUTPUT.PUT_LINE('a=' || a || ', b=' || b);
END LOOP; DBMS_OUTPUT.PUT_LINE(num || ' is a Prime
END; / Number');
DBMS_OUTPUT.PUT_LINE('Vowel Count: ' ||
count_vowel); -- 2: Create the add_employee function ELSE

END; / CREATE OR REPLACE FUNCTION add_employee ( DBMS_OUTPUT.PUT_LINE(num || is NOT a


Prime Number');
3. Find the Second Largest Number p_emp_id NUMBER, p_emp_name VARCHAR2,
END IF; END; /
DECLARE p_salary NUMBER, p_is_active CHAR
3. Find Factorial of a Number
TYPE num_array IS TABLE OF NUMBER; ) RETURN VARCHAR2
DECLARE
arr num_array := num_array(10, 45, 67, 89, 23, IS BEGIN
78); num NUMBER := 5; fact NUMBER := 1;
INSERT INTO employee (emp_id, emp_name,
max1 NUMBER := -99999; salary, hire_date, is_active) i NUMBER;

max2 NUMBER := -99999; VALUES (p_emp_id, p_emp_name, p_salary, BEGIN


SYSDATE, p_is_active);
BEGIN FOR i IN REVERSE 1 .. num LOOP
RETURN 'Employee added successfully';
FOR i IN 1..arr.COUNT LOOP fact := fact * i; END LOOP;
EXCEPTION
IF arr(i) > max1 THEN DBMS_OUTPUT.PUT_LINE('Factorial of ' || num ||
WHEN OTHERS THEN ' is ' || fact);
max2 := max1;
RETURN 'Error adding employee: ' || END; /
max1 := arr(i); SQLERRM;
4. Print Table of a Number
ELSIF arr(i) > max2 THEN END; /
DECLARE
max2 := arr(i); 3: Call the fxn to insert a sample employee
num NUMBER := 5; i NUMBER;
END IF; DECLARE result VARCHAR2(100);
BEGIN
END LOOP; BEGIN
FOR i IN 1 .. 10 LOOP
DBMS_OUTPUT.PUT_LINE('Second Largest: ' || result := add_employee(1, 'John Doe', 50000,
max2); 'Y');
DBMS_OUTPUT.PUT_LINE(num || ' x ' || i || ' =
' || (num * i));
END LOOP; END; / 8. Reverse a String INSERT INTO employee (emp_id, emp_name,
salary, dno)
5. Reverse of a Number CREATE OR REPLACE FUNCTION
reverse_string(p_str IN VARCHAR2) RETURN
DECLARE VARCHAR2

num NUMBER := 12345; IS

rev NUMBER := 0; digit NUMBER; rev_str VARCHAR2(100) := '';

BEGIN len NUMBER;

WHILE num > 0 LOOP BEGIN

digit := MOD(num, 10); len := LENGTH(p_str);

rev := (rev * 10) + digit; FOR i IN REVERSE 1 .. len LOOP

num := TRUNC(num / 10); rev_str := rev_str || SUBSTR(p_str, i, 1);

END LOOP; END LOOP;

DBMS_OUTPUT.PUT_LINE('Reversed Number: ' || RETURN rev_str;


rev);
END; /
END;
-- Calling the function
/
DECLARE
6. Fibonacci Series
result VARCHAR2(100);
DECLARE
BEGIN
n NUMBER := 10;
result := reverse_string('Hello');
a NUMBER := 0;
DBMS_OUTPUT.PUT_LINE('Reversed String: ' ||
b NUMBER := 1; result);

c NUMBER; END; /

i NUMBER;

BEGIN 9. Retrieve Employees in DNO=5 and


Increase Salary by 10%
DBMS_OUTPUT.PUT_LINE(a); SELECT 103, 'Alice Johnson', 55000, 5 FROM
-- Step 1: Create Employee Table (if not exists) DUAL WHERE NOT EXISTS (SELECT 1 FROM
DBMS_OUTPUT.PUT_LINE(b); employee WHERE emp_id = 103);
BEGIN
COMMIT;
EXECUTE IMMEDIATE '
FOR i IN 3 .. n LOOP EXCEPTION
CREATE TABLE employee (
c := a + b; WHEN OTHERS THEN
emp_id NUMBER(6) PRIMARY KEY,
DBMS_OUTPUT.PUT_LINE(c); DBMS_OUTPUT.PUT_LINE('Error inserting
emp_name VARCHAR2(50), data: ' || SQLERRM);
a := b; b := c;
salary NUMBER(8,2), END;
END LOOP;
dno NUMBER(3) NOT NULL /
END; /
)'; -- Step 3: Update Salary for Employees in DNO =
5
7. Check Number is Odd or Even
EXCEPTION
BEGIN
CREATE OR REPLACE PROCEDURE
check_even_odd(p_num IN NUMBER) WHEN OTHERS THEN
UPDATE employee
IS IF SQLCODE != -955 THEN -- Ignore "table
already exists" error SET salary = salary * 1.10
BEGIN
RAISE; WHERE dno = 5;
DBMS_OUTPUT.PUT_LINE('Salaries updated
IF MOD(p_num, 2) = 0 THEN
END IF; successfully for employees in DNO = 5');

DBMS_OUTPUT.PUT_LINE(p_num || ' is Even');


END; / EXCEPTION

ELSE
-- Step 2: Insert Sample Data (if table is empty) WHEN OTHERS THEN

DBMS_OUTPUT.PUT_LINE(p_num || ' is Odd');


BEGIN DBMS_OUTPUT.PUT_LINE('Error: ' ||
SQLERRM);
END IF;
INSERT INTO employee (emp_id, emp_name,
salary, dno) END;
END;

SELECT 101, 'John Doe', 50000, 5 FROM DUAL /


/
WHERE NOT EXISTS (SELECT 1 FROM employee
WHERE emp_id = 101);
-- Calling the procedure

INSERT INTO employee (emp_id, emp_name,


BEGIN
salary, dno)

check_even_odd(25);
SELECT 102, 'Jane Smith', 60000, 3 FROM DUAL
WHERE NOT EXISTS (SELECT 1 FROM employee
END; WHERE emp_id = 102);

/
INSERT INTO project (Pname, Pnumber, Plocation, SELECT d.Dnumber
Dnum)
FROM Department d
create schema 23mis7253;
VALUES
WHERE d.Dname = 'Research'
use 23mis7253;
('Delta', 16, 'Chennai', 4);
);
CREATE TABLE employee (
-- Create works_on table
-- 6)For every project located in 'Stafford', list the
Fname varchar(15) NOT NULL, Minit
CREATE TABLE works_on ( project number, the controlling department
varchar(1),
number, and the department manager's last
Essn varchar(9) NOT NULL, name, address, and birthdate:
Lname varchar(15) NOT NULL,

Pno int NOT NULL, Hours float NOT NULL, SELECT P.Pnumber, P.Dnum, D.Mgr_Ssn, E.Lname,
SSN varchar(20) NOT NULL, E.Address, E.Bdate
PRIMARY KEY (Essn, Pno),
Bdate date, Address varchar(30), FROM PROJECT P, DEPARTMENT D, EMPLOYEE E
FOREIGN KEY (Essn) REFERENCES
Sex varchar(1), Salary float, WHERE P.Plocation = 'Chennai'
employee(SSN),

Super_ssn varchar(9),no int NOT NULL, AND P.Dnum = D.Dnumber


FOREIGN KEY (Pno) REFERENCES
project(Pnumber)
PRIMARY KEY (SSN), AND D.Mgr_Ssn = E.Ssn;
);
FOREIGN KEY (Super_ssn) REFERENCES -- 7) retrive d location for research and HR
employee(SSN) department
INSERT INTO works_on (Essn, Pno, Hours)

); select Dlocation from dept_locations


VALUES

INSERT INTO employee (Fname, Minit, Lname, where Dnumber =(select Dnumber from
-- Create dependent table
SSN, Bdate, Address, Sex, Salary, Super_ssn, Dno) department where Dname='Research' or 'HR');
CREATE TABLE dependent (
VALUES -- 8) Find the employee who has no supervisor
Essn varchar(9) NOT NULL,
('David', 'T', 'Miller', '159753456', '1975-09-11', select Fname from employee
'101 Maple Ave, Seattle, WA', 'M', 75000.0,
Dependent_name varchar(15) NOT NULL,
'741852963', 1);
where super_ssn is NULL;
Sex varchar(1),
CREATE TABLE department (
-- 9)Retrieve the name of each employee who has
a dependent with the same first name as the
Bdate date,
Dname varchar(15) NOT NULL, Dnumber int employee:
NOT NULL,
Relationship varchar(8),
SELECT e.Fname, e.Lname FROM employee e
Mgr_ssn varchar(9) NOT NULL, Mgr_start_date
date, PRIMARY KEY (Essn, Dependent_name),
WHERE e.Fname IN (SELECT d.Dependent_name
FROM dependent d );
PRIMARY KEY (Dnumber), UNIQUE (Dname), FOREIGN KEY (Essn) REFERENCES
employee(SSN)
-- WEEK3-----
FOREIGN KEY (Mgr_ssn) REFERENCES
employee(SSN) );
-- 1)For each employee retrieve employee's name
and name of his/her supervisor
); INSERT INTO dependent (Essn, Dependent_name,
Sex, Bdate, Relationship)
SELECT E.Fname, E.Lname, S.Fname, S.Lname
INSERT INTO department (Dname, Dnumber, FROM EMPLOYEE E, EMPLOYEE S
Mgr_ssn, Mgr_start_date) VALUES

WHERE E.Super_ssn = S.Ssn;


VALUES ('159753456', 'Sophia', 'F', '2005-03-22',
'Daughter');
-- 2. Make a list of all project numbers for projects
('Marketing', 5, '123456789', '2025-05-12'); that involve an employee whose last name is
select * from dependent;
'Smith' as a manager of the department that
-- dept_locations table controls the project:
-- 1)Display all the employees’ information:
CREATE TABLE dept_locations ( SELECT Pnumber FROM PROJECT
SELECT * FROM employee;
Dnumber int NOT NULL, Dlocation WHERE Dnum = (SELECT Dnumber FROM
varchar(15), -- 2) Display the salaries of employees without DEPARTMENT WHERE Mgr_ssn = (SELECT Ssn
duplicates FROM EMPLOYEE WHERE Lname = 'Smith'));
PRIMARY KEY (Dnumber, Dlocation),
SELECT DISTINCT Salary FROM employee;
FOREIGN KEY (Dnumber) REFERENCES
department(Dnumber) -- 3) Find employee names with a salary greater -- 3. Make a list of all project numbers for projects
than 25000 that involve an employee whose last name is
); 'Smith' as a worker or as a manager of the
SELECT Fname, Lname department that controls the project:
INSERT INTO dept_locations (Dnumber, Dlocation)
FROM employee SELECT DISTINCT Pnumber
VALUES (5, 'Delhi');
WHERE Salary > 25000; FROM PROJECT
SELECT * FROM dept_locations;
-- 4)Find employee names whose salary is in the WHERE Dnum IN (SELECT Dnumber FROM
range between 30000 and 70000 DEPARTMENT WHERE Mgr_ssn IN (SELECT Ssn
-- project table
FROM EMPLOYEE WHERE Lname = 'Smith'))
SELECT Fname, Lname, Salary
CREATE TABLE project (
OR Pnumber IN (SELECT Pno FROM WORKS_ON
FROM employee WHERE Essn IN (SELECT Ssn FROM EMPLOYEE
Pname varchar(15) NOT NULL,
WHERE Lname = 'Smith'));
WHERE Salary BETWEEN 30000 AND 70000;
Pnumber int NOT NULL, Plocation varchar(15),
-- 4) Retrieve names of employees who have no
-- 5)Retrieve the name and address of all dependents
Dnum int NOT NULL,
employees who work for the 'Research'
department: SELECT Fname as Employee_name FROM
PRIMARY KEY (Pnumber), UNIQUE (Pname), Employee
SELECT e.Fname, e.Lname, e.Address
FOREIGN KEY (Dnum) REFERENCES WHERE SSN NOT IN (SELECT Essn FROM
department(Dnumber) dependent);
FROM Employee e

);
WHERE e.Dno IN (
-- 5)Retrieve the social security numbers of all WHERE Bdate LIKE '198_--';
employees who work on project numbers 12, 13,
or 14: -- 8) Display department names that start with 'R'

SELECT DISTINCT Essn FROM works_on SELECT Dname from department

WHERE Pno IN (12, 13, 14); WHERE Dname Like 'R%';

-- Experiment 5 ---

-- 1. Change the location and controlling


department number of project number 10 to
-- 1) Retrive Max, min, avg salary and total 'Bellaire' and 5, respectively. (UPDATE QUERY)
number of employees
UPDATE Project
SELECT MAX(Salary) AS Max_Salary,
SET Plocation = 'Bellaire', Dnum = 5
MIN(Salary) AS Min_Salary,
WHERE Pnumber = 12;
AVG(Salary) AS Avg_Salary
select * from Project;
,COUNT(*) AS Total_Employees
-- 2. Give all employees in the
FROM Employee; 'Research' department a 10% raise in salary.
(UPDATE QUERY)
-- 2) Find max, min, avg salary and total number
of employees in the Research department UPDATE Employee

SELECT MAX(Salary) AS Max_Salary, SET salary = salary * 1.10

MIN(Salary) AS Min_Salary, WHERE Dno = (SELECT Dnumber FROM


department WHERE Dname = 'Research');
AVG(Salary) AS Avg_Salary,
-- 3. Delete details of an employee
COUNT(*) AS Total_Employees whose last name is ‘Brown’.

FROM Employee DELETE FROM Employee

WHERE Dno = (SELECT Dnumber FROM WHERE Lname = 'Brog';


Department WHERE Dname = 'Research');
-- 4. Delete all employee details who
-- 3) Retrieve department number, number of work in the ‘Research’ department.
employees, and average salary for each
department DELETE FROM EMPLOYEE

SELECT Dnumber,Dname, WHERE Dno = (SELECT Dnumber FROM


department WHERE Dname = 'Research');
COUNT(*) AS no_of_Employees,
-- 5. Alter the table Department and add
AVG(Salary) AS avg_Salary column DepartmentPhoneNum and insert values.

FROM department,employee alter table Department

GROUP BY Dname; add Departmentphonenum varchar(20);

-- 4) Retrieve a list of employees and the projects update Department


each works on, ordered by the employee's
department set Departmentphonenum= ('123456789');

SELECT E.Fname, E.Lname, E.Dno, P.Pname -- 6. Rename Table Department as DEPT.

FROM Employee E, Works_On W, Project P RENAME TABLE Department TO DEPTT;

WHERE E.SSN = W.ESSN -- 7. Delete the table ‘Dependent’.

AND W.Pno = P.Pnumber DROP TABLE Dependent;

ORDER BY E.Dno;

-- 5) For each project with more than two


employees, retrieve project number, name, and
the number of employees working on it

select count(*),pname,project.pnumber

from project,works_on

where project.pnumber=works_on.pno

group by project.pnumber

having count(*)>2;

-- 6) Retrieve all employees whose address


contains "Houston" or "Texas"

SELECT * FROM Employee

WHERE Address LIKE '%Houston%' OR Address


LIKE '%Austin%';

-- 7) Retrieve employees who were born during


1960

SELECT * FROM Employee

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy