DBMS Lab Manual (BCS403)
DBMS Lab Manual (BCS403)
INTRODUCTION TO SQL
SQL86 (SQL1) : first by ANSI and ratified by ISO (SQL-87), minor revision on 89 (SQL-89)
SQL92 (SQL2) : major revision
SQL99 (SQL3) : add recursive query, trigger, some OO features, and non-scholar type
SQL2003 : XML, Window functions, and sequences (Not free)Supports all the three
sublanguages of DBMS: DDL, DML, DCL
Supports Aggregate functions, String Manipulation functions, Set theory operations, Date
Manipulation functions, rich set of operators ( IN, BETWEEN, LIKE, IS NULL,EXISTS)
Supports REPORT writing features and Forms for designing GUI based applications
Date and Time: DATE (YYYY-MM-DD) TIME( HH:MM:SS)
Timestamp: DATE + TIME
USER Defined types
CREATE SCHEMA
Specifies a new database schema by giving it a name
CREATE TABLE
Specifies a new base relation by giving it a name, and specifying each of its attributes
CREATE TABLE <table name> ( <Attribute A1> <Data Type D1> [< Constarints>],
<Attribute A2> <Data Type D2> [< Constarints>],
…….
<Attribute An> <Data Type Dn> [< Constarints>],
[<integrity-constraint1>, <integrity-constraint k> ] );
- A constraint NOT NULL may be specified on
an attribute A constraint NOT NULL may be specified
on an attribute
Ex: CREATE TABLE DEPARTMENT (
DNAME VARCHAR(10) NOT NULL,
DNUMBER INTEGER NOT NULL,
MGRSSN CHAR(9), MGRSTARTDATE CHAR(9) );
DROP TABLE
Used to remove a relation (base table) and its definition.
The relation can no longer be used in queries, updates, or any other commands since its
description no longer exists
ALTER TABLE:
Used to add an attribute to/from one of the base relations drop constraint -- The new attribute
will have NULLs in all the tuples of the relation right after the command is executed; hence,
the NOT NULL constraint is not allowed for such an attribute.
Example: ALTER TABLE EMPLOYEE ADD JOB VARCHAR2 (12);
The database users must still enter a value for the new attribute JOB for each EMPLOYEE
tuple. This can be done using the UPDATE command.
DROP A COLUMN (AN ATTRIBUTE)
“333445555”;
LAB EXPERIMENTS
Program 1:
Solution:
Step 1: Creating a user and granting permissions
Login as UN:System
PW: System
Again, Login with new Username and Password, what you have created.
UN: UName
PW: passwd
>desc Employee1;
BEGIN
INSERT INTO Employee1 VALUES(1, 'John Doe', 'Manager', NULL, 50000.00, 1000.00);
INSERT INTO Employee1 VALUES(2, 'Jane Smith', 'Developer', 1, 40000.00, 500.00);
INSERT INTO Employee1 VALUES(3, 'Alice Johnson', 'Analyst', 1, 35000.00, NULL);
END;
Output:
3 rows inserted.
>commit;
Output:
rollback;
Output:
Step 4: Adding primary key and not null constraints
Output:
Output:
ERROR
Violation of PK constraint.
ERROR
Solution:
Step 1:
>Desc Employee2;
Output:
>Desc Employee2;
Output:
Output:
Example: John Doe previous job role was Manager, now changed to Software
Engineer
>UPDATE Employee2
SET JOB = 'S/w Engnr'
WHERE ENAME = 'John Doe';
Output:
Step 5:Rename the column of the Employee table using the alter command.
Syntax:
>Desc Employee2;
Output:
Output:
3. Queries using aggregate functions(COUNT,AVG,MIN,MAX,SUM),Group by,
Orderby. Employee(E_id, E_name, Age, Salary)
1. Create Employee table containing all Records E_id, E_name, Age, Salary.
2. Count number of employee names from employee table
3. Find the Maximum age from employee table.
4. Find the Minimum age from employee table.
5. Find salaries of employee in Ascending Order.
6. Find grouped salaries of employees.
Solution:
Output:
Output:
Step 3. Find the Maximum age:
Output:
Output:
Output:
Step 6. Find grouped salaries of employees:
FROM Employee3
GROUP BY Salary;
Output:
4. Create a row level trigger for the customers table that would fire for INSERT
or UPDATE or DELETE operations performed on the CUSTOMERS table. This
trigger will display the salary difference between the old & new Salary.
CUSTOMERS (ID, NAME, AGE, ADDRESS, SALARY)
Solution:
Step 1: Create and insert values into customers table
-- Create the CUSTOMERS table
Output:
Table created;
Begin
INSERT INTO CUSTOMERS (ID, NAME, AGE, ADDRESS, SALARY) VALUES (1,
'John Doe', 30, '123 Main St', 50000);
INSERT INTO CUSTOMERS (ID, NAME, AGE, ADDRESS, SALARY) VALUES (2,
'Jane Smith', 35, '100 WashingtonDC', 75000);
INSERT INTO CUSTOMERS (ID, NAME, AGE, ADDRESS, SALARY) VALUES (3,
'Alice', 25, '104 Sanfransisco', 75000);
end;
Output:
Statement processed;
>
CREATE OR REPLACE TRIGGER salary_difference_trigger
BEFORE INSERT OR UPDATE OR DELETE ON CUSTOMERS
FOR EACH ROW
DECLARE
old_salary NUMBER;
new_salary NUMBER;
BEGIN
IF INSERTING OR UPDATING THEN
old_salary := NVL(:OLD.SALARY, 0);
new_salary := NVL(:NEW.SALARY, 0);
DBMS_OUTPUT.PUT_LINE('Salary difference: ' || (new_salary - old_salary));
ELSIF DELETING THEN
old_salary := NVL(:OLD.SALARY, 0);
DBMS_OUTPUT.PUT_LINE('Salary before deletion: ' || old_salary);
END IF;
END;
Output:
Trigger created;
>UPDATE CUSTOMERS
SET SALARY=90000
Where ID=2;
Output:
Salary difference: 15000
1 row(s) updated.
>INSERT INTO CUSTOMERS (ID, NAME, AGE, ADDRESS, SALARY) VALUES (4,
'James Bond', 40, '121 california', 58000);
output:
Salary difference: 58000
1 row(s) inserted.
output:
Salary before deletion: 58000
1 row(s) deleted.
Solution:
STEP1: Create table Employee
CREATE TABLE Employee (
E_id INT,
E_name VARCHAR(255),
Age INT,
Salary DECIMAL(10, 2)
);
OUTPUT:
Table created
OUTPUT:
Statement Processed.
CURSOR emp_cursor IS
SELECT E_id, E_name, Age, Salary
FROM Employee;
BEGIN
OPEN emp_cursor;
LOOP
FETCH emp_cursor INTO emp_id, emp_name, emp_age, emp_salary;
EXIT WHEN emp_cursor%NOTFOUND;
DBMS_OUTPUT.PUT_LINE('Employee ID: ' || emp_id || ', Name: ' || emp_name
|| ', Age: ' || emp_age || ', Salary: ' || emp_salary);
END LOOP;
CLOSE emp_cursor;
END;
/
OUTPUT:
Procedure created.
BEGIN
fetch_employee_data;
END;
OUTPUT:
Employee ID: 1, Name: Samarth, Age: 30, Salary: 50000
Employee ID: 2, Name: Ramesh Kumar, Age: 25, Salary: 45000
Employee ID: 3, Name: Seema Banu, Age: 35, Salary: 62000
Employee ID: 4, Name: Dennis Anil, Age: 28, Salary: 52000
Employee ID: 5, Name: Rehman Khan, Age: 32, Salary: 58000
Statement processed.
Program 6
Write a PL/SQL block of code using parameterized Cursor, that will merge the
data available in the newly created table N_RollCall with the data available in
the table O_RollCall. If the data in the first table already exist in the second
table then that data should be skipped.
Solution:
Step 1: First, let’s create the N_RollCall and O_RollCall tables with similar structure:
OUTPUT:
Table created
OUTPUT:
Table created
Begin
INSERT INTO O_RollCall VALUES (1,'Shivanna','08-15-1995');
INSERT INTO O_RollCall VALUES (3,'Cheluva','12-10-1990');
end;
OUTPUT:
Statement Processed.
Select * from O_Rollcall;
Begin
INSERT INTO N_RollCall VALUES(1, 'Shivanna', '08-15-1995');
INSERT INTO N_RollCall VALUES(2, 'Bhadramma','03-22-1998');
INSERT INTO N_RollCall VALUES(3, 'Cheluva', '12-10-1990');
INSERT INTO N_RollCall VALUES(4, 'Devendra', '05-18-2000');
INSERT INTO N_RollCall VALUES(5, 'Eshwar', '09-03-1997');
end;
OUTPUT:
Statement Processed.
OUTPUT:
-- Cursor declaration
CURSOR n_cursor IS
SELECT student_id, student_name, birth_date
FROM N_RollCall;
BEGIN
-- Open the cursor
OPEN n_cursor;
IF v_count = 0 THEN
-- Insert the record into O_RollCall
INSERT INTO O_RollCall (student_id, student_name, birth_date)
VALUES (n_id, n_name, n_birth_date);
END IF;
END LOOP;
OUTPUT:
Procedure created.
Begin
merge_rollcall_data;
End;
OUTPUT:
Statement Processed.
Install an Open Source NoSQL Data base MongoDB & perform basic
CRUD(Create, Read, Update & Delete) operations. Execute MongoDB basic
Queries using CRUD operations.
Solution
Learn how to install and configure MongoDB on Ubuntu step-by-step to power your
application’s database. This comprehensive guide covers everything from installation
and service setup to basic configuration and optional security measures. By following
these instructions, you’ll have MongoDB up and running smoothly on your Ubuntu
machine, ready to support your application’s data needs efficiently and securely.
From a terminal, install gnupg and curl if they are not already available:
--dearmor
Easily explore and manipulate your database with Compass, the GUI for MongoDB.
Intuitive and flexible, Compass provides detailed schema visualizations, real-time
performance metrics, sophisticated querying abilities, and much more.
1. Start MongoDB.
Active: active (running) since Wed 2024-05-08 12:54:07 IST; 1h 56min ago
Docs: https://docs.mongodb.org/manual
Memory: 78.6M
CPU: 31.213s
CGroup: /system.slice/mongod.service
Mongosh
Now you will see a MongoDB shell, where you can issue the queries. If you have
reached here, it means that you have successfully installed MongoDB on your
system.
Lab program
Please refer to the blog below which contains detailed procedure of installing Open
Source NoSQL Data base MongoDB.
1. Start MongoDB.
Mongosh
If you want to use a specific database, switch to that database using the use
command. If the database doesn’t exist, MongoDB will create it implicitly when you
insert data into it:
test>show dbs
—-
switched to db bookDB
bookDB>
bookDB> db.createCollection("ProgrammingBooks")
Output:
{ok:1 }
5. INSERT operations
bookDB> db.ProgrammingBooks.insertMany([
{
year: 2008
},
category: "JavaScript",
year: 2008
},
year: 1994
},
category: "Algorithms",
year: 1990
},
category: "Python",
year: 2015
])
Output:
acknowledged: true,
insertedIds: {
'0': ObjectId('668228977c76971b7bcc8988'),
'1': ObjectId('668228977c76971b7bcc8989'),
'2': ObjectId('668228977c76971b7bcc898a'),
'3': ObjectId('668228977c76971b7bcc898b'),
'4': ObjectId('668228977c76971b7bcc898c')
bookDB> db.ProgrammingBooks.insertOne({
year: 1999
})
Output:
acknowledged: true,
insertedId: ObjectId('668228c57c76971b7bcc898d')
bookDB> db.ProgrammingBooks.find().pretty()
Output:
_id: ObjectId('663eaaebae582498972202df'),
title: 'Clean Code: A Handbook of Agile Software Craftsmanship',
year: 2008
},
_id: ObjectId('663eaaebae582498972202e0'),
category: 'JavaScript',
year: 2008
},
_id: ObjectId('663eaaebae582498972202e1'),
year: 1994
},
_id: ObjectId('663eaaebae582498972202e2'),
category: 'Algorithms',
year: 1990
},
_id: ObjectId('663eaaebae582498972202e3'),
category: 'Python',
year: 2015
},
_id: ObjectId('663eab05ae582498972202e4'),
year: 1999
Output:
_id: ObjectId('663eaaebae582498972202df'),
year: 2008
},
_id: ObjectId('663eaaebae582498972202e0'),
category: 'JavaScript',
year: 2008
},
_id: ObjectId('663eaaebae582498972202e3'),
category: 'Python',
year: 2015
7. Update Operations
bookDB>db.ProgrammingBooks.updateOne(
Output:
acknowledged: true,
insertedId: null,
matchedCount: 1,
modifiedCount: 1,
upsertedCount: 0
[
{
_id: ObjectId('663eaaebae582498972202df'),
year: 2008
},
_id: ObjectId('663eaaebae582498972202e0'),
category: 'JavaScript',
year: 2008
To update multiple books (e.g., update the category of books published before 2010):
bookDB> db.ProgrammingBooks.updateMany(
Output:
{
acknowledged: true,
insertedId: null,
matchedCount: 5,
modifiedCount: 5,
upsertedCount: 0
//verify the update operation by displaying books published before year 2010
Output:
_id: ObjectId('663eaaebae582498972202df'),
year: 2008
},
_id: ObjectId('663eaaebae582498972202e0'),
year: 2008
},
_id: ObjectId('663eaaebae582498972202e1'),
year: 1994
},
_id: ObjectId('663eaaebae582498972202e2'),
year: 1990
},
_id: ObjectId('663eab05ae582498972202e4'),
8. Delete Operations
To delete a specific book from the collection (e.g., delete a book by title):
Output:
You can check whether the specified document is deleted by displaying the contents
of the collection.
bookDB> db.ProgrammingBooks.find().pretty()
Output:
To delete multiple books based on a condition (e.g., delete all books published before
1995):
Output:
bookDB> db.ProgrammingBooks.find().pretty()
Output:
bookDB> db.ProgrammingBooks.deleteMany({})
Output:
bookDB> db.ProgrammingBooks.find().pretty()
To delete a collection named ProgrammingBooks, use the drop() method with the
name of the collection:
ProgrammingBooks
bookDB> db.ProgrammingBooks.drop()
true
bookDB>
After deleting the collection, you can verify that it no longer exists by listing all
collections in the database using the command show collections.