0% found this document useful (0 votes)
46 views12 pages

DBMS Worksheet 3arjun

The document contains a worksheet with SQL queries and PL/SQL programs related to database management systems (DBMS). It includes: 1) Creating tables, inserting data, updating and deleting records from the DEPARTMENT and EMP tables 2) Writing a PL/SQL program to demonstrate using the LOOP and EXIT statements 3) Writing a PL/SQL program to calculate the factorial of a number
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 views12 pages

DBMS Worksheet 3arjun

The document contains a worksheet with SQL queries and PL/SQL programs related to database management systems (DBMS). It includes: 1) Creating tables, inserting data, updating and deleting records from the DEPARTMENT and EMP tables 2) Writing a PL/SQL program to demonstrate using the LOOP and EXIT statements 3) Writing a PL/SQL program to calculate the factorial of a number
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/ 12

DBMS

WORKSHEET – 3

Name : arjun karnwal Branch : CSE


UID : 20bcs3253 Subject : DBMS

1. Create a department table with the following structure.


Name Type
Deptno Number
Deptname Varchar2(10)
location Varchar2(10)

Sol:

CREATE TABLE department_1071(deptno NUMBER,deptname VARCHAR2(10),location VARCHAR2(10));

DESC department_1071;
a) Add column designation to the department table.

Sol: ALTER TABLE department_1071 ADD (designation VARCHAR2(10));

b) Insert values into the table.

Sol: INSERT INTO department_1071 VALUES(9,'accounting','hyderabad','manager');


INSERT INTO department_1071 VALUES(10,'research','chennai','professor');
INSERT INTO department_1071 VALUES(11,'sales','banglore','salesman');
INSERT INTO department_1071 VALUES(12,'operations','mumbai','operator');
INSERT INTO department_1071 VALUES(9,'accounting','chennai','manager');
SELECT * FROM department_1071;

c) List the records of the debt table grouped by deptno.

Sol: SELECT deptno,deptname FROM department_1071


GROUP BY deptno,deptname;
d) Update the record where deptno is 9.

Sol: UPDATE department_1071 SET designation = 'accountant'


WHERE deptno = 9;
SELECT * FROM department_1071;

e) Delete any column data from the table.

Sol: ALTER TABLE department_1071 DROP(designation);


SELECT * FROM department_1071;
f) List all the records of the database in descending order.

Sol: SELECT * FROM department_1071


ORDER BY deptno,deptname,location DESC;
2. Create table EMP with the following description :
Name Type
EMPNO NOT NULL NUMBER(4)
ENAME VARCHAR2(10)
JOB VARCHAR2(9)
MGR NUMBER(4)
HIREDATE DATE
SAL NUMBER(7,2)
COMM NUMBER(7,2)
DEPTNO NUMBER(3)
AGE NUMBER(3)
ESAL NUMBER

Sol:

CREATE TABLE EMP_1071 (EMPNO NUMBER(4) NOT NULL, ENAME VARCHAR2(10), JOB VARCHAR2(9), MGR NUMBER(4),
HIREDATE DATE, SAL NUMBER(7, 2), COMM NUMBER(7, 2), DEPTNO NUMBER(2),
AGE NUMBER(3), ESAL NUMBER );

a) Get the description EMP table.

Sol: DESC Emp_1071;


b) List all employees details.

