You are on page 1of 44

DATABASE MANAGEMENT

SYSTEM

Practical
File

Submitted To: Submitted
By:
ARUN SHARMA RAJESH KUMAR
SINGH

LECTURER ROLL NO: -
A1000708016

Amity Institute of Information Technology MCA (SEM – III), (08-
11)

1|Page
Amity Institute of Information Technology

Amity University, Uttar Pradesh

INDEX
SL NO QUESTION PAGE NO SIGNATURE
1 QUERY 1 3

2 QUERY 2 7

3 QUERY 3 11

4 QUERY 4 16

5 QUERY 5 17

6 QUERY 6 18

7 QUERY 7 19

8 QUERY 8 20

9 QUERY 9 21

10 QUERY 10 22

11 QUERY 11 23

12 QUERY 12 24

13 QUERY 13 25

14 QUERY 14 26

15 QUERY 15 28

16 PROCEDURE 34

17 FUNCTION 35

18 EXCEPTION 37

19 CURSOR 39

20 TRIGGER 40

2|Page
Program -1

Q1. Given the table STUDENT :

Student No. Class Name GAME Grade1 SUPW Grade2
22 7 Sameer Cricket B Photography A
11 8 Sujit Tennis A Gardening C
12 7 Kamal Swimming B Photography B

13 7 Veena Tennis C Cooking A
14 9 Archana BasketBall A Literature A

15 10 Arpit Cricket A Gardening C

(i) Display the names of the students who are getting a grade C in either GAME or SUPW.

(ii) Display the number of students getting grade A in cricket.

(iii) Display the different games offered in the school.

(iv) Display the SUPW taken by the students, whose name starts with ‘A’.

(v) Add a new column named ‘Marks’.

(vi) Assign a value 200 for Marks for all those who are getting grade B or above in GAME.

(vii) Arrange the whole table in alphabetical order to SUPW.

3|Page
Ans (1) : - Create table STUDENT

