Cs3481-Dbms Lab Manual
Cs3481-Dbms Lab Manual
LABORATORY MANUAL
REGULATION : R2021
1
LIST OF EXPERIMENTS:
1. Create a database table, add constraints (primary key, unique, check, Not null),
insert rows, update and delete rows using SQL DDL and DML commands.
2. Create a set of tables, add foreign key constraints and incorporate referential
integrity.
3. Query the database tables using different ‘where’ clause conditions and also
implement aggregate functions.
4. Query the database tables and explore sub queries and simple join operations.
5. Query the database tables and explore natural, equi and outer joins.
6. Write user defined functions and stored procedures in SQL.
7. Execute complex transactions and realize DCL and TCL commands.
8. Write SQL Triggers for insert, delete, and update operations in a database table.
9. Create View and index for database tables with a large number of records.
10. Create an XML database and validate it using XML schema.
11. Create Document, column and graph-based data using NOSQL database tools.
12. Develop a simple GUI based database application and incorporate all the above-
mentioned features
13. Case Study using any of the real life database applications from the following list
a) Inventory Management for a EMart Grocery Shop
b) Society Financial Management
c) Cop Friendly App – Eseva
d) Property Management – eMall
e) Star Small and Medium Banking and Finance
Build Entity Model diagram. The diagram should align with the business and
functional goals stated in the application.
Apply Normalization rules in designing the tables in scope.
Prepared applicable views, triggers (for auditing purposes), functions for enabling
enterprise grade features.
Build PL SQL / Stored Procedures for Complex Functionalities, ex EOD Batch Processing
for calculating the EMI for Gold Loan for each eligible Customer.
● Ability to showcase ACID Properties with sample queries with
appropriate settings
2
CREATE A DATABASE TABLE, ADD CONSTRAINTS (PRIMARY
EXP NO: 1 KEY, UNIQUE, CHECK, NOT NULL), INSERT ROWS, UPDATE
AND DELETE ROWS USING SQL DDL AND DML COMMANDS
AIM:
To Create a database table, add constraints (primary key, unique, check, not null),
insert rows, update and delete rows using SQL DDL and DML commands.
ALGORITHM:
Step 1: Creating a Database Table with Constraints.
Step 2: Inserting Rows into the Table.
Step 3: Updating Rows in the Table
Step 4: Deleting Rows from the Table
Step 5: Use select command to display the output.
PROGRAM:
CREATE DATABASE mydb; SHOW DATABASES;
USE mydb;
CREATE TABLE users (id INT PRIMARY KEY, username VARCHAR (50) NOT NULL UNIQUE,
password VARCHAR (50) NOT NULL, email VARCHAR (50) CHECK (email LIKE '%@%. %'));
INSERT INTO users (id, username, password, email) VALUES (1, 'john_doe', 'password123',
[email protected]'), (2, 'jane_doe', 'password456', '[email protected]'), (3,
'bob_smith', 'password789', '[email protected]');
3
OUTPUT:
john_doe@exampl
1 john_doe password123
e.com
jane_doe@example
2 jane_doe password456
.com
bob_smith@examp
3 bob_smith password789
le.com
john_doe@exampl
1 john_doe new_password
e.com
jane_doe@example
2 jane_doe password456
.com
bob_smith@examp
3 bob_smith password789
le.com
RESULT:
Thus, to Create a database table, add constraints (primary key, unique, check, not
null), insert rows, update and delete rows using SQL DDL and DML commands is
successfully executed and verified.
4
CREATE A SET OF TABLES, ADD FOREIGN KEY
EXP NO: 2 CONSTRAINTS AND INCORPORATE REFERENTIAL
INTEGRITY
AIM:
To Create a set of tables, add foreign key constraints and incorporate referential
integrity.
ALGORITHM:
Step 1: Determine the relationships between the tables.
Step 2: Create the tables, add primary keys and add foreign keys.
Step 3: Create the relationships.
Step 4: Enforce referential integrity, Test the database and incorporate referential
integrity
PROGRAM:
SHOW DATABASES;
USE mydatabase;
CREATE TABLE users2 (user_id INT PRIMARY KEY, username VARCHAR(50) NOT NULL, email
VARCHAR(50) NOT NULL, password VARCHAR(50) NOT NULL );
CREATE TABLE posts1 ( post_id INT PRIMARY KEY, user_id INT NOT NULL, title VARCHAR(100)
NOT NULL, content TEXT NOT NULL, created_at TIMESTAMP NOT NULL DEFAULT
CURRENT_TIMESTAMP, FOREIGN KEY (user_id) REFERENCES users2(user_id) ON DELETE
CASCADE );
CREATE TABLE comments1 ( omment_id INT PRIMARY KEY, user_id INT NOT NULL, post_id
INT NOT NULL, content TEXT NOT NULL, created_at TIMESTAMP NOT NULL DEFAULT
CURRENT_TIMESTAMP, FOREIGN KEY (post_id) REFERENCES posts1(post_id) ON DELETE
CASCADE );
INSERT INTO posts1 (post_id, user_id,title,content) VALUES (1,1,'My First Post','This is the
Content Of My First Post.');
INSERT INTO comments1 (comment_id, user_id,post_id,content) VALUES (1,1,1,'Great
Post!');
SELECT * FROM users2;
SELECT * FROM posts1;
SELECT * FROM comments1;
DELETE FROM posts1 WHERE post_id = 1;
SELECT * FROM posts1;
5
OUTPUT:
RESULT:
Thus, to create a set of tables, add foreign key constraints and incorporate
referential integrity is successfully executed and verified.
6
QUERY THE DATABASE TABLES USING DIFFERENT
EXP NO: 3 ‘WHERE’ CLAUSE CONDITIONS AND ALSO IMPLEMENT
AGGREGATE FUNCTIONS
AIM:
To query the database tables using different ‘where’ clause conditions and also
implement aggregate functions.
ALGORITHM:
Step 1: Connect to the database.
Step 2: Select the table.
Step 3: Specify the "where" clause conditions.
Step 4: Specify the aggregate function.
Step 5: Execute the SQL statement and retrieve the results.
PROGRAM:
SHOW DATABASES;
use sys;
CREATE TABLE sales (SalesID int primary key, Product varchar (50) not null, Date date not
null, Quantity int not null, Price decimal (10,2) not null);
7
OUTPUT:
RESULT:
Thus, to query the database tables using different ‘where’ clause conditions and also
implement aggregate functions is successfully executed and verified.
8
QUERY THE DATABASE TABLES AND EXPLORE SUB
EXP NO: 4
QUERIES AND SIMPLE JOIN OPERATIONS.
AIM:
To query the database tables and explore sub queries and simple join operations.
ALGORITHM:
Step 1: Connect to the database and select the database.
Step 2: Understand the database schema.
Step 3: Write a simple SELECT statement.
Step 4: Explore subqueries and use simple join operations.
Step 5: Refine your queries, Test and execute your queries.
PROGRAM:
CREATE TABLE orders (order_id int primary key, order_date date, total_amount decimal
(10,2), customer_id int, foreign key (customer_id) references customers1(customer_id));
9
OUTPUT:
RESULT
Thus, to query the database tables and explore sub queries and simple join
operations is successfully executed and verified.
10
QUERY THE DATABASE TABLES AND EXPLORE NATURAL,
EXP NO: 5
EQUI AND OUTER JOINS
AIM:
To query the database tables and explore natural, equi and outer joins.
ALGORITHM:
Step 1: Connect to the database and choose a database.
Step 2: Choose a table.
Step 3: Write a SELECT statement.
Step 4: Explore natural joins, explore equi joins and explore outer joins.
PROGRAM:
CREATE TABLE customers2( customer_id int primary key, name varchar(50), city
varchar(50));
CREATE TABLE orders1 ( order_id int primary key, customer_id int, city varchar(50), amount
decimal(10,2), foreign key (customer_id) references customers2(customer_id) );
11
OUTPUT
RESULT
Thus, To Query the database tables and explore natural, equi and outer joins is
successfully executed and verified.
12
WRITE USER DEFINED FUNCTIONS AND STORED
EXP NO: 6 PROCEDURES IN SQL
AIM:
ALGORITHM:
PROGRAM:
DELIMITER //
SELECT add_numbers1(2,3);
CREATE TABLE customers (customer_id int primary key, customer_name varchar (50),
country varchar (50));
DELIMITER $$
CREATE PROCEDURE get_customer_by_country(IN country_name VARCHAR(50))
BEGIN
SELECT * FROM customers WHERE country = country_name;
END$$
CALL get_customer_by_country('USA');
13
OUTPUT:
RESULT:
Thus, To Write user defined functions and stored procedures in SQL is successfully
executed and verified.
14
EXECUTE COMPLEX TRANSACTIONS AND REALIZE DCL AND
EXP NO: 7
TCL COMMANDS
AIM:
ALGORITHM:
Step 1: Connect to the database.
Step 2: Define the transaction, begin the transaction and execute the transaction.
Step 3: Commit or rollback the transaction.
Step 4: Execute DCL and TCL commands.
PROGRAM:
show databases;
use mysql;
CREATE TABLE employees (id int auto_increment primary key, first_name varchar(50) not
null, last_name varchar(50) not null, email varchar(100) not null, phone varchar(20),
hire_date date not null, job_title varchar(50) not null, department varchar(50) not null,
salary decimal(10,2) not null );
START TRANSACTION;
UPDATE employees set salary = 50000 where id = 3; insert into employees values
(4,"John","Doe","[email protected]","JD","2023-05-05","Software Engineer","CSE",60000);
COMMIT;
15
OUTPUT:
RESULT:
Thus, To Execute complex transactions and realize DCL and TCL commands is
successfully executed and verified.
16
WRITE SQL TRIGGERS FOR INSERT, DELETE, AND UPDATE
EXP NO: 8 OPERATIONS IN A DATABASE TABLE
AIM:
To Write SQL Triggers for insert, delete, and update operations in a database table.
ALGORITHM:
Step 1: Decide on the trigger event and timing.
Step 2: Choose the table.
Step 3: Determine the action
Step 4: Create the trigger and write the trigger actions.
Step 5: Test the trigger and modify the trigger as needed.
PROGRAM:
CREATE TABLE users (id int primary key not null, age int);
delimiter $$
CREATE TRIGGER insert_trigger1 after insert on users
for each row
begin
INSERT INTO audit_table (inserted_id, inserted_data) values (NEW.id,NEW.age);
end;
delimiter $$
CREATE TRIGGER delete_trigger1 after delete
on users
for each row
begin
INSERT INTO audit_table (deleted_id, deleted_data) values (OLD.id,OLD.age);
end;
delimiter $$
CREATE TRIGGER update_trigger1 after update
on users
for each row
begin
INSERT INTO audit_table (updated_id, old_data,new_data) values (NEW.id,OLD.age,NEW.age);
end;
17
OUTPUT:
Insert
Trigger: create trigger insert_trigger1 after insert on 0 0.093
users for each row begin insert into audit_table row(s) sec
(inserted_id, inserted_data) values affecte
(NEW.id,NEW.age); end; d
Delete Trigger:
Update Trigger:
RESULT:
Thus, to write SQL Triggers for insert, delete, and update operations in a
database table is successfully executed and verified.
18
CREATE VIEW AND INDEX FOR DATABASE TABLES WITH A
EXP NO: 9
LARGE NUMBER OF RECORDS
AIM:
To Create View and index for database tables with a large number of records.
ALGORITHM:
Step 1: Create a table and insert values into it.
Step 2: Create a view.
Step 3: Create an index.
Step 4: Use select statement to display the result.
PROGRAM:
CREATE TABLE orders5( order_id int primary key, customer_id int not null, order_date date
not null, order_total decimal(10,2) not null, ship_address varchar(255), ship_city
varchar(50), ship_country varchar(50));
19
OUTPUT:
RESULT:
Thus, to Create View and index for database tables with a large number of records is
successfully executed and verified.
20
CREATE AN XML DATABASE AND VALIDATE IT USING XML
EXP NO: 10
SCHEMA
AIM:
ALGORITHM:
PROGRAM:
XML Database:
<?xml version="1.0" encoding="UTF-8"?>
<library>
<book id="001">
<title>The Catcher in the Rye</title>
<author>J.D. Salinger</author>
<year>1951</year>
</book>
<book id="002">
<title>To Kill a Mockingbird</title>
<author>Harper Lee</author>
<year>1960</year>
</book>
<book id="003">
<title>1984</title>
<author>George Orwell</author>
<year>1949</year>
</book>
</library>
21
XSD schema:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="library">
<xs:complexType>
<xs:sequence>
<xs:element name="book" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="title" type="xs:string"/>
<xs:element name="author" type="xs:string"/>
<xs:element name="year" type="xs:gYear"/>
</xs:sequence>
<xs:attribute name="id" type="xs:string" use="required"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
OUTPUT:
RESULT:
Thus, To Create an XML database and validate it using XML schema is successfully
executed and verified.
22
CREATE DOCUMENT, COLUMN AND GRAPH- BASED DATA
EXP NO: 11 USING NOSQL DATABASE
AIM:
To Create Document, column and graph-based data using NOSQL database tools.
ALGORITHM:
Step 1: Create document and insert it in mongodb documentation and execute it.
Step 2: Create column by using aggregation.
Step 3: Graph is visualised using visual tree.
PROGRAM:
DOCUMENT:
{
"name": "John
Doe", "age": 35,
"email":
"[email protected]",
"address": {
"street": "123
Main St", "city":
"Anytown",
"state": "CA",
"zip": "12345"
}
}
COLUMN:
{
'name':1,
'age':1
}
23
OUTPUT:
DOCUMENT:
_id:
ObjectID('64561efc6ee99dd7cf4dfd
04') name: "John Doe",
age: 35,
email:
"[email protected]"
, address: Object
street: "123
Main St", city:
"Anytown",
state: "CA",
zip: "12345"
COLUMN:
_id:ObjectID('64561efc6ee99dd7cf4dfd 04') name:"John Doe", age:35
GRAPH:
VISUAL TREE:
{
"stage": "COLLSCAN",
"nReturned": 1,
"executionTimeMillisEstimate": 0,
"works": 3,
"advanced": 1,
"needTime": 1,
"needYield": 0,
"saveState": 0,
"restoreState": 0,
"isEOF": 1,
"direction":
"forward",
"docsExamined":
1
}
RESULT:
Thus, To Create Document, column and graph-based data using NOSQL database tools
is successfully executed and verified.
24
DEVELOP A SIMPLE GUI BASED DATABASE APPLICATION
EXP NO: 12 AND INCORPORATE ALL THE ABOVE-MENTIONED
FEATURES
AIM:
To Develop a simple GUI based database application and incorporate all the above-
mentioned features.
ALGORITHM:
Step 1: Install required packages.
Step 2: Create a GUI.
Step 3: Connect to MySQL.
Step 4: Perform CRUD operations.
Step 5: Display data in the GUI.
PROGRAM:
SQL CODE
show databases;
use mysql;
CREATE TABLE customers (id INT(11) NOT NULL AUTO_INCREMENT, name VARCHAR(255)
NOT NULL, email VARCHAR(255) NOT NULL, PRIMARY KEY (id) );
PYTHON CODE:
import tkinter as tk
import mysql.connector
25
entry2.grid(row=1, column=1)
26
OUTPUT:
RESULT:
Thus, To Develop a simple GUI based database application and incorporate all the
above-mentioned features is successfully executed and verified.
27
CASE STUDY USING ANY OF THE REAL-LIFE DATABASE
APPLICATIONS FROM THE FOLLOWING LIST.
EXP NO: 13
A) INVENTORY MANAGEMENT FOR A EMART GROCERY
SHOP
ER-DIAGRAM:
+ +
| Suppliers |
+ +
| supplier_id (PK) |
| supplier_name |
| supplier_phone |
| supplier_email |
+ +
|
|
|
+---------------------+ +
| Inventory |
+---------------------+ +
| item_id (PK) | supplier_id (FK)|
| item_name | item_quantity |
| item_price| item_category |
| item_description| item_location |
+---------------------+ +
|
|
|
+---------------------+ +
| Orders |
+---------------------+ +
| order_id (PK) | customer_id (FK)|
| order_date | item_id (FK) |
| order_quantity | order_status |
| order_total | order_type |
+---------------------+ +
|
|
|
+ +
| Customers |
+ +
| customer_id (PK) |
| customer_name |
| customer_phone |
| customer_email |
+ +
28
Normalization Rules:
1. First Normal Form (1NF):
All table column must have atomic value. (NO Multivalued Attribute / No
repeating group)
Each table must have a primary key that uniquely identifies each row.
2. Second Normal Form (2NF):
Must satisfy the requirement of 1NF.
No Partial dependentices. (Non-key attribute depend on part of primary key
3. Third Normal Form (3NF):
Must satisfy the requirement of 2NF.
No transitive dependentices. (Non-key attribute depend on other non-key
attribute)
Views:
A view can be created to show the current stock level of each item in the inventory table.
Triggers:
A trigger can be created to log all updates to the orders table for auditing purposes.
Functions:
A function can be created to calculate the total value of all the items in the inventory
table.
RESULT:
Thus, case study of Inventory Management for a EMart Grocery Shop is
successfully done.
30