DBMS Practicals-Compressed
DBMS Practicals-Compressed
DBMS Practicals-Compressed
Certificate
has successfully completed ____ / _____ practicals for the course code CMP-509 having
_____________________________ __________________________
Name of the Counsellor Name of the SC Co-ordinator
Signature: ___________________ Signature: __________________
Date: ______________ Date: ______________
_____________________________ __________________________
Name of the Internal Examiner Name of the External Examiner
Signature: ___________________ Signature: __________________
Date: ______________ Date: ______________
Index
No. Practical Date of Page Signature of the
Activities/Programs/Assignments practical No. Counsellor and
performed date
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
SYBCA PRACTICAL NO.1 ROLL NO.25
Aim: To draw ER Model and Relational Model for a given database a) Case study 1:List the
data requirements for the database of the company which keeps track of the company
employee, department and projects. The database designers provide the following
description.
1. The company is organized into departments. Each department has unique name, unique
number, and particular employee to manage the department. We keep track of the start
date and the employee begins managing the department. The department has several
locations.
Answer:
DEPT_ID
MANAGE_DEPT
DEPT_NAME
DEPARTMENT
START_DATE
D_LOCAT
2. The department controls a number of projects each of which has a unique name, unique
number and a single location.
Answer:
P_NAME
PROJECT P_ID
P_LOCAT
CONTROLLING_DEPT
3. We store each employee names social security number , address , salary, sex and dob. An
employee is assigned one department but may work on several projects which are not
necessarily controlled by the same department. We keep track of the department of each
employee works on each project and for insurance purpose. We keep each dependents
first name, sex, dob and relation.
Answer:
SEX SSN
DOB
EMPLOYEE
DEPT_NAME ADDRESS
NAME
SALARY WORK_ON
F_NAME
L_NAME
PROJECT
HOURS_NEED
b) Case study 2: Construct an E-R diagram for a hospital with a set of patients and a set of
medical doctors. Associate with each patient a log of the various tests and examinations
conducted. Also Construct appropriate tables for the ER Diagram.
Answer:
HOSPITAL CONTAIN
S DOCTORS
H_PHO
H_ID START_
NE SAL
DATE
TREAT
ADMIT SUGGEST
T
D_NAME
P_NAM P_AGE T_NAM
E E
TEST
PATIENT DO
Aim: Create one-to-many Relationship between Manager and Employee Relations Create
following Relations with the given fields
b) MANAGER Mgrid, MgrName, No. of Employees controlled Using above table solve the
following queries:
a. Display details of all those employees whose salary is higher than Rs.50
Code:
empname char(10),
department char(15),
salary int,
);
values(100,'Avni','Sales',50,14);
values(101,'Neil','Purchase',100,16);
values(102,'Shweta','Sales',70,13);
values(103,'Rajesh','Purchase',45,16);
values(104,'Rita','Purchase',35,14);
Output:
mgrname char(15),
emp_controlled int
);
values(12,'Shrushti',10);
values(13,'Pravin',12);
values(14,'Anuj',16);
values(15,'Pranali',7);
values(16,'Amita',13);
Output:
Output:
Output:
Aim: Determine the functional dependencies. Remove partial dependency and transitive
dependencies in given table. ( i.e. convert it into 3NF).
Aim: Creation of Database and table-DDL COMMAND .Create a table called EMP with the
following structure.
Name Type
EMPNO NUMBER(6)
ENAME VARCHAR2(20)
JOB VARCHAR2(10)
DEPTNO NUMBER(3)
SAL NUMBER(7,2)
a) -Add a column experience to the emp table. experience numeric null allowed.
Name Type
DEPTNO NUMBER(2)
DNAME VARCHAR2(10)
LOC VARCHAR2(10)
a) Create the emp1 table with ename and empno, add constraints to check the empnovalue
while entering (i.e) empno> 100.
Code:
Empno number(6),
Deptno number(3),
Salary number(7,2)
);
DESC Emp2;
DESC Emp2;
DESC Emp2;
Output:
DESC Emp2;
Dname varchar2(10),
Loc varchar2(10)
);
DESC Dept1;
Ouput:
Deptno number(2),
Ename varchar2(10),
Empno number(6),
);
Output:
DESC Emp2;
Output:
Insert at least 8 records into tables and solve the following given queries using Emp and Dept
table:
a) Create the emp1 table with ename and empno, add constraints to check the empno value
while entering (i.e) empno> 100.
b) Update the emp table to set the salary of all employees to Rs15000/- who are working as
ASP.
d) List the records in the emp table orderby salary in ascending order.
f) Add constraints to the emp table that empno as the primary key and deptno as the foreign
key.
Code:
Empno number(6),
Deptno number(3),
Salary number(7,2)
);
Output:
Dname varchar2(10),
Loc varchar2(10)
);
Output:
(
Deptno number(2),
Ename varchar2(10),
Empno number(6),
CHECK (Empno > 100)
);
Output:
Output:
Output:
Output:
Output:
1. Display all the details of the records whose employee name starts with ‘A’.
2. Display all the details of the records whose employee name does not start with ‘A’.
3. Calculate the total and average salary amount of the emp table.
4. Determine the max and min salary and rename the column as max_salary and min_salary.
Code:
a).1. The SUM() function returns the total sum of a numeric column.
Output:
Output:
3. The MAX() function returns the largest value of the selected column.
Output:
4. The MIN() function returns the smallest value of the selected column.
Output:
5. The COUNT() function returns the number of rows that matches a specified criterion.
Output:
b). 1. This function is used to convert the lower case string into upper case.
Output:
2. This function is used to convert the upper case string into lower case.
Output:
Output:
4. The initcap function converts the first letter of each word in a string to uppercase, and
converts any remaining characters in each word to lowercase.
Output:
Output:
Output:
Output:
Output:
FROM Emp2;
Output:
FROM EMP2;
Output:
FROM EMP2;
Output:
1. Display all the dept numbers available with the dept and emp tables avoiding duplicates.
2. Display all the dept numbers available with the dept and emp tables.
3. Display all the dept numbers available in emp and not in dept tables and vice versa.
Code:
a). 1. The UNION operator is used to combine the result-set of two or more SELECT
statements.
3. The MINUS set operator removes the second query's results from the output if they are
also found in the first query's results.
UNION
Output:
UNION ALL
Output:
MINUS
Output:
MINUS
Output:
1. Display all employee names and salary whose salary is greater than minimum salary of the
company and job title starts with ‘M’.
2. Issue a query to find all the employees who work in the same job as Arjun.
3. Issue a query to display information about employees who earn more than any employee in
dept.
Code:
Output:
Output:
1. Display the employee details, departments that the departments are same in both the
emp and dept.
2. Display all the employees and the departments implementing a left outer join.
3. Display the employee name and department name in which they are working
implementing a right outer join.
4. Display the employee name and department name in which they are working
implementing a full outer join.
Code:
a). A SQL Join statement is used to combine data or rows from two or more tables based on
a common field between them. Different types of Joins are:
• INNER JOIN
• LEFT JOIN
• RIGHT JOIN
• FULL JOIN
1.INNER JOIN: The INNER JOIN keyword selects all rows from both the tables as long
as the condition satisfies. This keyword will create the result-set by combining all rows
from both the tables where the condition satisfies i.e value of the common field will be
same.
Syntax:
SELECT table1.column1,table1.column2,table2.column1,....
FROM table1
INNER JOIN table2
ON table1.matching_column = table2.matching_column;
table1: First table.
table2: Second table
matching_column: Column common to both the tables.
2. LEFT JOIN: This join returns all the rows of the table on the left side of the join and
matching rows for the table on the right side of join. The rows for which there is no
matching row on right side, the result-set will contain null. LEFT JOIN is also known as
LEFT OUTER JOIN.
Syntax:
SELECT table1.column1,table1.column2,table2.column1,....
FROM table1
LEFT JOIN table2
ON table1.matching_column = table2.matching_column;
table1: First table.
table2: Second table
matching_column: Column common to both the tables.
3. RIGHT JOIN: RIGHT JOIN is similar to LEFT JOIN. This join returns all the rows of
the table on the right side of the join and matching rows for the table on the left side of join.
The rows for which there is no matching row on left side, the result-set will contain null.
RIGHT JOIN is also known as RIGHT OUTER JOIN.
Syntax:
SELECT table1.column1,table1.column2,table2.column1,....
FROM table1
RIGHT JOIN table2
ON table1.matching_column = table2.matching_column;
table1: First table.
table2: Second table
matching_column: Column common to both the tables.
4. FULL JOIN: FULL JOIN creates the result-set by combining result of both LEFT JOIN
and RIGHT JOIN. The result-set will contain all the rows from both the tables. The rows
for which there is no matching, the result-set will contain NULL values.
Syntax:
SELECT table1.column1,table1.column2,table2.column1,....
FROM table1
FULL JOIN table2
ON table1.matching_column = table2.matching_column;
table1: First table.
5. SELF JOIN: As the name signifies, in SELF JOIN a table is joined to itself. That is,
each row of the table is joined with itself and all other rows depending on some conditions.
In other words we can say that it is a join between two copies of the same table.
Syntax:
Output:
Output:
1. SELECT eid,ename,dname
FROM emp
INNER JOIN dept
ON emp.DeptNo = dept.deptno;
Output:
2. SELECT eid,ename,dname,loc
FROM emp
LEFT OUTER JOIN dept
ON emp.Deptno = dept.deptno;
Output:
3. SELECT eid,ename,dname,loc
FROM emp
RIGHT OUTER JOIN dept
ON emp.DeptNo = dept.deptno;
Output:
4. SELECT eid,ename,dname,loc
FROM emp
FULL OUTER JOIN dept
ON emp.DeptNo = dept.deptno;
Output:
Aim: Advanced SQL queries using PL-SQL. Solve the following PL-SQL programs.
Code:
A]. delcare
a number;
b number;
temp number;
begin
a:= 50;
b:= 60;
temp:= a;
a:= b;
b:= temp;
end;
Output:
B]. declare
a number;
b number;
begin
a:= 50;
b:= 60;
IF a>b THEN
ELSE
END IF;
end;
Output:
Code:
{IS | AS}
BEGIN
<procedure_body>
END;
Syntax of Function :
RETURN return_datatype
{IS | AS}
BEGIN
<function_body>
END function_name;
B]. declare
a number;
begin
a:= 25;
if mod(a,2) = 0 then
dbms_output.put_line('Value of a is even');
else
dbms_output.put_line('Value of a is odd');
end if;
end;
Output:
declare
a number;
begin
a:= 24;
if mod(a,2) = 0 then
dbms_output.put_line('Value of a is even');
else
dbms_output.put_line('Value of a is odd');
end if;
end;
Output:
declare
a number;
begin
a:= 1;
while a <= 10
loop
a:= a+2;
end loop;
end;
Output:
Aim: Write a pl/sql program to find the total and average of 6 subjects and display the grade.
Code:
declare
js number(10);
dbms number(10);
cn number(10);
ds number(10);
c number(10);
html number(10);
total number(10);
per number(10);
begin
js:= 77;
dbms:= 70;
cn:= 68;
ds:= 80;
c:= 90;
html:= 85;
total:= js+dbms+cn+ds+c+html;
if per<=45 then
dbms_output.put_line('Grade F');
dbms_output.put_line('Grade 'D);
dbms_output.put_line('Grade C');
dbms_output.put_line('Grade B');
dbms_output.put_line('Grade A');
else
dbms_output.put_line('Invalid marks');
end if;
end if;
end if;
end if;
end if;
end;
Output: