0% found this document useful (0 votes)
20 views

Dbms Lab Record - Part B

Uploaded by

anubhadrinathh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Dbms Lab Record - Part B

Uploaded by

anubhadrinathh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

DATABASE MANAGEMENT SYSTEM LAB RECORD

b)TCL commands: COMMIT, ROLLBACK, SAVEPOINT

SQL> connect
Enter user-name: system
Enter password:
Connected.

SQL> create table employeee


2(
3 empid varchar(10) primary key,
4 empname varchar(20),
5 location varchar(20)
6 );

OUTPUT:

SQL> insert into employeee values('E101','POOJA','BELGAUM');


SQL> insert into employeee values('E102','VIKAS','HUBALI');
SQL> insert into employeee values('E103','DEEPA','DHARWAD');
SQL> insert into employeee values('E104','AMAR','BELGAUM');

SQL> select * from employeee;


OUTPUT:

pg. 1
DATABASE MANAGEMENT SYSTEM LAB RECORD

SQL> commit;

OUTPUT:

SQL> insert into employeee values('E105','PRIYA','BELGAUM');

OUTPUT:

SQL> select * from employeee;

OUTPUT:

SQL> rollback;

OUTPUT:

SQL> select * from employeee;

OUTPUT:

SQL> insert into employeee values('E105','PRIYA','BELGAUM');


OUTPUT:

pg. 2
DATABASE MANAGEMENT SYSTEM LAB RECORD

SQL> savepoint first;

OUTPUT:

SQL> update employeee set empname='VIJAY' where empid='E102';

OUTPUT:

SQL> savepoint second;

OUTPUT:

SQL> select * from employeee;

OUTPUT:

SQL> rollback to first;

OUTPUT:

SQL> select * from employeee;

OUTPUT:

pg. 3
DATABASE MANAGEMENT SYSTEM LAB RECORD

PROGRAM: 05
Implement the nested queries.
Demonstrate the following:
1. Create the following tables
a. DEPT (DID INT, DNAME VARCHAR (25))
b. EMPL (EID INT, ENAME VARCHAR (25), SALARY INT, DID INT)
2. Insert minimum 3 tuples in each relation
3. Display the list of employees who work in HR department
4. Increase the salary of employees who work in Finance department by
Rs.1000/-
5. Delete the details of all the employees who are working for marketing
department.

1) Create the following tables


create table dept
(did int primary key,
dname varchar (25));

create table empl


(eid int primary key,
ename varchar (25),
salary int,
did references dept (did) on delete cascade);

OUTPUT:

2) Insert minimum 3 tuples in each relation


insert into dept values (101,'hr');
insert into dept values (102,'finance');
insert into dept values (103,'marketing');

pg. 4
DATABASE MANAGEMENT SYSTEM LAB RECORD
insert into empl values (1,'raju',20000, 101);
insert into empl values (2,'ramu',250000, 102);
insert into empl values (3,'ravi',30000, 103);
insert into empl values (4,'hari',32000,102);
insert into empl values (5,'om',29000,102);
OUTPUT:

select * from empl;

OUTPUT:

select*from dept;
OUTPUT:

3) Display the list of employees who work in HR department


select ename from empl
where did in
(select did from dept where dname='hr');
OUTPUT:

pg. 5
DATABASE MANAGEMENT SYSTEM LAB RECORD

4)Increase the salary of employees who work in Finance department by


Rs.1000/-
update empl
set salary=salary+1000
where did in
(select did from dept where dname='finance');
OUTPUT:

select * from empl;


OUTPUT:

5)delete the details of all the employees who are working for marketing
department.
delete from empl
where did in
(select did from dept where dname='marketing');

OUTPUT:

select * from empl;


OUTPUT:

pg. 6
DATABASE MANAGEMENT SYSTEM LAB RECORD

PROGRAM: 06
Implement JOIN operation in SQL.
Demonstrate the following:
1. Create the following Relation
a. eemployee(empid int, name varchar(20), dep varchar(20))
b. ttransaction(tranid int,empid number(5),agency varchar(20),amount number(10))
2. Insert 5 records in each relation.
3. Demonstrate Inner Join
4. Demonstrate Right Outer Join
5. Demonstrate Left Outer Join
6. Demonstrate Full Outer Join

1.Create the following Relation


create table eemployee(empid number(5) primary key,
name varchar(20),
dep varchar(20));

Create table ttransaction(tranid number(5) primary key,


empid number(5),
agency varchar(20),
amount number(10));

2.Insert 5 records in each relation.


insert into eemployee values(1,'vivek','IT');
insert into eemployee values(2,'roger','Operations');
insert into eemployee values(3,'maria','Sales');
insert into eemployee values(4,'rohit','Sales');
insert into eemployee values(5,'pooja','HR');

insert into ttransaction values(1,3,'direct',500);


insert into ttransaction values(2,3,'direct',600);
insert into ttransaction values(3,4,'direct',700);
insert into ttransaction values(4,4,'direct',800);
insert into ttransaction values(5,6,'direct',1100);
insert into ttransaction values(6,6,'direct',200);
insert into ttransaction values(7,6,'direct',900);
insert into ttransaction values(8,2,'direct',1000);
insert into ttransaction values(9,2,'direct',1500);

pg. 7
DATABASE MANAGEMENT SYSTEM LAB RECORD
insert into ttransaction values(10,2,'direct',1300);
select * from eemployee;

select * from ttransaction;

3.Demonstrate Inner Join


select e.empid,sum(amount)
from eemployee e
join
ttransaction t
on e.empid=t.empid
group by e.empid
OUTPUT:

4.Demonstrate Right Outer Join


select e.empid,sum(amount)
from eemployee e
right outer join
ttransaction t
on e.empid=t.empid

pg. 8
DATABASE MANAGEMENT SYSTEM LAB RECORD
group by e.empid
OUTPUT:

5.Demonstrate Left Outer Join

select e.empid,sum(amount)
from eemployee e
left outer join
ttransaction t
on e.empid=t.empid
group by e.empid
OUTPUT:

6.Demonstrate Full Outer Join


select e.empid,sum(amount)
from eemployee e
full outer join
ttransaction t
on e.empid=t.empid
group by e.empid
OUTPUT

pg. 9
DATABASE MANAGEMENT SYSTEM LAB RECORD

PROGRAM: 07
Create view for particular table
Demonstrate the following:
1. Create a table “studentdetails” & enter 5 records.
2. Create a view by name “detailsview” & select “sid & name only”
3. Insert the record in view
4. Update the record in view
5. Drop view

_____________________________________________________________________
Step 1: Create a table “studentdetails” & enter 5 records.
create table studentdetails(sid number(5) primary key, name varchar(10),address
varchar(10));

insert into studentdetails values(1,'Harsh','Kolkata');


insert into studentdetails values(2,'Ashish','Delhi');
insert into studentdetails values(3,'Pratik','Bihar');
insert into studentdetails values(4,'Dhanraj','Mumbai');
insert into studentdetails values(5,'Ram','Pune');

select * from studentdetails;


OUTPUT:

Step 2: Create a view by name “detailsview” & select “sid & name only”
create view detailsview as
select sid, name
from studentdetails;
select * from detailsview;

pg. 10
DATABASE MANAGEMENT SYSTEM LAB RECORD

Step 3: Insert the record in view


insert into detailsview values(6,'Sham');
select * from detailsview;
OUTPUT

Step 4: Update the record in view


update detailsview set name='Soumya' where sid=1;
select * from detailsview;
OUTPUT

Step5 : Drop view


drop view detailsview;

pg. 11
DATABASE MANAGEMENT SYSTEM LAB RECORD

PROGRAM: 08
Implement Locks for particular table.

Step 1: Login to admin user USERNAME:system PASSWORD:student/system


SQL> connect
Enter user-name: system
Enter password:
Connected.

Step 2:Create a table device8 and insert 5 records.

SQL> create table device8


2(
3 did number(5) primary key,
4 dname varchar(20),
5 cost number(5));

SQL> insert into device8 values(1,'printer',6000);


SQL> insert into device8 values(2,'mouse',1000);
SQL> insert into device8 values(3,'keyboard',8000);
SQL> insert into device8 values(4,'monitor',5000);
SQL> insert into device8 values(5,'pendrive',500);
SQL> select * from device8;
OUTPUT:

SQL>commit;
Step 3:Execuate the following query.
SQL> update device8 set cost=800 where did=5;
OUTPUT

pg. 12
DATABASE MANAGEMENT SYSTEM LAB RECORD
SQL> set autocommit off;
SQL> lock table device8 in exclusive mode nowait;
OUTPUT:

Step 4: Create a new user USERNAME:sem3 PASSWORD dbms.


SQL> create user sem3 identified by dbms;
OUTPUT:

Step 5: Grant permission to new user sem3.


SQL> grant connect,resource,dba to sem3;
OUTPUT

Step 6:Now open new window(command prompt) login to new user you have
created.
SQL> connect
Enter user-name: sem3
Enter password:
Connected.
SQL> select * from system.device8;
OUTPUT

pg. 13
DATABASE MANAGEMENT SYSTEM LAB RECORD

Step 7: Execute the following query.


SQL> update system.device8 set cost=200 where did=5;
OUTPUT

Step 8: Go to Admin user & try to update the record.


SQL> update device8 set cost=100 where did=5;
|
This happens because the admin user has locked the table device8.

Step 9:Go to Sem3 user & type commit.(When you enter commit in sem3 user
automatically record will be updated in admin user).
SQL> commit;
Commit complete.

pg. 14
DATABASE MANAGEMENT SYSTEM LAB RECORD

