Anda di halaman 1dari 37

DEPARTMENT

OF
COMPUTER SCIENCE AND ENGINEERING

Data Base Management System Laboratory


(CS553)

A Laboratory Record submitted in partial fulfilment of the

Department Of Computer Science And Engineering

requirements of Data Base Management System Laboratory


of V - B.Tech (CSE)

October 2016

Certificate
This is to certify that the record titled Data Base Management Systems
Laboratory (CS553) is a bonafide record of work done by ATUL PRADEEP in
partial fulfillment of requirement of V B.Tech CSE during the year 2016.

HEAD OF THE DEPARTMENT

FACULTY-IN CHARGE

EXAMINER 1:

Department Of Computer Science And Engineering

EXAMINER 2:
Name

Register Number

Examination Center

: Faculty Of Engineering

Date of Examination

INDEX

PRGM
NO.

DATE

PROGRAM NAME

PAGE
NO.

MARKS

SIGNATURE

1
2
3
4
5
6
7
8
9

Department Of Computer Science And Engineering

10

Department Of Computer Science And Engineering

EXPERIMENT NO: 1
DATE: 21.06.2016
DATA DEFINITION LANGUAGE (DDL)
AIM: Create a database to implement DDL basic commands using the Employee database as
given below.

Department (Dept_id: Primary key, Dept_name: Not null, Contact_no: Unique)


Employee (Emp_id: Primary Key, Dept_id: Foreign Key, Emp_name, Designation, Salary,
City: default Bangalore)
SYNTAX:
CREATE TABLE TABLE_NAME (VARIABLE DATATYPE (LENGTH));
ALTER TABLE TABLE_NAME ALTER COLUMN COLUMN_NAME TYPE DATATYPE ( );
ALTER TABLE TABLE_NAME ADD COLUMN_NAME DATATYPE ( );
ALTER TABLE TABLE_NAME RENAME COLUMN COLUMN_NAME TO
NEW_COLUMN_NAME;
Department Of Computer Science And Engineering

ALTER TABLE TABLE_NAME RENAME TO NEW_TABLE_NAME;


ALTER TABLE TABLE_NAME DROP COLUMN COLUMN_NAME;
QUERIES:
1.

CREATE THE DEPARTMENT AND EMPLOYEE TABLE WITH PROPER SPECIFICATION


OF PRIMARY AND FOREIGN KEYS
Create table department (dept_id char (7) primary key, dept_name char (20)
contact_no int unique);

not null,

Create table employee(emp_id char(7) primary key, dept_id char(7), emp_name


char(20),designation char(10),salary int ,city char(20) default 'Bangalore', foreign key(dept_id)
references department );

2.

ADD A NEW CLOUMN IN DEPARTMENT TABLE


Alter table department add city varchar (5);

Department Of Computer Science And Engineering

3.

CHANGE THE DATATYPE OF SALARY TO CHAR(10) IN EMPLOYEE


Alter table employee alter column salary type char(10);

4.

DELETE THE CITY COLUMN FROM THE DEPARTMENT TABLE


Alter table department drop column city;

5.

RENAME A COLUMN IN DEPARTENT TABLE


Alter table department rename column dept_name to d_n;
Department Of Computer Science And Engineering

6.

RENAME THE TABLE EMPLOYEE AS WORKERS


Alter table employee rename to workers;

Department Of Computer Science And Engineering

EXPERIMENT NO: 2
DATE: 30.06.2016
DATA MANIPULATION LANGUAGE (DML)
AIM: Create a database in order to implement basic DML commands using the Employee
database as given below.

Department (Dept_id: Primary key, Dept_name: Not null, Contact_no: Unique)


Employee (Emp_id: Primary Key, Dept_id: Foreign Key, Emp_name, Designation, Salary,
City: default Bangalore)
SYNTAX:
1. INSERT INTO TABLE_NAME VALUES (VALUE);
2. UPDATE TABLE_NAME SET ATTRIBUTE_NAME = VALUE,
ATTRIBUTE_NAME = VALUE WHERE ATTRIBUTE_NAME = VALUE;
3. SELECT ATTRIBUTE1_NAME, ATTRIBUTE2_NAME, ATTRIBUTE3_NAME FROM
TABLE_NAME;
4. SELECT * FROM TABLE_NAME;
Department Of Computer Science And Engineering

5. SELECT * FROM TABLE_NAME WHERE ATTRIBUTE_NAME > VALUE;


