Dbms Record 2021 Even
Dbms Record 2021 Even
COLLEGE OF ENGINEERING
Coimbatore-641 105
[Approved by AICTE, New Delhi || Affiliated to Anna University, Chennai]
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.
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.
11. Create Document, column and graph based data using NOSQL database tools.
12. Develop a simple GUI based database application and incorporate all the abovementioned features
13. Case Study using any of the real life database applications from the following list.
PREPARED BY
Mrs.R.Ramya/AP
4
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]'),
5
OUTPUT
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.
6
DATE:
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,
7
);
CREATE TABLE comments1 (
comment_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
);
OUTPUT
RESULT
Thus, to create a set of tables, add foreign key constraints and incorporate
referential integrity is successfully executed and verified.
10
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
);
insert into
sales(SalesID,Product,Date,Quantity,Price)
values(1,'Widget','2023-05-03',10,19.99);
insert into
sales(SalesID,Product,Date,Quantity,Price)
11
values(2,'Gizmo','2023-05-03',5,12.99);
12
insert into
sales(SalesID,Product,Date,Quantity,Price)
values(3,'Thing','2023-05-02',2,7.99);
insert into
sales(SalesID,Product,Date,Quantity,Price)
values(4,'Widget','2023-05-01',10,19.99);
select * from sales where Product = 'Widget';
select * from sales where Quantity >= 10;
select * from sales where Date = '2023-05-03';
select Product, COUNT(*) AS num_sales from sales group by Product;
select sum(Quantity * Price) as total_revenue, avg(Price) as avg_price from sales;
select sum(Quantity * Price) as total_revenue from sales where Product = 'Gizmo';
select max(Price) as max_price, min(Price) as min_price from sales;
select avg(Quantity) as avg_quantity from sales where date >= '2023-05-01';
OUTPUT
RESULT
Thus, to query the database tables using different ‘where’ clause conditions and also
implement aggregate functions is successfully executed and verified.
13
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 customers1(
customer_id int primary
key, first_name varchar(50),
last_name varchar(50),
email varchar(100),
country varchar(50)
);
select customers1.first_name,orders.order_date,orders.total_amount
from customers1
left join orders
on customers1.customer_id = orders.customer_id;
select customers1.first_name,orders.order_date,orders.total_amount
from customers1
right join orders
on customers1.customer_id = orders.customer_id;
OUTPUT
16
RESULT
Thus, to query the database tables and explore sub queries and simple join operations
is successfully executed and verified.
17
DATE:
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)
);
select * from
customers2 natural join
orders1;
select * from
customers2 join orders1
on customers2.customer_id=orders1.customer_id;
select * from
customers2 left outer
join orders1
on customers2.customer_id=orders1.customer_id;
select * from
customers2 right outer
join orders1
on customers2.customer_id=orders1.customer_id;
19
OUTPUT
RESULT
Thus, To Query the database tables and explore natural, equi and outer joins
is successfully executed and verified.
20
DATE:
AI
M
To write user defined functions and stored procedures in SQL.
ALGORITHM
Step 1: Create a user-defined
function. Step 2: Create a stored
procedure.
Step 3: Test your user-defined function and stored procedure.
PROGRAM
DELIMITER //
CREATE FUNCTION add_numbers1(num1 INT, num2
INT) RETURNS INT DETERMINISTIC BEGIN
DECLARE result INT; SET result =
num1+num2; RETURN result;
END//
SELECT add_numbers1(2,3);
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');
OUTPUT
RESULT
Thus, To Write user defined functions and stored procedures in SQL is
successfully executed and verified.
22
DATE:
AI
M
To Execute complex transactions and realize DCL and TCL commands.
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;
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;
select * from employees;
grant select on employees to 'root'@'localhost';
revoke select on employees from 'root'@'localhost';
OUTPUT
RESULT
Thus, To Execute complex transactions and realize DCL and TCL commands is
successfully executed and verified.
24
AI
M
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;
25
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;
26
OUTPUT
Insert Trigger:
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.
27
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)
);
OUTPUT
RESULT
Thus, to Create View and index for database tables with a large number of records is
successfully executed and verified.
29
DATE:
AI
M
To Create an XML database and validate it using XML schema.
ALGORITHM
Step 1: Create the XML
database. Step 2: Save the XML
database. Step 3: Create the XSD
schema.
Step 4: Validate the XML database using XSD schema.
30
PROGRAM
XML Database:
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:schema>
31
OUTPUT
The file is valid
RESULT
Thus, To Create an XML database and validate it using XML schema is successfully
executed and verified.
32
DATE:
AI
M
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
}
33
OUTPUT
DOCUMENT:
_id: ObjectID('64561efc6ee99dd7cf4dfd04')
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.
34
AI
M
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)
);
select * from customers;
35
PYTHON CODE:
import tkinter as tk
import mysql.connector
label2 = tk.Label(root,
text="Age:") label2.grid(row=1,
column=0) entry2 = tk.Entry(root)
entry2.grid(row=1, column=1)
36
OUTPUT
RESULT
Thus, To Develop a simple GUI based database application and incorporate all
the above-mentioned features is successfully executed and verified.
37
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 |
+ +
38
Normalization Rules:
Views:
● A view can be created to show the current stock levels for 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 items in the inventory table
A stored procedure can be created to calculate the EMI for a gold loan for each eligible cu
To showcase the ACID properties of the database, sample queries with appropriate setting
Atomicity: This property ensures that a transaction is treated as a single unit of work tha
Example query:
39
BEGIN TRANSACTION;
'Online'); COMMIT;
Example query:
BEGIN TRANSACTION;
COMMIT;
Example query:
-- Session 1
BEGIN TRANSACTION;
-- Session 2
BEGIN TRANSACTION;
40
-- Session 1 COMMIT;
-- Session 2
COMMIT;
Durability: This property ensures that once a transaction is committed, it will remain so,
Example query:
-- Upon system restart, the database will recover the committed transaction and ensure t
RESULT
Thus, case study of Inventory Management for a EMart Grocery Shop is
successfully done.