0% found this document useful (0 votes)
30 views58 pages

Dbms

Uploaded by

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

Dbms

Uploaded by

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

SCHOOl Of COMPUTING

DEPARTMENT O INFORMATION TECHNOLOGY

LAB MANUAL

INT18R371 – DATABASE MANAGEMENT SYSTEM


LABORATORY

Prepared By

M.Lakshmi Shree

NAME : M.Lakshmi Shree Register Number 9919008021

1
SCHOOL OF COMPUTING

DEPARTMENT OF INFORMATION TECHNOLOGY

BONAFIDECERTIFICATE

Bonafide record of work done by Lakshmi shree M of Register number 9919008021 in

Database Management System (INT18R371) during odd semester in academic year

2024 - 2025_

Staff In-charge Head ofthe department

Submitted to the practical Examination held at Kalasalingam Academy of Research and

Education, Krishnankoil on ___________________

REGISTER NUMBER:

9 9 1 9 0 0 8 0 2 1

Internal Examiner External Examiner

2
EXPERIMENT EVALUATION SUMMARY

Name : Lakshmi shree M Reg.No. : 9919008021

Section : B.Tech(IT) Faculty:Dr.S.P.Balakannan

S.No Date Experiment Marks Faculty


(100) Signature

1 Working with DDL commands

2 Working with DML and DCL


Commands

3 Working with Built-in functions of


SQL

4 Simple PL/SQL programs

5 PL/SQL programs using cursor

3
TABLE OF CONTENTS

S.No Topic Page

No

1. Working with DDL commands

2. Working with DML and DCL commands

3. Working with Built-in functions of SQL

4. Simple PL/SQL programs

5. PL/SQL programs using cursor

4
DEPARTMENT OF INFORMATION TECHNOLOGY
ODD SEMESTER 2024-2025

LABORATORY COURSE PLAN

PRE-REQUISITE:

General understanding of programming concepts and some programming experience in C language.


(CSE181 – Programming Languages Lab)

COURSE DESCRIPTION:

The major objective of this lab is to provide a strong foundation in database concepts, technology and
practice to the participants to groom them into database application developers.

COURSE OBJECTIVES:

1. To give a good foundation on the relation model of data.


2. To impart systematic database design approaches covering conceptual, logical and physical design.
3. To present the concepts and techniques relating to query processing by SQL and ODBC.

PROGRAM EDUCATIONAL OBJECTIVES COMPONENTS (PEOS)

PEO1: The Graduates will be technically competent to excel in IT industry and to pursue higher studies.

PEO2: The Graduates will possess the skills to design and develop economically and technically feasible computing
systems using modern tools and techniques.

PEO3: The Graduates will have effective communication skills, team spirit, ethical principles and the desire for self
learning to succeed in their professional career.

5
PROGRAMME OUTCOMES (POS)

PO1: Ability to apply knowledge of mathematics, science and computer engineering to solve computational problems.

PO2: Ability to identify, formulates, analyze and derive conclusions in complex computing problems.

PO3: Capability to design and develop computing systems to meet the requirement of industry and society with due
consideration for public health, safety and environment.

PO4: Ability to apply the knowledge of design of experiment and data analysis to derive solutions in complex
computing problems.

PO5: Ability to develop and apply modeling, simulation and prediction tools and techniques to engineering problems.

PO6: Ability to assess and understand the professional, legal, security and societal responsibilities relevant to computer
engineering practice.

PO7: Ability to understand the impact of computing solutions in economic, environmental and societal context for
sustainable development.

PO8: Applying ethical principles and commitment to ethics of IT and software profession.

PO9: Ability to work effectively as an individual as well as in teams.

PO10: Ability to communicate effectively with technical community and with society.

PO11: Demonstrating and applying the knowledge of computer engineering and management principles in software
project development and in multidisciplinary areas.

PO12: Understanding the need for technological changes and engage in life-long learning.

PROGRAMME SPECIFIC OBJECTIVES (PSO)

A graduate of the Computer Science and Engineering Program will have

PSO1: Problem-Solving Skills: The ability to apply mathematics, science and computer engineering knowledge to
analyze, design and develop cost effective computing solutions for complex problems with environmental considerations.

PSO2: Professional Skills: The ability to apply modern tools and strategies in software project development using modern
programming environments to deliver a quality product for business accomplishment.

PSO3: Communication and Team Skill: The ability to exhibit proficiency in oral and written communication as individual
or as part of a team to work effectively with professional behaviors and ethics.

PSO 4 : Successful Career and Entrepreneurship : The ability to create a inventive career path by applying innovative
project management techniques to become a successful software professional, an entrepreneur or zest for higher studies

6
COURSE OUTCOMES:
CO1: Design and build a database schema for a given problem domain.
CO2: Populate and query a database using SQL DML/DDL commands.
CO3: Programming PL/SQL including stored procedure, stored functions,
and cursors.

CO and PO Mapping
P P P P P P P P P PO PO PO PSO PSO PSO PSO
O O O O O O O O O 10 11 12 1 2 3 4
1 2 3 4 5 6 7 8 9
CO1 S M M S
CO2 S S S S S S S M L
CO3 S S S L
S- Strong Correlation M- Medium Correlation L – Low Correlation

Assessment Method:

S.no Assessment Split up


Regular Lab Exercises (10)
1 Model Lab (15)
Internal Assessment (50 marks)
Record (10)
Mini-project(15)
2 External Assessment (50 marks) End semester Lab (50)

Rubrics for Assessment - Project

Component Rubrics for assessment Marks (100)


Understanding the problem given
Problem analysis 5
Excellent(4-5) Good( 4-3) Average(3-2) Poor(2-0)
Specifying the inputs needed such as software, hardware
Requirement and
and network required 5
specification
Excellent(4-5) Good( 4-3) Average(3-2) Poor(2-0)
Aim and motivation of the work with virtual view (work
Project objective and flow)
5
outline Excellent( 15-12) Good( 12-9) Average(9-5)
Poor(5-0)
1. Project represent in diagrammatic view such as flow
diagram, UML diagram and ER diagram
Project design 15
Excellent( 15-12) Good( 12-9) Average(9-5)
Poor(5-0)
2. List of modules with coding and database connection
Implementation 20
using ODBC and JDBC

7
Excellent( 15-12) Good( 12-9) Average(9-5)
Poor(5-0)
3. Running project with working explanation
Results and
Excellent( 20-15) Good( 15-10) Average(10-5) 20
Discussion
Poor(5-0)
4. Group members cooperation and documentation
Team work and
Excellent( 10-8) Good( 8-5) Average(5-2) 10
documentation
Poor(2-0)
5. PPT presentation and viva voce
Communication Skill Excellent( 10-8) Good( 8-5) Average(5-2) 10
Poor(2-0)

Rubrics for Assessment – Regular Lab Experiments

Component Rubrics for assessment Marks (100)


Efficiency of writing Database schema
Database schema Excellent( 25-20) Good( 20-15) Average(15-10) 25
Poor(10-0)
Efficiency of Writing query
Query Excellent( 25-20) Good( 20-15) Average(15-10) 15
Poor(10-0)
Efficiency of Back end Connection
Database
Excellent( 25-20) Good( 20-15) Average(15-10) 15
Connectivity
Poor(10-0)
Output
Output Excellent( 20-15) Good( 15-10) Average(10-5) 15
Poor(5-0)
Answering the technical questions in viva voce
Technical Skill Excellent( 20-15) Good( 15-10) Average(10-5) 20
Poor(5-0)
6. Communicating the answers in viva voce
Communication Skill Excellent( 10-8) Good( 8-6) Average(6-4) 10
Poor(4-0)

Mapping the Experiments:

VIVA VOCE
S.N Database
Experiments Query Output
o schema Techn Commu
ical nication

1 Working with DDL commands 25 25 20 20 10

8
Working with DML and DCL
2 25 25 20 20 10
commands
Working with Built-in functions of
3 25 25 20 20 10
SQL
Programs using PL/SQL and
4 25 25 20 20 10
Cursors

5 Working with Triggers 25 25 20 20 10

Working with Stored Procedures


6 25 25 20 20 10
and Functions

7 Embedded SQL 25 25 20 20 10

8 Database Connectivity using ADO 25 25 20 20 10

Database Connectivity using


9 25 25 20 20 10
ODBC

10 Database Connectivity using JDBC 25 25 20 20 10

INTRODUCTION

A data base management system is a collection of programs that enables users to create and maintain
a database. DBMS is a general purpose software system that facilitates the process of defining,
constructing, manipulating databases for various applications.
Functions of DBMS

Database Definition -how data is to be stored and organized


Database Creation -storing data in a defined database
Data Retrieval-Querying and reporting

Updating-changing the contents of the data base

Programming user facilities for system development.

Database revision and restructuring

Database integrity control.

Performance monitoring.

9
Ex. No: 1 DATA DEFINITION LANGUAGE (DDL)

Date :

AIM:

To execute the various Data Definition Language commands in RDBMS.

OBJECTIVE:

After completing the exercise the students can able to Understand how to create a table with list of fields, Modify
a row using where clause, Drop a table, Delete the unwanted rows in a table.

DATA DEFINITION LANGUAGE

It is used to communicate with database. DDL is used to:


Create an object

Alter the structure of an object

To drop the object created.

ALGORITHM:

Step 1: Start the program

Step 2: Go to SQL.

Step 3: Enter the user name and password.

Step 4: Connect to the database.

Step 5: Type the commands for creating tables and perform various operations on the
tables.

Step 6: The output is displayed.

Step 7: Stop the program

10
DDL COMMAND:

CREATE

ALTER

DROP

TRUNCATE

RENAME

CREATION OF TABLE

QUERY: 01
Q1: Write a query to create a table employee with empno, ename, designation, and salary.

Syntax: It is used to create a table

SQL: CREATE <OBJ.TYPE><OBJ.NAME> (COLUMN NAME.1 <DATATYPE> (SIZE),

COLUMN NAME.2 <DATATYPE> (SIZE));

Command:

SQL>CREATE TABLE EMP (EMPNO NUMBER (4), ENAME VARCHAR2 (10), DESIGNATIN
VARCHAR2 (10),SALARY NUMBER (8, 2));

Table created.

Constraints with Table Creation:

Constraints are condition for the data item to be stored into a database.
There are two types of Constraints viz., Column Constraints and Table Constraints.

Syntax

[CONSTRAINT constraint name]

{[NOT] NULL / UNIQUE / PRIMARY

KEY}(Column[,column]..) FOREIGN KEY (column [, colum]…)