PROGRAM: 09
Write a PL/SQL procedure for an application using exception
handling.
CREATE TABLE CUST
(
EID NUMBER(5) PRIMARY KEY,
NAME VARCHAR(20),
ADDRESS VARCHAR(20),
SALARY NUMBER(5));

INSERT INTO CUST VALUES(1,'RAM','BELGAUM',20000);


INSERT INTO CUST VALUES(2,'VIVEK','HUBALI',20000);
INSERT INTO CUST VALUES(3,'RAMAN','GOA',20000);
INSERT INTO CUST VALUES(4,'SOUMYA','DHARWAD',20000);

SELECT * FROM CUST;


OUTPUT

DECLARE
c_id cust.eid%type := 8;
c_name cust.name%type;
c_addr cust.address%type;
BEGIN
SELECT name, address INTO c_name, c_addr
FROM cust
WHERE eid = c_id;
DBMS_OUTPUT.PUT_LINE ('Name: '|| c_name);
DBMS_OUTPUT.PUT_LINE ('Address: ' || c_addr);
EXCEPTION
WHEN no_data_found THEN
dbms_output.put_line('No such customer!');
WHEN others THEN
dbms_output.put_line('Error!');
END;

pg. 15
DATABASE MANAGEMENT SYSTEM LAB RECORD

PROGRAM: 10
Write a PL/SQL procedure for an application using cursors.

CREATE TABLE CUST


(
EID NUMBER(5) PRIMARY KEY,
NAME VARCHAR(20),
ADDRESS VARCHAR(20),
SALARY NUMBER(5));

INSERT INTO CUST VALUES(1,'RAM','BELGAUM',20000);


INSERT INTO CUST VALUES(2,'VIVEK','HUBALI',20000);
INSERT INTO CUST VALUES(3,'RAMAN','GOA',20000);
INSERT INTO CUST VALUES(4,'SOUMYA','DHARWAD',20000);

SELECT * FROM CUST;


OUTPUT

DECLARE
c_id cust.eid%type;
c_name cust.name%type;
c_addr cust.address%type;
CURSOR c_cust is
SELECT eid, name, address FROM cust;
BEGIN
OPEN c_cust;
LOOP
FETCH c_cust into c_id, c_name, c_addr;
EXIT WHEN c_cust%notfound;
dbms_output.put_line(c_id || ' ' || c_name || ' ' || c_addr);
END LOOP;
CLOSE c_cust;
END;

pg. 16
DATABASE MANAGEMENT SYSTEM LAB RECORD

PROGRAM: 11
Write a PL/SQL procedure for an application using functions.
CREATE TABLE CUST
(
EID NUMBER(5) PRIMARY KEY,
NAME VARCHAR(20),
ADDRESS VARCHAR(20),
SALARY NUMBER(5));

INSERT INTO CUST VALUES(1,'RAM','BELGAUM',20000);


INSERT INTO CUST VALUES(2,'VIVEK','HUBALI',20000);
INSERT INTO CUST VALUES(3,'RAMAN','GOA',20000);
INSERT INTO CUST VALUES(4,'SOUMYA','DHARWAD',20000);

SELECT * FROM CUST;


OUTPUT

1)Function Creation.
create or replace function adder(n1 in number, n2 in number)
return number
is
n3 number(8);
begin
n3 :=n1+n2;
return n3;
end;

pg. 17
DATABASE MANAGEMENT SYSTEM LAB RECORD

2)Function Defination.
DECLARE
n3 number(2);
BEGIN
n3 := adder(11,22);
dbms_output.put_line('Addition is: ' || n3);
END;

pg. 18
DATABASE MANAGEMENT SYSTEM LAB RECORD

PROGRAM: 12
Write a PL/SQL procedure for an application using package.

create table student


( roll_no number(10) primary key,
sname varchar(20),
age number(5),
course varchar(20));

insert into student values(11,'veena',22,'belgaum');


insert into student values(12,'vinay',21,'dharwad');
insert into student values(13,'vijay',25,'hubali');
insert into student values(14,'vidhya',24,'mysore');
insert into student values(15,'vivek',23,'banglore');

select * from student;


OUTPUT

1)Package Creation
CREATE OR REPLACE PACKAGE pkg_student IS
PROCEDURE updateRecord(sno student.roll_no%type);
END pkg_student;

pg. 19
DATABASE MANAGEMENT SYSTEM LAB RECORD

2)Package body Creation.


CREATE OR REPLACE PACKAGE BODY pkg_student IS
PROCEDURE updateRecord(sno student.roll_no%type) IS
BEGIN
Update student set age=27 where roll_no=sno;
IF SQL%FOUND THEN
dbms_output.put_line('RECORD UPDATED');
ELSE
dbms_output.put_line('RECORD NOT FOUND');
END IF;
END updateRecord;
END pkg_student;

DECLARE
sno student.roll_no%type;
s_age student.age%type;
snm student.SName%type;
BEGIN
sno:=:sno;
pkg_student.updateRecord(sno);
END;

select * from student;

pg. 20

You might also like