values (Student_no number (10),

Class number(10),

Name varchar2(10),

GAME varchar2(10),

(i) Select name from STUDENT

where Grade1 = ‘C’ or Grade2 = ’C’ ;

STUDENT_NO CLASS NAME GAME GRADE1 SUPW GRADE2

13 8 kanjar Tennis A Gardening C

23 7 teena Tennis C playing A

Sardar

16 10 Cricket A Gardening C

(ii) Select COUNT(Name) from STUDENT

where Grade1=’A’ and Game= ’Cricket’;

COUNT(*)

1

4|Page
(iii) Select distinct(Games) from STUDENT;

GAME

baseball

Cricket

badminton

Tennis

Literature

Gardening

(iv) Alter table STUDENT

Add (Marks number(10));

5|Page
(v) Update STUDENT

Set Marks= 200 where Grade1=’B’ or ‘A’ ;

STUDENT_NO CLASS NAME GAME GRADE1 SUPW GRADE2 MARKS

10 7 Sameer Cricket B Photography A 200

11 8 Sujit Tennis A Gardening C

12 7 kamal Swimming B Photography B 200

13 7 Veena Tennis C Cooking A 200

14 9 Archana Basket Ball A Literature A

15 10 Arpit Cricket A Gardening C

(vi) Select * from STUDENT

order by SUPW desc ;

STUDENT_NO CLASS NAME GAME GRADE1 SUPW GRADE2 MARKS

6|Page
13 7 teena Tennis C Cooking A 200

11 8 meena Tennis A Gardening C

15 10 sood Cricket A Gardening C

14 9 Archana Basket Ball A Literature A

10 7 Sameer Cricket B Photography A 200

12 7 kamal Swimming B Photography B 200

Program-2

Q2. Given the table SPORTS :
Student No. Class Name GAME1 Grade1 GAME2 Grade2
10 7 Sameer Cricket B Swimming A
11 8 Sujit Tennis A Skating C
12 7 Kamal Swimming B Football B

13 7 Veena Tennis C Tennis A
14 9 Archana Basket A Cricket A
Ball
15 10 Arpit Cricket A Atheletics C

7|Page
(i) Display the names of the students who are getting a grade C in either GAME1 or GAME2.

(ii) Display the number of students getting grade A in cricket.

(iii) Display the names of the students who have same game for both GAME1 and GAME2.

(iv) Display the games taken by the students, whose name starts with ‘A’.

(v) Add a new column named ‘Marks’.

(vi) Assign a value 200 for Marks for all those who are getting grade B or above in GAME.

(vii) Arrange the whole table in alphabetical order of GAME.

Ans(2) :- Create table SPORTS

values (Student_no number (10),

Class number(10),

GAME2 varchar2(10),

Grade2 varchar2(2)

);

8|Page
Insert into SPORTS

values ( &Student_no , &Class, ’& Name’, ‘&GAME1’ , ‘&Grade1’ , ‘ &GAME2 ’, ‘&Grade2’) ;

(i) Select name from SPORTS

where Grade1 = ‘C’ or Grade2 = ’C’ ;

NAME

Sujit

Veena

Arpit

(ii) Select COUNT(Student_no) from SPORTS

where (Grade1 = ‘A’ OR Grade2 = ‘A’) AND (Game1 = ‘Cricket’ OR Game2 = ‘Cricket’);

COUNT(*)

2

(iii) Select distinct(GAME1 & Game2) from SPORTS ;

NAME

Veena

9|Page
(iv) Alter table SPORTS

Add (Marks number(10));

(v) Update SPORTS

Set Marks= 200 where Grade1>’B’ or Grade2 > ‘B’ ;

STUDENT_NO CLASS NAME GAME1 GRADE1 GAME2 GRADE2 MARKS

10 7 Sameer Cricket B swimming A 200

10 | P a g e
11 8 Sujit Tennis A skating C

12 7 kamal Swimming B football B 200

13 7 Veena Tennis C Tennis A 200

14 9 Archana Basket Ball A Cricket A 200

15 10 Arpit Cricket A Athletics C

(vi) Select name from SPORTS

order by name desc ;

STUDENT_NO CLASS NAME GAME1 GRADE1 GAME2 GRADE2 MARKS

14 9 Archana Basket Ball A Cricket A 200

15 10 Arpit Cricket A Athletics C

10 7 Sameer Cricket B swimming A 200

11 8 Sujit Tennis A skating C

13 7 Veena Tennis C Tennis A 200

12 7 Kamal Swimming B Football B 200

11 | P a g e
Program-3

Q3. Given the table STUDENT :
Student No. Name Stipen Stream Avg.Marks Grade Class
d
1 Karan 400.00 Medical 78.5 B 12B
2 Divakar 450.00 Commerce 89.2 A 11C
3 Divya 300.00 Commerce 68.6 C 12C

4 Arun 350.00 Humanities 73.1 B 12C
5 Sabina 500.00 Nonmedical 90.6 A 11A

6 John 400.00 Medical 75.4 B 12B
7 Robert 250.00 Humanities 64.4 C 11A
8 Rubina 450.00 Nonmedical 88.5 A 12A
9 Vikas 500.00 Nonmedical 92.0 A 12A
10 Mohan 300.00 Commerce 67.5 C 12C

(i) Select all the Nonmedical stream students from STUDENT.

(ii) List the names of those students who are in class 12 sorted by stipend.

(iii) List all students sorted by Avg. Marks in descending order.

(iv) Display a report listing Name, Stipend, Stream, and Amount of Stipend received in a year
assuming that the stipend is paid every month.

(v) Count the number of students with Grade ’A’.

(vi) Insert a new student in the STUDENT table and fill all the columns with some values.

(vii) Give the output of the following SQL statement:

a) Select MIN (Avg. Marks) from STUDENT where Avg. Marks >75.

b) Select SUM (Stipend) from STUDENT where Grade =’ B’.

c) Select AVG (Stipend) from STUDENT where Class = ‘12A’.

12 | P a g e
d) Select COUNT (DISTINCT).

Ans(3) :- Create table STUDENT