REFERENCES table

[ON DELETE CASCADE] [CHECK (condition)]

11
TABLE DESCRIPTION

It is used to view the table structure to confirm whether the table was created correctly.

QUERY: 02

Q2: Write a query to display the column name and data type of the table employee.

Syntax: This is used to view the structure of the table. SQL: DESC <TABLE NAME>;
Command:

SQL> DESC EMP;

Name Type
--------------- ----------------
EMPNO NUMBER(4)
ENAME VARCHAR2(1)
DESIGNATION VARCHAR2(1)
SALARY NUMBER(8,2)

QUERY: 03

Q3: Write a query for create a from an existing table with all the fields

Syntax: syntax for create a table from an existing table with all fields.

SQL> CREATE TABLE <TRAGET TABLE NAME> SELECT * FROM<SOURCE TABLE NAME>;

Command:

SQL> CREATE TABLE EMP1 AS SELECT * FROM EMP;


Table created.

Command:

12
SQL> DESC EMP1

Name Null? Type


---------------- --------------------------
EMPNO NUMBER (4)
ENAME VARCHAR2 (10)
DESIGNATIN VARCHAR2 (10)
SALARY NUMBER (8,2)

QUERY: 04

Q4: Write a query for create a from an existing table with selected fields

Syntax: Syntax for create a from an existing table with selected fields.

SQL> CREATE TABLE <TRAGET TABLE NAME> AS SELECT EMPNO, ENAMEFROM <SOURCE
TABLE NAME>;

Command:

SQL> CREATE TABLE EMP2 AS SELECT EMPNO, ENAME FROM EMP;


Table created.

Command:

SQL> DESC EMP2

Name Null? Type


----------------- --------------------- ---
EMPNO NUMBER (4)

ENAME VARCHAR2 (10)

QUERY: 05

Q5: Write a query for create a new table from an existing table without any record:

13
Syntax: The syntax for create a new table from an existing table without any record.

SQL> CREATE TABLE <TRAGET TABLE NAME> AS SELECT * FROM<SOURCE TABLE NAME>
WHERE <FALSE CONDITION>;

Command:

SQL> CREATE TABLE EMP3 AS SELECT * FROM EMP


WHERE1>2; Table created.
Command:

SQL> DESC EMP3;

Name Null? Type


------------------ -----------------------
EMPNO NUMBER(4)
ENAME VARCHAR2(10)
DESIGNATIN VARCHAR2(10)
SALARY NUMBER(8,2);

ALTER & MODIFICATION ON TABLE

To modify structure of an already existing table to add one more columns and also modify
the existing columns.

Alter command is used to:

1. Add a new column.


2. Modify the existing column definition.
3. To include or drop integrity constraint.

QUERY: 06

Q6: Write a Query to Alter the column EMPNO NUMBER (4) TO EMPNO NUMBER (6).

Syntax: The syntax for alter & modify on a single column.

SQL > ALTER <TABLE NAME> MODIFY <COLUMN NAME><DATATYPE>(SIZE);

Command:

SQL>ALTER TABLE EMP MODIFY EMPNO NUMBER


(6); Table altered.
Command:

14
SQL> DESC EMP;

Name Null? Type


--------------------- -----------------
EMPNO NUMBER(6)
ENAME VARCHAR2(10)
DESIGNATIN VARCHAR2(10)
SALARY NUMBER(8,2)

QUERY: 07
Q7. Write a Query to Alter the table employee with multiple columns (EMPNO,ENAME.)

Syntax: To alter table with multiple column.

SQL > ALTER <TABLE NAME> MODIFY <COLUMN NAME1><DATATYPE>(SIZE),

MODIFY <COLUMN NAME2><DATATYPE>(SIZE)………….;

Command:

SQL>ALTER TABLE EMP MODIFY (EMPNO NUMBER (7), ENAMEVARCHAR2(12)); Table


altered.

15
Command:

SQL> DESC EMP;

Name Null? Type


----------------- ---------------------------
EMPNO NUMBER(7)

ENAME VARCHAR2(12)

DESIGNATIN VARCHAR2(10)
SALARY NUMBER(8,2);

QUERY: 08

Q8. Write a query to add a new column in to employee

Syntax: To add a new column.


SQL> ALTER TABLE <TABLE NAME> ADD (<COLUMN NAME><DATATYPE><SIZE>);

Command:

SQL> ALTER TABLE EMP ADD QUALIFICATION


VARCHAR2(6); Table altered.

SQL> DESC EMP;


Name Null? Type
---------------- --------------------
---- EMPNO
NUMBER(7) ENAME
VARCHAR2(12) DESIGNATIN

VARCHAR2(10)
SALARY
NUMBER(8,2) QUALIFICATION

VARCHAR2(6)

16
QUERY: 09

Q9: Write a query to add multiple columns in to employee Syntax: Syntax for add a new column.

SQL> ALTER TABLE <TABLE NAME> ADD (<COLUMN NAME1><DATATYPE><SIZE>,


(<COLUMN NAME2><DATA TYPE><SIZE>…);
Command:

SQL>ALTER TABLE EMP ADD (DOB DATE, DOJ


DATE); Table altered.

SQL> DESC EMP;

Name Null? Type


------------------ -----------------
EMPNO NUMBER(7)
ENAME VARCHAR2(12)
DESIGNATIN VARCHAR2(10)
SALARY NUMBER(8,2)
QUALIFICATION VARCHAR2(6)
DOB DATE
DOJ DATE

REMOVE / DROP

It will delete the table structure provided the table should be empty.

QUERY: 10

Q10. Write a query to drop a column from an existing table employee

Syntax: syntax for add a new column.

SQL> ALTER TABLE <TABLE NAME> DROP COLUMN <COLUMN NAME>;

17
Command:
SQL> ALTER TABLE EMP DROP COLUMN

DOJ; Table altered.

SQL> DESC EMP;


Name Type
----------------- ---------------------------
EMPNO NUMBER(7)

ENAME VARCHAR2(12)
DESIGNATIN VARCHAR2(10)
SALARY NUMBER(8,2)
QUALIFICATION VARCHAR2(6)

DOB DATE

QUERY: 11

Q11. Write a query to drop multiple columns from employee

Syntax:The Syntax for add a new column.

SQL> ALTER TABLE <TABLE NAME> DROP <COLUMNNAME1>,<COLUMN NAME2>,………..


;

Command:

18
SQL> ALTER TABLE EMP DROP (DOB,
QUALIFICATION); Table altered.

SQL> DESC EMP;


Name Null? Type
------------------ -----------------------
EMPNO NUMBER(7)
ENAME VARCHAR2(12)
DESIGNATIN VARCHAR2(10)
SALARY NUMBER(8,2)

RENAME

QUERY: 12
Q12. Write a query to rename table emp to employee

Syntax: The Syntax for add a new column.

SQL> ALTER TABLE RENAME <OLD NAME> TO <NEW NAME>

Command:
SQL> ALTER TABLE RENAME EMP TO
EMPLOYEE; SQL> DESC EMPLOYEE;

Name Null? Type


------------------- --------------------------
EMPNO NUMBER(7)
ENAME VARCHAR2(12)
DESIGNATIN VARCHAR2(10)
SALARY NUMBER(8,2)

TRUNCATE TABLE

If there is no further use of records stored in a table and the structure has to be retained then the
records alone can be deleted.

Syntax:

TRUNCATE TABLE <TABLE N


AME>;

Example:

Truncate table EMP;

DROP

19
To remove a table along with its structure and data.

Syntax:The Syntax for add a new column.

SQL> Drop table<table name>;

Command:

SQL> drop table employee;

EVALUATION

Sl.No Module Max. Marks Marks Obtained


1 Database schema 25
2 Database query 25
3 Output 20
4 Technical Skill 20
5 Communication Skill 10
Total 100
Faculty Signature

RESULT:

Thus the SQL commands for DDL commands in DBMS has been verified and executed successfully.

20
Ex. No: 2 DATA MANIPILATION LANGUAGE (DML)

Date :

AIM:

To execute and verify the DML commands are the most frequently used SQL commands and
is used to query and manipulate the existing database objects.

DML (DATA MANIPULATION LANGUAGE)

SELECT
INSERT
DELETE

UPDATE

ALGORITHM:

STEP 1: Start the DBMS.

STEP 2: Create the table with its essential attributes.

STEP 3: Insert the record into table

STEP 4: Update the existing records into the table

STEP 5: Delete the records in to the table

STEP 6: use save point if any changes occur in any portion of the record to undo its original state.

STEP 7: use rollback for completely undo the records

STEP 8: use commit for permanently save the records

INSERT

The SQL INSERT INTO Statement is used to add new rows of data to a table in the database.

Insert a record from an existing table:

QUERY: 01

Q1. Write a query to insert the records in to employee.

Syntax: syntax for insert records in to a table


21
SQL :> INSERT INTO <TABLE NAME> VALUES< VAL1, ‘VAL2’,…..);

Command:
SQL>INSERT INTO EMP VALUES (101,'NAGARAJAN','LECTURER',15000);
1 row created.

Insert A Record Using Substitution Method:

QUERY: 02
Q2. Write a query to insert the records in to employee using substitution method.

Syntax: syntax for insert records into the table.

SQL :> INSERT INTO <TABLE NAME> VALUES< ‘&column name’, ‘&column name 2’, …..);

Command:
SQL> INSERT INTO EMP
VALUES(&EMPNO,'&ENAME','&DESIGNATIN','&SALARY'); Enter value for empno: 102
Enter value for ename:

SARAVANAN
Enter value for designatin:
LECTURER

Enter value for salary: 15000

1 row created.

old 1: INSERT INTO EMP VALUES(&EMPNO,'&ENAME','&DESIGNATIN','&SALARY')

new 1: INSERT INTO EMP


VALUES(102,'SARAVANAN','LECTURER','15000') SQL> /

Enter value for empno: 103


Enter value for ename:
PANNERSELVAM
Enter value for designatin: ASST. PROF

Enter value for salary: 20000


1 row created.

old 1: INSERT INTO EMP VALUES(&EMPNO,'&ENAME','&DESIGNATIN','&SALARY')

new 1: INSERT INTO EMP VALUES(103,'PANNERSELVAM','ASST.PROF','20000')

SQL> /
Enter value for empno: 104

22
Enter value for ename: CHINNI

Enter value for designatin:


HOD, PROF
Enter value for salary: 45000
1 row created.

old 1: INSERT INTO EMP VALUES (&EMPNO,'&ENAME','&DESIGNATIN','&SALARY')

new 1: INSERT INTO EMP VALUES (104,'CHINNI','HOD, PROF','45000')

SQL> SELECT * FROM EMP;


EMPNO ENAME DESIGNATIN SALARY
----------- ------------ ------------------- --------------

101 NAGARAJAN LECTURER 15000


102 SARAVANAN LECTURER 15000
103 PANNERSELVAM ASST. PROF 20000
104 CHINNI HOD, PROF 45000

SELECT

SELECT Statement is used to fetch the data from a database table which returns data in the
form of result table. These result tables are called result-sets.

Display the EMP table:

QUERY: 03

Q3. Write a query to display the records from employee.


23
Syntax: Syntax for select Records from the
table. SQL> SELECT * FROM <TABLE
NAME>; Command:

SQL> SELECT * FROM EMP;

EMPNO ENAME DESIGNATIN SALARY


---------- ------------ ---------- ----------
101 NAGARAJAN LECTURER 15000

UPDATE

The SQL UPDATE Query is used to modify the existing records in a table. You can use
WHERE clause with UPDATE query to update selected rows, otherwise all the rows would be
affected.

QUERY: 04

Q4. Write a query to update the records from employee.

Syntax: syntax for update records from the table.SQL> UPDATE <<TABLE NAME> SET
<COLUMNANE>=<VALUE> WHERE <COLUMN NAME=<VALUE>;

Command:

SQL> UPDATE EMP SET SALARY=16000 WHERE EMPNO=101;


1 row updated.

SQL> SELECT * FROM EMP;

24
EMPNO ENAME DESIGNATIN SALARY
---------- ------------ ------------------- ---------------
101 NAGARAJAN LECTURER 16000
102 SARAVANAN LECTURER 15000
103 PANNERSELVAM ASST. PROF 20000
104 CHINNI HOD,PROF 45000

Update Multiple

Columns:

QUERY: 05
Q5. Write a query to update multiple records from employee.

Syntax: syntax for update multiple records from the table.

SQL> UPDATE <<TABLE NAME> SET <COLUMNANE>=<VALUE> WHERE


<COLUMN NAME=<VALUE>;

Command:

SQL>UPDATE EMP SET SALARY = 16000, DESIGNATIN='ASST. PROF' WHERE EMPNO=102;

1 row updated.

SQL> SELECT * FROM EMP;

EMPNO ENAME DESIGNATIN SALARY


---------- ------------ -------------------- --------------
101 NAGARAJAN LECTURER 16000
102 SARAVANAN ASST. PROF 16000
103 PANNERSELVAM ASST. PROF 20000
104 CHINNI HOD, PROF 45000

DELETE

25
The SQL DELETE Query is used to delete the existing records from a table. You can use
WHERE clause with DELETE query to delete selected rows, otherwise all the records would be
deleted.

QUERY: 06

Q6. Write a query to delete records from employee.

Syntax: Syntax for delete Records from the table:

SQL> DELETE <TABLE NAME> WHERE <COLUMN NAME>=<VALUE>;

Command:
SQL> DELETE EMP WHERE EMPNO=103;

1 row deleted.

SQL> SELECT * FROM EMP;

EMPNO ENAME DESIGNATIN SALARY

---------- ------------ --------------------- --------------

101 NAGARAJAN LECTURER 16000

102 SARAVANAN ASST. PROF 16000

104 CHINNI HOD, PROF 45000

26
EVALUATION

Sl.No Module Max. Marks Marks Obtained


1 Database schema 25
2 Database query 25
3 Output 20
4 Technical Skill 20
5 Communication Skill 10
Total 100
Faculty Signature

RESULT:

Thus the SQL commands for DML has been verified and executed successfully.

27
Ex. No: 3 TCL COMMANDS

Date :

AIM:

To create the SAVE POINT for the transaction and verify the various operations of TCL
commands.

OBJECTIVE:

The SAVEPOINT statement names and marks the current point in the processing of a transaction. With
the ROLLBACK TO statement, savepoints undo parts of a transaction instead of the whole transaction.

An implicit savepoint is marked before executing an INSERT, UPDATE, or DELETE statement. If the
statement fails, a rollback to the implicit savepoint is done. Normally, just the failed SQL statement is
rolled back, not the whole transaction; if the statement raises an unhandled exception, the host
environment

ALGORITHM:

STEP 1: Start the DMBS.


STEP 2: Connect to the existing database (DB)
STEP 3: Create the table with its essential attributes.

STEP 4: Insert record values into the table or perform any kind of DML operation.
STEP 5: Create the SAVE POINTs for some set of statement on the transaction of database object.

STEP 6: Use the COMMIT command to save the effect of the previous command operation except
DDL command

STEP 7: Use the ROLLBACK TO SP_LABLE / ROLLBACK command for restore the
database status up to the save point

STEP 8: Check the status of the database.

STEP 9: Stop the DBMS.

Syntax:

SAVEPOINT<SAVEPOINT_NAME>;
Ex:
SQL> create table ORDER_PROCESSING( Order_ID number(3), Product_ID varchar2(10), Quantity
number(3,2), Price number(4,2));
28
Table created.

SQL> insert into ORDER_PROCESSING values(101,'RICE-22','6.5','30.50');


1 row created.
SQL> insert into ORDER_PROCESSING values(102,'OIL','2.0','90.50');
1 row created.

