OF
COMPUTER SCIENCE AND ENGINEERING
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.
FACULTY-IN CHARGE
EXAMINER 1:
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
10
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.
not null,
2.
3.
4.
5.
6.
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.
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';
11
12
13
14
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;
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;
2.
INNER JOIN
Select emp_id, name, dept from company inner join department2 on
company.id=department2.emp_id;
3.
4.
5.
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
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
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
QUERIES:
1. FUNCTIONS: MAXIMUM AND SECOND MAXIMUM VALUES FROM TABLE
--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]);
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);
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
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 )
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);
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;