NISANTH DBT LAB 24MCT12 FINAL
NISANTH DBT LAB 24MCT12 FINAL
(Autonomous)
Perundurai, Erode – 638 060
Name : G.NISANTH
Semester :I
Certified that this is a bonafide record of work for the above student for
Examiner - 1 Examiner - 2
KONGU ENGINEERING COLLEGE
(Autonomous)
Perundurai, Erode – 638 060
LIST OF EXPERIMENTS
3. Database Objects 20
6. Basic PL/SQL
8. Cursor operations
Date:
AIM:
To perform DDL commands like create, alter, rename, truncate, drop and DML commands like
insert, update, select, delete and DDL with integrity constraints for University Database.
DDL COMMANDS:
1. Create
2. Alter
3. Rename
4. Truncate
5. Drop
1.CREATE
Table created.
SQL> describe student;
SNAME VARCHAR(20)
DPMT VARCHAR(5)
EMAIL VARCHAR(25)
SNAME VARCHAR(20)
DPMT VARCHAR(5)
EMAIL VARCHAR(25)
CONTACTNO BIGINT
SNAME VARCHAR(20)
DPMT VARCHAR(5)
S_ EMAIL VARCHAR(25)
CONTACTNO BIGINT
SNAME VARCHAR(20)
DPMT VARCHAR(5)
EMAIL VARCHAR(25)
CONTACTNO BIGINT
3.RENAME
SQL>
describe student_info;
Name Null? Type
SNAME VARCHAR(20)
DPMT VARCHAR(5)
EMAIL VARCHAR(25)
CONTACTNO BIGINT
4.TRUNCATE
SQL>
truncate table student_info;
Table truncated.
SQL>
select * from student_info;
no rows selected
SQL> describe student_info;
Name Null? Type
SNAME VARCHAR(20)
DPMT VARCHAR(5)
EMAIL VARCHAR(25)
CONTACTNO BIGINT
5.DROP
DML COMMANDS
1.Insert
2.Update
3.Select
4.Delete
Table Creation:
Table created.
SQL> describe student;
SNAME VARCHAR(20)
DPMT VARCHAR(5)
EMAIL VARCHAR(25)
1.Insert command
SQL> insert into student values
(101, 'Nisanth', 'MCA', '[email protected]', 9345292781),
(102, 'Sivakumar', 'MCA', '[email protected]', 8072363074),
(103, 'Sachin', 'MCA', '[email protected]', 8754681258),
(104, 'Siva', 'MCA', '[email protected]', 6380603146);
2.Select Command
select*from student;
4.Delete command
1 row deleted.
select*from student;
1.Primary Key
2.Foreign Key
3.Unique Key
4.Check constraint
6.Null constraint
create table student (sid int (10) primary key, sname varchar (20) not null, dpmt varchar
(5) not null, email varchar (25) not null unique, contactno numeric (10) not null, age int
check (age >= 18), enrollment_date date default current_date);
select*from student;
create table courses (course_id int primary key, course_name varchar(100) not null,
credits int check (credits > 0), sid int,foreign key (sid) references student(sid));
select*from courses;
COE (30)
RECORD(20)
VIVA (10)
TOTAL (60)
RESULT:
Thus, the execution of DDL, DML commands and integrity constraints for University Database
system has been done successfully.
Ex.no: 02
Date:
AIM:
To perform transaction control language commands like savepoint, rollback, commit and data
control language commands like grant, revoke for University Database system.
TCL COMMANDS:
1. Savepoint
2. Rollback
3. Commit
Table creation:
sid int(10),
sname varchar(20),
dpmt varchar(5),
email varchar(25),
contactno numeric(10)
);
Table created.
SQL> insert into student values
(101, 'Nisanth', 'MCA', '[email protected]', 9345292781),
(102, 'Sivakumar', 'MCA', '[email protected]', 8072363074),
(103, 'Sachin', 'MCA', '[email protected]', 8754681258),
(104, 'Siva', 'MCA', '[email protected]', 6380603146);
SQL> select * from student;
Creating a Savepoint:
1.Savepoint
SQL>SAVEPOINT s1;
Savepoint created.
1 row created.
SQL>SAVEPOINT s2;
Savepoint created.
1 row created.
2.Rollback
Rollback complete.
COMMIT;
Commit complete.
DCL COMMANDS:
1. Grant
2. Revoke
1.Creating Users:
SQL>create user nisanth identified by password_nisanth;
User created.
SQL>create user sivakumar identified by password_sivakumar;
User created.
SQL> connect
Enter user-name:sivakumar
Enter password: …….
Connected.
1 row inserted.
4. Revoking Privileges:
Revoke succeeded.
SQL> connect
Enter user-name: nisanth
Enter password: …….
Connected.
COE (30)
RECORD (20)
VIVA (10)
TOTAL (60)
RESULT:
Thus, the execution of TCL, DCL commands for University Database system has been done
successfully.
Ex.no: 03
Date:
DATABASE OBJECTS
AIM:
To perform database objects such as view, sequences, synonyms and indexes for University
Database System.
DATABASE OBJECTS:
1. View
2. Sequences
3. Synonyms
4. Indexes
1.View
Table creation:
sid int(10),
sname varchar(20),
dpmt varchar(5),
email varchar(25),
contactno numeric(10)
);
Table created.
SQL> insert into student values
(101, 'Nisanth', 'MCA', '[email protected]', 9345292781),
(102, 'Sivakumar', 'MCA', '[email protected]', 8072363074),
(103, 'Sachin', 'MCA', '[email protected]', 8754681258),
(104, 'Siva', 'MCA', '[email protected]', 6380603146);
SQL> create view v1 as select sid, sname from student with read only;
View created.
101 Nisanth
102 Sivakumar
103 Sachin
104 Siva
View created.
1 row created.
Sequence created.
1 row created.
4. Synonyms:
Synonym created.
This removes the index idx_sid from the student table, meaning the database will no longer
use this index for queries involving the sid column.
COE (30)
RECORD (20)
VIVA (10)
TOTAL (60)
RESULT:
Thus, the execution of database objects for University Database System has been done
successfully.
Ex.no: 04
Date:
AIM:
To implement single row functions, aggregate functions and set operations for University
Database system.
1. General functions
3. Character functions
4. Date functions
5. Number functions
Table creation:
sid int(10),
sname varchar(20),
dpmt varchar(5),
email varchar(25),
contactno numeric(10)
);
Table created.
SQL> insert into student values
(101, 'Nisanth', 'MCA', '[email protected]', 9345292781),
(102, 'Sivakumar', 'MCA', '[email protected]', 8072363074),
(103, 'Sachin', 'MCA', '[email protected]', 8754681258),
(104, 'Siva', 'MCA', '[email protected]', 6380603146);
1. General Functions
SQL> select sid, nvl (contactno, 'no contact') as contact from student;
SID CONTACTNO
101 9345292781
102 8072363074
103 8754681258
104 6380603146
Insert with NULL for Contact Number:
SQL> insert into student (sid, sname, dpmt, email, contactno) values (105, 'vasanth',
'mca', '[email protected]', null);
Query to Display Contact with NVL():
SQL> select sid, sname, nvl(contactno, 'no contact') as contact
from student;
SID SNAME
101 NISANTH
102 SIVAKUMAR
103 SACHIN
104 SIVA
SID DPMT
101 mca
102 mca
103 mca
104 mca
101 Mca
102 Mca
103 Mca
104 Mca
3. Character Functions
a) SUBSTR () - Extract substring:
SQL> select sid, substr(email, 1, 10) as email from student;
SID EMAIL
101 nisanthg.2
102 sivakumar
103 sachins.24
104 sivak.24mc
SID SNAME
101 7
102 9
103 6
104 4
4. Date Functions
CURRENT_DATE
-------------------------
2024-10-06 12:34:56
CONVERTED_DATE
-------------------
2024-10-06 00:00:00
FORMATTED_DATE
-------------------
06-OCT-20254
5. Number Functions
MODULUS_VALUE
--------------
1
2. POWER: Returns a number raised to the power of another number.
POWER_VALUE
------------
SQUARE_ROOT
------------
ROUNDED_VALUE
--------------
123.46
AGGREGATE FUNCTIONS:
1. Average
2. Count
3. Max
4. Min
5. Sum
Table creation:
sid int(10),
sname varchar(20),
dpmt varchar(5),
email varchar(25),
contactno numeric(10)
);
Table created.
SQL> insert into student values
(101, 'Nisanth', 'MCA', '[email protected]', 9345292781),
(102, 'Sivakumar', 'MCA', '[email protected]', 8072363074),
(103, 'Sachin', 'MCA', '[email protected]', 8754681258),
(104, 'Siva', 'MCA', '[email protected]', 6380603146);
MIN(CONTACTNO)
--------------
6380603146
COUNT(SID)
-----------
4
SUM(SID)
--------
410
AVG(SID)
---------
102.5
SET OPERATIONS
1. Union
2. Union all
3. Intersect
4. Minus
Table creation:
sid int(10),
sname varchar(20),
dpmt varchar(5),
email varchar(25),
contactno numeric(10)
);
Table created.
SQL> insert into student values
(101, 'Nisanth', 'MCA', '[email protected]', 9345292781),
(102, 'Sivakumar', 'MCA', '[email protected]', 8072363074),
(103, 'Sachin', 'MCA', '[email protected]', 8754681258),
(104, 'Siva', 'MCA', '[email protected]', 6380603146);
1 DBMS CS 3
2 ML CS 4
3 DM BUSINESS 3
4 DSA CS 4
5 ML CS 4
6 DSA CS 4
1. UNION
NAME
Nisanth
Sivakumar
Sachin
Siva
DBMS
ML
DM
DSA
2. UNION ALL
NAME
Nisanth
Sivakumar
Sachin
Siva
DBMS
ML
DM
DSA
ML
DSA
3. INTERSECT
4. MINUS
NAME
Nisanth
Sivakumar
Sachin
Siva
COE (30)
RECORD (20)
VIVA (10)
TOTAL (60)
RESULT:
Thus, the execution of single row functions, aggregate functions and set operations for
University Database system has been done successfully.
Ex.no: 05
Date:
AIM:
Types of Joins :
1. Inner Join
2. Left Join (Or Left Outer Join)
3. Right Join (Or Right Outer Join)
4. Full Outer Join
5. Cross Join
6. Self-Join
Table creation:
sid int(10),
sname varchar(20),
dpmt varchar(5),
email varchar(25),
contactno numeric(10)
);
Table created.
SQL> insert into student values
(101, 'Nisanth', 'AM', '[email protected]', 9345292781),
(102, 'Sivakumar', 'DBT', '[email protected]', 8072363074),
(103, 'Sachin', 'C', '[email protected]', 8754681258),
(104, 'Siva', 'IOT', '[email protected]', 6380603146);
An inner join returns only the rows where there is a match in both tables based on the dpmt
field in student and dname field in department.
A left join returns all rows from the student table and matched rows from the department
table. If there is no match, NULL values are returned.
student.dpmt
FROM student
Expected Output:
Based on the current data, no two students share the same department. Therefore, the result of
2. Self-Join on the department Table to Find Departments with the Same Head
department2_name, department.head
FROM department
Expected Output:
Each department has a unique head in the current data, so this query would also return an
empty set.
FROM student
GROUP BY dpmt
ORDER BY dpmt;
Explanation: This query counts the number of students in each department and orders the
Expected Output:
DPMT STUDENT_COUNT
AM 1
C 1
DBT 1
IOT 1
FROM department
ORDER BY dname;
Explanation: This retrieves all departments along with their heads and orders the results
Expected Output:
DNAME HEAD
AM Dr. Smith
C Dr. Kim
DS Dr. Lee
FROM department
Explanation: This retrieves all departments where the head's name starts with "Dr.".
Expected Output:
DNAME HEAD
AM Dr. Smith
DS Dr. Lee
C Dr. Kim
COE (30)
RECORD (20)
VIVA (10)
TOTAL (60)
RESULT:
The execution of join operations, subqueries, and the use of aggregate functions has
been performed successfully on the university database system
Ex No: 06
Date:
PL/SQL
AIM:
To create and execute PL/SQL functions for fo University Database Management System.
QUERIES
1.Write a PL/SQL code block to declare a basic variable, assign it a value, and then
SQL> declare
2 var varchar2(50);
3 begin
4 var:='hello world';
5 dbms_output.put_line(var);
6 end;
7 /
O/P
Database Technologies
2.Write a PL/SQL code block that takes two numbers as input from the user, calculates
their sum, and displays the result. Use DBMS_OUTPUT.PUT_LINE to print the output
a NUMBER := &a;
b NUMBER := &b;
sum NUMBER;
BEGIN
sum := a + b;
END;
O/P
3.Write a SQL script to create a table named student with the following columns: sid (a
number for student ID), sname (a string for student name), and sdept (a string for
student department). Then, insert the following records into the table:
After inserting the records, display the contents of the student table.
CREATE TABLE student (
sid NUMBER(5),
sname VARCHAR2(10),
sdept VARCHAR2(5)
);
O/P
5 Nisanth MCA
6 Sivakumar MCA
7 Sachin MCA
8 Siva MCA
4. Write a PL/SQL block that retrieves and displays the name and department of a student
from the student table based on a specified student ID. Use the following details:
name VARCHAR2(10);
dept VARCHAR2(5);
BEGIN
SELECT sname, sdept INTO name, dept FROM student WHERE sid = id;
END;
O/P
5.Write a PL/SQL block that takes a number as input from the user and determines
whether the number is even or odd. Use a substitution variable (&num) to prompt the
user for the input. The block should use the MOD function to check if the number is
even or odd and display the result using DBMS_OUTPUT.PUT_LINE in the following
DECLARE
BEGIN
IF MOD(num, 2) = 0 THEN
ELSE
dbms_output.put_line(num || ' is odd');
END IF;
END;
input num = 7:
O/P
7 is odd
6. Write a PL/SQL block that takes two numbers as input from the user and determines
which number is greater. Use substitution variables (&a and &b) to prompt the user for
the two input values. The block should compare the numbers using an IF statement and
<greater_number> is greater
DECLARE
a NUMBER := &a;
b NUMBER := &b;
BEGIN
IF a > b THEN
ELSE
END IF;
END;
inputs a = 15 and b = 10
O/P
15 is greater
7. Write a PL/SQL block that checks whether a given number is divisible by 3. Use a
substitution variable (&a) to prompt the user for the input number. The block should
<number> is divisible by 3
DECLARE
BEGIN
IF MOD(a, 3) = 0 THEN
ELSE
END IF;
END;
O/p
If the user inputs 10, the output will be: 10 is not divisible by 3
8. Write a PL/SQL block that uses a loop to print the numbers from 1 to 10. The block
of i.
DECLARE
i NUMBER := 1;
BEGIN
LOOP
dbms_output.put_line(i);
i := i + 1;
END LOOP;
END;
O/P
5
6
10
9. Write a PL/SQL block to generate and display the Fibonacci series up to a user-
specified number of terms. Use a substitution variable (`&n`) to take the input for the
using`DBMS_OUTPUT.PUT_LINE`.
DECLARE
n NUMBER;
a NUMBER := 0;
b NUMBER := 1;
temp NUMBER;
BEGIN
n := &n;
dbms_output.put_line('Fibonacci Series:');
dbms_output.put_line(a);
temp := a + b;
a := b;
b := temp;
END LOOP;
END;
O/P
Fibonacci Series:
10.What does the following PL/SQL code produce, and how does it create a pattern
based on user input? Describe the roles of the variables n, i, and j in the nested loops.
DECLARE
n NUMBER := &n; -- Take input from the user for the number of rows
i NUMBER;
j NUMBER;
BEGIN
END LOOP;
END;
O/P
22
333
4444
55555
COE (30)
RECORD (20)
VIVA (10)
TOTAL (60)
RESULT:
Thus, the execution of basic PL/SQL queries for University Database Management System.
Date:
AIM:
To create and execute PL/SQL functions and procedures to manage the payroll process,
including calculating gross salaries, displaying payroll details, incrementing salaries, and
generating payroll reports.
Table creation:
RETURN gross_salary;
END;
Test the Function
O/P
GROSS_SALARY
55000.00
Payroll Details:
Employee Name: Nisanth
Gross Salary: 55000
This procedure increments the basic salary of employees based on their designation.
BEGIN
increment_salary('AM', 5000); -- Increment for Assistant Manager
END;
O/P
O/P
COE (30)
RECORD (20)
VIVA (10)
TOTAL (60)
RESULT:
PL/SQL functions and procedures were effectively implemented to manage the payroll process.
Ex No: 08
Date:
AIM:
To perform implicit and explicit cursor operations on an employee table to iterate through and
display employee details.
1.Table Creation
emp_name VARCHAR2(50),
emp_designation VARCHAR2(50),
email VARCHAR2(100),
contactno VARCHAR2(15),
allowances NUMBER(10, 2)
);
Table created
2.Insert Values
COMMIT;
DECLARE
total_employees NUMBER;
BEGIN
-- Implicit Cursor Usage
SELECT COUNT(*) INTO total_employees FROM employee;
DBMS_OUTPUT.PUT_LINE('Total Number of Employees: ' || total_employees);
END;
O/P
5. Explicit Cursor
DECLARE
-- Declare a cursor
CURSOR employee_cursor IS
SELECT emp_id, emp_name, emp_designation, basic_salary FROM employee;
O/P
Employee Details:
----------------------------
Emp_ID: 101, Name: Nisanth, Designation: AM, Basic Salary: 40000
Emp_ID: 102, Name: Sivakumar, Designation: DBT, Basic Salary: 45000
Emp_ID: 103, Name: Sachin, Designation: C, Basic Salary: 38000
Emp_ID: 104, Name: Siva, Designation: IOT, Basic Salary: 42000
COE (30)
RECORD (20)
VIVA (10)
TOTAL (60)
Result
The PL/SQL programs to perform implicit and explicit cursor operations on the employee table
were executed successfully, and the employee details were displayed as expected.
Ex. No.: 09
Date:
TRIGGERS AND PACKAGES
AIM:
To implement PL/SQL triggers and packages for the university database system
Table Structure (student):
CREATE TABLE student (
sid INT(10),
sname VARCHAR(20),
dpmt VARCHAR(5),
email VARCHAR(25),
contactno NUMERIC(10)
);
Inserting Records:
INSERT INTO student VALUES
(101, 'Nisanth', 'AM', '[email protected]', 9345292781),
(102, 'Sivakumar', 'DBT', '[email protected]', 8072363074),
(103, 'Sachin', 'C', '[email protected]', 8754681258),
(104, 'Siva', 'IOT', '[email protected]', 6380603146);
Select *from student;
PACKAGES
2. Package Body:
SQL> CREATE OR REPLACE PACKAGE BODY p1 AS
PROCEDURE find_s(id student.sid%TYPE) IS
stu_name student.sname%TYPE;
dept student.dpmt%TYPE;
contact student.contactno%TYPE;
BEGIN
SELECT sname, dpmt, contactno
INTO stu_name, dept, contact
FROM student
WHERE sid = id;
COE (30)
RECORD (20)
VIVA (10)
TOTAL (60)
Result:
Thus, the execution of PL/SQL triggers and packages for the university database system has
been successfully implemented and verified with appropriate outputs.
Ex. No.: 10
Date:
EXCEPTION HANDLING
AIM:
To implement PL/SQL programs for performing exception handling in a university database
system.
Table Structure (student):
CREATE TABLE student (
sid INT(10),
sname VARCHAR(20),
dpmt VARCHAR(5),
email VARCHAR(25),
contactno NUMERIC(10)
);
Inserting Records:
INSERT INTO student VALUES
(101, 'Nisanth', 'AM', '[email protected]', 9345292781),
(102, 'Sivakumar', 'DBT', '[email protected]', 8072363074),
(103, 'Sachin', 'C', '[email protected]', 8754681258),
(104, 'Siva', 'IOT', '[email protected]', 6380603146);
Select *from student;
Output:
No student found with ID: 105
PL/SQL procedure successfully completed.
Output:
Error: Query returned more than one row.
PL/SQL procedure successfully completed.
Output:
Student ID 5000 is out of range.
PL/SQL procedure successfully completed.
COE (30)
RECORD (20)
VIVA (10)
TOTAL (60)