SQL> SELECT * FROM ORDER_PROCESSING;

ORDER_IDPRODUCT_ID QUANTITY PRICE


-------------- ---------------- -------------- ----------
101 RICE-22 6.5 30.5
102 OIL 2 90.5

SQL> COMMIT; Commit complete.

SQL> insert into ORDER_PROCESSING values(103,'BAGS','2','95');

1 row created.

SQL> insert into ORDER_PROCESSING values(104,'WATER BOTS','2','20');

1 row created.

SQL> SAVEPOINT A; Savepoint created.

SQL> insert into ORDER_PROCESSING values(105,'EGG','8','40.50');

1 row created.

SQL> insert into ORDER_PROCESSING values(106,'SHAMPOO','1','75.50');

1 row created.

SQL> SAVEPOINT B; Savepoint created.

SQL> insert into ORDER_PROCESSING values(107,'BAR SOAP','1','45.50');

1 row created.

SQL> insert into ORDER_PROCESSING values(108,'TONER','1','75.50');

1 row created.

SQL> SAVEPOINT C;
Savepoint created.

SQL> insert into ORDER_PROCESSING values(109,'SUGAR','2.0','60.50');


1 row created.

29
SQL> SELECT * FROM ORDER_PROCESSING;

ORDER_ID PRODUCT_ID QUANTITY PRICE

-------------- ---------------- --------------- ----------


101 RICE-22 6.5 30.5
102 OIL 2 90.5
103 BAGS 2 95
104 WATER BOTS2 20

105 EGG 8 40.5


106 SHAMPOO 1 75.5
107 BAR SOAP 1 45.5
108 TONER 1 75.5
109 SUGAR 2 60.5

9 rows selected.

SQL> ROLLBACK TO B;

Rollback complete.

SQL> SELECT * FROM ORDER_PROCESSING;

ORDER_ID PRODUCT_ID QUANTIT PRICE


---------------- ------------------- -------------- ------------
101 RICE-22 6.5 30.5
102 OIL 2 90.5
103 BAGS 2 95
104 WATER BOTS 2 20
105 EGG 8 40.5
106 SHAMPOO 1 75.5
6 rows

SQL> ROLLBACK TO A;

Rollback complete.

30
SQL> SELECT * FROM ORDER_PROCESSING;

ORDER_ID PRODUCT_ID QUANTITY PRICE


---------------- -------------------- ---------------- -----------
101 RICE-22 6.5 30.5
102 OIL 2 90.5
103 BAGS 2 95
104 WATER BOTS 2 20

SQL> ROLLBACK;
Rollback complete.
SQL> SELECT * FROM ORDER_PROCESSING;
ORDER_ID PRODUCT_ID QUANTITY PRICE
--------------- -------------------- ----------------
-------------
101 RICE-22 6.5 30.5
102 OIL 2 90.5
SQL> ROLLBACK;
Rollback complete.
SQL> SELECT * FROM ORDER_PROCESSING;
ORDER_ID PRODUCT_ID QUANTITY PRICE
---------------- ------------------- ---------------- -----------
101 RICE-22 6.5 30.5
102 OIL 2 90.5

31
EVALUATION

Sl.No Module Max. Marks Marks Obtained


1 Database schema 25
2 Database query 25
3 Output 20
4 Technical Skill 20
5 Communication Skill 10
Total 100
Faculty Signature

RESULT:

Thus the SQL commands for creation and various operations on transaction (TCL
COMMAND) save point has been verified and executed successfully.
32
33
Ex. No: 4 WORKING WITH BUILT-IN FUNCTION IN SQL

Date :

Aim

To implement the built-in function in SQL.

ALGORITHM

Built-in functions are predefined functions that perform a specific task. Built-in functions based
on the values that they take to perform a task, can be classified into two types. They are

1. Scalar or single row function

2. Aggregate or group function

Scalar functions
Number functions
Character functions
Date functions
Conversion functions
Other functions

Number functions

ABS(n)

FLOOR(n)

CEIL(n)

EXP(n)

LN(n)

LOG(n)

MOD(n)

POWER(m,n)

ROUND(n[,m])

34
SIGN(n)

SQRT(n)

TRUNC(n[,m])

Character Functions
a. returning number values

ASCII(char)

INSTR(char1,char2[,n[,m]])

INSTRB(char1,char2[,n[,m]])

LENGTH(char)

LENGTHB(char)

b. returning char value

CHR(n)

CONCAT(char1,char2)

INITCAP(char)

LPAD(char1,n[,char2])

LTRIM(char[,set])

REPLACE(char,search-string[,replacement-string])

RPAD(char1,n[,char2])

RTRIM(char[,set])

SOUNDEX(char)

SUBSTR(char,m[,n])

SUBSTRB(char,m[,n])

TRANSLATE(char,from,to)

35
UPPER(char)

LOWER(char)

c. Date functions

ADD_MONTHS(d,n)

Last_day(d)

MONTHS_BETWEEN(d1,d2)

NEXT_DAY(d,char)

ROUND(d[,fmt])

TRUNC(d[,fmt])

Date format elements

Element Meaning

YYYY 4 digit year

YY Last 2 digits of year

MM Month(01-12 ; Jan=01)

MONTH Name of month,

MON Abbreviated name of month

DDD Day of year(1-366)

DD Day of month(1-31)

