RUSTAMJI INSTITUTE OF TECHNOLOGY
BSF ACADEMY, TEKANPUR
Lab File for
CS502 (Database Management System)
Submitted by
Shubham Goswami (0902CS171054)
B.Tech. Computer Science & Engineering 5th Semester
(2017-2021 batch)
Subject Teacher File Checked by
Dr. Jagdish Makhijani Mr. Yashwant Pathak
TABLE OF CONTENTS
S. No. Practical Description Page Nos.
1 Introduction to DDL Commands with example
2 Introduction to DDL Commands with example
3 Use of where, like, in, between, not in select command with
example
4 Use of select subquery in SELECT, INSERT, DELETE
command
5 Write Commands for:
i. Create table given in sample schema (next page)
ii. Insert data as given on next page.
iii. Delete duplicate row from the table.
iv. Display the alternate row from table.
v. Delete alternate row from table.
vi. Update multiple rows in using single update
statement.
vii. Find the third highest paid and third lowest paid
salary.
viii. Display the 3rd, 4th, 9th rows from table.
ix. Display the ename, which is start with j, k, l or m.
x. Show all employees who were hired the first half of
the month.
xi. Display the three record in the first row and two
records in the second row and one record in the
third row in a single sql statements.
6 Write a pl/sql for select, insert, update and delete
statements.
7 Write a pl/sql block to delete a record. If delete operation is
successful return 1 else return 0.
8 Display name, hire date of all employees using cursors.
9 Display details of first 5 highly paid employees using
cursors.
10 Write a database trigger which fires if you try to insert,
update, or delete after 7’o’ clock.
11 Write a data base trigger, which acts just like primary key
and does not allow duplicate values.
Practical No.: 6
Problem Description: Write a pl/sql for select, insert, update and delete statements.
Solution:
Table : Friends
Name Gender Location Age Mobile Company
Ally Female New York 24 909549867 Abbott Laboratories
Francesco Male Chicago 30 944983414 Ace Hardware
Lorenzo Male San Francisco 23 972002668 Alliance Rubber Company
Chloe Female Dallas 29 913971025 American Express
Frank Male Huntington 32 992498262 Yahoo!
Aaliyah Female Washington 25 901802210 Yahoo!
INSERT Command :-
INSERT INTOfriends (name, age, gender, location, company,
mobile) VALUES('Henery',25, 'Male', 'Fort Worth', 'Cartoon
Network Studios', '923027416');
UPDATE Command :-
UPDATE friends SET mobile = '996452314' WHERE name='Ally';
DELETE Command:-
DELETE FROM friends WHERE age > 30;
File Submitted by: Shubham Goswami (0902CS171054)
Output:
Output for INSERT command:-
Henry Male Fort Worth 25 923027416 Cartoon Network Studios
Output for UPDATE command:-
Ally Female New York 24 99645231 Abbott Laboratories
4
Output for DELETE command:-
Name Gender Location Age Mobile Company
Ally Female New York 24 909549867 Abbott Laboratories
Lorenzo Male San Francisco 23 972002668 Alliance Rubber Company
Aaliyah Female Washington 25 972002668 Alliance Rubber Company
Chloe Female Dallas 29 901802210 Yahoo!
File Submitted by: Shubham Goswami (0902CS171054)
Practical No.: 8
Problem Description: Display name, hire date of all employees using cursors.
Table : Employee
SELECT first_name, last_name, hire_date
FROM employees
WHERE YEAR(hire_date) LIKE '1987%';
Output:
Steven King 1987-06-17
Neena Kochhar 1987-06-18
Lex De Haan 1987-06-19
Alexander Hunold 1987-06-20
Bruce Ernst 1987-06-21
David Austin 1987-06-22
Valli Pataballa 1987-06-23
Diana Lorentz 1987-06-24
Nancy Greenberg 1987-06-25
Daniel Faviet 1987-06-26
File Submitted by: Shubham Goswami (0902CS171054)
Practical No.: 9
Problem Description: Display details of first 5 highly paid employees using cursors.
Solution:
ENAME EMPNO SAL
---------- ----------- -------------------------------
KING 7839 5000
SCOTT 7788 3000
FORD 7902 3000
JONES 7566 2975
BLAKE 7698 2850
CLARK 7782 2450
ALLEN 7499 1600
TURNER 7844 1500
MILLER 7934 1300
WARD 7521 1250
MARTIN 7654 1250
ADAMS 7876 1100
JAMES 7900 950
SMITH 7369 800
PL/SQL Block
DECLARE
CURSOR c1 is
SELECT ename, empno, sal FROM emp
ORDER BY sal DESC; -- start with highest paid employee
File Submitted by: Shubham Goswami (0902CS171054)
my_ename CHAR(10);
my_empno NUMBER(4);
my_sal NUMBER(7,2);
BEGIN
OPEN c1;
FOR i IN 1..5 LOOP
FETCH c1 INTO my_ename, my_empno, my_sal;
EXIT WHEN c1%NOTFOUND; /* in case the number requested */
/* is more than the total */
/* number of employees */
INSERT INTO temp VALUES (my_sal, my_empno, my_ename);
COMMIT;
END LOOP;
CLOSE c1;
END;
Output:
COL1 COL2 MESSAGE
5000 7839 KING
3000 7902 FORD
3000 7788 SCOTT
2975 7566 JONES
2850 7698 BLAKE
File Submitted by: Shubham Goswami (0902CS171054)
Practical No.: 10
Problem Description: W
rite a database trigger which fires if you try to insert, update, or
delete after 7’o’ clock.
Solution:
DECLARE
A VARCHAR2(10);
BEGIN
SELECT TO_CHAR(SYSDATE,'HH:MI') INTO A FROM DUAL;
IF A > '07:00' then
RAISE_APPLICATION_ERROR(-20500,'YOU CANT DO THIS OPERATION NOW');
END IF;
END
OUTPUT : -
SQL> insert into emp(empno,deptno) values(7788,20);
insert into emp(empno,deptno) values(7788,20)
ERROR at line 1:
ORA-20500: YOU CAN’T DO THIS OPERATION NOW
ORA-06512: at "GEETHA.GEETIME", line 6
ORA-04088: error during execution of trigger 'GEETHA.GEETIME
File Submitted by: Shubham Goswami (0902CS171054)