0% found this document useful (0 votes)
12 views27 pages

DBMS Lab Mannual

The document outlines various SQL operations including executing single line queries, DDL, DML, DCL, and TCL commands, as well as implementing nested queries and join operations. It provides detailed examples of creating tables, inserting data, and executing various SQL functions such as aggregate functions and joins. Additionally, it includes instructions for creating views and managing user permissions in a database context.

Uploaded by

hdshb786
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views27 pages

DBMS Lab Mannual

The document outlines various SQL operations including executing single line queries, DDL, DML, DCL, and TCL commands, as well as implementing nested queries and join operations. It provides detailed examples of creating tables, inserting data, and executing various SQL functions such as aggregate functions and joins. Additionally, it includes instructions for creating views and managing user permissions in a database context.

Uploaded by

hdshb786
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 27

Database Management System (DBMS)

SI.NO PROGRAMS PAGE REMARKS


NO
1. Execute a single line query and group
functions.
2. Execute DDL Commands.

3. Execute DML Commands.

4. Execute DCL AND TCL Commands.

5. Implement the Nested Queries.

6. Implement Join operations in SQL.

7. Create views for a particular table.

8. Implement Locks for a particular Table

9. Write PL/SQL procedure for an


application using exception handling.
10. Write PL/SQL procedure for an
application using cursors.
11. Write PL/SQL procedure for an
application using functions.
12. Write PL/SQL procedure for an
application using package.
SHREE MEDHA DEGREE COLLEGE, BALLARI

1. Execute a single line query and group functions.


Queries:
1. Create the table Employee with the following attributes: (Empid
number (5), EmployeeName varchar (25), Empsal number (7));
2. Enter five tuples into the existing table and display.
3. Execute sum() to get value of numerical column and display.
4. Execute Avg() to get value of numerical column and display.
5. Execute Max() to get value of numerical column and display.
6. Execute Min() to get value of numerical column and display.
7. Execute count(expression) to get number of records in a particular
column and display.
8. Execute count(*) to get number of rows in all the column and display.
Answer:
Query1: Create the table Employee with the following attributes: (Empid
number (5), EmployeeName varchar (25), Empsal number (7));
Command 1: create table Employee(Empid number(5), EmployeeName
varchar (25), Empsal number (7));

Query2: Enter five tuples into the existing table and display.
Command 2:
insert into Employee values(1001,’Ramesh’,25000);
insert into Employee values(1002,’Tagore’,20000);
insert into Employee values(1003,’Bharath’,10000);
insert into Employee values(1004,’Prakesh’,5000);
insert into Employee values(1005,’Shreedhar’100000);

DEPT. OF COMPUTER SCIENCE Page No.


SHREE MEDHA DEGREE COLLEGE, BALLARI
select*from Employee;

Query3: Execute sum() to get value of numerical column and display.


Command 3: select sum (Empsal)from Employee;

Query4: Execute Avg() to get value of numerical column and display.


Command 4: select Avg(Empsal) from Employee;

Query5: Execute Max() to get value of numerical column and display.


Command 5: select Max(Empsal) from Employee;

Query6: Execute Min() to get value of numerical column and display.


Command 6: select Min(Empsal) from Employee;

Query7: Execute count(expression) to get number of records in a


particular column and display.
Command 7: select count (Empsal) from Employee;

Query 8: Execute count(*) to get number of rows in all the columns and
display.
Command 8: select count(*) from Employee;

DEPT. OF COMPUTER SCIENCE Page No.


SHREE MEDHA DEGREE COLLEGE, BALLARI

2. EXECUTE DDL COMMANDS


Queries:
1. Create the table student with following attributes: (Regno number (10),
studentname varchar (25), Totalmarks number (4));
2. Add a new attribute Avg with datatype as number with size (5,2) to
the existing table.
3. change the size of avg from (5,2) to (7,2).
4. Rename the table student as scholar.
5. Enter five tuples into the existing table.
6. Delete all the records of the table at once.
7. Drop the existing table scholar.
Answer:
Query1: Create the table student with following attributes:
(Regno number (10), studentname varchar (25), Totalmarks number (4));
Command1: Create table student (Regno number (10), studentname varchar
(25), Totalmarks number (4));
Desc student;

Query2: Add a new attribute Avg with datatype as number with size (5,2) to
the existing table.
Command2: alter table student add (Avg number(5,2));
Desc student;

DEPT. OF COMPUTER SCIENCE Page No.


SHREE MEDHA DEGREE COLLEGE, BALLARI
Query3: change the size of Avg from (5,2) to (7,2).
Command3: alter table student modify (Avg number (7,2));
Desc student;

Query4: Rename the table student as scholar.


Command4: Rename student to scholar;
Desc scholar;

Query5: Enter five tuples into the existing table.


Command5: insert into scholar values(101,’prashanth’,680,99.71);
insert into scholar values(102,’Raju’,690,79.82);
insert into scholar values(103,’Ramesh’,700,91.91);
insert into scholar values(104,’Suresh’,400,89.72);
insert into scholar values(105,’Raghu’,900,321.735);
select*from scholar;

Query6: Delete all the records of the data at once.


Command6: truncate table scholar;
Select*from scholar;

DEPT. OF COMPUTER SCIENCE Page No.


SHREE MEDHA DEGREE COLLEGE, BALLARI
Query7: Drop the existing table scholar.
Command7: drop table scholar;

DEPT. OF COMPUTER SCIENCE Page No.


SHREE MEDHA DEGREE COLLEGE, BALLARI

3. EXECUTE DML COMMANDS


Queries:
1. Create the table of a professor with the following attributes: (Profid
number (10), ProfName varchar (25), Department varchar (25), Phno
number (10));
2. Enter five tuples into the existing table and display it.
3. Display the records of the attributes ProfName and Department.
4. Display any single record from the existing table.
5. update the Profid of any one Professor and display the existing table.
6. Delete the record of any one Professor.
7. Display the table.
Answer:
Query 1: Create the table of a professor with the following attributes:
(Profid number (10), ProfName varchar (25), Department varchar (25), Phno
number (10));
Command 1: Create table Professor (Profid number (10), ProfName varchar
(25), department varchar (25), Phno number (10));
Desc Professor;

Query2: Enter five tuples into the existing table and display it.
Command 2:
insert into Professor values(501,’Amarnath’,’Zoology’,9851236880);
insert into Professor values(502,’Srinivas’,’Botony’,9834205651);
insert into Professor values(503,’Ram’,’Chemistry’,8996565430);
insert into Professor values(504,’Rehman’,’Botony’,9943210381);
insert into Professor values(505,’John’,’Psychology’,9833209811);
select*from Professor;

DEPT. OF COMPUTER SCIENCE Page No.


SHREE MEDHA DEGREE COLLEGE, BALLARI

Query3: Display the records of the attributes ProfName and Department.


Command 3: select ProfName, Department from Professor;

Query4: Display any single record from the existing table.


Command 4: select*from Professor where Profid=502;

Query5: update the Profid of any one Professor and display the existing table.
Command 5: update Professor set Profid =509 where Profid=505;

Query 6: Delete the records of any one Professor.


Command 6: Delete from professor where Profid =509;
Select*from professor;

DEPT. OF COMPUTER SCIENCE Page No.


SHREE MEDHA DEGREE COLLEGE, BALLARI

4. TO EXECUTE DCL AND TCL COMMANDS


NOTE:
1.Go to search bar
2.Type: run SQL command line and open it
3.Type: connect<user_name>;
4. Password: <password>
Note: create two users before executing TCL commands that is for grant and
revoke
Queries:
i. Create table of the Cities with following attributes:(ID number (1), name
varchar (20), city varchar (10), contact number (10));
ii. Enter five tuples and display it
iii. Update any one of attribute from the table and display it
iv. Use commit command
v. Update any one of attribute from the table and display it
vi. Use rollback command and display it
vii. Update any one of attribute from the table
viii. Create a savepoint commands
ix. Update any one of attribute from the table
x. Create another savepoint command and display it
xi. Rollback to <savepoint_name> and display it
xii. Use grant command and display it
xiii. Use revoke command

Answer:
Query 1: Create table of the Cities with following attributes:(ID number (1),
name varchar (20), city varchar (10), contact number (10));
Command 1: Create table Cities (ID number (1), name varchar (20),
city varchar (10), contact number (10));