D Day of week(1-7)

DAY Name of day

DY Abbreviated name of day

HH or HH12 Hour of day(1-12)

HH24 Hour of day(0-23)

MI Minute(0-59)

SS Second(0-59)

36
Conversion Functions

TO_CHAR(d[,fmt])

TO_CHAR(n[,fmt])

TO_DATE(char[,fmt])

TO_NUMBER(char[,fmt[,'nlsparams']])

Other functions

greatest(expr[,expr])

least(expr[,expr])

Nvl(expr1,expr2)

UID(user)

Sysdate

Aggregate functions
AVG()

MAX()

MIN()

COUNT()

SUM()

LIST OF EXERCISES

String Functions

1. Find the length of the column ‘Designation’

2. Display the first three characters of Department name

37
3. Change all ‘Block A’ in Deplocation column as ‘Block F’

4. Remove the ‘0’ s in the strings ‘ABC0000’, ‘000ABC’

5. Display the distinct Designations in title case

6. Display the Depname in lower case

7. Display the Deplocation in upper case

8. Translate Grade to the corresponding designation

Numeric Functions

9. Find the total number of employees.

10. Find the employee with maximum salary, minimum salary in each department in the
ascending order of depno.

11. Find the total salary paid to the employees.

12. Find the average salary paid to the employees.

13. Display the rounded value of the ‘salary’ column in ‘Employee’ table.

14. Display the salary such that it contains no decimal places.

Date Functions

15.Display the system date in the format mentioned below “27th October 1996”.

16.Display “20th September 1996” in the date format.

17.Display the date two months after the date-of-join for all the employees.

18.Display the last date of the month in the date-of-join for all the employees.

19.Display the months between the current date and the date-of-join.

20.Display the next occurrence of ‘Friday’ to the current date.

21.Display the first day of the year 2000.

38
39
40
41
42
EVALUATION

Sl.No Module Max. Marks Marks Obtained


1 Database schema 25
2 Database query 25
3 Output 20
4 Technical Skill 20
5 Communication Skill 10
Total 100
Faculty Signature

RESULT:

Thus the SQL commands for built in function has been verified and executed successfully

43
Ex. No: 5 SIMPLE PL / SQL PROGRAM

Date :

AIM:

To write a PL/SQL block using different control (if, if else, for loop, while loop,…) statements.

OBJECTIVE:

PL/SQL Control Structure provides conditional tests, loops, flow control and branches that
let to produce well-structured programs

PL/SQL

PL/SQL is Oracle’s procedural language extension to SQL. PL/SQL allows you to mix SQL

statements with procedural statements like IF statement, Looping

structures etc. It is extension of SQL the following or advantages of

PL/SQL.

1. We can use programming features like if statement loops etc.

2. PL/SQL helps in reducing network traffic.

3. We can have user defined error massages by using concept of exception handling.

4. We can perform related actions by using concept of Triggers.

5. We can save the source code permanently for repeated execution.

PL/SQL Block:

DECLARE

Declaration of variable

Declaration of cursor----------

44
(OPTIONAL) Declaration of exception

BEGIN

Executable commands--------- (MANDATORY)

EXCEPTION

Exception handlers---------- (OPTIONAL)

END;

/ To execute the program / command

Declare:

This section is used to declare local variables, cursors, Exceptions and etc. This section is optional.

Executable Section:

This section contains lines of code which is used to complete table. It is mandatory.

Exception Section:

This section contains lines of code which will be executed only when exception is raised.
This section is optional.

Simplest PL/SQL Block:

Begin

--------

END;

SERVEROUTPUT

This will be used to display the output of the PL/SQL programs. By default this will be off.

Syntax:

Set serveroutput on | off

Ex:

45
SQL>set serveroutput on

PL/SQL CONTROL STRUCTURES

PL/SQL has a variety of control structures that allow you to control the behaviour of the block
as it runs. These structures include conditional statements and loops.
If-then else Case

Location is NEW YORK


Case with no else

Labeled case

Searched

case Simple loop

While loop

For loop

Goto and Labels


IF-THEN-ELSE

Syntax:

If <condition1> then

Sequence of statements; Elseif <condition1> then

Sequence of statements;

……

Else

Sequence of statements;

End if;

CASE

46
Syntax:

Case test-variable

When value1 then sequence of statements;

When value2 then sequence of statements;

When valuen then sequence of statements;

Else sequence of statements;

End case;
Location is NEW YORK

CASE WITHOUT
ELSE

Syntax:

Case test-variable

When value-1 then sequence of statements;

When value-2 then sequence of statements;

……

When value-n then sequence of statements;

End case;

LABELED CASE

Syntax:

<<label>>

Case test-variable

When value1 then sequence of

statements; When value2 then

sequence of statements;

47
……

When value then sequence of

statements; End case;

SEARCHED

CASE Syntax:

Case

When <condition-1> then sequence of statements;

When <condition-2> then sequence of statements;

……
Location is NEW YORK
When <condition-n> then sequence of statements;

End case;

SIMPLE LOOP

Syntax:

Loop

Sequence of statements;

Exit when

<condition>; End loop;

WHILE LOOP SYNTAX

While <condition> loop

Sequence of statements;

End loop;

FOR LOOP SYNTAX

For <loop_counter_variable> in low_bound..high_bound loop


48
Sequence of statements;

End loop;