6. SELECT * FROM TABLE_NAME WHERE ATTRIBUTE_NAME BETWEEN
VALUE1 AND VALUE2;
7. SELECT * FROM TABLE_NAME WHERE CITY IN (VALUE1,VALUE2);
8. SELECT * FROM TABLE_NAME WHERE ATTRIBUTE_NAME NOT IN
(VALUE1, VALUE2);
9. SELECT * FROM TABLE_NAME WHERE ATTRIBUTE_NAME LIKE VALUE;
10. SELECT * FROM TABLE_NAME WHERE ATTRIBUTE_NAME IS NULL;
11. SELECT * FROM TABLE_NAME ORDER BY ATTRIBUTE_NAME ASC;
SELECT * FROM TABLE_NAME ORDER BY ATTRIBUTE_NAME DESC;
12. SELECT ATTRIBUTE1_NAME, AVG (ATTRIBUTE2_NAME) AS ALIAS_NAME
FROM TABLE_NAME GROUP BY ATTRIBUTE1_NAME;
13. SELECT ATTRIBUTE1_NAME, AVG (ATTRIBUTE2_NAME) AS ALIAS_NAME
FROM TABLE_NAME GROUP BY ATTRIBUTE1_NAME HAVING (ALIAS_NAME
>= VALUE);

Department Of Computer Science And Engineering

10

QUERIES:
1. INSERT INTO DEPARTMENT VALUES('DEP-03', 'ACCOUNTING', 1213143);
2. UPDATE EMPLOYEE SET CONTACT_NO='0115', CITY='BANGALORE' WHERE
EMP_ID='EMP-06';

3. SELECT EMP_ID, EMP_NAME, DESIG FROM EMPLOYEE;

4. SELECT * FROM EMPLOYEE;

Department Of Computer Science And Engineering

11

5. SELECT * FROM EMPLOYEE WHERE SALARY > 30000;

6. SELECT * FROM EMPLOYEE WHERE SALARY BETWEEN 15000 AND 30000;

7. SELECT * FROM EMPLOYEE WHERE CITY IN('BANGALORE', 'NEW DELHI');

Department Of Computer Science And Engineering

12

8. SELECT * FROM EMPLOYEE WHERE CITY NOT IN ('BANGALORE', 'NEW


DELHI');

9. SELECT * FROM EMPLOYEE WHERE EMP_NAME LIKE 'A____%';

10. SELECT * FROM EMPLOYEE ORDER BY SALARY ASC;

11. SELECT * FROM EMPLOYEE ORDER BY SALARY DESC;

Department Of Computer Science And Engineering

13

12. SELECT DEPT_ID, AVG(SALARY) AS AVG_SAL FROM EMPLOYEE GROUP BY


DEPT_ID;

13. SELECT DEPT_ID, AVG(SALARY) AS AVG_SAL FROM EMPLOYEE GROUP BY


DEPT_ID HAVING (AVG_SAL>30000);

Department Of Computer Science And Engineering

14

Database Management Systems Lab

EXPERIMENT NO: 3
DATE: 05.07.2016
JOINS
AIM: Create a Company and a department Database and solve the various join operations.
Company (id, name, age, address, salary, join date)
Department (id, dept, emp id)

SYNTAX:
SELECT ATTRIBUTE1, ATTRIBUTE2 ..., FROM TABLE 1 CROSS JOIN TABLE2;
SELECT ATTRIBUTE1, ATTRIBUTE2 ..., FROM TABLE 1 INNER JOIN/LEFT OUTER
JOIN/RIGHT OUTER JOIN/ FULL OUTER JOIN TABLE2 ON CONDITION;

QUERIES:
CREATE AND INSERT VALUES INTO THE COMPANY AND DEPARTMENT TABLE
Create table department2 (id int, dept varchar (20), emp_id int);
Insert into department2 values (1,'t billing', 1);
Insert into department2 values (2,'engineering', 2);
Insert into department2 values (3,'finance', 41);
Select *from department2;

Department Of Computer Science And Engineering

Database Management Systems Lab

Create table company (id int, name varchar(10), age int, address varchar(30), salary
numeric(8,2), join_date date );
Insert into company values (1,'paul', 32,'California', 20000,'2001-07-13');
Insert into company values (3,'allen', 23,'Norway', 20000);
Insert into company values (4,'david', 25,'Richmond', 65000,'2010-10-25');
Insert into company values (5,'mark', 27,'Texas', 35000,'2015-11-02');
Insert into company values (2,'teddy', 25,'Los vegas', null,'2013-09-01');
Select *from company;

1.

CROSS JOIN
Select emp_id, name, dept from company cross join department2;

Department Of Computer Science And Engineering

Database Management Systems Lab

2.

INNER JOIN
Select emp_id, name, dept from company inner join department2 on
company.id=department2.emp_id;

3.

LEFT OUTER JOIN


Select emp_id, name, dept from company left outer join department2 on
company.id=department2.emp_id;

Department Of Computer Science And Engineering

Database Management Systems Lab

4.

RIGHT OUTER JOIN


Select emp_id , name, dept from company right outer join department2 on
company.id=department2.emp_id;

5.

FULL OUTER JOIN


Select emp_id, name, dept from company full outer join department2 on
company.id=department2.emp_id;

Department Of Computer Science And Engineering

Database Management Systems Lab

EXPERIMENT NO: 4
DATE: 21.07.2016
VIEWS
AIM:
Consider the following schemas:
Employee (empID, fName, lName, address, DOB, deptNo)
Department (deptNo, deptName, mgrID)]
Project (projNo, projName, deptNo)
WorksOn (empID, projNo, hoursWorked)
Questions
1. Create tables for above listed schemas and corresponding Simple Views.
2. Insert values into each of the table in given order using views only.
3. Perform Update and Delete operation on WorksOn table using the view.
4. Create a view and perform Select Operation for all given below conditions using created
views only.
a. List name and address of all employees who are Managers.
b. Find out how many employees are managed by Kapil
Department Of Computer Science And Engineering

