DMBS lab 6 to 20
DMBS lab 6 to 20
AI-DS(B1)
AI-DS(B1)
Practical 1: Introduction to Database
Management System (DBMS)
Aim: To comprehend the fundamental principles of Database Management Systems (DBMS)
and Structured Query Language (SQL), explore the historical development of DBMS, and
gain practical experience in creating and manipulating a basic database using SQL.
1. Introduction to Databases:
A Database is a systematically organized collection of data, typically stored and accessed
electronically from a computer system. Databases are designed to manage and organize
data in a structured format, enabling efficient storage, retrieval, and manipulation of
information. They serve as the backbone for various applications, ranging from customer
relationship management (CRM) systems to educational institutions' student information
systems.
DDL (Data Definition Language): Commands like CREATE, ALTER, and DROP are used to
define and modify the database schema.
DML (Data Manipulation Language): Commands like SELECT, INSERT, UPDATE, and
DELETE allow users to manipulate data within tables.
DCL (Data Control Language): GRANT and REVOKE commands manage user access and
permissions within the database.
AI-DS(B1)
consistently organized and remains easily accessible. Popular DBMS software includes
MySQL, Oracle, Microsoft SQL Server, PostgreSQL, and MongoDB, each offering unique
features to meet various data management needs.
1970s: Edgar F. Codd introduced the relational database model, revolutionizing the field
and laying the groundwork for modern DBMS.
2000s-Present: The rise of NoSQL databases, like MongoDB and Cassandra, addresses the
growing demand for handling large volumes of unstructured data in distributed computing
environments.
Relation: A table within a relational database, consisting of rows (records) and columns
(attributes).
Primary Key: A unique identifier for each record in a table, ensuring data integrity.
Foreign Key: A field in one table that uniquely identifies a row in another table,
establishing a relationship between the two tables.
-- Creating a Table
CREATE TABLE Students (
StudentID INT PRIMARY KEY,
FirstName VARCHAR(50),
LastName VARCHAR(50),
Age INT,
Major VARCHAR(50)
AI-DS(B1)
);
7. Conclusion:
This practical has provided an introductory exploration of databases, DBMS, and SQL, along
with an overview of their historical context. Mastering these foundational concepts is
essential for advancing in the field of database management and understanding the
intricacies of modern data systems.
AI-DS(B1)
Experiment– 6
Aim: To perform various operations on views using SQL on the EMPLOYEE table. This
includes:
Objective:
2. Learn how to create views with constraints using the CHECK OPTION to ensure
data integrity.
4. Demonstrate how to drop views when they are no longer needed, freeing up
database resources.
Given Problem: -
AI-DS(B1)
CREATE VIEW Employee_View AS
FROM EMPLOYEE;
This query creates a simple view called Employee_View that shows the first name, last
name, sex, and salary of employees.
FROM EMPLOYEE
This creates a view called High_Salary_View, showing only employees with a salary
greater than 40,000. The WITH CHECK OPTION ensures that any future changes to the
base table through this view must satisfy the condition SALARY > 40000.
This will return the details of employees (first name, last name, sex, and salary) as per
the defined view.
This query will return details of employees whose salary is greater than 40,000.
3. Dropping Views
AI-DS(B1)
To drop a view when it is no longer needed, use the following syntax:
Dropping Employee_View:
Dropping High_Salary_View:
AI-DS(B1)
Experiment- 7
Aim:
Objective:
Program:
AI-DS(B1)
The FOR loop iterates through values from 1 to 10 and inserts them into the
number_table using the INSERT INTO statement.
After inserting, the COMMIT statement ensures the changes are saved
permanently to the database.
Output: -
SELECT * FROM number_table;
num
AI-DS(B1)
6
10
Program 2
The value of i is: 1
The value of i is: 2
The value of i is: 3
The value of i is: 4
The value of i is: 5
The value of i is: 6
The value of i is: 7
The value of i is: 8
The value of i is: 9
The value of i is: 10
AI-DS(B1)
Experiment- 8
Aim:
To write a PL/SQL program using a cursor to retrieve and display the top five
highest-paid employees from the EMPLOYEE table.
Objective:
1. Understand how to declare and use cursors in PL/SQL to handle result sets.
EmpNo NUMBER,
Name VARCHAR2(50),
Salary NUMBER,
Designation VARCHAR2(50),
DeptID NUMBER
);
AI-DS(B1)
INSERT INTO EMPLOYEE (EmpNo, Name, Salary, Designation, DeptID)
DECLARE
CURSOR emp_cursor IS
FROM EMPLOYEE
emp_record emp_cursor%ROWTYPE;
BEGIN
OPEN emp_cursor;
AI-DS(B1)
DBMS_OUTPUT.PUT_LINE('------------------------------------------------------------');
LOOP
emp_record.DeptID);
END LOOP;
CLOSE emp_cursor;
END;
Output: -
------------------------------------------------------------
AI-DS(B1)
Experiment- 9
Aim:
Objective:
3. Use the necessary Java Database Connectivity (JDBC) for interacting with a
database.
A PL/SQL block will deduct a specified amount from the account balance if
sufficient funds exist.
Steps:
AI-DS(B1)
AccountNo NUMBER PRIMARY KEY,
Name VARCHAR2(50),
Balance NUMBER
);
COMMIT;
This PL/SQL block will debit an account and check for sufficient balance before
deducting.
DECLARE
v_balance BANK_ACCOUNT.Balance%TYPE;
BEGIN
UPDATE BANK_ACCOUNT
AI-DS(B1)
DBMS_OUTPUT.PUT_LINE('Debit transaction successful. Amount deducted:
1000');
ELSE
DBMS_OUTPUT.PUT_LINE('Insufficient balance.');
END IF;
COMMIT;
END;
Here, the Java program connects to the database and executes the PL/SQL block to
perform the debit transaction.
import java.sql.*;
try {
// Load and register Oracle JDBC Driver (or any other DB driver)
Class.forName("oracle.jdbc.driver.OracleDriver");
AI-DS(B1)
conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe",
"username", "password");
+ "BEGIN "
+ "END; }";
stmt = conn.prepareCall(plsqlBlock);
stmt.execute();
AI-DS(B1)
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
se.printStackTrace();
Output:
When the program is run, if AccountNo = 1001 has sufficient balance, the output
would be:
Insufficient balance.
AI-DS(B1)
Experiment- 10
Aim:
Objective:
ID NAME
1 abc
2 def
PL/SQL Procedure:
BEGIN
COMMIT;
-- Output message
END;
AI-DS(B1)
Execution:
BEGIN
END;
Output:
When the procedure is executed with the input i = 5, it inserts the tuple (5, 'xxx')
into the TEST_TABLE. The output will be:
Output:
ID NAME
1 abc
2 def
5 xxx
AI-DS(B1)
Experiment 11: Hello World Program
Aim:
To write a PL/SQL block to print "Hello World".
Objective:
To demonstrate the basic structure of a PL/SQL program and how to output a
simple message.
Code:
BEGIN
DBMS_OUTPUT.PUT_LINE('Hello World');
END;
Output:
Hello World
AI-DS(B1)
Experiment 12: Program to Add Two
Numbers
Aim:
To write a PL/SQL program to add two numbers and display the result.
Objective:
To demonstrate how to perform arithmetic operations in PL/SQL.
Code:
DECLARE
sum NUMBER;
BEGIN
END;
Output:
Sum = 30
AI-DS(B1)
Experiment 13: Program to Check Even
or Odd
Aim:
To write a PL/SQL program to check whether a number is even or odd.
Objective:
To demonstrate conditional statements in PL/SQL.
Code:
DECLARE
num NUMBER := 5;
BEGIN
IF MOD(num, 2) = 0 THEN
ELSE
END IF;
END;
Output:
5 is Odd
AI-DS(B1)
Experiment 14: Program to Find
Factorial of a Number
Aim:
To write a PL/SQL program to find the factorial of a given number.
Objective:
To demonstrate the use of loops in PL/SQL.
Code:
DECLARE
num NUMBER := 5;
fact NUMBER := 1;
BEGIN
fact := fact * i;
END LOOP;
END;
Output:
Factorial = 120
AI-DS(B1)
Experiment 15: Program to Reverse a
String
Aim:
To write a PL/SQL program to reverse a given string.
Objective:
To manipulate strings using PL/SQL.
Code:
DECLARE
BEGIN
END LOOP;
END;
Output:
AI-DS(B1)
Experiment 16: Display First 10 Natural
Numbers Using a Loop
Aim:
To write a PL/SQL program to display the first 10 natural numbers using a loop.
Objective:
To demonstrate loops in PL/SQL.
Code:
BEGIN
DBMS_OUTPUT.PUT_LINE(i);
END LOOP;
END;
Output:
10
AI-DS(B1)
Experiment 17: Program to Find
Maximum of Two Numbers
Aim:
To write a PL/SQL program to find the maximum of two numbers.
Objective:
To demonstrate conditional comparisons in PL/SQL.
Code:
DECLARE
BEGIN
ELSE
END IF;
END;
Output:
Max = 20
AI-DS(B1)
Experiment 18: Check if a String is a
Palindrome
Aim:
To write a PL/SQL program to check if a string is a palindrome.
Objective:
To manipulate and compare strings in PL/SQL.
Code:
DECLARE
BEGIN
END LOOP;
ELSE
END IF;
END;
Output:
MADAM is a palindrome
AI-DS(B1)
Experiment 19: Program to Calculate
Fibonacci Series
Aim:
To write a PL/SQL program to generate the Fibonacci series up to a given number of
terms.
Objective:
To demonstrate recursion and sequence generation in PL/SQL.
Code:
DECLARE
num1 NUMBER := 0;
num2 NUMBER := 1;
num3 NUMBER;
BEGIN
DBMS_OUTPUT.PUT_LINE(num1);
DBMS_OUTPUT.PUT_LINE(num2);
DBMS_OUTPUT.PUT_LINE(num3);
num1 := num2;
num2 := num3;
END LOOP;
END;
AI-DS(B1)
Output:
13
21
34
AI-DS(B1)
Experiment 20: Check if a Number is an
Armstrong Number
Aim:
To write a PL/SQL program to check if a given number is an Armstrong number.
Objective:
To implement mathematical logic in PL/SQL to check if the sum of the cubes of the
digits of a number is equal to the number itself.
Code:
DECLARE
temp NUMBER;
digit NUMBER;
sum NUMBER := 0;
BEGIN
temp := num;
END LOOP;
ELSE
AI-DS(B1)
DBMS_OUTPUT.PUT_LINE(num || ' is not an Armstrong number');
END IF;
END;
Output:
AI-DS(B1)
AI-DS(B1)
AI-DS(B1)