DEPT. OF COMPUTER SCIENCE Page No.


SHREE MEDHA DEGREE COLLEGE, BALLARI
Query 2: Enter five tuples and display it
Command 2: insert into Cities values(1,’Robo’,’Chandigarh’,9999988888);
insert into Cities values(2,’Sana’,’Ambala’,7777755555);
insert into Cities values(3,’Dimple’,’Delhi’,4444400000);
insert into Cities values(4,’Rishi’,’Mumbai’,3333322222);
insert into Cities values(5,’Chiti’,’Goa’,6666611111);
select*from Cities;

Query 3: Update any one of attribute from the table and display it
Command 3: update Cities set city=’Delhi’ where ID=1;
Select*from Cities;

Query 4: Use commit command


Command 4: commit;
Query 5: Update any one of attribute from the table and display it
Command 5: update Cities set city=’Pune’ where ID=1;
select*from Cities;

DEPT. OF COMPUTER SCIENCE Page No.


SHREE MEDHA DEGREE COLLEGE, BALLARI
Query 6: Use rollback command and display it
Command 6: rollback;
Select*from Cities;

Query 7: Update any one of attribute from the table


Command 7: update Cities set City=’Pune’ where ID=1;

Query 8: Create a Savepoint


Command 8: Savepoint A;

Query 9: update anyone of the attribute from the table


Command 9: update Cities set City=’Pune’ where ID=3;

Query 10: create another savepoint and display it


Command 10: Savepoint B;
Select*from Cities;

DEPT. OF COMPUTER SCIENCE Page No.


SHREE MEDHA DEGREE COLLEGE, BALLARI
Query 11: rollback to <savepoint_name> and display it
Command 11: rollback to A;
Select*from Cities;

 Create a table in system database user by name Student with


following attributes:
Create table Student (S.no number (1), Name varchar (20), class varchar (7),
course varchar (7));
 Insert five tuples into it
Query 12: use grant command and display it
Command 12: grant select on Student to system;
Select*from Student;
 grant succeeded

Query 13: use revoke command


Command 13: revoke select on Student from system;
 Revoke succeeded

DEPT. OF COMPUTER SCIENCE Page No.


SHREE MEDHA DEGREE COLLEGE, BALLARI

5. IMPLEMENT THE NESTED QUERIES


Queries:
i. Create table Employee with the following attributes:(EmpID number (1),
EName varchar (10), Empsal number (5));
ii. Enter five tuples into the existing table and display it
iii. Implement nested query and display the name who has got highest salary
Answer:
Query 1: Create table Employee with the following attributes:(EmpID
number (1), EName varchar (10),Empsal number(5));
Command 1: Create table Employee (EmpID number (1), EName varchar
(10), Empsal number (5));

Query 2: Enter five tuples into the existing table and display it
Command 2:
Insert into Employee values(1, ‘Robo’,25000);
Insert into Employee values(2, ‘Chiti’, 10000);
Insert into Employee values(3, ‘Sana’,75000);
Insert into Employee values(4, ‘Vasi’, 20000);
Insert into Employee values(5, ‘Rishi’,30000);
Select*from Employee;

Query 3: Implement nested query and display the name who has got the
Highest salary
Command 3: EName from Employee where Empsal= (select max (EmpSal)
from Employee);

DEPT. OF COMPUTER SCIENCE Page No.


SHREE MEDHA DEGREE COLLEGE, BALLARI

6. Implement Join operations in SQL.


Queries:
i. Create the table Student with the following attributes:
(Rno number (3), Sname varchar (15), Courseid number (5));
ii. Create the table Course with the following
attributes: (Courseid number (5), Cname varchar
(10));
iii. Enter Three tuples into Student table and display it.
iv. Enter Three tuples into Course table and display it.
v. Execute Inner join or Equi join Operation and display it.
vi. Execute Non-Equi join Operation and display it.
vii. Execute Left Outer Join Operation and display it.
viii. Execute Right Outer Join Operation and display it.
ix. Execute Full Outer Join Operation and display it.
x. Execute Cartesian Product Join Operation and display it.

