DBMS List of Practicals AND MANUAL
DBMS List of Practicals AND MANUAL
COURSE OUTCOMES
After competition of the course students will be able to:
CO 1
CO 2
CO 3
LIST OF EXPERIMENTS
INDEX
PRACTICAL NO: 1
THEORY:- Data Definition Language (DDL) o DDL changes the structure of the table like creating a table,
deleting a table, altering a table, etc. All the command of DDL are auto-committed that means it permanently save
all the changes in the database. Here are some commands that come under
d. TRUNCATE: It is used to delete all the rows from the table and free the space containing the table.
Syntax: TRUNCATE TABLE table_name;
Creating a database The following example demonstrates how the CREATE query can be used to create a
database in MS SQL Server:
1 CREATE DATABASE LibraryDB
The script above creates a database named “LibraryDB” in MS SQL Server.
Creating a table
The CREATE query is also used to add tables in an existing database as shown in the following script:
1 USE LibraryDB
CREATE TABLE Books(Id INT PRIMARY KEY IDENTITY(1,1), Name VARCHAR (50) NOT
NULL, Price INT )
The above script creates a table named “Books” in the “LibraryDB” database that we created earlier.
The “Books” table contains three columns: Id, Name, and Price. The Id column is the primary key
column and it cannot be NULL. A column with a PRIMARY KEY constraint must contain unique
values. However, since we have set the IDENTITY property for the Id column, every time a new
record is added in the Books table, the value of the Id column will be incremented by 1, starting from
1. You need to specify the values for the Name column as well as it cannot have NULL. Finally, the
Price column can have NULL values. To view all the tables in the LibraryDB, execute the following
QL DDL script:
USE LibraryDB
GO
SELECT * FROM INFORMATION_SCHEMA.TABLES
GO
You should see the following output:
Similarly, to see all the columns in the Books table, run the following script:
Dept. CSE[ 3RD YR 2023-2024] Page4
GOVINDRAO WANJARI COLLEGE OF ENGINEERING
& TECHNOLOGY, NAGPUR
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
You can see how the CREATE query can be used to define the structure of a table and the type of
data that will be stored in a table. Note, we have not added any record to the Books table yet as
SQL DDL commands are only concerned with the structure of the database and not with the
database records. The SQL DML commands are used for inserting, modifying and deleting
database records.
ALTER
The ALTER command in SQL DDL is used to modify the structure of an already existing table.
Adding a new column
For example, if we want to add a new column e.g. ISBN to the existing Books table in the
LibraryDB database, the ALTER command can be used as follows:
1 USE LibraryDB
2 ALTER TABLE Books
3 ADD ISBN INT NOT NULL;
The syntax of the ALTER command is straightforward. The ALTER statement is used followed by the
object type and the name of the object, which in this case are TABLE and Books, respectively. Next,
you need to specify the operation that you need to perform, which is ADD in our case. Let’s now again
SELECT the columns from the Books table and see if the ISBN column has been added to the Books
table:
1 SELECT COLUMN_NAME, DATA_TYPE
2 FROM INFORMATION_SCHEMA.
3 COLUMNS WHERE TABLE_NAME='Books'
Here is the result set:
In the output, you can see the newly added ISBN column. Modifying an existing column Let’s see
another case where ALTER command can be useful. Suppose, instead of adding a new column to a
table, you want to modify an existing column.
For example, you want to change the data type of the ISBN column from INT to VARCHAR (50). The
ALTER query can be used as follows:
1 USE LibraryDB ALTER TABLE Books ALTER COLUMN ISBN VARCHAR(50);
You can see that to modify an existing column within a table, we first have to use the ALTER
command with the table name and then another ALTER command with the name of the column that is
to be modified. If you again select the column names, you will see the updated data type (VARCHAR)
for the ISBN column.
DROP
The DROP command is a type of SQL DDL command, that is used to delete an existing database or
an object within a database.
Deleting a database The following DROP command deletes the LibraryDB database that we created
earlier:
1 DROP DATABASE LibraryDB
Note: If you execute the above command, the LibraryDB database will be deleted. To execute the rest
of the queries in this article, you will again need to CREATE the LibraryDB database, along with the
Books table.
Deleting a table The DROP command is a type of SQL DDL command that is used to delete an
existing table.
RD
Dept. CSE[ 3 YR 2023-2024] Page6
GOVINDRAO WANJARI COLLEGE OF ENGINEERING
& TECHNOLOGY, NAGPUR
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
For instance, the following command will delete the Books table:
1 DROP TABLE Books
Deleting a column
To delete a column within a database, the DROP query is used in combination with the ALTER query.
The ALTER query specifies the table that you want to delete whereas the DROP query specifies the
column to delete within the table specified by the ALTER query. Let’s drop the ISBN column from the
Books:
1 ALTER TABLE Books
2 DROP COLUMN ISBN
TRUNCATE
The TRUNCATE command in SQL DDL is used to remove all the records from a table. Let’s insert a
few records in the Books table:
INSERT INTO Books VALUES ('Book-A', 100), ('Book-B', 200), ('Book-C', 150)
Let’s see if the records have been actually inserted:
1 SELECT * FROM Books
Here is the result set:
You can see the three records that we inserted in the Books table.
The TRUNCATE command will remove all the records from the Books table as shown below:
1 TRUNCATE TABLE Books
If you again select all the records from the Books table, you will see that the table is empty.
RESULT:
Hence, we execute the DDL (Data Definition Command) command successfully.
PRACTICAL NO: 2
THEORY:
Data Manipulation Language (DML) commands in SQL deals with manipulation of data records stored within
the database tables. It does not deal with changes to database objects and its structure. The commonly
known DML commands are INSERT, UPDATE and DELETE. Liberally speaking, we can consider even SELECT
statement as a part of DML commands. Albeit, it strictly forms part of the Data Query Language (DQL)
1. SELECT SELECT command or statement in SQL is used to fetch data records from the database table and present it
in the form of a result set. It is usually considered as a DQL command but it can also be considered as DML.
The basic syntax for writing a SELECT query in SQL is as follows :
SELECT column_name1, column_name2, … FROM table_name WHERE condition_ expression;
The parameters used in the above syntax are as follows :
• column_name1, column_name2, … : Specify the column_names which have to be fetched or selected for the final
result set.
• table_name: Specify the name of the database table from which these results have to be fetched.
• condition_expression: Specify the condition expression for filtering records for the final result set.
Here are a few examples to illustrate the use of SELECT command.
SELECT customer_id, sale_date, order_id, store_state FROM customers;
The query returns the following output.
In this example, we have fetched fields such as customer_id, sale_date, order_id and store_state from
customers table. Next, suppose if we want to fetch all the records from the customers table. This can be
achieved by a simple query as shown below.
SELECT * FROM customers;
The query returns the following output.
2. INSERT
INSERT commands in SQL are used to insert data records or rows in a database table. In an INSERT statement, we
specify both the column_names for which the entry has to be made along with the data value that has to be
inserted.
The basic syntax for writing INSERT statements in SQL is as follows :
INSERT INTO table_name (column_name_1, column_name_2, column_name_3, ...) VALUES (value1, value2,
value3, ...)
By VALUES, we mean the value of the corresponding columns.
Here are a few examples to further illustrate the INSERT statement.
INSERT INTO public.customers( customer_id, sale_date, sale_amount, salesperson, store_state, order_id) VALUES
(1005,'2019-12-12',4200,'R K Rakesh','MH','1007');
Here we have tried to insert a new row in the Customers table using the INSERT command. The query accepts
two sets of arguments, namely field names or column names and their corresponding values.
Suppose if we have to insert values into all the fields of the database table, then we need not specify the
column names, unlike the previous query.
Follow the following query for further illustration.
INSERT INTO customers VALUES ('1006','2020-03-04',3200,'DL', '1008');
1. UPDATE
UPDATE command or statement is used to modify the value of an existing column in a database table.
UPDATE table_name
SET column_name_1 = value1, column_name_2 = value2, ...
WHERE condition;
Having learnt the syntax, let us now try an example based on the UPDATE statement in SQL.
UPDATE customers
SET store_state = 'DL'
WHERE store_state = 'NY';
1.DELETE
DELETE statement in SQL is used to remove one or more rows from the database table. It
does not delete the data records permanently. We can always perform a rollback operation
to undo a DELETE command. With DELETE statements we can use the WHERE clause for
Having learnt the syntax, we are all set to try an example based on the DELETE command in SQL.
DELETE FROM customers
WHERE store_state = 'MH'
AND customer_id = '1001';
RESULT:
AIM:- To study and execute Primary key and foreign key concept.
Theory:-
Primary Key:- Primary key is a unique column we set in a table to easily identify and locate data in
queries. A table can have only one primary key. The primary key column has a unique value and doesn’t store
repeating values. A Primary key can never take NULL values.
For example, in the case of a student when identification needs to be done in the class, the roll number of the
student plays the role of Primary key.
Similarly, when we talk about employees in a company, the employee ID is functioning as the Primary key for
identification.
Let us now understand the Syntax of creating the table with the Primary key specified.
Syntax:
CREATE TABLE tableName (
col1 int NOT NULL,
col2 varchar(50) NOT NULL,
col3 int,
…………….
PRIMARY KEY (col1)
);
Foreign key :-
A Foreign key is beneficial when we connect two or more tables so that data from both can be put to use
parallelly.
A foreign key is a field or collection of fields in a table that refers to the Primary key of the other table. It is
responsible for managing the relationship between the tables.
The table which contains the foreign key is often called the child table, and the table whose primary key is being
referred by the foreign key is called the Parent Table.
For example: When we talk about students and the courses they have enrolled in, now if we try to store all the
data in a single table, the problem of redundancy arises.
Dept. CSE[ 3RD YR 2023-2024] Page13
GOVINDRAO WANJARI COLLEGE OF ENGINEERING
& TECHNOLOGY, NAGPUR
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
To solve this table, we make two tables, one the student detail table and the other department table. In the student
table, we store the details of students and the courses they have enrolled in.
And in the department table, we store all the details of the department. Here the courseId acts as the Primary key
for the department table whereas it acts as the Foreign key in the student table.
Let us now look at the syntax of creating a table with a foreign key.
Syntax:
CREATE TABLE childTable (
col3 int,
………...
);
Query:
CREATE TABLE DataFlair(
name varchar(50),
location varchar(50),
experience int,
PRIMARY KEY(emp_id));
Output:
Query:
CREATE TABLE location(
office_size int,
PRIMARY KEY(location_id),
Output:
RESULT: Hence, we executed the Primary key and foreign key concept.
PRACTICAL NO:4
AIM:- To study and execute aggregate function. (MIN, MAX, SUM, Count &AVG).
Theory:-
o SQL aggregation function is used to perform the calculations on multiple rows of a single column
of a table. It returns a single value.
o It is also used to summarize the data.
1. COUNT FUNCTION
o COUNT function is used to Count the number of rows in a database table. It can work on both
numeric and non-numeric data types.
o COUNT function uses the COUNT(*) that returns the count of all the rows in a specified table.
COUNT(*) considers duplicate and Null.
Syntax
1. COUNT(*)
2. or
3. COUNT( [ALL|DISTINCT] expression )
Example: COUNT()
1. SELECT COUNT(*)
2. FROM PRODUCT_MAST;
Output:
10
1. SELECT COUNT(*)
2. FROM PRODUCT_MAST;
3. WHERE RATE>=20;
Output:
7
Output:
Dept. CSE[
3 3RD YR 2023-2024] Page18
GOVINDRAO WANJARI COLLEGE OF ENGINEERING
& TECHNOLOGY, NAGPUR
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
Output:
Com1 Com2
5 Com3
3
2
Output:
Com1 Com2
5
3
3. SUM Function
Sum function is used to calculate the sum of all selected columns. It works on numeric fields only.
Syntax
1. SUM()
2. or
3. SUM( [ALL|DISTINCT] expression )
Example: SUM()
1. SELECT SUM(COST)
2. FROM PRODUCT_MAST;
Output:
670
1. SELECT SUM(COST)
2. FROM PRODUCT_MAST
3. WHERE QTY>3;
Output:
320
1. SELECT SUM(COST)
2. FROM PRODUCT_MAST
3. WHERE QTY>3
4. GROUP BY COMPANY;
Output:
Com1 Com2
150
170
Output:
Com1 Com3
335
170
3. AVG function
The AVG function is used to calculate the average value of the numeric type. AVG function returns the
average of all non-Null values.
Syntax
1. AVG()
2. or
3. AVG( [ALL|DISTINCT] expression )
Example:
1. SELECT AVG(COST)
2. FROM PRODUCT_MAST;
67.00
Output:
4. MAX Function
MAX function is used to find the maximum value of a certain column. This function
determines the largest value of all selected values of a column.
Syntax
1. MAX()
2. or
3. MAX( [ALL|DISTINCT] expression )
Examples:
1. SELECT MAX(RATE)
2. FROM PRODUCT_MAST;
30
5. MIN Function
MIN function is used to find the minimum value of a certain column. This function
determines the smallest value of all selected values of a column.
Syntax
1. MIN()
2. or
3. MIN( [ALL|DISTINCT] expression )
Example:
1. SELECT MIN(RATE)
2. FROM PRODUCT_MAST;
Output:
10
RESULT: Hence, we executed the Aggregate function (MIN, MAX, SUM, Count & AVG).
PRACTICAL NO: 5
AIM:- To perform queries based on Group By, Having By, Order By clause.
Theory:-
SQL Clauses
1. GROUP BY
o SQL GROUP BY statement is used to arrange identical data into groups. The GROUP BY statement is used
with the SQL SELECT statement.
o The GROUP BY statement follows the WHERE clause in a SELECT statement and precedes the ORDER BY
clause.
o The GROUP BY statement is used with aggregation function.
Syntax
1. SELECT column
2. FROM table_name
3. WHERE conditions
4. GROUP BY column
5. ORDER BY column
Dept. CSE[ 3RD YR 2023-2024] Page24
GOVINDRAO WANJARI COLLEGE OF ENGINEERING
& TECHNOLOGY, NAGPUR
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
Sample table:
PRODUCT_MAST
Item1 Com1 2 10 20
Item2 Com2 3 25 75
Item3 Com1 2 30 60
Item4 Com3 5 10 50
Item5 Com2 2 20 40
Item6 Cpm1 3 25 75
Item8 Com1 3 10 30
Item9 Com2 2 25 50
Example:
2. FROM PRODUCT_MAST
3. GROUP BY COMPAN
Output:
Com1 5
Com2 3
2. HAVING
Syntax:
Example:
Output:
Com1 5
Com2 3
3. ORDER BY
Dept. CSE[ 3RD YR 2023-2024] Page26
GOVINDRAO WANJARI COLLEGE OF ENGINEERING
& TECHNOLOGY, NAGPUR
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
Syntax:
Where
Table:
CUSTOM
ER
12 Kathrin US
23 David Bangkok
34 Alina Dubai
45 John UK
56 Harry US
1. SELECT *
2. FROM CUSTOMER
3. ORDER BY NAME;
Output:
34 Alina Dubai
23 David Bangkok
56 Harry US
45 John UK
12 Kathrin US
1. SELECT *
2. FROM CUSTOMER
3. ORDER BY NAME DESC;
Output:
12 Kathrin US
45 John UK
56 Harry US
23 David Bangkok
34 Alina Dubai
RESULT:
PRACTICAL NO:-6
AIM:- To study & execute join operation. (Inner join, Left join, Right join , Outer join, Natural join, Equal
join, Cartesian join operation).
Theory:-
SQL join is used to fetch data from two or more tables, which is joined to appear as single set of data.
Sql join is used for combining column from two or more tables by using values common to both tables. Minimum
required condition for joining tables, is (n-1) where n is number of tables.
STUDENT
ID NAME
1 Abhi
2 Neha
3 Renu
STUDENT 1
ID ADDRESS
1 Mumbai
2 Nagpur
3 Akola
Types of join:
Inner
Outer
Left
Right
In this type of join returns the Cartesian product of rows from the tables in join. It will return a table which consist
of records which combines each row from the 1st table with each row of 2nd table.
Syntax:
SELECT column_ name_ list
Ex:
SELECT *
From student,
This is a simple JOIN in which the result is based on matched data as per the equality condition specified in the
query.
Syntax:
INNER JOIN
Table_ name 2
Ex:
Student 1 where
ID NAME ID ADDRESS
1 Abhi 1 Mumbai
2 Neha 2 Nagpur
3 Renu 3 Akola
NATURAL JOIN:
Natural join is a type of Inner join which is based on column having same name and same datatype present
in both the tables to be joined.
Syntax:
NATURAL JOIN
Table_ name 2;
Ex:
NATURAL JOIN
Student1;
ID NAME ADDRESS
1 Abhi Mumbai
2 Neha Nagpur
3 Renu Akola
OUTER JOIN:
Outer join is based on both matched and unmatched data. Outer joins subdivide further into
Dept. CSE[ 3RD YR 2023-2024] Page34
GOVINDRAO WANJARI COLLEGE OF ENGINEERING
& TECHNOLOGY, NAGPUR
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
The left outer join returns a result table with the matched data of two tables then remaining rows of the left
table and null for the right tables column.
Syntax:
Table_ name 2
Ex:
ID NAME ID ADDRESS
1 Abhi 1 Mumbai
2 Neha 2 Nagpur
3 Renu 3 Akola
4 Pihu Null Null
5 Sona Null Null
The right outer join returns a result table with the matched data of two tables then remaining rows of the
Syntax:
Ex:
ID NAME ID ADDRESS
1 Abhi 1 Mumbai
2 Neha 2 Nagpur
3 Renu 3 Akola
NULL NULL 7 Pune
NULL NULL 8 amravati
The full outer join returns a result table with the matched data of two table then remaining rows of both left
table & then the right tables.
Syntax:
Table_ name 2
Ex:
ID NAME ID ADDRESS
1 Abhi 1 Mumbai
2 Neha 2 Nagpur
3 Renu 3 Akola
4 Pihu Null null
NULL NULL 7 pune
RESULT:
Hence, we successfully execute join operation. (Inner join, Left join, Right join, Outer join, Natural join,
Equal join, Cartesian join operation
PRACTICAL:-7
AIM:- To study and execute set operation (union, insert, minus).
THEORY:
The SQL Set operation is used to combine the two or more SQL SELECT statements.
Union
Intersect
Minus
1. Union
o The SQL Union operation is used to combine the result of two or more SQL SELECT queries.
o In the union operation, all the number of datatype and columns must be same in both the tables on which
UNION operation is being applied.
o The union operation eliminates the duplicate rows from its resultset.
Syntax
ID NAME
1 Jack
2 Harry
3 Jackson
ID NAME
3 Jackson
4 Stephan
5 Davi
d
ID NAME
1 Jack
2 Harry
3 Jackson
4 Stephan
5 Davi
d
2. Intersect
o It is used to combine two SELECT statements. The Intersect operation returns the common rows
o In the Intersect operation, the number of datatype and columns must be the same.
o It has no duplicates and it arranges the data in ascending order by default.
Syntax
SELECT * FROM
First INTERSECT
ID NAME
3 Jackso
n
3. Minus
o It combines the result of two SELECT statements. Minus operator is used to display the rows
which are present in the first query but absent in the second query.
Syntax:
Example
SELECT * FROM
First MINUS
SELECT * FROM Second;
Dept. CSE[ 3RD YR 2023-2024] Page42
GOVINDRAO WANJARI COLLEGE OF ENGINEERING
& TECHNOLOGY, NAGPUR
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
ID NAME
1 Jack
2 Harr
y
RESULT:
PRACTICAL NO:-8
THEORY:
Views in SQL are kind of virtual tables. A view also has rows and columns as they are in a
Dept. CSE[ 3RD YR 2023-2024] Page43
GOVINDRAO WANJARI COLLEGE OF ENGINEERING
& TECHNOLOGY, NAGPUR
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
real table in the database. We can create a view by selecting fields from one or more tables
present in the database. A View can either have all the rows of a table or specific rows
based on certain condition.
In this article we will learn about creating , deleting and updating Views.
Sample Tables:
Student Details
Student Marks
CREATING VIEWS
We can create View using CREATE VIEW statement. A View can be created from a single table or multiple
tables.
Syntax:
condition;
Examples:
In this example we will create a View named DetailsView from the table
StudentDetails. Query:
CREATE VIEW DetailsView AS
SELECT NAME, ADDRESS
FROM StudentDetails
WHERE S_ID < 5;
To see the data in the View, we can query the view in the same manner as we
query a table.
SELECT * FROM DetailsView;
Output:
In this example we will create a View named MarksView from two tables Student Details and
Student Marks. To create a View from multiple tables we can simply include multiple tables in
the SELECT statement. Query:
CREATE VIEW Marks View AS
SELECT StudentDetails.NAME, StudentDetails.ADDRESS,
Output:
DELETING VIEWS
We have learned about creating a View, but what if a created View is not needed any
more? Obviously we will want to delete it. SQL allows us to delete an existing View. We can
delete or drop a View using the DROP statement.
Syntax:
For example, if we want to delete the View MarksView, we can do this as:
UPDATING VIEWS
There are certain conditions needed to be satisfied to update a view. If any one of these conditions is not met, then
we will not be allowed to update the view.
The SELECT statement which is used to create the view should not include GROUP BY clause or ORDER BY clause.
The view should not be created using nested queries or complex queries.
The view should be created from a single table. If the view is created using multiple tables then we will not be
allowed to update the view.
We can use the CREATE OR REPLACE VIEW statement to add or remove fields from a view.
Syntax:
SELECT column1,coulmn2,..
FROM table_name
WHERE condition;
For example, if we want to update the view MarksView and add the field AGE to
this View from StudentMarks Table, we can do this as:
Output:
Example:
In the below example we will insert a new row in the View DetailsView which we have created above in the
example of “creating views from a single table”.
VALUES("Suresh","Gurgaon");
Output:
Deleting rows from a view is also as simple as deleting rows from a table. We can use the DELETE statement of SQL
to delete rows from a view. Also deleting a row from a view first delete the row from the actual table and the
change is then reflected in the view.Syntax:
DELETE FROM
view_name WHERE
condition;
Example:
In this example we will delete the last row from the view DetailsView which we just added in the above example of
inserting rows.
DELETE FROM
DetailsView WHERE
NAME="Suresh";
Output:
The WITH CHECK OPTION clause in SQL is a very useful clause for views. It is applicable to a updatable
view. If the view is not updatable, then there is no meaning of including this clause in the CREATE VIEW
statement.
The WITH CHECK OPTION clause is used to prevent the insertion of rows in the view where the condition
in the WHERE clause in CREATE VIEW statement is not satisfied.
If we have used the WITH CHECK OPTION clause in the CREATE VIEW statement, and if the UPDATE or
INSERT clause does not satisfy the conditions then they will return an error.
Example:
In the below example we are creating a View SampleView from StudentDetails Table with WITH CHECK OPTION
clause.
FROM StudentDetails
OPTION;
In this View if we now try to insert a new row with null value in the NAME column then it will give an error because
the view is created with the condition for NAME column as NOT NULL.
For example,though the View is updatable but then also the below query for this View is not valid:
INSERT INTO
SampleView(S_ID)
VALUES(6);
Uses of a View :
Rename Columns –
Views can also be used to rename the columns without affecting the base tables provided the number of columns
in view must match the number of columns specified in select statement. Thus, renaming helps to to hide the
names of the columns of the base tables.
RESULT:
PRACTICAL NO. 9
AIM: To study about PL-SQL & execute a PL-SQL simple program of IF-else command.
THEORY:
PL_SQL
The PL/SQL programming language was developed by oracle corporation in the late 1980s as procedural
extension language for SQL & the oracle relational data.
The If-Then-else statement allows you to choose between several alternatives. An if-then statement can be
followed by an optional else If-else statement. The ELSIF clause lets you add additional condition.
Syntax:
ELSE
END IF;
Dept. CSE[ 3RD YR 2023-2024] Page53
GOVINDRAO WANJARI COLLEGE OF ENGINEERING
& TECHNOLOGY, NAGPUR
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
1) PL/SQL allows sending an entire block of statements to the database at one time. This reduce network
traffic & provides high performance for the application.
2) PL/SQL gives high productivity to programmers as it can query transform & update data in a database.
Program:
DECLARE
a number(3):=100;
BEGIN
If (a=10) THEN
dbms_output.put_line(“value of a is 20”);
dbms_output.put_line(“value of a is 30”);
ELSE
END IF;
END;
OUTPUT:
None of the values is matching
RESULT:
Hence, we successfully study and executed the PL/SQL simple program of IF-else command.
PRACTICAL NO. 10
THEORY:
A subprogram is a program module that perform a particular task. These subprograms are combined to larger
programs. A subprogram can be involved by another subprogram are program which is called the calling program.
At schema level:
A schema level subprogram is standalone subprogram. It is created with CREATE PROCEDURE OR DROP FUNCTION
statement. It is deleted with the DROP PROCEDURE OR DROP FUNCTION statement.
Inside a package:
A subprogram created inside a package is a packaged subprogram. It is stored in the database & can be
deleted only when the package is deleted with the DROP PACKAGE statement.
Function: There subprogram return a single value, mainly used to compute & return a value.
Procedure: There subprogram do not return a value directly, mainly used to perform an action.
Creating a procedure:
Syntax:
{ IS /AS}
BEGIN
Ex:
AS
BEGIN
END;
RESULT:
Procedure created.
1. Positional notation:
In the positional notation , you can call the procedure.
Find min(a, b, c, d );
In positional notation the first actual parameter is substituted for the first formal parameter;
The 2nd actual parameter is substituted for the 2nd formal parameter, & so on.
So a is substituted for x, b is substituted for y, c is substituted for z.
2. Named notation:
In named notation , the actual parameter is associated with the formal parameter using the arrow
symbol => so the procedure call would look like :
Find min(x=>a, y=>b, z=>c, m=>d).
3. Mixed notation:
In mixed notation, you can mixed both notation in procedure call ;
However the positional notation should proceed the named notation.
Find min(a, b, c, m=>d).
4. Functions
A PL/SQL function is same as a procedure except that it returns a value.
Creating function :
A standalone function is created using the CREATE FUNCTION statement. The simplified syntax the create
or replace procedure.
Syntax:
{ IS
/AS}
BEGIN
END [ function_
name];
Dept. CSE[ 3RD YR 2023-2024] Page57
GOVINDRAO WANJARI COLLEGE OF ENGINEERING
& TECHNOLOGY, NAGPUR
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
Ex:
RETURN number IS
BEGIN
RETURN total;
END;
/
OUTPUT:
Function created.
Calling function :
A calling function performs defined task and when its return statement is executed or when it last end statement is
reached, it returns program control back to the main program.
Ex:
DECLARE
a number;
b number;
c number;
Dept. CSE[ 3RD YR 2023-2024] Page58
GOVINDRAO WANJARI COLLEGE OF ENGINEERING
& TECHNOLOGY, NAGPUR
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
RETURN number
IS
z number;
BEGIN
IF X>Y THEN
Z=X;
ELSE
Z=Y;
END IF ;
RETURN Z ;
END ;
BEGIN
a:=23;
b:=45;
END;
Output:
We have seen that a program or subprogram may call another subprogram. when a subprogram call itself , it
is referred to as a recursive call & the process is known as recursion.
DECLARE
Num number;
Factorial number;
RETURN number
IS
F number;
BEGIN
Dept. CSE[ 3RD YR 2023-2024] Page60
GOVINDRAO WANJARI COLLEGE OF ENGINEERING
& TECHNOLOGY, NAGPUR
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
IF x=0 THEN
F:=1
ELSE
END IF;
RETURN F;
END;
BEGIN
num:=6
Factorial :=fact(num);
END;
Output:
Factorial 6 is 720
RESULT: