SESHADRIPURAM EDUCATIONAL TRUST
SESHADRIPURAM
COLLEGE,TUMAKURU
Approved by AICTE-New Delhi | Recognized by Government of Karnataka
Affiliated to Tumkur University | ISO 9001:2015 Certified Institution
#3, “Vikasa Bharati”,Kalpatharu Badavane,Gangasandra Road,Melekote
,Tumkuru-572105.
Department Of BCA
Lab Manual
Subject
nd rd
DBMS Lab
2 Year 3 SEM (NEP)
3SEM_BCA_DBMS LAB COURSE CODE: DSC4LAB
Database Management System Lab Manual
BASIC SQL COMMANDS (DDL and DML)
Create Database
The SQL CREATE DATABASE statement is used to create a new SQL database.
Syntax
The basic syntax of this CREATE DATABASE statement is as follows −
CREATE DATABASE DatabaseName;
Always the database name should be unique within the RDBMS.
Example
If you want to create a new database <testDB>, then the CREATE DATABASE statement would be as shown
below −
SQL> CREATE DATABASE testDB ;
Create Table:
Creating a basic table involves naming the table and defining its columns and each column's data type.
The SQL CREATE TABLE statement is used to create a new table.
Syntax
The basic syntax of the CREATE TABLE statement is as follows −
CREATE TABLE table_name (
column1 datatype, column2
datatype, column3 datatype,
.....
columnN datatype,
PRIMARY KEY( one or more columns )
);
DEARMENT OF CS, SESHADRIPURAM COLLEGE, TUMAKURU-5 1
3SEM_BCA_DBMS LAB COURSE CODE: DSC4LAB
CREATE TABLE is the keyword telling the database system what you want to do. In this case, you want to create
a new table. The unique name or identifier for the table follows the CREATE TABLE statement.
Then in brackets comes the list defining each column in the table and what sort of data type it is. The syntax
becomes clearer with the following example.
A copy of an existing table can be created using a combination of the CREATE TABLE statement and the
SELECT statement. You can check the complete details at Create Table Using another Table.
Example
The following code block is an example, which creates a CUSTOMERS table with an ID as a primary key and
NOT NULL are the constraints showing that these fields cannot be NULL while creating records in this table −
SQL> CREATE TABLE CUSTOMERS(
ID INT NOT NULL,
NAME VARCHAR (20) NOT NULL,
AGE INT NOT NULL,
ADDRESS CHAR (25) ,
SALARY DECIMAL (18, 2),
PRIMARY KEY (ID)
);
You can verify if your table has been created successfully by looking at the message displayed by the SQL
server, otherwise you can use the DESC command as follows −
DEARMENT OF CS, SESHADRIPURAM COLLEGE, TUMAKURU-5 2
3SEM_BCA_DBMS LAB COURSE CODE: DSC4LAB
DROP table
The SQL DROP TABLE statement is used to remove a table definition and all the data, indexes, triggers,
constraints and permission specifications for that table.
NOTE − You should be very careful while using this command because once a table is deleted then all the
information available in that table will also be lost forever.
Syntax
The basic syntax of this DROP TABLE statement is as follows −
DROP TABLE table_name;
The SQL INSERT INTO Statement is used to add new rows of data to a table in the database.
Syntax
There are two basic syntaxes of the INSERT INTO statement which are shown below.
INSERT INTO TABLE_NAME (column1, column2, column3,...columnN) VALUES
(value1, value2, value3,...valueN);
DEARMENT OF CS, SESHADRIPURAM COLLEGE, TUMAKURU-5 3
3SEM_BCA_DBMS LAB COURSE CODE: DSC4LAB
Here, column1, column2, column3,...columnN are the names of the columns in the table into which you want to
insert the data.
You may not need to specify the column(s) name in the SQL query if you are adding values for all the columns of
the table. But make sure the order of the values is in the same order as the columns in the table.
The SQL INSERT INTO syntax will be as follows −
INSERT INTO TABLE_NAME VALUES (value1,value2,value3,...valueN);
Example
DEARMENT OF CS, SESHADRIPURAM COLLEGE, TUMAKURU-5 4
3SEM_BCA_DBMS LAB COURSE CODE: DSC4LAB
SELECT Command
The SQL SELECT statement is used to fetch the data from a database table which returns this data in the form of
a result table. These result tables are called result-sets.
Syntax
The basic syntax of the SELECT statement is as follows −
SELECT column1, column2, columnN FROM table_name;
Here, column1, column2... are the fields of a table whose values you want to fetch. If you want to fetch all the
fields available in the field, then you can use the following syntax.
SELECT * FROM table_name;
For the customer table
The following code is an example, which would fetch the ID, Name and Salary fields of the customers available in
CUSTOMERS table.
SQL> SELECT ID, NAME , SALARY FROM CUSTOMERS;
DEARMENT OF CS, SESHADRIPURAM COLLEGE, TUMAKURU-5 5
3SEM_BCA_DBMS LAB COURSE CODE: DSC4LAB
If you want to fetch all the fields of the CUSTOMERS table, then you should use the following query.
SQL> SELECT * FROM CUSTOMERS ;
DEARMENT OF CS, SESHADRIPURAM COLLEGE, TUMAKURU-5 6
3SEM_BCA_DBMS LAB COURSE CODE: DSC4LAB
DBMS LAB PROGRAMS
1. Execute DDL Commands
Consider the table:
STUDENTS (regno number, name varchar2,dob date, marks number)
a) Create the above table with suitable constraints.
b) Remove the existing attribute marks from the table.
c) Change the data type of regno from number to varchar2.
d) Add a new attribute phno to the existing table.
e) Insert 5 tuples into the table.
f) Display the tuples in table.
a) Create the above table with suitable constraints.
Create table STUDENTS (regno number not null,
Name varchar2(20) not null,
Dob date,
Marks number,
Primary key(regno));
desc STUDENTS;
0/p
DEARMENT OF CS, SESHADRIPURAM COLLEGE, TUMAKURU-5 7
3SEM_BCA_DBMS LAB COURSE CODE: DSC4LAB
b) Remove the existing attribute marks from the table.
ALTER table STUDENTS drop column marks;
c) Change the data type of regno from number to varchar2.
ALTER table STUDENTS MODIFY regno varchar2(20);
d) Add a new attribute phno to the existing table.
ALTER table STUDENTS ADD phno number;
e) Insert 5 tuples into the table.
Insert into Students values(101, ’sagar’, '9-jan-1997', 8088132301);
Insert into STUDENTS values (102, ’Lokesh’, ’5-Jun-1999’, 8088191010);
Insert into STUDENTS values (103, ’Ramya’, ’9-Oct-2000’, 8520108813);
Insert into STUDENTS values (104, ’Savitha’, ’9-Nov-2004’, 9805458813);
Insert into STUDENTS values (105, ’Ravi’, ’9-Feb-2008’, 9825808813);
f) Display the tuples in table.
2. Execute DML Commands
Consider the table: Library (bid number, title varchar2, author varchar2, publisher varchar2, yearof_pub year,
price number)
a) Create the above table.
b) Enter 5 tuples into the table.
c) Display all the tuples from the table.
d) Display different publisher from table.
DEARMENT OF CS, SESHADRIPURAM COLLEGE, TUMAKURU-5 8
3SEM_BCA_DBMS LAB COURSE CODE: DSC4LAB
e) Update price of all books with 5% GST amount.
f) Delete the details of book published by a specific author.
g) Arrange the tuples in the alphabetical order of book title.
h) List the details of all books whose price range between 100rs and 300rs.
a) Create the above table.
Insert into Library val
Create table Library (bid number not null,
Title varchar2(20) not null,
Author varchar2(20),
Publisher varchar2(20),
Yearof_pub varchar2(20),
Price number,
Primary key(bid));
o/p
Desc Library;
b) Enter 5 tuples into the table.
Insert into Library values (1101, ’C-programming’, ’Dennis Ritchie’, ‘Somanath’, 1987, 500);
Insert into Library values (1102, ’Animal Farm’, ’George Orwell’, ‘Leo’, 2001, 1500);
ues (1103, ’A
Passage to India’, ’E M Forster’, ‘Jake’, 1997, 1200);
DEARMENT OF CS, SESHADRIPURAM COLLEGE, TUMAKURU-5 9
3SEM_BCA_DBMS LAB COURSE CODE: DSC4LAB
Insert into Library values (1104, ’Time Machine’, ’H G Wells’, ‘Scott’, 1999, 300); Insert
into Library values (1105, ’Shakuntala’, ’Kalidasa’, ‘Sagar ’, 1885, 450);
c) Display all the tuples from the table.
Select * from library;
DEARMENT OF CS, SESHADRIPURAM COLLEGE, TUMAKURU-5 10
3SEM_BCA_DBMS LAB COURSE CODE: DSC4LAB
d) Display different publisher from table.
Select publisher from Library;
e) Update price of all books with 5% GST amount.
Update Library set price = price + price* 0.05;
DEARMENT OF CS, SESHADRIPURAM COLLEGE, TUMAKURU-5 11
3SEM_BCA_DBMS LAB COURSE CODE: DSC4LAB
f) Delete the details of book published by a specific author.
Delete from Library where author = ‘E M
Forster’; o/p 1 row deleted.
g) Arrange the tuples in the alphabetical order of book title.
Select * from library ORDER BY title asc;
h) List the details of all books whose price range between 100rs and 300rs.
Select * from library where price between 100 and 300;
o/p
no rows selected;
3. Execute DCL and group functions
Consider the table EMPLOYEE1 (EMPID, EmpName, Dept, Salary, DOJ, Branch)
Perform the following operations: a) Create the table
DEARMENT OF CS, SESHADRIPURAM COLLEGE, TUMAKURU-5 12
3SEM_BCA_DBMS LAB COURSE CODE: DSC4LAB
b) Insert 5 tuples into the table
c) Retrieve average salary of all employees
d) Retrieve number of employees
e) Retrieve distinct number of employees
Retrieve total salary of employee group by employee name and count similar names
g) Display details of employees whose salary is greater than 50000
h) Perform commit and Rollback operations.
a) Create the table
Create table Employee1(EMPID number NOT NULL, EmpName varchar2(20), Dept
varchar2(20), Salary number, DOJ date, Branch varchar2(20), primary key(EMPID));
desc Employee1;
0/p
b) Insert 5 tuples into the table f)
Insert into employee1 values(111, 'Chandru', 'IT Sector', 26000, '23-jan-2005', 'Tumkur');
Insert into employee1 values(112, 'Ravikanth', 'Agriculture', 56000, '3-Mar-2008', 'Kolar');
Insert into employee1 values(113, 'Mohanlal', 'Police', 32000, '2-Feb-2010', 'Bengaluru');
Insert into employee1 values(114, 'Chakravarthy', 'Politics', 15000, '24-Sep-2015', 'Tumkur');
Insert into employee1 values(115, 'Shekar', 'Government', 38000, '18-jan-2018', 'Manglore');
DEARMENT OF CS, SESHADRIPURAM COLLEGE, TUMAKURU-5 13
3SEM_BCA_DBMS LAB COURSE CODE: DSC4LAB
Insert into employee1 values(112, 'Chandru', 'Agriculture', 26000, '3-Mar-2008', 'Kolar');
c) Retrieve average salary of all employees
Select avg(Salary) from employee1;
d) Retrieve number of employees
Select count(*) from employee1;
e) Retrieve distinct number of employees
Select distinct count(*) from employee1;
Retrieve total salary of employee group by employee name and count similar names f)
select sum(salary) as Total_Salary, count(Empname) as Similar_names, Empname
from Employee1 group by EmpName;
DEARMENT OF CS, SESHADRIPURAM COLLEGE, TUMAKURU-5 14
3SEM_BCA_DBMS LAB COURSE CODE: DSC4LAB
g) Display details of employees whose salary is greater than 50000
select * from
Employee1 2 where
salary > 50000;
h) Perform commit and Rollback operations.
o/p
DEARMENT OF CS, SESHADRIPURAM COLLEGE, TUMAKURU-5 15
3SEM_BCA_DBMS LAB COURSE CODE: DSC4LAB
4. Implement the Nested Queries.
An INVENTORY database has the following table.
ITEM(itemcode number, name varchar2, price number)
PURCHASE1(itemcode number, Qty number)
DEARMENT OF CS, SESHADRIPURAM COLLEGE, TUMAKURU-5 16
3SEM_BCA_DBMS LAB COURSE CODE: DSC4LAB
a) Create the tables with the above attributes.
b) Enter 5 Tuples into the tables.
c) List the items purchased.
d) List the items which are not purchased by anyone.
a) Create the tables with the above attributes.
Create table ITEM(item_code number, item_name varchar2(20), price number, primary key(item_code));
Create table PURCHASE1(item_code number, Qty number);
o/p
b) Enter 5 Tuples into the tables.
For ITEMS table:
Insert into ITEM values(201, ‘Book’, 900);
Insert into ITEM values(202, 'Pen', 500);
Insert into ITEM values(203, 'Keyboard', 450);
Insert into ITEM values(204, 'Mouse', 330);
Insert into ITEM values(205, 'Monitor', 850);
Insert into ITEM values(206, 'Pencil', 850);
Insert into ITEM values(207, 'Eraser', 850);
Insert into ITEM values(208, 'scale', 500);
DEARMENT OF CS, SESHADRIPURAM COLLEGE, TUMAKURU-5 17
3SEM_BCA_DBMS LAB COURSE CODE: DSC4LAB
o/p
For PURCHASE1 table:
Insert into PURCHASE1 values(201, 25);
Insert into PURCHASE1 values(202, 65);
Insert into PURCHASE1 values(206, 20);
Insert into PURCHASE1 values(207, 48);
Insert into PURCHASE1 values(208, 34);
C) List the items purchased.
Select * from purchase1;
d) List the items which are not purchased by anyone
select Item_code , item_name from ITEM where item_code not in(select item_code from Purchase1);
5. Implement the JOIN Operation in SQL.
DEARMENT OF CS, SESHADRIPURAM COLLEGE, TUMAKURU-5 18
3SEM_BCA_DBMS LAB COURSE CODE: DSC4LAB
The COMPANY database consists of the tables
EMPLOYEE2 (SSN, Name, Address, Sex, Salary, SuperSSN, Dno)
Department (Dno, Dname, MgrSSN, MgrStartDate)
Create tables, Insert 5 Tuples each and perform the following
a) Give a 10 percent raise in salary for all employees working in the ‘Research’ Department.
b) Retrieve the name of each employee controlled by department number 5 (use EXISTS
operator).
c) Retrieve the name of employees working in each department which has at least 2 employees
d) Retrieve the name of employees and their department name(using NATURAL JOIN)
e) Perform EQUI join operation on the given tables.
f) Perform NON-EQUI join operation on the given tables.
g) Perform OUTER join operations on the given tables.
Create table EMPLOYEE2(SSN number, Name varchar2(20), Address varchar2(20),
Sex varchar2(20),Salary number, SuperSSN number, Dno number);
Create table Department(Dno number, Dname varchar2(20), MgrSSN number, MgrStartDate
date);
FOR EMPLOYEE2:
Insert into EMPLOYEE2 values(1011, 'Jake', 'Tumkur', 'Male', 58000, 1011, 2010);
Insert into EMPLOYEE2 values(1012, 'Mahesh', 'Haveri', 'Male', 35400, 1013,
2011);
Insert into EMPLOYEE2 values(1013, 'Ramya', 'Bengaluru', 'Female', 65400, 1012,
2012); Insert into EMPLOYEE2 values(1014, 'Siddesh', 'Kolar', 'Male', 87500, 1011,
2013);
Insert into EMPLOYEE2 values(1015, 'Sangeetha', 'Tumkur', 'Female', 25500, 1014,
2014); Insert into EMPLOYEE2 values(1016, 'Sanjay', 'Tumkur', 'Male', 24500, 1013,
2015);
For Department:
Insert into Department values(2012, 'Manager', 1014, '23-Jan-
2013'); Insert into Department values(2013, 'Sales', 1015, '15Sep-2019');
Insert into Department values(2010, 'CEO', 1012, '17Feb-2021'); Insert
into Department values(2014, 'HR Dept', 1011,
'30-Mar-2018'); Insert into Department values(2011, 'Secretory',
1015, '4-Oct-2020'); Insert into Department values(2015,
'Research', 1015, '4-Nov-2021');
DEARMENT OF CS, SESHADRIPURAM COLLEGE, TUMAKURU-5 19
3SEM_BCA_DBMS LAB COURSE CODE: DSC4LAB
a) Give a 10 percent raise in salary for all employees working in the ‘Research’ Department.
Update Employee2 set Salary = Salary * 1.10 where Dno in (select Dno from Department
where Dname='Research');
DEARMENT OF CS, SESHADRIPURAM COLLEGE, TUMAKURU-5 20
3SEM_BCA_DBMS LAB COURSE CODE: DSC4LAB
select * from Employee2;
b) Retrieve the name of each employee controlled by department number 5 (use EXISTS operator).
SELECT e.Name FROM Employee2 e WHERE EXISTS (SELECT d.* FROM Department d WHERE
d.Dno=e.Dno and d.Dname='Secretory');
o/p:
DEARMENT OF CS, SESHADRIPURAM COLLEGE, TUMAKURU-5 21
3SEM_BCA_DBMS LAB COURSE CODE: DSC4LAB
Mahesh
c) Retrieve the name of employees working in each department which has at least 2 employees
Select DName, count(*) AS Number_Employee
From Department
INNER JOIN
Employee2
ON Department.Dno =
Employee2.DnoGroup by Dname
Having count(*) >=2;
d) Retrieve the name of employees and their department name(using NATURAL JOIN)
Select e.Name, d.Dname
From Employee2 e
NATURAL JOIN department d;
e) Perform EQUI join operation on the given tables.
Select * from
Employee2 INNER
JOIN Department
DEARMENT OF CS, SESHADRIPURAM COLLEGE, TUMAKURU-5 22
3SEM_BCA_DBMS LAB COURSE CODE: DSC4LAB
ON Employee2.SSN = Department.MgrSSN;
DEARMENT OF CS, SESHADRIPURAM COLLEGE, TUMAKURU-5 23
3SEM_BCA_DBMS LAB COURSE CODE: DSC4LAB
f) Perform NON-EQUI join operation on the given tables.
Select e2.Name,
d2.Dname From
Employee2 e2
INNER JOIN
Department d2ON
e2.Salary = d2.MgrSSN;
g) Perform OUTER join operation on the given tables.
Select * from Employee2
LEFT OUTER JOIN Department
ON Employee2.Dno = Department.Dno;
DEARMENT OF CS, SESHADRIPURAM COLLEGE, TUMAKURU-5 24
3SEM_BCA_DBMS LAB COURSE CODE: DSC4LAB
DEARMENT OF CS, SESHADRIPURAM COLLEGE, TUMAKURU-5 25
3SEM_BCA_DBMS LAB COURSE CODE: DSC4LAB
6 Create Views for a Particular table The RAILWAY RESERVATION SYSTEM database consists of the tables:
TRAIN(TraiNo, TrainName, StartPlace, destination)
AVAILABILITY(TrainNo, Class, StartPlace,Destination,No_of_seats)
a) Create view sleeper to display train no, start place, destination which have sleeper class and perform
the following
• Insert new record
• Update destination=’Manglore’ where train no=’RJD16’
• Delete a record which have train no=’KKE55’ create view Sleeper as select
TrainNo,StartPlace, Destination from TRain; desc Sleeper;
DEARMENT OF CS, SESHADRIPURAM COLLEGE, TUMAKURU-5 26
3SEM_BCA_DBMS LAB COURSE CODE: DSC4LAB
insert into Sleeper values ('RJD16','Bangalore','Tumkur');
insert into Sleeper values ('KKE55','Bangalore','Karvar');
insert into Sleeper values ('GLB12','Bangalore','Bijapur');
Update and delete of view sleeper update Sleeper set
destination='Mangalore' where TrainNo='RJD16'; delete train where
TrainNo='KKE55';
DEARMENT OF CS, SESHADRIPURAM COLLEGE, TUMAKURU-5 27
3SEM_BCA_DBMS LAB COURSE CODE: DSC4LAB
b) create view Details as select a.TrainNo, t.TrainName, a.Class from TRain t join availability a on t.TrainNo=
a.TrainNo;
desc Details;
c) Create view total seats to display train number, start place, use count function to no of seats, group by start
place and perform the following
• Insert new record
• Update start place=’Hubli’ where train no=’JNS8’
• Delete last row of the view
create view TotalSeats as select TrainNo,StartPlace,COUNT(No_of_seats) as seats from availability group
by StartPlace,TrainNo; desc TotalSeats;
DEARMENT OF CS, SESHADRIPURAM COLLEGE, TUMAKURU-5 28
3SEM_BCA_DBMS LAB COURSE CODE: DSC4LAB
• Insert new record
insert into availability values ('KKE55','Sleeper','Tumkur','Bangalore',3)
insert into availability values ('JNS8','Sleeper','Tumkur','Mangalore',30)
insert into availability values ('RCE43','Sleeper','Tumkur','Hubli',30)
update availability set StartPlace='Hubli' where [TrainNo]='JNS8' delete
from availability where destination='Hubli';
DEARMENT OF CS, SESHADRIPURAM COLLEGE, TUMAKURU-5 29
3SEM_BCA_DBMS LAB COURSE CODE: DSC4LAB
d) Rename view sleeper to class
rename Sleeper to Class;
e) Delete view details
drop view details;
7. Write PL/SQL procedure to compute factorial of a number using recursion
SET SERVEROUTPUT ON DECLARE
n number;
DEARMENT OF CS, SESHADRIPURAM COLLEGE, TUMAKURU-5 30
3SEM_BCA_DBMS LAB COURSE CODE: DSC4LAB
factorial number;
FUNCTION Fact(x number) /* function definition */
RETURN number
IS
BEGIN
IF x = 1
THEN
RETURN
1;
ELSE
RETURN
x * Fact(x-
1);
END IF;
END;
BEGIN
DBMS_OUTPUT.PUT_LINE(' Factorial '|| n || ' is ' || factorial);
n:= &n; factorial
:= Fact(n);
END;
DEARMENT OF CS, SESHADRIPURAM COLLEGE, TUMAKURU-5 31
3SEM_BCA_DBMS LAB COURSE CODE: DSC4LAB
8. Given the table EMPLOYEE(EmpNo, Name, Salary, Designation, DeptID) write a cursor in PL/SQL
to select the five highest paid employees from the table.
Create table Emp(EmpNo number, Name varchar2(20), Salary number, Designation
varchar2(20), DeptID number);
Insert 10 Tuples into the Table
Insert into Emp values(111, ‘Viru’, 90000, ‘Teacher’, 1011);
Insert into Emp values(112, ‘Shiva’, 50000, ‘Trainer’, 1012);
Insert into Emp values(113, ‘Poornachandra’, 10000, ‘Watchman’, 1013);
Insert into Emp values(114, ‘deekshi’, 100000, ‘Principal’, 1014);
Insert into Emp values(115, ‘Sadashiva’, 1000000, ‘Cricketer’, 1015);
Insert into Emp values(116, ‘Sada’, 10000000, ‘MP’, 1016);
Insert into Emp values(117, ‘Sweccha’, 10600000, ‘HOME MINISTER’, 1017);
DEARMENT OF CS, SESHADRIPURAM COLLEGE, TUMAKURU-5 32
3SEM_BCA_DBMS LAB COURSE CODE: DSC4LAB
Insert into Emp values(118, ‘Vismitha’, 1600000, ‘Actor’, 1018);
Insert into Emp values(69, ‘Manya’, 80000, ‘Actor’, 1019);
Insert into Emp values(169, ‘Khushi’, 8000000, ‘Doctor’, 1020);
Arrange the salary in the Descending Order
Select name, salary from emp order by salary desc;
Pl/sql code:
Set serveroutput on
Declare cursor c1 is
Select name,salary from emp order by salary desc;
V_name varchar2(20);
V_salary number(10);
Begin
Open C1;
DEARMENT OF CS, SESHADRIPURAM COLLEGE, TUMAKURU-5 33
3SEM_BCA_DBMS LAB COURSE CODE: DSC4LAB
Loop
Fetch C1 into v_name, v_salary;
Exit when C1 %rowcount >5;
Dbms_output.put_line(v_name || ‘ ‘ || v_salary);
End loop;
End;
DEARMENT OF CS, SESHADRIPURAM COLLEGE, TUMAKURU-5 34
3SEM_BCA_DBMS LAB COURSE CODE: DSC4LAB
9. Given the table MOVIE(MID, MTitle, Language, Director, Year) write a functional in PL/SQL
to find the total number of Movies in the table.
Create table MOVIE(MID varchar2(20), MTitle varchar2(20), Language varchar2(20), Director
varchar2(20), Year number);
INSERT 8 Tuples into the above table:
Insert into MOVIE values ('m101', 'K.G.F:chapter 1', 'kannada', 'Prashanth Neel', 2018);
Insert into MOVIE values ('m102', 'Lucia', 'Kannada', 'Pavan Kumar', 2013);
Insert into MOVIE values ('m103', 'RangiTaranga', 'Kannada', 'Anup Bhandari', 2015);
Insert into MOVIE values ('m104', 'Ugramm', 'Kannada', 'Prashanth Neel', 2014);
Insert into MOVIE values ('m105', 'Mungaru Male', 'Kannada', 'Yogaraj Bhat', 2006);
Insert into MOVIE values ('m106', 'Jogi', 'Kannada', 'Prem', 2005);
Insert into MOVIE values ('m107', 'Navagraha', 'kannada', 'Dinakar Thoogudeep', 2008);
Insert into MOVIE values ('m108', 'Googly', 'Kannada', 'Pawan Wadeyar', 2013);
TO CREATE FUNCTION:
create or replace function Total_Movies return
number
is
total number:=0; begin
select count(*) into total
from Movie; return
total;
end;
/
Function created.
TO CALL FUNCTION:
Set server output on
Declare n
number;
Begin
End; n := Total_Movies;
DEARMENT OF CS, SESHADRIPURAM COLLEGE, TUMAKURU-5 35
3SEM_BCA_DBMS LAB COURSE CODE: DSC4LAB
/ dbms_output.put_line(‘Total No. of Movies=’ || n);
10. Given the table CUSTOMERS(CID,CName, Address) write a PL/SQL program which
asks for customer ID. If the user enters invalid ID then the exception invalid_id is
raised.
Create table Customers(CID number, CName varchar2(20), Address varchar2(20));
INSERT 10 Tuples into the above table:
DEARMENT OF CS, SESHADRIPURAM COLLEGE, TUMAKURU-5 36
3SEM_BCA_DBMS LAB COURSE CODE: DSC4LAB
Insert into customers values (11, ‘Disha T N’, ‘Tumkur’);
Insert into customers values (12, ‘Rajanikanth’, ‘Banglore’);
Insert into customers values (13, ‘Darshan’, ‘Gulbarga’);
Insert into customers values (14, ‘Sneha’, ‘Udupi’);
Insert into customers values (15, ‘Vismitha’, ‘Tumkur’);
Insert into customers values (16, ‘Lavanya’, ‘Manglore’);
Insert into customers values (17, ‘Manikanta’, ‘Gadag’);
Insert into customers values (18, ‘Sanjay’, ‘Hubli’);
Insert into customers values (19, ‘Madan’, ‘Shikaripura’);
Insert into customers values (20, ‘Nandini’, ‘Shivmoga’);
c_id customers.cid%type :=&cid;
c_name customers.cname%type;
c_address customers.address%type;
-- USER DEFINED EXCEPTION
ex_invalid_id EXCEPTION;
BEGIN
IF c_id <= 0 THEN
RAISE ex_invalid_id;
Select cname, address into c_name,
FROM customers WHERE cid = c_id;
dbms_output.put_line('NAME:'|| c_name);
dbms_output.put_line('ADDRESS:'|| c_address);
END IF;
EXCEPTION
DECLARE
ELSE c_address
DEARMENT OF CS, SESHADRIPURAM COLLEGE, TUMAKURU-5 37
3SEM_BCA_DBMS LAB COURSE CODE: DSC4LAB
WHEN ex_invalid_id THEN dbms_output.put_line('ID must
be greater than Zero!');
WHEN no_data_found THEN
dbms_output.put_line('INVALID_ID');
WHEN others THEN
dbms_output.put_line('ERROR!'); END;
/
DEARMENT OF CS, SESHADRIPURAM COLLEGE, TUMAKURU-5 38
3SEM_BCA_DBMS LAB COURSE CODE: DSC4LAB
DEARMENT OF CS, SESHADRIPURAM COLLEGE, TUMAKURU-5 39