Database Management Systems Lab

c. For each project on which more than two employees worked, list the project
number, project name and number of employees worked on that project.
SYNTAX:
CREATE VIEW VIEW_NAME AS SELECT ATTRIBUTE1, ATTRIBUTE 2 FROM
TABLE_NAME;

QUERIES:
1. CREATE THE TABLES AND THEIR SIMPLE VIEWS
create table employee(
empid int primary key,
fname varchar(30),
lname varchar(30),
address varchar(100),
dob date
);
create table department(
deptno int primary key,
deptname varchar(30),
mgrid int references employee(empid)
);
alter table employee add column deptno int references department(deptno);
create table project(
projno int,
projname varchar(30),
deptno int references department(deptno)
);
create table workson(
empid int,
projno int,
hoursworked int,
primary key(empid, projno)
);
create view employee_view as (select * from employee);
create view department_view as (select * from department);
create view project_view as (select * from project);
create view workson_view as (select * from workson);
2. INSERT VALUES INTO THE SIMPLE VIEWS
insert into employee_view values
(100,'Jus','Gag','#Stretside westside, Uptown Funk-23','6-9-69'),
(1,'Paul','Lewis','#91/7, Togotik flat, Ramdan, Bangalore - 69','21-7-1990'),
(2,'Kapil','Miranda','#21/8, Hoho villas, Lopsided street, Mumbai - 73','21-7-1982'),
(3,'Princeton','Church','#45/6, Jackfruit shores, Chennai - 23','21-7-1990'),
(4, 'Robert', 'Langdon', '#9/11, Twin Towers, Delhi - 44','17-6-1978'),
(5, 'Rain','Man', '#6, Rain Maker, NigaHiga, GeorgeTown - 56','23-3-1968'),
Department Of Computer Science And Engineering

Database Management Systems Lab


(6,'Tom','Dale','#7, Dickson Road, Bangalore - 64','21-4-1989');
insert into department_view values
(145,'Accounts',1),
(135,'Sales',2),
(125,'IT',3),
(115,'Creative Team',4);
update employee_view set deptno = 135 where empid = 100;
update employee_view set deptno = 145 where empid = 1;
update employee_view set deptno = 135 where empid = 2;
update employee_view set deptno = 135 where empid = 3;
update employee_view set deptno = 115 where empid = 4;
update employee_view set deptno = 145 where empid = 5;
update employee_view set deptno = 145 where empid = 6;
insert into project_view values
(901,'Rainwater Harvesting',135),
(902,'Promo Videos',135),
(801,'Upgrade Windows',125),
(1000,'Use Excel',145);
insert into workson values
(2,901,290),
(3,801,150),
(4,801,250),
(5,801,27),
(1,1000,34),
(6,1000,56),
(3,1000,23),
(100,902,10),
(2,801,145);
EMPLOYEE TABLE VIEW
select * from employee_view;