Sol:
INSERT INTO EMP_1071 VALUES (7369, 'SMITH','CLERK',7902, TO_DATE('17-DEC-1980',
'DD-MON-YYYY'), 800, NULL, 20, 18, 5);
INSERT INTO EMP_1071 VALUES (7499, 'ALLEN','SALESMAN',7698, TO_DATE('20-FEB-1981',
'DD-MON-YYYY'), 1600, 300, 30, 19, 6);
INSERT INTO EMP_1071 VALUES (7521, 'WARD', 'SALESMAN',7698, TO_DATE('22-FEB-1981',
'DD-MON-YYYY'), 1250, 500, 30, 20, 7);
INSERT INTO EMP_1071 VALUES (7566, 'JONES', 'MANAGER', 7839, TO_DATE('2-APR-1981',
'DD-MON-YYYY'), 2975, NULL, 20, 21, 8);
INSERT INTO EMP_1071 VALUES (7654, 'MARTIN', 'SALESMAN', 7698, TO_DATE('28-SEP-1981',
'DD-MON-YYYY'), 1250, 1400, 30, 22, 9);
INSERT INTO EMP_1071 VALUES (7698, 'BLAKE', 'MANAGER', 7839, TO_DATE('1-MAY-1981',
'DD-MON-YYYY'), 2850, NULL, 30, 23, 4);
INSERT INTO EMP_1071 VALUES (7782, 'CLARK', 'MANAGER', 7839, TO_DATE('9-JUN-1981',
'DD-MON-YYYY'), 2450, NULL, 10, 17, 8);
INSERT INTO EMP_1071 VALUES (7788, 'SCOTT', 'ANALYST', 7566, TO_DATE('09-DEC-1982',
'DD-MON-YYYY'), 3000, NULL, 20, 25, 4);
INSERT INTO EMP_1071 VALUES (7839, 'KING', 'PRESIDENT', NULL, TO_DATE('17-NOV-1981',
'DD-MON-YYYY'), 5000, NULL, 10, 19, 3);
INSERT INTO EMP_1071 VALUES (7844, 'TURNER', 'SALESMAN', 7698, TO_DATE('8-SEP-1981',
'DD-MON-YYYY'), 1500, 0, 30, 22, 2);
INSERT INTO EMP_1071 VALUES (7876, 'ADAMS', 'CLERK', 7788, TO_DATE('12-JAN-1983',
'DD-MON-YYYY'), 1100, NULL, 20, 30, 7);
INSERT INTO EMP_1071 VALUES (7900, 'JAMES', 'CLERK', 7698, TO_DATE('3-DEC-1981',
'DD-MON-YYYY'), 950, NULL, 30, 14, 5);
INSERT INTO EMP_1071 VALUES (7902, 'FORD', 'ANALYST', 7566, TO_DATE('3-DEC-1981',
'DD-MON-YYYY'), 3000, NULL, 20, 30, 6);
INSERT INTO EMP_1071 VALUES (7934, 'MILLER', 'CLERK', 7782, TO_DATE('23-JAN-1982',
'DD-MON-YYYY'), 1300, NULL, 10, 28, 4);

SELECT * FROM Emp_1071;


c) List all employee names and their salaries, whose salary lies between 1500/- and 3500/-
both inclusive.

Sol: SELECT ename FROM Emp_1071 WHERE sal BETWEEN 1500 AND 3500;

d) List all employee names and their and their manager whose manager is 7902 or 7566 0r 7789.

Sol: SELECT ename FROM Emp_1071 WHERE mgr IN(7602,7566,7789);


e) List all employees who belongs to the department 10 or 20

Sol: SELECT ename FROM Emp_1071 WHERE deptno IN(10,20);


3. Write a program in PL/SQL to print the value of a variable inside and outside a loop using LOOP
EXIT statement.

Sol:

20BET1071
DECLARE
n NUMBER := 0;
BEGIN
LOOP
DBMS_OUTPUT.PUT_LINE('The value of n inside the loop is: ' || TO_CHAR(n));
n := n + 1;
EXIT WHEN n > 5;
END LOOP;
DBMS_OUTPUT.PUT_LINE('The value of n after exit from the loop is: ' || TO_CHAR(n));
END;
4. Write a program in PL/SQL to print factorial of a number.

Sol:

20BET1071
DECLARE
num NUMBER := 8;
fact NUMBER := 1;
temp NUMBER;
BEGIN
temp :=num;
WHILE( temp>0 )
LOOP
fact := fact*temp;
temp := temp-1;

END LOOP;
DBMS_OUTPUT.PUT_LINE('factorial of '|| num || ' is ' || fact);
END;

You might also like