values (Student_no number (10),

Name varchar2 (10),

Stipend number (10),

Stream varchar2 (10),

Insert into STUDENT

values ( &Student_no , ’& Name’, &Stipend , ‘&Stream’ , &Avg_marks, ‘&Grade’, ‘&Class’);

(i) Select name from STUDENT

where stream = ‘Nonmedical’ ;

STUDENT_NO NAME STIPEND STREAM AVGMARKS GR CLAS

5 Sabina 500 Nonmedical 90.6 A 11A

8 Rubina 450 Nonmedical 88.5 A 12A

9 Vikas 500 Nonmedical 92 A 12A

13 | P a g e
(ii) Select name from STUDENT

NAME

Divya

Mohan

Arun

John

Rubina

Vikas

(iii) Select name from STUDENT

order by Avg_marks desc ;

STUDENT_NO NAME STIPEND STREAM AVGMARKS GR CLAS

9 Vikas 500 Nonmedical 92 a 12A

14 | P a g e
5 Sabina 500 Nonmedical 90.6 A 11A

2 Divakar 450 Commerce 89.2 A 11C

8 Rubina 450 Nonmedical 88.5 A 12A

6 John 400 Medical 75.4 B 12B

4 Arun 350 Humanities 73.1 B 12C

3 Divya 300 Commerce 68.6 C 12C

10 Mohan 300 Commerce 67.5 C 12C

7 Robert 250 Humanities 64.4 c 11A

(iv) Select count (name) from STUDENT

where grade=’A ’;

COUNT(*)

3

(v) Insert into STUDENT

STUDENT_NO NAME STIPEND STREAM AVGMARKS GR CLAS

1 karan 400 medical 78.5 B 12B

2 Divakar 450 Commerce 89.2 A 11C

3 Divya 300 Commerce 68.6 C 12C

4 Arun 350 Humanities 73.1 B 12C

5Meena 150Commerce 65.3C 11A

6 John 400 Medical 75.4 B 12B

15 | P a g e
7 Robert 250 Humanities 64.4 c 11A

8 Rubina 450 Nonmedical 88.5 A 12A

9 Vikas 500 Nonmedical 92 a 12A

10 Mohan 300 Commerce 67.5 C 12C

11 Sumit 350 Humanities 74.5 B 12C

(vi)

(a) Select MIN(Avg_marks)

from STUDENT

where Avg_marks >75;

MIN(AVGMARKS)

75.4

16 | P a g e
(b) Select SUM(Stipend)

SUM(STIPEND)

1500

(c) Select AVG(Stipend)

from STUDENT

where class=’12A’;

AVG(STIPEND)

475

(d) Select count distinct (Stream)

from STUDENT ;

COUNT(DISTINCTSTREAM)

5

17 | P a g e
Program-4

Q4. Write SQL statement to create EMPLOYEE relations which contain EmpNo, Name, Skill, Pay
Rate.

Ans(4) :- CREATE table EMPLOYEE

( EmpNo number(5) NOT NULL PRIMARY KEY,

Name char(20),

DESC EMPLOYEE;

Name Null? Type

EMPNO NOT NULL NUMBER(5)

NAME CHAR(20)

18 | P a g e
SKILL CHAR(15)

PAYRATE NUMBER(8)

Program-5

Q5. Create a table with the under-mentioned structure.(Table name is Emp)

19 | P a g e
EmpNo Number(4)

DeptNo Number(2)

EmpName Char(10)

Job Char(10)

Manager Number(4)

HireDate Date

Salary Number(7,2)

Commission Number(7,2)

Ans5 :- Create table Emp

( Emp_No number (4),

Dept_No number(2),

Hire_Date date,

Salary number(7,2),

Commission number(7,2));

Name Null? Type
EMP_NO NUMBER(4)
DEPT_NO NUMBER(2)
EMP_NAME CHAR(10)
JOB CHAR(10)
MANAGER NUMBER(4)
HIRE_DATE DATE
SALARY NUMBER(7,2)
COMMISSION NUMBER(7,2)

20 | P a g e
Program-6

Q6. Find the number of employees having manager as job.

Ans 6 :-SELECT COUNT(*)

FROM EMP

WHERE Job = ‘Manager’;

JOB

0

21 | P a g e
Program-7

Q7. Display only the jobs with maximum salary greater than or equal to 3000.

22 | P a g e
Ans 7 :- SELECT Job

WHERE MAX(SALARY)>=3000;

JOB

CHAIRMAN

VICE PRESIDENT

SENIOR VICE PRESIDENT

PRESIDENT

23 | P a g e
JOB

CHAIRMAN

VICE PRESIDENT

SENIOR VICE PRESIDENT

PRESIDENT

DIRECTOR

EXECUTIVE SENIOR DIRECTOR

CEO

Program-8
Q8. Find all those employees whose job does not start with ‘M’.

Ans 8 :-SELECT Empname,Job

FROM EMP

WHERE Job NOT LIKE ‘M%’;

24 | P a g e
Program-9
Q9. List the minimum and maximum salary of each job type.

Ans 9 :- SELECT MAX(SALARY)

FROM EMP ;

MAX(SALARY)

9000

SELECT MIN(SALARY)

FROM EMP ;

MIN(SALARY)

3000

25 | P a g e
Program-10
Q10. Find all the employees who have no manager.

Ans .: FROM EMP

WHERE JOB !='MANAGER';

EMPNO DEPTNO EMPNAME JOB MANAGER HIREDATE SALARY COMMISION

1 13012 RAJ CHAIRMAN 1 01-JAN-10 9000.55 1000

VICE
24 23015 NAMIT 16 09-OCT-10 8550.45 100
PRESIDENT

32 1200 SHIPRA SENIOR VP 2 01-FEB-10 7545.50 0

35 33422 AKASH PRESIDENT 17 10-OCT-10 3900.45 900

43 34890 POOJA DIRECTOR 3 02-DEC-10 3300.90 4500.75

EX.SENIOR
64 1236 NEHA 4 02-DEC-10 4500.45 0
DIRECTOR

95 2390 VIKAS CEO 5 10-DEC-10 3000.00 0

7 rows selected.

26 | P a g e
Program-11
Q11. Create a table with the under-mentioned structure. (Table name is Dept.)

DeptNo. Number (2)

DeptName Char (12)

Location Char (12)

Ans 11 :- CREATE table DEPT

(Dept_No number(2) NOT NULL PRIMARY KEY,

Dept_name char(12),

Location char(12));

DESC DEPT;

Name Null? Type

DEPT_NO NOT NULL NUMBER(2)

DEPT_NAME CHAR(12)

LOCATION CHAR(12)

27 | P a g e
Program-12
Q12. Create a table PROJECT with the given structure.

Ans 12 :- CREATE table PROJECT

(Pro_Id number(4) NOT NULL PRIMARY KEY,

Pro_End_Dt date,

Budget_Amt number(7),

Max_No_staff number (2));

DESC PROJECT;

Name Null? Type

PRO_ID NUMBER(4)

PROJ_DESIGN CHAR(20)

PRO_START_DT DATE

PRO_END_DT DATE

BUDGET_AMT NUMBER(7)

MAX_NO_STAFF NUMBER(2)

28 | P a g e
Program-13
Q13. Create a table SALGRADE with the given structure.

Ans 13 :- CREATE table SALGRADE

Highsal number(7,2),

Grade number(2));

DESC SALGRADE;

Name Null? Type

LOWSAL NUMBER(7,2)

HIGHSAL NUMBER(7,2)

GRADE NUMBER(2)

29 | P a g e
Program-14
Q14. Write SQL statements to list all employees in the following format:

Ans 14 :- EMPLOYEE WORKS IN DEPARTMENT Dept No

SMITH WORKS IN DEPARTMENT 20

SMITHS WORKS IN DEPARTMENT 30

SANTOSH WORKS IN DEPARTMENT 30

CREATE table EMP (Emp_name varchar2(30),

DESC EMP;

Name Null? Type

EMP_NAME VARCHAR2(30)

DEPTNO NUMBER(4)

30 | P a g e
SELECT emp_name” works in department ”, deptno

FROM EMP;

EMP_NAME WORKS IN DEPARTMENT DEPTNO

Smith works in department 20

Sudhir works in department 20

Raj works in department 10

Smiths works in department 30

Santosh works in department 30

31 | P a g e
Program-15
Q15. Given the table MOV :

NO TITLE TYPE RATIN STARS QTY PRICE TOTAL

1 Gone with the Wind Drama G Grable 4 40 160

2 Friday the 13th Horror R Jason 2 70 140

3 Top Gun Drama PG Cruise 7 50 350

4 Splash Comedy PG13 Hanks 3 30 90

5 Independence Day Drama R Turner 3 20 60

6 Risky Business Comedy R Cruise 2 45 90

7 Cocoon Scifi PG Amche 2 32 64

8 Crocodile Dundee Comedy PG13 Harris 2 70 140

9 101 Dalmations Comedy G 3 60 180

10 Tootsie Comedy PG Hoffman 1 30 30

(i) Find the total value of cassettes available in the library.
SELECT SUM(Total)

FROM MOV;

SUM(TOTAL)

1304

32 | P a g e
(ii) Display a list of all movies with Price over 20 and sorted by Price.
SELECT Title, Price

FROM MOV

WHERE Price > 20

ORDER BY Price;

TITLE PRICE

Splash 30

Tootsie 30

Cocoon 32

Gone With The Wind 40

Risky Business 45

Top Gun 50

101 Dalmations 60

Friday The 13th 70

Crocodile Dundee 70

33 | P a g e
(iii) Display all the movies sorted by Qty in descending order.

SELECT Title,Qty

FROM MOV

ORDER BY Qty DESC;

TITLE QTY

Top Gun 7

Gone with the Wind 4

Splash 3

101 Dalmations 3

Independence Day 3

Friday the 13th 2

Risky Business 2

Cocoon 2

Crocodile Dundee 2

Tootsie 1

34 | P a g e
(iv) Display a report listing a movie number, current value and replacement value for each
movie in the above table. Calculate the replacement value for all movies as
Qty*Price*1.15.

SELECT No, Price, “Replacement Value’=Qty*Prcie*1.15

FROM MOV;

NO PRICE REPLACEMENT PRICE

1 39.95 183.77

2 69.95 160.89

3 49.95 402.1

4 29.95 103.33

5 19.95 68.83

6 44.95 103.39

7 31.95 73.49

8 69.95 160.89

9 59.95 206.83

10 29.95 34.44

(v) Count the number of movies where rating is not ‘G’.
SELECT COUNT(*)

FROM MOV

WHERE Rating <> ‘G’;

COUNT(*)

8

(vi) Insert a new movie in the table. Fill in all the columns with some values.
INSERT INTO MOV values(11 , ‘Tokyo Drift’ , ‘Drama’ , ‘G’ , ‘Nelson’ , 5 , 50);

35 | P a g e
NO TITLE TYPE RATIN STARS QTY PRICE TOTAL

1 Gone with the Wind Drama G Grable 4 40 160

2 Friday the 13th Horror R Jason 2 70 140

3 Top Gun Drama PG Cruise 7 50 350

4 Splash Comedy PG13 Hanks 3 30 90

5 Independence Day Drama R Turner 3 20 60

6 Risky Business Comedy R Cruise 2 45 90

7 Cocoon Scifi PG Amche 2 32 64

8 Crocodile Dundee Comedy PG13 Harris 2 70 140

9 101 Dalmations Comedy G 3 60 180

10 Tootsie Comedy PG Hoffman 1 30 30

11Tokyo Drift Drama G Nelson 5 50 250

(vii) Give the output of the following SQL Statements :

36 | P a g e
a. SELECT AVG(Price) FROM MOV WHERE Price < 30;
AVG(PRICE)

26.6166667

b. SELECT MAX(Price) FROM MOV WHERE Price > 30;
MAX(PRICE)

69.95

c. SELECT SUM(Price*Qty) FROM MOV WHERE Qty < 4;
SUM(PRICE*QTY)

843.05

d. SELECT COUNT(DISTINCT Type);
COUNT(DISTINCT TYPE)

5

PL/SQL

37 | P a g e
Write a procedure which will update the

i> ha=30% of basic,

Ii> ta=300

Iii> Ca=300

IV> da=15% of basic * (64%)

v> total

Depending on the empno.

SOLUTION

create table employee(eno number, basic number, ha number, ta number, ca number, d number, total
number);

insert into employee (eno) values (8001);

select * from employee;

ENO BASIC HA TA CA D TOTAL
8001 1000
8002 10000

create or replace procedure total_8001(empno in number)

is

bas number;

h number;

t number;

c number;

d number;

tot number;

begin

select basic into bas from employee where empno=eno;

h:=.3*bas;

t:=300;

c=:=800;

38 | P a g e
da:=1.5*bas*.64;

tot:=bas+h+t+c+da;

update employee set ha=h,ta=t,ca=c,d=da,tottal=tot where empno=eno;

end;

begin

total_8001 (8002);

end;

ENO BASIC HA TA CA D TOTTAL
8001 1000 300 300 800 960 3360
8002 10000 3000 300 800 9600 23700

Write a function which will update the

i> ha=30% of basic,

Ii> ta=300

39 | P a g e
Iii> Ca=300

IV> da=15% of basic * (64%)

v> total

Depending on the empno.

SOLUTION

create table employee(eno number, basic number, ha number, ta number, ca number, d number, total
number);

insert into employee (eno) values (8001);

select * from employee;

ENO BASIC HA TA CA D TOTAL
8001 1000

8002 10000

create or replace function total1_8001(empno in number)

return number

is

bas number;

h number;

t number;

c number;

da number;

tot number;

begin

select basic into bas from employee where eno=empno;

h:=.3*bas;

t:=300;

c:=800;

da:=1.5*bas*.64;

40 | P a g e
tot:=bas+h+t+c+da;

update employee set ha=h,ta=t,ca=c,d=da,tottal=tot where eno=empno;

return tot;

end;

declare

a number;

begin

a:=total1_8001(8003);

end;

ENO BASIC HA TA CA D TOTTAL
8001 1000 300 300 800 960 3360
8002 10000 3000 300 800 9600 23700
8003 30000 9000 300 800 28800 68900

Demonstrate Exception :-

declare

41 | P a g e
num number;

k number;

begin

num:=10;

k:=num/0;

dbms_output.put_line(k);

exception

when zero_divide then

dbms_output.put_line('division by zero');

end;

OUTPUT

division by zero

Statement processed.

Demonstrate cursor

WRITE a programme that will display the basic and the total amount .

42 | P a g e
ENO BASIC HA TA CA D TOTTAL
8001 1000 300 300 800 960 3360
8002 10000 3000 300 800 9600 23700
8003 30000 9000 300 800 28800 68900

declare
cursor cur is select * from employee;
x employee% rowtype;
begin
open cur;
loop
fetch cur into x;
exit when cur%notfound ;
dbms_output.put_line(x.basic);
dbms_output.put_line(x.tottal);
end loop;
close cur;
end;

output

1000
3360
10000
23700
30000
68900

Statement processed.

Demonstrate Trigger

ENO BASIC HA TA CA D TOTTAL
8001 1000 300 300 800 960 3360

43 | P a g e
8002 10000 3000 300 800 9600 23700
8003 30000 9000 300 800 28800 68900

create or replace trigger tig before insert on employee for each row
begin
if :new.basic>5000 then
raise_application_error(-20001,'invalid entry');
end if;
end;

Trigger created.

insert into employee (basic) values (7000);
ORA-20001: invalid entry
ORA-06512: at "SYSTEM.TIG", line 3
ORA-04088: error during execution of trigger 'SYSTEM.TIG'

1. insert into employee (basic) values (7000);

44 | P a g e