Answer:
Query 1: Create the table Student with the following attributes:
(Rno number (3), Sname varchar (15), Courseid number (5));
Command1: Create table Student (Rno number (3), Sname varchar (15),
Courseid number (5));

Desc Student;

Query 2: Create the table Course with the following attributes:


(Courseid number(5), Cname varchar(10));
Command 2: Create table Course(Courseid number(5), Cname varchar(10));
Desc Course;

Query 3: Enter Three tuples into Student table and display it.
Command 3: Insert into Student values(701,’Preethi’,4001);
Insert into Student values(702,’Chaithra’,4002);
Insert into Student values(703,’Nithya’,4003);

DEPT. OF COMPUTER SCIENCE Page No.


SHREE MEDHA DEGREE COLLEGE, BALLARI
Select *from Student;

Query 4: Enter Three tuples into Course table and display it.
Command 4: Insert into Course values(4001,’C lang’);
Insert into Course values(4003,’Java’);
Insert into Course values(4004,’Python’);
Select *from Course;

Query 5: Execute Inner join or Equi join Operation and display it.
Command 5: Select Sname,Cname from Student inner join Course on
Student.Courseid=Course.Courseid;

Query 6: Execute Non-Equi join Operation and display it.


Command 6: Select Sname,Cname from Student,Course where
Student.Courseid<>Course.Courseid;

Query 7: Execute Left Outer Join Operation and display it.


Command 7: Select Sname,Cname from Student left join Course on
Student.Courseid=Course.Courseid;

DEPT. OF COMPUTER SCIENCE Page No.


SHREE MEDHA DEGREE COLLEGE, BALLARI

Query 8: Execute Right Outer Join Operation and display it.


Command 8: Select Sname,Cname from Student right join Course on
Student.Courseid=Course.Courseid;

Query 9: Execute Full Outer Join Operation and display it.


Command 9: Select Sname,Cname from Student full join Course on
Student.Courseid=Course.Courseid;

Query 10: Execute Cartesian Product Join Operation and display it.
Command 10: Select Sname,Cname from Student,Course;

DEPT. OF COMPUTER SCIENCE Page No.


SHREE MEDHA DEGREE COLLEGE, BALLARI

7. Execute View Table in SQL


Queries:
i. Create the table student with the following attributes:
(stdRno number(1),stdName varchar(10),Total number(3));
ii. Enter five tuples into the existing table and display
iii. Create view table for the existing table with following attributes
stdRno and stdName.
iv. Display the view table.

Answer:
Query 1: Create the table student with the following attributes:
(stdRno number(1),stdName varchar(10),Total number(3));
Command 1: Create table student (stdrno number (1), stdName varchar (10),
total number(3));
 Desc student;

Query 2: Enter five tuples into the existing table and display.
Command: insert into student values(1,'preethi',700);
insert into student values(2,'chaithra',900);
insert into student values(3,'nithya',850);
insert into student values(4,'sneha',700);
insert into student values(5,'chitti',680);
• select*from student;

DEPT. OF COMPUTER SCIENCE Page No.


SHREE MEDHA DEGREE COLLEGE, BALLARI
Query 3: Create view table for the existing table with following attributes
stdRno and stdName.
Command 3: create view studentview as select stdrno,stdname from student;
 View Created
Query 4: Display the view Table
Command 4: select*from studentview;

DEPT. OF COMPUTER SCIENCE Page No.


SHREE MEDHA DEGREE COLLEGE, BALLARI

8. Implement Locks in a particular Table.


Queries:
i. Create table Student with the following Attributes(Rno
number(10), Stdname varchar(20),Course varchar(10));
ii. Insert Tuples into Student table and Display it.
iii. Use Grant command
iv. Implement Locks on student table.
Answer:
Query 1: Create table Student with the following Attributes(Rno number(10),
Stdname varchar(20),Course varchar(10));
Command 1: create table student(Rno number(10),Stdname varchar(20),
Course varchar(10));
Query 2: Insert Tuples into Student table and Display it.
Command 2: insert into student values(1,‘Preethi’,‘BCA’);
insert into student values(2,‘Nithya’,‘BBA’);
insert into student
values(3,‘Chaitra’,‘BCOM’); insert into
student values(4,‘Sneha’,‘BSC’); insert into

