DBMSTushar
DBMSTushar
EXPERIMENT 1
Aim: Study and practice various database management systems like MySQL/Oracle/PostgreSQL/SQL
Server and others.
1. MySQL
Type: Open-source RDBMS
Developer: Oracle Corporation
Language: SQL
Popular Use Cases: Web applications (e.g., WordPress, Joomla), LAMP stack
Key Features:
2. Oracle Database
Key Features:
3. PostgreSQL
Key Features:
Developer: Microsoft
Popular Use Cases: Enterprise applications, business intelligence, .NET apps
Key Features:
5. SQLite
Key Features:
6. MariaDB
Type: Open-source RDBMS (MySQL fork)
Developer: MariaDB Corporation
Popular Use Cases: Replaces MySQL in many web applications
Key Features:
Backward-compatible with MySQL
Enhanced security, performance, and storage engines (Aria, ColumnStore)
Open-source development community
Active development with frequent updates
7. IBM Db2
Key Features:
High performance for OLTP and OLAP
Advanced data compression
Integrated with AI and machine learning
Support for JSON, XML, and spatial data
High availability and disaster recovery option
EXPERIMENT 2
Aim: Implement simple queries of DDL and DML.
CODE:
DDL Queries (Data Definition Language)
DROP DATABASE IF EXISTS Schoolcd;
CREATE DATABASE Schoolcd;
USE Schoolcd;
CREATE TABLE Students (
StudentID INT PRIMARY KEY,
Name VARCHAR(100),
Age INT,
Course VARCHAR(50),
Email VARCHAR(100)
);
EXPLANATION:
1. DROP DATABASE IF EXISTS Schoolcd;
Removes the Schoolcd database if it already exists to avoid conflicts.
OUTPUT:
EXPERIMENT 3
Aim: Implement basic queries to Create, Insert, Update, Delete and Select Statements for two different
scenarios (For instance: Bank, College etc.)
CODE: (A) BANKDB
DROP DATABASE IF EXISTS BankDB;
CREATE DATABASE BankDB;
USE BankDB;
UPDATE Accounts
SET Balance = 6000.00
WHERE AccountID = 101;
OUTPUT:
EXPERIMENT 3
Aim: Implement basic queries to Create, Insert, Update, Delete and Select Statements for two different
scenarios (For instance: Bank, College etc.)
UPDATE Students
SET Age = 21
WHERE StudentID = 1;
EXPLANATION:
OUTPUT:
EXPERIMENT 4
Aim: Implement queries including various functions- mathematical, string, date etc.
CODE:
DROP DATABASE IF EXISTS FunctionDB;
CREATE DATABASE FunctionDB;
USE FunctionDB;
OUTPUT:
EXPERIMENT 5
Aim: Implement queries including Sorting, Grouping and Subqueries- like any, all, exists, not exists
CODE:
DROP DATABASE IF EXISTS SalesDB;
CREATE DATABASE SalesDB;
USE SalesDB;
Explanation:
OUTPUT:
EXPERIMENT 6
Aim: Implement queries including various Set operations (Union, Intersection, Except etc.).
CODE:
DROP DATABASE IF EXISTS CollegeDB;
CREATE DATABASE CollegeDB;
USE CollegeDB;
Explanation:
OUTPUT:
EXPERIMENT 7
Explanation:
OUTPUT:
EXPERIMENT 8
Aim: Write a PL/SQL program using FOR loop to insert ten rows into a database table.
CODE:
DROP PROCEDURE IF EXISTS InsertEmployees;
DELIMITER $$
CREATE PROCEDURE InsertEmployees()
BEGIN
DECLARE i INT DEFAULT 1;
WHILE i <= 10 DO
INSERT INTO Employee (EmployeeID, EmployeeName)
VALUES (i, CONCAT('Employee ', i));
SET i = i + 1;
END WHILE;
END $$
DELIMITER ;
CALL InsertEmployees();
SELECT * FROM Employee;
Explanation:
OUTPUT:
EXPERIMENT 9
Aim: Given the table EMPLOYEE (Emp No, Name, Salary, Designation, DeptID), write a cursor to select the
five highest-paid employees from the table.
CODE:
Step 1: Create the Database
CREATE DATABASE IF NOT EXISTS CompanyDB;
DELIMITER $$
OPEN emp_cursor;
-- Loop to fetch data from cursor and insert into temporary table
read_loop: LOOP
FETCH emp_cursor INTO empno, empname, empsalary, empdesignation, empdeptid;
IF done THEN
LEAVE read_loop;
END IF;
CLOSE emp_cursor;
DELIMITER ;
Explanation:
1. Database Creation:
The script begins by creating a database CompanyDB (if it doesn't exist) and selects it for use.
2. Table Creation:
The EMPLOYEE table is created with columns: EmpNo (Employee ID), Name, Salary, Designation,
and DeptID.
3. Inserting Sample Data:
10 sample employee records are inserted into the EMPLOYEE table, including their names, salaries,
designations, and department IDs.
4. Procedure Creation:
a. The existing GetTop5Employees procedure is dropped if it exists.
OUTPUT:
EXPERIMENT 10
Aim: Illustrate how you can embed PL/SQL in a high-level host language such as C/Java And demonstrates
how a banking debit transaction might be done.
PL/SQL can be embedded in high-level host languages like C or Java using an OCI (Oracle Call Interface) or
JDBC (Java Database Connectivity). Here, I'll demonstrate how you can embed PL/SQL in Java to simulate
a banking debit transaction.
First, we'll create a PL/SQL block that handles a simple debit transaction. It will check the account balance,
subtract the debit amount, and update the balance.
DECLARE
v_account_balance NUMBER;
BEGIN
-- Fetch account balance based on account ID
SELECT balance INTO v_account_balance
FROM accounts
WHERE account_id = :account_id;
COMMIT;
DBMS_OUTPUT.PUT_LINE('Transaction Successful');
ELSE
DBMS_OUTPUT.PUT_LINE('Insufficient Balance');
END IF;
END;
1. Explanation:
a. :account_id and :debit_amount are bind variables.
b. It fetches the balance for the account with the given account_id.
c. If the balance is sufficient, the debit is processed; otherwise, an error message is printed.
In Java, PL/SQL can be executed using JDBC by calling the PL/SQL block. The following is a simple example:
Java Code:
import java.sql.*;
try {
// Step 1: Establish connection to Oracle DB
Connection conn = DriverManager.getConnection(jdbcURL, username, password);
} catch (SQLException e) {
e.printStackTrace();
}
}
}
1. JDBC Connection:
o Establish a connection to the Oracle database using JDBC (DriverManager.getConnection).
2. PL/SQL Block:
o The PL/SQL block (as a string) is embedded inside the Java program. It is designed to check
the account balance, perform the debit if sufficient funds are available, and print a success or
failure message.
3. Binding Parameters:
o The values for account_id and debit_amount are passed as input parameters using stmt.setInt()
and stmt.setDouble().
4. CallableStatement:
o CallableStatement is used to execute the PL/SQL block in the database. The execute() method
runs the PL/SQL block.
5. Database Output:
o The PL/SQL DBMS_OUTPUT.PUT_LINE function prints the transaction result. In a real-world
application, you might capture these outputs in Java and display them to the user.
6. Transaction Management:
o The PL/SQL block performs the debit by updating the balance. If the balance is sufficient, it is
debited and committed. If not, a message indicating insufficient balance is displayed.
1. JDBC Driver:
o Add the Oracle JDBC driver (ojdbc8.jar) to your classpath.
2. Database Configuration:
o Ensure the Oracle database is running, and the accounts table is created with appropriate data:
PL/SQL Block: Handles the debit transaction by checking the account balance and updating it if
sufficient funds are available.
Java Code: Uses JDBC to embed the PL/SQL block, pass parameters (account ID and debit amount),
and execute it on the Oracle database.
Database Operations: The PL/SQL block performs a SELECT to fetch the balance, checks if the debit
can be processed, and uses UPDATE to subtract the debit amount from the balance.