DBMS Lab Record
DBMS Lab Record
AIM:
To implement database creation and do the operations in database using Data Definition Language.
ALGORITHM:
Step 1: Create table by using create table command with column name, data type and size.
Step 2: Display the table by using desc command.
Step 3: Add any new column with the existing table by alter table command.
Step 4: Modify the existing table with modify command.
Step 5: Delete the records in files by truncate.
Step 6: Delete the Entire table by drop command.
Syntax:
Example:
use acet;
Syntax:
desc table_name;
Example:
desc employee;
3) ALTER TABLE
Syntax:
alter table table_name add column_name datatype;
Example:
alter table employee add dept varchar(10);
desc employee;
4) RENAME
To rename column name
Syntax:
alter table table_name rename column existing _name to new_name;
Example:
alter table employee rename column address to addr;
table employee altered.
Example:
alter table employee rename to empl;
Query OK, 0 rows affected (0.02 sec)
select * from employee;
ORA-00942: table or view does not exist
00942. 00000 - "table or view does not exist"
*Cause:
*Action:
Error at Line: 21 Column: 15
5) TRUNCATE
Syntax:
truncate table table_name;
Example:
truncate table emp;
select * from empl;
no data found
6) DROP TABLE
Syntax:
drop table table_name;
Example:
RESULT:
Thus the queries to implement data definition commands are executed successfully.
EX.NO:1b DATA MANIPULATION LANGUAGE
DATE:
AIM:
To implement and execute a query for manipulating & storing data items in a database using Structured
Query Language commands
ALGORITHM:
Step 1: Create table by using create table command.
Step 2: Insert values into the table
Step 3: Delete any records from the table
Step 4: Update any values in the table.
Step 5: Display the values from the table by using select command.
1) CREATE A TABLE
create table student(id int,Name varchar(30) NOT NULL,Department varchar(10), UNIQUE (id));
desc student;
2) INSERT
Syntax:
insert into table_name values(column1,column2,….);
Example:
insert into student values(1,'ABC','CSE');
insert into student values(2,'DEF','CSE');
select * from student;
Example:
insert into student values(3,’alex’,default);
1 rows inserted.
select * from student;
Example:
create table stud(id int,Name varchar(30),Department varchar(10));
insert into stud select * from student;
3) UPDATE
Syntax:
update table_name set column1 = value1, column2 = value2. .. , columnn = valuen
where [condition];
Example: (single column)
4) DELETE
Example:
delete from stud where id=2;
Syntax:
delete from table_name;
Example:
delete from stud;
select * from stud;
no data found
RESULT:
Thus, a query for manipulating & storing data items in a database using
Structured Query Language commands were implemented and executed successfully.
EX.NO:2 CREATE SET OF TABLES ADD FOREIGN KEY CONSTRAINT AND
DATE: INCORPORATE REFERENTIAL INTEGRITY
Aim:
To create a set of tables in SQL with a foreign key constraint to ensure referential integrity between them.
Algorithm:
Step 1: Create the parent table with a primary key.
Step 2: Create the child table with a foreign key that references the primary key of the parent table.
Step 3: Ensure referential integrity by setting constraints like ON DELETE CASCADE
Step 4: Insert data into the parent table first.
Step 5: Insert data into the child table referencing the parent table’s primary key.
Step 6: Test referential integrity by attempting to delete or update records.
Query:
-- Step 1: Create the Parent Table (Department)
CREATE TABLE Department (
DeptID INT PRIMARY KEY,
DeptName VARCHAR(50) NOT NULL);
-- Step 2: Create the Child Table (Employee) with Foreign Key Constraint
CREATE TABLE Employee (
EmpID INT PRIMARY KEY,
EmpName VARCHAR(50) NOT NULL,
DeptID INT,
FOREIGN KEY (DeptID) REFERENCES Department(DeptID) ON DELETE CASCADE);
-- This will delete 'HR' and also Alice (due to ON DELETE CASCADE)
RESULT:
Thus, to create a set of tables add foreign key constraint and incorporate referential integrity were
implemented and executed successfully.
EX.NO:3 DATABASE TABLES WITH DIFFERENT 'WHERE CLAUSE' CONDITIONS
DATE:
Aim:
To query database tables using different WHERE clause conditions and apply aggregate functions
to perform calculations on data.
Algorithm:
Step 1: Create two tables: Department (parent) and Employee (child) with a foreign key constraint.
Step 2: Insert sample data into both tables.
Step 3: Use different WHERE conditions to filter data:
Equality (=)
Range (BETWEEN)
Pattern matching (LIKE)
Multiple conditions (AND, OR)
Null checking (IS NULL, IS NOT NULL)
Step 4: Implement aggregate functions (COUNT, SUM, AVG, MAX, MIN, GROUP BY).
Step 5: Execute queries and verify results.
Query:
+-------+---------+--------+--------+
| EmpID | EmpName | Salary | DeptID |
+-------+---------+--------+--------+
| 101 | Alice | 50000 | 1 |
| 102 | Bob | 60000 | 2 |
| 103 | Charlie | 45000 | 1 |
+-------+---------+--------+--------+
+-------+---------+--------+--------+
| EmpID | EmpName | Salary | DeptID |
+-------+---------+--------+--------+
| 101 | Alice | 50000 | 1 |
+-------+---------+--------+--------+
+-------+---------+--------+--------+
| EmpID | EmpName | Salary | DeptID |
+-------+---------+--------+--------+
| 101 | Alice | 50000 | 1 |
| 103 | Charlie | 45000 | 1 |
| 102 | Bob | 60000 | 2 |
| 105 | Eve | NULL | 2|
+-------+---------+--------+--------+
+----------------+
| TotalEmployees |
+----------------+
| 5|
+----------------+
+------------+
| AvgSalary |
+------------+
| 56250.0000 |
+------------+
+--------+---------------+
| DeptID | EmployeeCount |
+--------+---------------+
| 1| 2|
| 2| 2|
| 3| 1|
+--------+---------------+
RESULT:
Thus the SQL query the database tables with different 'where clause' conditions also implement
aggregate function areexecuted successfully.
EX.NO:4 SIMPLE QUERIES, SUB QUERIES, JOINS
DATE:
Aim:
To query database tables using subqueries and JOIN operations for efficient data retrieval.
Algorithm:
Step 1: Create two tables: Category (parent) and Product (child) with a foreign key constraint.
Step 2: Insert sample data into both tables.
Step 3: Use subqueries: Retrieve data using a nested query and correlated subqueries.
Step 4: Perform JOIN operations
Queries:
Step 1: Create Tables
CREATE TABLE Category (
CategoryID INT PRIMARY KEY,
CategoryName VARCHAR(50) NOT NULL);
Step 3: Subqueries
1. Retrieve Products Belonging to the Most Expensive Category (Subquery in WHERE)
+------------+
| CategoryID |
+------------+
| 1|
| 2|
+------------+
3. Find the Most Expensive Product Using a Subquery
+-----------+-------------+-------+------------+
| ProductID | ProductName | Price | CategoryID |
+-----------+-------------+-------+------------+
| 101 | Laptop | 75000 | 1|
+-----------+-------------+-------+------------+
+-------------+-------+--------------+
| ProductName | Price | CategoryName |
+-------------+-------+--------------+
| Smartphone | 40000 | Electronics |
| Laptop | 75000 | Electronics |
| Sofa | 25000 | Furniture |
| Table | 12000 | Furniture |
| T-shirt | 500 | Clothing |
+-------------+-------+--------------+
RESULT:
Thus the SQL commands to execute simple queries, sub queries and joins areexecuted
successfully.
EX.NO:5 NATURAL, EQUI AND OUTER JOIN
DATE:
Aim :
To write DQL Commands to join, Restrict and Retrieve information from one or more
tables execute it and verify the same.
Algorithm:
SQL> select
employee.empid,employee.name,employee.depid,department.deptname,depa
rtment.location from employee,department where
employee.depid=department.depid;
6 rows selected.
2. Write a query to display the “dinesh” depid and deptname and verify it.
7 rows selected.
2. Write a query to display the name, salary,depid and deptname of all employees.
Makesure that departments without employees are included as well and verify.
RESULT:
Thus the SQL commands to execute natural, equi and outer joins areexecuted successfully.
EX.NO:6 PROCEDURES AND FUNCTIONS
DATE:
AIM:
To implement and execute Procedures in Oracle Database using Procedural Language
concepts.
ALGORITHM:
QUERY:
DELIMITER ;
-- Call the procedure to add a new employee
CALL AddEmployee(1, 'John', 'Doe', 'Sales', 50000.00);
DELIMITER ;
-- Calculate the total price of an item with a price of 100 and a tax rate of 5%
SELECT CalculateTotalPrice(100, 5) AS TotalPrice;
OUTPUT:
RESULT:
Thus the PL/SQL procedures and functions are executed successfully
EX.NO.7 TRANSACTION CONTROL AND DATA CONTROL STATEMENTS
DATE:
AIM:
To implement Transaction Control statements using structured Query Language
ALGORITHM:
1) CREATE A TABLE
create table employeepersonaldetails(id int primary key,name varchar(30) not null,age int not
null,mobilenumber int not null unique);
commit;
Statement executed
savepoint a;
savepoint created
savepoint b;
rollback to a;
RESULT:
Thus Transaction Control and Data Control statements using structured Query Language is
executed successfully
EX.NO:8 TRIGGERS
DATE:
Aim:
To implement PL/SQL triggers and do the operations in database automatically.
Algorithm:
Step 1: Create Logs Table: Table to store log entries for insert, update, and delete actions.
Step 2: Create Main Table: Replace TableName and columns with your actual table name and
columns.
Step 3: Define Triggers: Create triggers for insert, update, and delete operations.
Step 4: Test the Triggers: Perform insert, update, and delete operations to test the triggers.
Step 5: Verify Logs: Retrieve logs to verify that the triggers are working as expected.
Query:
-- 1. Create Tables
CREATE TABLE ProductLogs (
LogID INT PRIMARY KEY AUTO_INCREMENT,
ActionType VARCHAR(10),
ProductID INT,
ProductName VARCHAR(100),
ActionDate TIMESTAMP
);
-- 3. Define Triggers
DELIMITER //
DELIMITER ;
-- 4. Test Triggers
INSERT INTO Products (ProductID, ProductName, CategoryID, SupplierID, Price, StockQuantity)
VALUES
(4, 'Sprite', 1, 2, 1.60, 120);
-- 5. Verify Logs
SELECT * FROM ProductLogs;
OUTPUT:
+-------+------------+-----------+-------------+---------------------+
| LogID | ActionType | ProductID | ProductName | ActionDate |
+-------+------------+-----------+-------------+---------------------+
| 1 | INSERT | 4 | Sprite | 2025-02-06 12:30:00 |
| 2 | UPDATE | 1 | Coca Cola | 2025-02-06 12:31:00 |
| 3 | DELETE | 2 | Pepsi | 2025-02-06 12:32:00 |
+-------+------------+-----------+-------------+---------------------+
RESULT:
Thus the SQL triggers are executed successfully.
EX.NO:9 VIEWS AND INDEX
DATE:
AIM:
To write a DDL command to create views to restrict data access from one or more tables,execute
it and verify the same.
Creating views:
OUTPUT:
View created.
SQL> select * from vw_emp80;
ID NAME SALARY
RESULT:
Thus the PL/SQL program for implementing views and index has been successfully executed.
EX.NO:10 XML SCHEMA
DATE:
AIM:
PROGRAM:
create database acet;
use acet;
CREATE TABLE Students (
ID INT PRIMARY KEY AUTO_INCREMENT,
StudentData TEXT NOT NULL
);
SELECT
ID,
ExtractValue(StudentData, '/student/name') AS Name,
ExtractValue(StudentData, '/student/age') AS Age,
ExtractValue(StudentData, '/student/department') AS Department
FROM Students;
OUTPUT
RESULT:
Thus the XML Database and validate it using XML Schema is executed successfully.
EX.NO:11 NOSQL DATABASE TOOLS
DATE
Aim:
To Create a Document, column and graph based data using NOSQL database tools.
Procedure:
1. Go to the MongoDB download page and download the Community Server version for your
operating system.
2. Follow the installation instructions specific to your OS.
bash
mongod
bash
mongo
1. Insert Document:
use mydatabase;
db.mycollection.insertOne({
name: "John Doe",
age: 30,
address: {
street: "123 Main St",
city: "Example City"
}
});
2. Find Documents:
db.mycollection.updateOne(
{ name: "John Doe" },
{ $set: { age: 31 } }
);
4. Delete Document:
OUTPUT:
{
"acknowledged" : true,
"insertedId" : ObjectId("67a43ba0eab1610044ec0fc4")
}
{ "_id" : ObjectId("67a43ba0eab1610044ec0fc4"), "name" : "John Doe", "age" : 30,
"address" : { "street" : "123 Main St", "city" : "Example City" } }
{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }
{ "acknowledged" : true, "deletedCount" : 1 }
RESULT
Thus the Document, column and graph based data using NOSQL database has been successfully
executed.
EX.NO:12 GUI BASED DATABASE APPLICATIONS
DATE:
AIM:
To implement an online exam registration page with database connectivity using php
ALGORITHM:
STEP 1: Develop a webpage which includes all the fields for registering for an online exam
STEP 2: Develop a PHP page which receives the values that are given as an input in the registration page
STEP 3: Connect the registration page with the PHP page
STEP 4: Receive the values from registration page
STEP 5: Establish a connection with the database
STEP 6: Store the received values in the database
PROGRAM:
Registration.html
<html>
<body bgcolor="lightblue">
<p><center>REGISTRATION FORM</center></p>
<form action="Registration.php" method="post">
<center><pre><b> Student name: <input type="text" name="n" value=" ">
Register Number: <input type="text" name="reg" value=" ">
CGPA: <input type="text" name="cgpa" value=" ">
YEAR: <input type="text" name="y" value=" ">
Branch: <input type="text" name="branch" value=" ">
<input type="submit" name="button" value="SUBMIT">
</b></center></pre>
</body>
</html>
Registration.php
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "Online_exam";
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error)
{
die("Connection failed: " . $conn->connect_error);
}
/*else
echo "Connection established";*/
$Name=$_POST['n'];
$RegNo=$_POST['reg'];
$CGPA=$_POST['cgpa'];
$Year=$_POST['y'];
$Branch=$_POST['branch'];
/*echo $Name;
echo $RegNo;
echo $CGPA;
echo $Year;
echo $Branch;*/
$sql = "CREATE TABLE regDetails(Name varchar(30) not null, RegNo int(15) not null,CGPA float
not null, Year varchar(5) not null, Branch varchar(5) not null)";
RESULT:
Thus the real life database application for exam registration has been successfully executed.
EX.NO:13 DATABASE DESIGN USING ER MODELING, NORMALIZATION AND
DATE : IMPLEMENTATION FOR ANY APPLICATION
AIM:
The aim of this project is to design and implement an Inventory Management System for an EMart
grocery shop using a relational database.
ALGORITHM:
PROCEDURE:
-- 1. Create Tables
CREATE TABLE Category (
CategoryID INT PRIMARY KEY,
CategoryName VARCHAR(100) NOT NULL
);
-- 3. Views
CREATE VIEW CurrentInventory AS
SELECT ProductID, ProductName, StockQuantity
FROM Product
JOIN Inventory ON Product.ProductID = Inventory.ProductID;
-- 4. Triggers
CREATE TRIGGER UpdateInventoryOnOrder
AFTER INSERT ON OrderDetails
FOR EACH ROW
BEGIN
UPDATE Inventory
SET StockQuantity = StockQuantity - NEW.Quantity
WHERE ProductID = NEW.ProductID;
END;
-- 5. Functions
CREATE FUNCTION CalculateTotalOrderAmount(OrderID INT)
RETURNS DECIMAL(10, 2)
BEGIN
DECLARE total DECIMAL(10, 2);
SELECT SUM(Quantity * UnitPrice) INTO total
FROM OrderDetails
WHERE OrderID = OrderID;
RETURN total;
END;
-- 6. Stored Procedures
CREATE PROCEDURE CalculateEMIForGoldLoan()
BEGIN
DECLARE customer_id INT;
DECLARE loan_amount DECIMAL(10, 2);
DECLARE interest_rate DECIMAL(5, 2) DEFAULT 10.0;
DECLARE emi DECIMAL(10, 2);
OPEN cur;
FETCH cur INTO customer_id, loan_amount;
CLOSE cur;
END;
-- Atomicity
START TRANSACTION;
INSERT INTO `Order` (CustomerID, OrderDate, TotalAmount) VALUES (2, 2, '2025-02-06', 2.80);
INSERT INTO OrderDetails (OrderID, ProductID, Quantity, UnitPrice) VALUES (2, 2, 2, 1.40);
COMMIT;
-- Consistency
ALTER TABLE OrderDetails ADD CONSTRAINT fk_order FOREIGN KEY (OrderID) REFERENCES
`Order`(OrderID);
-- Isolation
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;
START TRANSACTION;
-- Perform operations
COMMIT;
-- Durability
START TRANSACTION;
-- Perform operations
COMMIT;
OUTPUT:
+-----------+-------------+--------------+
| ProductID | ProductName | StockQuantity|
+-----------+-------------+--------------+
|1 | Coca Cola | 100 |
|2 | Pepsi | 150 |
|3 | Lays Chips | 200 |
+-----------+-------------+--------------+
+-----------+--------------+-------------+
| ProductID | StockQuantity| LastUpdated |
+-----------+--------------+-------------+
|2 | 148 | 2025-02-06 |
+-----------+--------------+-------------+
+------------------------------+
| CalculateTotalOrderAmount(1) |
+------------------------------+
| 3.00 |
+------------------------------+
RESULT:
Thus normalization has been implemented for student database successfully.
EX.NO:14 EXPLICIT CURSORS
DATE :
AIM:
To demonstrate the use of explicit cursors in SQL for database programming.
ALGORITHM:
STEP 2: For explicit cursor: Declare an explicit cursor for the SELECT statement, open the cursor,
fetch rows from the cursor, display them in a loop, and close the cursor.
QUERY:
-- Create Employees table
CREATE TABLE IF NOT EXISTS Employees (
EmployeeID INT PRIMARY KEY,
FirstName VARCHAR(50),
LastName VARCHAR(50),
Department VARCHAR(50),
Salary DECIMAL(10, 2)
);
-- Verify data
SELECT * FROM Employees;
-- Drop the procedure if it already exists
DROP PROCEDURE IF EXISTS ShowEmployees;
-- Declare cursor
DECLARE employee_cursor CURSOR FOR
SELECT EmployeeID, FirstName, LastName, Department, Salary FROM Employees;
-- Close cursor
CLOSE employee_cursor;
END //
DELIMITER ;
OUTPUT:
RESULT:
Thus explicit cursors in SQL database programming has been implemented successfully.