Uploaded by hirtik gupta

meraj

advertisement
BHAI PARMANAND DSEU SHAKARPUR
CAMPUS-II
(DELHI SKILL AND ENTREPRENEURSHIP UNIVERISITY)
Database Management System
Practical File
MCA- DC-102
SUBMITTED TO :
MR. SHRIKANT PATEL
(ASSISTANT PROFESSOR)
Submitted By :
Meraj khan [50122016]
Master in Computer Application (MCA)
1
Semester – I
INDEX
Practical Performed
Date
Teacher
Signature
1. Create following two tables including integrity constraints
using SQL commands :
Table Name : Employee
Field Name
Data Type
Employee ID
Number
Text
Employee
Name
Street
Text
City
Text
Table Name : Works
Field Name Data Type
Employee ID Number
Company
Name
Salary
Field Size
8
20
15
3
Field Size
8
Text
20
Number
10,2
Constraints
Primary key
Not Null
‘Del’, ’Bom’,
‘Cal’
Constraints
Primary key,
Foreign Key
Primary Key
>10000 AND
<25000
Now, perform the following operations using SQL commands :
(i)
List all employees names working in ‘FBC’.
(ii)
Find the number of employees earning a salary greater than
average salary of all employees.
2. Create following tables :
Student(StudentID, Student_name, age)
Registered(StudentID, CourseID, dt_of_Join)
Add the following constraints to the table using alter command:
(i)
Primary Key and respective foreign key for all tables.
(ii)
age>20 and age<60
List the student id and name of each student along with the total
number of courses that the student is registered for.
3. Create following two tables including integrity constraints
using SQL commands :
Table Name : Part
2
Field Name
Part ID
Name
Data Type
Text
Text
Table Name : Subpart
Field Name Data Type
Part ID
Text
Sub Part ID
Count
Cost
Text
Number
Number
Field Size
8
20
Field Size
8
20
5
10,2
Constraints
Primary key
Not Null
Constraints
Primary key,
Foreign Key
Primary Key
>=0 AND <100
Default 0
Now, perform the following operations using SQL commands :
(i)
List the name of all the subparts whose part-id is “P-100”.
(iii)
Find the total cost of part “P-100” including subparts.
4. Create following two tables including integrity constraints
using SQL commands :
Table Name : Student
Field Name
Data Type
Roll No
Number
Name
Text
Address
Text
Table Name : Enrollment
Field Name Data Type
Roll No
Number
Course
Grade
Text
Text
Field Size
8
20
50
Field Size
8
20
1
Constraints
Primary key
Not Null
Constraints
Primary key,
Foreign Key
Primary Key
‘A’,’B’,’C’,’D’,’E’
Now, perform the following operations using SQL commands :
(i)
List all the students enrolled for ‘BBA’ and have grade ‘A’.
(ii)
Find the number of students enrolled in each course.
5. Create following tables :
Employee(empno, name, office,age)
Books(isbn, title, author, publisher)
Loan(empno, isbn, date)
Add the following constraints to the table using alter command:
(i)
Primary Key and respective foreign key for all tables.
(ii)
age>20 and age<60
List the names of employees having any book published by ‘TMH”.
6. Create following two tables including integrity constraints
using SQL commands :
3
Table Name : Student
Field Name Data Type
CSTID
CSTNAME
CSTCITY
Number
Text
Text
Field
Size
4
25
3
CSTDEP
Number
8,2
Constraints
Primary key
Not Null
Must beUDP,MNG,BNG,PJM,MAR
Perform the following operations using SQL commands :
(i)
Provide constraint names while creating table.
(ii)
Drop the check constraint on CSTCITY.
(iii)
Create a new constraint chkdep to check that the deposit is
within the range 1500 to 30000.
7. Consider EMP table and create a new table based on the EMP
table. Add the necessary constraints to the new table and
perform the following using SQL commands :
(i)
List employee number, employee name, total salary (i.e.
salary+commission).
(Note : Manipulate the NULL values accordingly)
(ii)
Display the name, job and salary of allemployees whose
job is CLERK or ANALYST and their salary is not equal
to 1000, 3000 or 5000.
(iii)
Display the name, salary and commission for all
employees whose commission amount is greater than
their salary increased by 10%.
8. Create following two tables including integrity constraints
using SQL commands :
Table Name : Student
Field Name
Data Type
Roll No
Number
Name
Text
Address
Text
Table Name : Enrollment
Field Name Data Type
Roll No
Number
Course
Grade
Text
Text
Field Size
8
20
50
Field Size
8
20
1
Constraints
Primary key
Not Null
Constraints
Primary key,
Foreign Key
Primary Key
‘A’,’B’,’C’,’D’,’E’
Now, perform the following operations using SQL commands :
(i)
List all the students who have the same grade as that of
‘SUNIL’.
(ii)
List all the students who have enrolled in maximum
number of courses.
4
9. Consider EMP table and perform the following using SQL
commands :
(i) Add a new column called total sal to the table.
(ii) Update the total sal column with sal+commission.
(iii) Find the employees having salary greater than average salary
of the employees working in dept 10.
(iv) List employee name and yearly salary and arrange the output
on the basis of yearly salary in descending order.
(v) Retrieve the names of departments in ascending order and
their employees in descending order.
10. Consider EMP table and perform the following operations using
SQL commands :
(i)
Select the name, job, salary and department number of
all employees except MANAGER from department
number 5.
(ii)
List all departments in which more than 2 employees
are working.
(iii)
Find the employee earning the second highest salary.
(iv)
List all the employees whose salary is greater than yearly
commission.
Practical 11 (PL/SQL code Block)
1. Write a PL/SQL code block to calculate the area of a circle for a
value of radius varying from 3 to 7.Store the radius and the
corresponding values of calculated area in an empty table named
Areas.
2. Write a PL/SQL Block of code for inverting a number 5639 to 9365.
3. Write a PL/SQL Block of code that will merge the data available in
the newly created table NEW_BRANCHES with the data available
in the table BRANCH_MSTR .if data in the first table already exist
in the second table then that data should be skipped.
4. Write a PL/SQL block to display whether the given number is Odd
or Even.
Practical 12 (PL/SQL code Block)
1. Write a PL/SQL block to display BPIBS 10 times using for loop
2. Write a PL/SQL Block to print the sum of numbers from 1 to 50.
3. Write a PL/SQL block to find the salary of a given employee and
raise his salary by 20%.
Practical 13 (PL/SQL code Block)
1. Write a PL/SQL block to calculate the average salary from table
‘Emp’ and print increase the salary if the average salary is less
that 10,000.
2. Write a PL/SQL block to print the deptno from the employee table
using the case statement if the deptname is ‘Technical’ then
deptno is 1, if the deptname is ‘HR’ then the deptno is 2 else
deptno is 3.
3. Write a PL/SQL block to display the detail about given employee
from EMP table.
5
Practical 14 (PL/SQL Procedures)
1. Create a procedure on table employee to display the details of
employee whose salary will be provided during execution.
2. Write a PL/SQL procedure to print the following output.
* * *
* *
*
Practical 15 (PL/SQL Functions)
1. Create a function that accepts the programmer_id and checks if
the programmer_id exits in the table PROGRAMMER. If the
programmer_id exits, display a message valid client and if the
programmer_id does not exit, then display an appropriate error
message.
2. Write a function on programmer table to calculate number of
female programmers.
3. Write a function on programmer table to return total number of
programmers having knowledge of specified language.
4. Write a function on programmer table to return age of specified
person.
5. Write a function on studies table to return course fee of a
specified course.
6. Write a function on studies and programmer table to return
number of months required to overcome the course fee he/she
has studied.
Practical 16 (PL/SQL Triggers)
1. Write a database trigger to check that date_of_joining is not
2.
3.
4.
empty. Also check that the date_of_joining should be greater than
date_of_birth field in EMPLOYEE table.
Write a database trigger on EMPLOYEE table to check that first
letter of the emp_id is ‘E’.
Create a trigger on EMPLOYEE table for deletion where the whole
table is displayed whenever a delete operation is performed.
Create trigger on EMPLOYEE table on update or insert of
emp_name to convert emp_name into capital letter
Practical 17 (PL/SQL Cursors)
1. Write a PL/SQL block using cursor to update salary of a
given programmer by 25%.
2. Create a cursor emp_cur,fetch record from emp table and check
whether sal>10000 then update Grade = ‘A’ else if sal = > 5000 and
sal <= 10000 then update Grade = ‘B’
3. Write a PL/SQL block with cursor ,showing the use of SQL%FOUND
attribute
4. Write a PL/SQL block with cursor ,showing the use of SQL%
ROWCOUNT attribute
5. Write a PL/SQL block with cursor ,showing the use of SQL%
ISOPEN attribute
6
Practical 18 (PL/SQL Exception Handling)
1. Write a PL/SQL block using to define exception handling.
7
Ques 1
Create following two tables including integrity constraints using SQL commands :
Table Name : Employee
Field Name
Employee ID
Employee Name
Street
City
Table Name : Works
Field Name
Data Type
Employee ID Number
Company
Name
Salary
Data Type
Number
Text
Text
Text
Field Size
8
Text
20
Number
10,2
Field Size
8
20
15
3
Constraints
Primary key
Not Null
‘Del’, ’Bom’, ‘Cal’
Constraints
Primary key,
Foreign Key
Primary Key
>10000 AND
<25000
Employee Table & Works Table
create table employee
(employeeid number(8,0),
employename varchar2(20),
street varchar2(15),
city varchar2(3))
create table works
(employeeid number(8,0),
companyname varchar2(20),
salary number(10))
alter table works
ADD CONSTRAINT pk PRIMARY KEY (employeeid);
alter table employee
ADD CONSTRAINT pk1 PRIMARY KEY (employeeid);
ALTER TABLE works
8
ADD CONSTRAINT fk_name
FOREIGN KEY (employeeid) REFERENCES employee(employeeid);
insert into employee values(101,'garima','delhi','del');
insert into employee values(102,'shivani','delhi','del');
insert into employee values(103,'harry','delhi','del');
insert into works values(101,'FBC',15000);
insert into works values(102,'FBC',17000);
insert into works values(103,'bca',18000);
Now, perform the following operations using SQL commands :
1) List all employees names working in ‘FBC’.
select employename from employee
INNER JOIN works
ON works.employeeid = employee.employeeid
where works.companyname ='FBC'
9
2) Find the number of employees earning a salary greater than average salary of all
employees.
Select Count (employeeid) from works
Where Salary > (select avg(salary) from works);
10
Ques 2
Create following tables :
Student(StudentID, Student_name, age)
Registered(StudentID, CourseID, dt_of_Join)
Add the following constraints to the table using alter command:
1) Primary Key and respective foreign key for all tables.
2) age>20 and age<60
Student Table & Registered Table
create table student
(
studentid number(8,0),
studentname varchar2(20),
age number(10)
)
create table registered
(
studentid number(8,0),
courseid varchar2(20),
dateofjoining date
)
alter table student
ADD CONSTRAINT spk PRIMARY KEY (studentid);
alter table registered
ADD CONSTRAINT spk1 PRIMARY KEY (studentid);
11
ALTER TABLE registered
ADD CONSTRAINT fk_name1
FOREIGN KEY (studentid) REFERENCES student(studentid);
insert into registered values(101,'s01',to_date('05/05/05','dd-mm-yy'))
insert into registered values(102,'s02',to_date('22/07/22','dd-mm-yy'))
insert into registered values(103,'s02',to_date('05/04/21','dd-mm-yy'))
12
Ques 3
Create following two tables including integrity constraints using SQL commands :
Table Name : Part
Field Name
Part ID
Name
Data Type
Text
Text
Table Name : Subpart
Field
Data Type Field
Name
Size
Part ID
Text
8
Sub Part
ID
Count
Text
20
Number
5
Cost
Number
10,2
Field Size
8
20
Constraints
Primary key
Not Null
Constraints
Primary
key,
Foreign Key
Primary
Key
>=0 AND
<100
Default 0
Part Table & Subpart Table
create table part( part_id varchar2(8) primary key, name varchar2(20) not null);
create table subpart
(
part_id varchar2(8),
subpart_id varchar2(20),
count number(5,0) CHECK (count>=0 AND count<100),
cost number(10,2) default 0
);
alter table subpart
ADD CONSTRAINT pk2 PRIMARY KEY (part_id,subpart_id);
ALTER TABLE subpart
ADD CONSTRAINT fk_name3
FOREIGN KEY (part_id) REFERENCES part(part_id);
13
insert into part values('p-100','name')
insert into part values('p-102','nitin')
insert into part values('p-103','simran')
insert into part values('p-104','anjali')
insert into subpart values('p-100','s',20,100)
insert into subpart values('p-100','t',20,100)
insert into subpart values('p-100','u',20,100)
insert into subpart values('p-102','t',40,500)
Now, perform the following operations using SQL commands :
(i)
List the name of all the subparts whose part-id is “P-100”.
select part_id,subpart_id from subpart where part_id='p-100'
(ii)
Find the total cost of part “P-100” including subparts.
Select sum(cost) from subpart where part id=’p-100’
14
15
Ques 4
Create following two tables including integrity constraints using SQL commands :
Table Name : Student
Field Name
Data Type
Roll No
Number
Name
Text
Address
Text
Table Name : Enrollment
Field
Data
Field
Name
Type
Size
Roll No
Number
8
Course
Grade
Text
Text
20
1
Field Size
8
20
50
Constraints
Primary key
Not Null
Constraints
Primary key,
Foreign Key
Primary Key
‘A’,’B’,’C’,’D’,’E’
Student Table & Enrollment Table
create table STUDENT2
(rollno number(8,0) Primary key,
name varchar2(20)Not Null,
address varchar2(50)
)
create table enrollment
(
rollno number(8,0) ,
course varchar2(20),
grade varchar2(1)
)
alter table enrollment
ADD CONSTRAINT pk4 PRIMARY KEY (rollno,course);
ALTER TABLE enrollment
16
ADD CONSTRAINT fk_name5
FOREIGN KEY (rollno) REFERENCES STUDENT2(rollno);
alter table enrollment
ADD CONSTRAINT c1 check (grade in ('A','B','C','D','E'));
insert into Student2 values (01,'simran','sn')
insert into Student2 values (02,'nitin','nd');
insert into Student2 values (03,'simran sac','gurgao');
insert into Student2 values (04,'anjali','delhi')
insert into enrollment values (01,'bba','A')
insert into enrollment values (02,'bba','b')
insert into enrollment values (03,'bba','A')
insert into enrollment values (04,'bca','B')
Now, perform the following operations using SQL commands :
(i)
List all the students enrolled for ‘BBA’ and have grade ‘A’.
SELECT *
FROM student2
inner JOIN enrollment
ON Student2.rollno=enrollment.rollno
where grade ='A' and course ='bba'
17
(ii)
Find the number of students enrolled in each course.
select COUNT(rollno),course from enrollment
group by course;
18
Ques 5
Create following tables :
Employee(empno, name, office,age)
Books(isbn, title, author, publisher)
Loan(empno, isbn, date)
Add the following constraints to the table using alter command:
(i)
Primary Key and respective foreign key for all tables.
(ii) age>20 and age<60
Employee Table
create table Employe
(
empno number(8,0),
name varchar2(20),
office varchar2(50),
age varchar2(50)
)
Book Table
create table book
(
isbn number(8,0),
tittle varchar2(20),
author varchar2(50),
publisher varchar2(50)
)
Loan Table
create table Loan
(
empno number(8,0),
isbn number(8,0),
19
datel varchar2(50)
)
alter table employe
ADD CONSTRAINT pn PRIMARY KEY (empno);
alter table book
ADD CONSTRAINT py PRIMARY KEY (isbn);
alter table loan
ADD CONSTRAINT we PRIMARY KEY (empno,isbn);
ALTER TABLE loan
ADD CONSTRAINT fk_name8
FOREIGN KEY (empno) REFERENCES employe(empno);
ALTER TABLE loan
ADD CONSTRAINT fk_name81
FOREIGN KEY (isbn) REFERENCES book(isbn);
ALTER TABLE employe
ADD CONSTRAINT aa
check (age>20 and age<60)
insert into employe values(101,'simran','gurgao','34')
insert into employe values(102,'nitin','nodia','36')
insert into employe values(103,'anjali','chennai','36')
insert into employe values(104,'lak','gurgao','34')
insert into book values(11,'spy','ABC','TMH')
insert into book values(12,'MOOC','AC','TMH')
20
insert into book values(13,'PY','JP','MH')
insert into loan values(101,11,'22-08-2002')
insert into loan values(102,12,'25-09-2007')
1) List the names of employees having any book published by ‘TMH”
select name from employe
INNER JOIN loan
ON loan.empno = employe.empno
INNER JOIN book
ON book.isbn = loan.isbn
where book.publisher ='TMH'
21
Ques 6
Create following two tables including integrity constraints using SQL commands :
Table Name : Student
Field Name
Data Type
CSTID
Number
CSTNAME
Text
CSTCITY
Text
CSTDEP
Number
Field Size
4
25
3
Constraints
Primary key
Not Null
Must beUDP,MNG,BNG,PJM,MAR
8,2
Perform the following operations using SQL commands :
(i)
Provide constraint names while creating table..
Student Table
create table Student3
(CSTID number(4,0),
cstname varchar2(25) ,
cstcity varchar2(3),
cstdep number(8,2),
constraint c11 check(cstcity in ('UDP','MGP',''BNG','PNJ','MAR')),
constraint c4 primary key( CSTID),
constraint c6 Not null (cstname)
)
(ii)
Drop the check constraint on CSTCITY.
ALTER TABLE Student3
DROP CONSTRAINT c11;
22
(iii)
Create a new constraint chkdep to check that the deposit is within the range 1500
to 30000.
alter table Student3
ADD CONSTRAINT c11 check (cstdep>1500 and cstdep<3000);
23
Ques 7
Consider EMP table and create a new table based on the EMP table. Add the
necessary constraints to the new table.
Emp Table
create table emp
(
empno number(8,0) Primary key,
name varchar2(20)Not Null,
salary number(8,0)Not Null,
commission number(8,0)Not Null
)
New Table
create table new
(
empno number(8,0),
job varchar2(20),
depno number(8,0)
)
ALTER TABLE new
ADD CONSTRAINT fk_name15
FOREIGN KEY (empno) REFERENCES emp(empno);
insert into emp values(101,'simran','30000','500')
insert into emp values(102,'sonam','50000','5000')
insert into emp values(103,'simran k','60000','7000')
insert into emp values(104,'rahul','30000','500')
insert into new values(101,'clerck',6)
insert into new values(102,'analyst',5)
24
insert into new values(103,'clerck',5)
insert into new values(104,'tester',6)
Perform the following using SQL commands :
(i)
List employee number, employee name, total salary (i.e. salary+commission).
(Note : Manipulate the NULL values accordingly)
select empno,name,salary+commission AS total_salary from emp
(ii)
Display the name, job and salary of allemployees whose job is CLERK or
ANALYST and their salary is not equal to 1000, 3000 or 5000.
SELECT name,salary
FROM emp
inner JOIN new
ON emp.empno=new.empno
where new.job ='clerck' or new.job ='analyst'
and
salary NOT IN (3000,5000,1000)
25
(iii)
Display the name, salary and commission for all employees whose commission
amount is greater than their salary increased by 10%.
select employee_name, salary, commission from emp_employee where commission>(salary*10/100);
26
Ques 8
Create following two tables including integrity constraints using SQL commands :
Table Name : Student
Field Name
Data Type
Roll No
Number
Name
Text
Address
Text
Field Size
8
20
50
Constraints
Primary key
Not Null
Table Name : Enrollment
Field Name
Data Type
Roll No
Number
Field Size
8
Constraints
Primary key,
Foreign Key
Primary Key
‘A’,’B’,’C’,’D’,’E’
Course
Grade
Text
Text
20
1
create table STUDENT2
(
rollno number(8,0) Primary key,
name varchar2(20)Not Null,
address varchar2(50)
)
create table enrollment
(
rollno number(8,0) ,
course varchar2(20),
grade varchar2(1)
)
alter table enrollment
ADD CONSTRAINT pk4 PRIMARY KEY (rollno,course);
ALTER TABLE enrollment
27
ADD CONSTRAINT fk_name5
FOREIGN KEY (rollno) REFERENCES STUDENT2(rollno);
alter table enrollment
ADD CONSTRAINT c1 check (grade in ('A','B','C','D','E'));
alter table Student3
ADD CONSTRAINT c11 check (cstcity in ('UDP','MGP','BNG','PNJ','MAR'));
alter table Student3
ADD CONSTRAINT c23 check(cstcity in('UDP','MGP','BNG','PNJ','MAR')),
alter table Student3
ADD CONSTRAINT c4 primary key (CSTID)
alter table Student3
ADD CONSTRAINT c6 NOT NULL (cstname)
insert into Student2 values (01,'simran','sn')
insert into Student2 values (02,'nitin','nd');
insert into Student2 values (03,'simran sac','gurgao');
insert into Student2 values (04,'anjali','delhi')
insert into Student2 values (05,'sunil','delhi')
insert into enrollment values (01,'bba','A')
insert into enrollment values (02,'bba','b')
insert into enrollment values (03,'bba','A')
insert into enrollment values (04,'bca','B')
insert into enrollment values (05,'bca','B')
28
Now, perform the following operations using SQL commands :
(i)
List all the students who have the same grade as that of ‘SUNIL’.
select name from student where roll_no in(select roll_no from enr_enrollment where
grade=(select grade from enrollment where roll_no in (select roll_no from student where
name='Sunil')));
29
Ques 9
Consider EMP table and perform the following using SQL commands :
Emp Table
create table emp
(
employee_number number(10) primary key,
employee_name varchar2(15),
dept_number number(10),
dept_name varchar2(25),
salary number(20) not null,
commission number(20)
);
(i)
Add a new column called total sal to the table.
ALTER TABLE emp
ADD total_sal number(10,2)
30
(ii)
Update the total sal column with sal+commission.
update emp set total_sal=salary+commission
select*from emp
(iii)
Find the employees having salary greater than average salary of the employees
working in dept 10.
select employee_name from emp where salary>(select avg(salary) from emp where dept_number=04);
31
(iv)
List employee name and yearly salary and arrange the output on the basis of
yearly salary in descending order.
select name,salary from emp
ORDER BY salary DESC
(v)
Retrieve the names of departments in ascending order and their employees in
descending order.
select dept_name, employee_name from emp order by dept_name asc, employee_name desc;
32
33
Ques 10
Consider EMP table and perform the following operations using SQL commands :
create table emp
(
employee_number number(10) primary key,
employee_name varchar2(15),
dept_name varchar2(25),
dept_number number(10),
salary number(20) not null,
commission number(20)
)
insert into emp values(101,'Abhishek','Manager',10,8000,1000);
insert into emp values(102,'Nitin','Assistant',10,12000,5000)
insert into emp values(103,'Nidhi','Analyst',05,25000,2000)
insert into emp values(104,'Prakshi','Assistant',02,10000,1500)
insert into emp values(105,'Riya','Consultant',02,20000,5000)
insert into emp values(106,'Rohit','Clerk',01,2000,5000)
(i)
Select the name, job, salary and department number of all employees except
MANAGER from department number 5.
34
select employee_name, dept_name, salary, dept_number from emp where dept_name not in 'Manager' or
dept_number!=5
(ii)
List all departments in which more than 2 employees are working.
SELECT dept_name, count(employee_name) as "No. of employees" FROM emp GROUP BY dept_name
HAVING COUNT(dept_number) > = 2
35
(iii)
Find the employee earning the second highest salary.
SELECT max(salary) as "Second Highest salary" FROM emp WHERE SALARY < (SELECT MAX(SALARY)
FROM emp).
36
(iv)
List all the employees whose salary is greater than yearly commission.
select employee_name from emp where salary>12*commission;
37
Download