student values(5,‘Dimple’,‘MCA’);

Query 3: Use Grant command


Command 3: grant select on student to user;
 Grant Succeeded

DEPT. OF COMPUTER SCIENCE Page No.


SHREE MEDHA DEGREE COLLEGE, BALLARI
Query 4: Implement Locks on student table
Command 4: lock table student in share mode;

DEPT. OF COMPUTER SCIENCE Page No.


SHREE MEDHA DEGREE COLLEGE, BALLARI

9. Write a PL/SQL procedure for an application


using Exception handling
Queries
i. Create table emp with the following attributes(empno
number(10), empname varchar(10));
ii. Insert tuples into emp table
Answer:
Query 1: Create table emp with the following attributes(empno number(10),
empname varchar(10));
Command 1: create table emp(empno number(10),empname varchar(10));

Query 2: Insert tuples into emp table


Command 2: insert into emp values(69000, ‘Julius’);
declare
Eid emp.empno%type:=69000;
name emp.ename%type;
begin
select empno,ename into eid,name
from emp
where empno=eid;
dbms_output.put_line(‘empno:' ||eid);
dbms_output.put_line(‘name:' ||name);
exception
when no_data_found then
dbms_output.put_line(‘no such employee');
when others then
dbms_output.put_line(‘error’);

DEPT. OF COMPUTER SCIENCE Page No.


SHREE MEDHA DEGREE COLLEGE, BALLARI
end;
/
Output:

DEPT. OF COMPUTER SCIENCE Page No.


SHREE MEDHA DEGREE COLLEGE, BALLARI

10. Write a PL/SQL procedure for an application


using Cursors.
Queries:
i. create table emp2 of the following attributes empno , ename , job and sal
ii. insert four tuples into the existing table.
Answers:
Query 1: create table emp2 of the following attributes empno , ename , job
and sal
Command 1: create table emp2(empno number(1),ename varchar(10),job
varchar(10),sal varchar(5));
Query 2: insert four tuples into the existing table.
Command 2: insert into emp2 values(1,’Rishi’,’Manager’,’25k’);
insert into emp2 values(2,’Dimple’,’Programmer’,’75k’);
insert into emp2 values(3,’Shanu’,’Developer’,’80k’);
insert into emp2 values(4,’Vaishnavi’,’Manager’,’60k’);
declare
cursor c11 is
select*from emp2 order by job,sal;
cval emp2%rowtype;
begin
dbms_output.put_line(‘empno’ || ‘ename’);
open c11;
loop
fetch c11 into cval;
exit when c11%rowcount=4;
dbms_output.put_line(cval.empno||' '||cval.ename);
end loop;
close c11;
end;
/

DEPT. OF COMPUTER SCIENCE Page No.


SHREE MEDHA DEGREE COLLEGE, BALLARI
Output:

DEPT. OF COMPUTER SCIENCE Page No.


SHREE MEDHA DEGREE COLLEGE, BALLARI

11.Write a PL/SQL procedure for an application using


Functions.
create or replace function fname(a in number,b in out number)
return number is
begin
b:=a;
return b;
end;
/
--Function created.
declare
X number;
begin
X:=fname(23,x);
dbms_output.put_line(x);
end;
/
Output:

DEPT. OF COMPUTER SCIENCE Page No.


SHREE MEDHA DEGREE COLLEGE, BALLARI

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


package.
Step 1: Package specification
create package cust_sal as
procedure find_sal(c_id customers.id%type);
end cust_sal;
/
Step 2: Package body
create or replace package body cust_sal as
procedure find_sal(c_id customers.id%type) is
c_sal customers.salary%type;
begin
select salary into c_sal
from customers
where id = c_id;
dbms_output.put_line('Salary: '|| c_sal);
end find_sal;
end cust_sal;
/
Step 3: Calling function
declare
code customers.id%type := &c_id;
begin
cust_sal.find_sal(code);
end;
/

DEPT. OF COMPUTER SCIENCE Page No.


SHREE MEDHA DEGREE COLLEGE, BALLARI
Output:

DEPT. OF COMPUTER SCIENCE Page No.

You might also like