GOTO AND LABELS Syntax:

Goto label;

Where label is a label defined in the PL/SQL block. Labels are enclosed in double angle

brackets. When a goto statement is evaluated, control immediately passes to the statement

identified by the label.

Location is NEW YORK

PL/SQL PROGRAMS

1: Write PL/SQL block which will calculate some of two numbers and display the output?

DECLARE

A number(2);

B number(2);

C number(3);

BEGIN

A := 10;

B := 20;

C := A + B; DBMS_OUTPUT.PUT_LINE(C);

DBMS_OUTPUT.PUT_LINE( ‘sum of two numbers’ || C);

END;

49
Output:

30

sum of two numbers 30

PL/SQL procedure successfully completed.

2: Write a PL/SQL block which accepts employee number and increment is salary by 1000?

DECLARE
A number(4); A := &Empno;
Location is NEW YORK

Update emp set sal = sal + 1000 where Empno = A;

END;
/

50
3: Write a PL/SQL block which empno and delete that row from the emp table?

DECLARE

A number(4);
BEGIN
A := &Empno;
Delete from emp where Empno = A;
END;
/

Q4: PL/SQL for reversing the given string

Algorithm:

1. Get the input string.


2. Find the length of the string.
3. Extract the characters one by one from the end of the string.
4. Concatenate the extracted characters.
5. Display the concatenated reversed string.
6. Stop the program.

Location is NEW YORK

Program:

declare
b varchar2(10) := '&b';
c varchar2(10);
l number(2);
i number(2);
g number(2); dvarchar2(10);

51
begin

l:=length(b);
g:=l;
for i in 1..l loop
g,1);
g := g - 1;
d := d ||
c;
end loop;

dbms_output.put_line('revised string is'); dbms_output.put_line(d);

End;

c:=substr(b);
Location is NEW YORK

OUTPUT:

Enter value for b: ramu

old 2: b varchar2(10) := '&b';

new 2: b varchar2(10) :=

'ramu'; revised string isumar

PL/SQL procedure successfully completed.

5: PL/SQL for Fibonacci Series.


52
Algorithm:

1. Get the no.of terms N, in the fibonacci series to be generated.

2. If N is less than 2, then raise an exception and display the message.

3. Otherwise initialize the value of A as 0 and B as 1 and display them.

4. Repeat the steps 5&6 in N-3 times.

5. C:=A+B & Display C.

6. A:=B&B:=A

7. Stop the program.

Program:

declare

a number(3);b number(3);c number(3);n number(3):=&n; negative exception; begin

if n < 2 then raise negative

end if;

a := 0;
b := 1;
dbms_output.put_line('fibonacci series is');
dbms_output.put_line(a);
dbms_output.put_line(b);

for i in 3 ..n loop

c := a + b;
dbms_output.put_line(c);
a := b;
b :=c;
end loop;

exception
when negative then
dbms_output.put_line('n should be greater than 1');
end

53
;

SQL>

Enter value for n:5

old 5: n number(3):=&n; new 5:n number(3):=5; Fibonacci

0
1

PL/SQL procedure successfully completed

6: Program to check whether given number is Armstrong or not.

Algorithm:

Step 1: Declare the variable N, S, D and DUP.


Step 2: Store the value in var. N and var. DUP..

Step 3: check for the value of N, which is not equal to 0.


Step 4: divide value stored in N by 10 and store it var. D. (D=n%10).
Step 5: the reminder will be multiply 3 times and store it in Var. S.
Step 6: The coefficient will be calculated using FLOOR function. And store it in var. N.
Step 7: repeat the Steps 3, 4, 5, and 6 till loop will be terminated.
Step 8: Check whether the stored value and calculated values are same

Step 9: if both the values are same, then display “The given number is Armstrong”
54
Step 10: Otherwise display “it is not Armstrong” and terminate the loop.

declare

n number:=1634;

s number:=0;

r number;

len number;

m number;

begin

dbms_output.put_line('Given number : '||n);

m := n;

len := length(to_char(n));

while n>0

loop

r := mod(n , 10);

s := s + power(r , len);

n := trunc(n / 10);

end loop;

if m = s

then

dbms_output.put_line('Amstrong Number');

else

dbms_output.put_line('Not an Amstrong

Number');

end if;

55
end;

Test Valid Data Set:

Enter value
of n 153

Output:

number is Armstrong

Q7: Write a program to generate all prime numbers below 100.

AIM: to generate all prime numbers below 100.

DECLARE
i NUMBER(3);
j NUMBER(3);
BEGIN
dbms_output.Put_line('The prime numbers are:');
dbms_output.new_line;
i := 2;
LOOP
j := 2;
LOOP
EXIT WHEN( ( MOD(i, j) = 0 )
OR ( j = i ) );
j := j + 1;
END LOOP;
IF( j = i )THEN
56
dbms_output.Put(i||' ');
END IF;
i := i + 1;
exit WHEN i = 100;
END LOOP;
dbms_output.new_line;
END;

Valid Test Data

OUTPUT:
1
2 3 5 5 7 11

EVALUATION

Sl.No Module Max. Marks Marks Obtained


1 Database schema 25
2 Database query 25
3 Output 20
4 Technical Skill 20
5 Communication Skill 10
Total 100
Faculty Signature

RESULT:

57
Thus the PL/SQL program has been verified and executed successfully

58

You might also like