Department Of Computer Science And Engineering

Database Management Systems Lab

DEPARTMENT TABLE VIEW


select * from department_view;

PROJECT TABLE VIEW


select * from project_view;

Department Of Computer Science And Engineering

Database Management Systems Lab

WORKSON TABLE VIEW


select * from workson_view;

3. UPDATE AND DELETE VALUES INTO THE TABLE VIEWS


update workson set hoursworked = hoursworked + 20 where empid=1;
delete from workson where empid=2 and projno=801;
select * from workson;

Department Of Computer Science And Engineering

Database Management Systems Lab

4. CREATE VIEWS AND PERFORM THE QUERIES


A) create view q1 as(select fname,lname,address from employee
inner join department on employee.empid = department.mgrid);
select fname,lname,address from q1;

Department Of Computer Science And Engineering

Database Management Systems Lab


B) create view q2 as(select e2.fname,e2.lname,e2.address from employee e1, employee
e2,department d where e1.fname='Kapil' and e1.empid = d.mgrid and e2.deptno =
d.deptno and e2.fname <> 'Kapil');
select count(fname) from q2;

C) c create view q3 as (select w.projno,p.projname, count(w.projno) from project p,workson


w where p.projno=w.projno group by w.projno,p.projno,p.projname having
count(w.projno) >2);
select * from q3;

Department Of Computer Science And Engineering

Database Management Systems Lab

EXPERIMENT NO: 5
DATE: 2.09.2016
Functions and Cursors
AIM:
Write a Postgre PL/SQL programs that make use of functions and cursors to solve the
following questions
Questions
1. Write a Postgre PL/SQL program to find the maximum and second maximum integer in an
array of 5 elements. Explore all possibilities to return multiple values from function
2. Consider a scheme Employee(Eid, Ename, job, salary,mgrid).
a. Create table and insert values into it.
b. Use cursors to fetch the values from the table and find the manager for each
employee. Display the result in given format. Ename is manager of Ename
c. Find all employees having their manager with mgrid as 100.
d. Find the employee earning the nth maximum salary for any given value n. Read n
as input from user.
e. Display the total number of employees earning salary greater than 10000

SYNTAX:
CREATE [ OR REPLACE ] FUNCTION
name ( [ [ argmode ] [ argname ] argtype [ { DEFAULT | = } default_expr ] [, ...] ] )
[ RETURNS rettype
| RETURNS TABLE ( column_name column_type [, ...] ) ]
{ LANGUAGE lang_name
Department Of Computer Science And Engineering

Database Management Systems Lab

| TRANSFORM { FOR TYPE type_name } [, ... ]


| WINDOW
| IMMUTABLE | STABLE | VOLATILE | [ NOT ] LEAKPROOF
| CALLED ON NULL INPUT | RETURNS NULL ON NULL INPUT | STRICT
| [ EXTERNAL ] SECURITY INVOKER | [ EXTERNAL ] SECURITY DEFINER
| COST execution_cost
| ROWS result_rows
| SET configuration_parameter { TO value | = value | FROM CURRENT }
| AS 'definition'
| AS 'obj_file', 'link_symbol'
} ...
[ WITH ( attribute [, ...] ) ]

QUERIES:
1. FUNCTIONS: MAXIMUM AND SECOND MAXIMUM VALUES FROM TABLE

CREATE or replace FUNCTION findmax( int[],out l1 int, out l2 int) AS $$


declare
x int;
BEGIN
l1 := $1[1];
l2 := $1[2];
foreach x in array $1 loop

Department Of Computer Science And Engineering

Database Management Systems Lab

--for i in 2 .. 5 loop
if l1 < x and l2 < x
then
l2 := l1;
l1 := x;
elsif l1 > x and l2 < x
then
l2 = x;
end if;
end loop;
END;
$$ LANGUAGE plpgsql;
select findmax(array[6,2,3,4,5]);

Department Of Computer Science And Engineering

Database Management Systems Lab

2. CURSORS
a) Create table
create table emp(
eid int primary key,
ename varchar(30),
job varchar(30),
salary int,
mgrid int);
insert into emp values
(1,'Jack','Manager', 18000, null),
(34, 'Slack', 'Clerk', 3000, 100),
(47, 'Tom', 'Clerk', 4500, 100),
(37, 'Loki', 'Clerk', 3500, 26),
(100, 'Jason', 'Officer', 8000, 1),
(26, 'Ron', 'Officer', 8200, 1);

Department Of Computer Science And Engineering

Database Management Systems Lab

b) Find managers
create or replace function bans() returns text as $$
declare
line text default '';
cur_rec Record;
begin
for cur_rec in
select e2.ename as A, e1.ename as B from emp e1, emp e2 where e1.mgrid = e2.eid
loop
line := line || cur_rec.a || ' is manager of ' || cur_rec.b || ', ';
end loop;
return line;
end; $$
Department Of Computer Science And Engineering

Database Management Systems Lab


language plpgsql;
select * from bans();

c) Find employees with manager ID 100


create or replace function cans() returns text as $$
declare
line text default '';
cur_rec Record;
begin
for cur_rec in
select ename from emp where emp.mgrid = 100
loop
line := line || cur_rec.ename || ', ';
end loop;
return line;
end; $$
language plpgsql;
select * from cans();

Department Of Computer Science And Engineering

Database Management Systems Lab

d) Find employees earning nth maximum salary


create or replace function dans(n int) returns text as $$
declare
line text default '';
cur_rec Record;
begin
for cur_rec in
select * from emp order by emp.salary desc
loop
n := n - 1;
if n = 0 then
line := cur_rec.ename || ', ';
end if;
end loop;
return line;
end; $$
language plpgsql;
select * from dans(3);

Department Of Computer Science And Engineering

Database Management Systems Lab

e) Number employees earning more than 10000


create or replace function eans() returns int as $$
declare
line text default '';
cur_rec Record;
n int := 0;
begin
for cur_rec in
select eid from emp where salary > 10000
loop
n := n + 1;
end loop;
return n;
end; $$
language plpgsql;
select * from eans();

Department Of Computer Science And Engineering

Database Management Systems Lab

EXPERIMENT NO: 6
DATE: 2.09.2016
Triggers
AIM:
Create Triggers for the previous table schema and understand the working of the trigger
functions
Questions
1. Create a trigger for the below mentioned scenario and bind it to the previous employee
schema
a. If any insert operation is performed on the schema, print the no of records available
before and after inserting.
b. Perform update operation to increase salary of all emplyees by 10% and display old
and new salary of employee. If the new salary of any employee crosses the limit set
for the respective categories(clerk 5000, officer 10000, manager - 20000) ABORT
the update operation.

SYNTAX:
CREATE [ CONSTRAINT ] TRIGGER name { BEFORE | AFTER | INSTEAD OF } { event [
OR ... ] }
ON table
[ FROM referenced_table_name ]
[ NOT DEFERRABLE | [ DEFERRABLE ] { INITIALLY IMMEDIATE | INITIALLY
DEFERRED } ]
[ FOR [ EACH ] { ROW | STATEMENT } ]
[ WHEN ( condition ) ]
EXECUTE PROCEDURE function_name ( arguments )

Department Of Computer Science And Engineering

Database Management Systems Lab

where event can be one of:

INSERT
UPDATE [ OF column_name [, ... ] ]
DELETE
TRUNCATE

QUERIES:
1. INSERT TRIGGER:
CREATE or replace FUNCTION triga() RETURNs trigger AS $$
declare
i integer;
begin
select count(eid) from emp into i;
raise notice 'Number of rows % ', i;
return null;
end; $$
language plpgsql;
create trigger a_trig_bef before insert
on emp
execute procedure triga();
create trigger a_trig_aft after insert
on emp
execute procedure triga();
insert into emp values
(40,'Flint', 'Clerk', 4500, 100);

Department Of Computer Science And Engineering

Database Management Systems Lab

2. INSERT TRIGGER:
CREATE or replace FUNCTION trigc() RETURNs trigger AS $$
declare
i integer;
begin
raise notice 'Old value % New value %', old.salary, new.salary;
if (new.job = 'Clerk' and new.salary > 5000) or (new.job = 'Officer' and new.salary > 10000)
or (new.job = 'Manager' and new.salary > 20000)
then
raise exception 'Exceeded Maximum Salary';
end if;
return null;
end; $$
language plpgsql;
create trigger c_trig_bef after update
on emp
for each row execute procedure trigc();
update emp set salary = salary + salary*0.1;

Department Of Computer Science And Engineering

Database Management Systems Lab

Department Of Computer Science And Engineering

Anda mungkin juga menyukai