0% found this document useful (0 votes)
2 views

Tausif and stanzin practical

The document is a laboratory file for the MC-302 Database Management System course at Delhi Technological University, detailing the submission by Stanzin Wangdus for the Bachelor of Technology degree in Mathematics and Computing. It includes various practical implementations such as creating and managing a Stock Management System database, executing SQL statements, and developing PL/SQL blocks. The file outlines objectives, methodologies, and specific SQL queries related to database management tasks.

Uploaded by

nawaztausif63
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Tausif and stanzin practical

The document is a laboratory file for the MC-302 Database Management System course at Delhi Technological University, detailing the submission by Stanzin Wangdus for the Bachelor of Technology degree in Mathematics and Computing. It includes various practical implementations such as creating and managing a Stock Management System database, executing SQL statements, and developing PL/SQL blocks. The file outlines objectives, methodologies, and specific SQL queries related to database management tasks.

Uploaded by

nawaztausif63
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 32

DEPARTMENT OF APPLIED MATHEMATICS

DELHI TECHNOLOGICAL UNIVERSITY


(FORMERLY Delhi College of Engineering)
Bawana Road, Delhi-110042

LABORATORY FILE
For the course of

MC-302 Database Management System


Submitted in complete fulfillment of the requirements for the award of the
degree of
Bachelor of Technology
In
MATHEMATICS AND COMPUTING
Submitted By:

STANZIN WANGDUS (2K20/MC/143)


Submitted To: Worked under the supervision of
Mrs. Sumedha Mr. Rohit Kumar
Table of Contents

S.No. Program Date

1. Synopsis of the Project, along with its ER diagram

2. Implement DDL statements like Create Table (with and


without constraints), ALTER Table, DROP Table.

3. Implement DML statements like INSERT, UPDATE,


DELETE< TRUNCATE

4. Implement SELECT statements with WHERE, IN/NOT IN,


GROUP BY, ORDER BY clause, aggregate functions, views

5. Implement and perform NESTED QUERIES alog with JOINS


(Inner Join, Outer Join, Left Join, Right Join)

6. Create a PL/SQL block and implement the following:


Variables, Packages, Procedures, Functions
7. Perform EXCEPTION HANDLING in a PL/SQL block.

8. Project Report with Code and Screenshots.

9. Implement TRIGGERS in SQL.

10. Implement the following transactions statements: Commit,


Rollback, Savepoint.
Practical – 1

AIM: To write synopsis of the Project, along with its ER diagram

Title: Stock Management System

Introduction:

This helps us to access and manage the information easily. This also helps to
verify the stock currently available with them and to update the stock when
necessary. This also reduces the time to search the product from the available
stock.The role of an inventory system is to track your products and supplies.
Inventory management is the process of controlling of the ordering, storage
and use of components that a company uses in the production of the products
it sells.

Objectives:

The primary objectives of the Stock Management System are:


1) To create a centralized database for storing and managing
information related to stocks of fertilizers, bill no., mfd,
etc.
2) To provide a user-friendly interface for accessing and managing
the inventory and sales data.
3) To automate various administrative tasks related to Stock
management.
4) To improve the efficiency and accuracy of Stock management.
5) To ensure the security and confidentiality of the Stock data.

The Stock Management System is an efficient and reliable solutionfor


Stocks to manage their inventory and sales data. With its user- friendly
interface and powerful features, the system simplifies administrative
tasks and improves the accuracy and efficiency of Stock management.
E-R Diagram
Practical – 2

AIM: Implement the following DDL statements:

a. Create table with constraints (NOT NULL, UNIQUE,


DEFAULT, CHECK, PRIMARY KEY, FOREIGN KEY)

 The following are the required queries to create tables according


to the ER diagram, using all the necessary constraints.

CREATE TABLE IF NOT EXISTS `bill` (


`item_id` varchar(10) NOT NULL,
`item_name` varchar(30) NOT NULL,
`quantity` int(20) NOT NULL,
`price` float NOT NULL,
`totprice` float NOT NULL,
`biilno` int(30) NOT NULL UNIQUE,
`date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`biilno`)
);

CREATE TABLE IF NOT EXISTS `cart` (


`item_id` varchar(10) NOT NULL,
`item_name` varchar(30) NOT NULL,
`quantity` int(20) NOT NULL,
`price` float NOT NULL,
`totprice` double NOT NULL
);

CREATE TABLE IF NOT EXISTS `inventory` (


`item_id` varchar(10) NOT NULL,
`item_name` varchar(30) NOT NULL,
`quantity` int(20) NOT NULL,
`price` float NOT NULL,
`mfd` varchar(30) NOT NULL,
`exp` varchar(30) NOT NULL,
`batchno` varchar(30) NOT NULL,
PRIMARY KEY (`item_id`)
);

CREATE TABLE IF NOT EXISTS `purchase` (


`biilno` int(10) NOT NULL ,
`item_id` varchar(30) NOT NULL,
`item_name` varchar(20) NOT NULL,
`quantity` int(20) NOT NULL,
`price` double NOT NULL,
`totprice` double NOT NULL,
`date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`biilno`),
FOREIGN KEY (billno) REFERENCES bill (billno),
);
b. Alter Table:
i. Add Column
ii. Drop Column
iii. Add/drop Constraint
iv. Rename Column

DESC bill;

ALTER TABLE bill ADD MobileNo


int; DESC bill;

ALTER TABLE bill DROP COLUMN MobileNo;


DESC Books;
DESC cart;

ALTER TABLE cart ALTER


Status DROP DEFAULT;
DESC cart;

ALTER TABLE cart


ALTER Status SET DEFAULT 'ACTIVE';
DESC cart;
DESC cart;

ALTER TABLE cart RENAME


column quantity to stock;DESC cart;

c. Drop Table

To drop the table Cart, MySQL query is –

DROP TABLE cart;


Practical – 3

AIM: Implement the following DML statements:

a. Insert

-- Insert into Bill table

INSERT INTO `bill` (`item_id`, `item_name`, `quantity`, `price`, `totprice`,


`biilno`, `date`) VALUES
('ft4', 'ammonia', 7, 700, 4900, 41, '2018-10-28 21:54:53'),
('ft4', 'ammonia', 4, 700, 2800, 42, '2018-10-28 21:57:27'),
('sd2', 'rice ', 4, 500, 2000, 43, '2018-10-28 22:19:38'),
('ft6', 'groundnut', 6, 400, 2400, 44, '2018-10-29 00:08:16'),
('ft6', 'groundnut', 5, 400, 2000, 45, '2018-10-29 00:10:22'),
('ft2', 'ammonia', 5, 150, 750, 46, '2018-10-29 01:29:37'),
('ft3', 'NH4', 5, 700, 3500, 47, '2018-10-29 01:34:45'),
('ft3', 'NH4', 5, 700, 3500, 48, '2018-10-29 10:34:47'),
('ft1', 'urea', 2, 200, 400, 49, '2018-10-29 19:48:20'),
('ft1', 'urea', 2, 200, 400, 50, '2018-10-29 20:11:42'),
('ft5', 'sulphate', 2, 900, 1800, 51, '2020-02-27 15:18:26'),
('ft5', 'sulphate', 2, 900, 1800, 52, '2020-02-27 15:19:24'),
('ft1', 'urea', 2, 200, 400, 53, '2020-03-03 15:18:54');

-- Insert into Invetory table

INSERT INTO `inventory` (`item_id`, `item_name`, `quantity`, `price`, `mfd`,


`exp`, `batchno`) VALUES
('ft5', 'sulphate', 70, 900, '5/10/2017', '6/10/2018', 'h789'),
('ft4', 'ammonia', 39, 700, '03/10/2017', '03/10/2018', 't200'),
('ft3', 'NH4', 40, 700, '22/11/2017', '22/11/2018', 'b85'),
('ft2', 'ammonia', 85, 150, '20/08/2018', '11/12/2019', 'am123'),
('ft1', 'urea', 370, 200, '20/10/2018', '20/11/2019', 'asdc1'),
('sd1', 'Grounnuts', 400, 300, '4/5/2016', '4/3/2018', 'h78'),
('sd2', 'rice ', 66, 500, '4/6/2017', '3/8/2108', 'g77'),
('sd3', 'chilli', 80, 500, '3/5/2017', '5/7/2019', 'g67'),
('sd4', 'wheat', 90, 300, '7/2/2017', '5/5/2019', 'h67'),
('sd5', 'corn', 50, 900, '8/7/2017', '7/5/2019', 'hf6'),
('ft6', 'groundnut', 49, 400, '4/5/2017', '4/4/2019', 'h23'),
('12', 'one', 500, 5, '29-02-2019', '29-02-2020', '45');

-- Insert into Purchase table

INSERT INTO `purchase` (`biilno`, `item_id`, `item_name`, `quantity`, `price`,


`totprice`, `date`) VALUES
(9, 'ft5', 'sulphate', 5, 900, 4450, '2020-02-27 15:21:12'),
(8, 'ft1', 'urea', 20, 200, 3800, '2018-10-29 20:16:01'),
(7, 'ft1', 'urea', 10, 200, 1900, '2018-10-29 19:51:05'),
(10, 'ft1', 'urea', 6, 200, 1140, '2020-03-03 15:21:22');
b. Update

select * from purchase where biilno=8;

UPDATE purchase
SET quantity = 7
WHERE biilno = 8;
select * from purchase where biilno=8;

c. Delete

select * from purchase;


Delete from purchase where biilno=8; select * from purchase;

d. Truncate

Select * from purchase;

Truncate Table purchase;


Select * from purchase;
desc purchase;
Program – 4

AIM: Implement the following select statements:

a. Simple SELECT statement

select * from purchase;

b. Where clause + IN/NOT IN

select * from purchase


where quantity in ('20');

select * from purchase


where item_name NOT IN ('urea');
c. Aggregate Functions

SELECT COUNT(*)
from purcahse
where item_name= 'sulphate';

SELECT SUM(quantity)from
purchase;

SELECT MAX(quantity) AS
LargestStock FROM purchase;

SELECT MIN(quantity) AS Smallest


FROM purchase;

SELECT AVG(quantity)
FROM purchase;
d. Group by + having

SELECT COUNT(biilno), item_id FROM purchase


GROUP BY item_id;

e. Order by

Select item_name,
quantity from
purchase ORDER
BY quantity;
f. Views

CREATE VIEW ID_Name AS


SELECT item_id, item_name, quantity FROM purchase;
SELECT * from ID_Name;
DEPARTMENT OF APPLIED MATHEMATICS
DELHI TECHNOLOGICAL UNIVERSITY
(FORMERLY Delhi College of Engineering)
Bawana Road, Delhi-110042

PROJECT REPORT
For the course of

MC-302 Database Management System


Submitted in complete fulfillment of the requirements for the award of the
degree of
Bachelor of Technology
In
MATHEMATICS AND COMPUTING
Submitted By:

STANZIN WANGDUS (2K20/MC/143)


Submitted To: Worked under the supervision of
Mrs. Sumedha Mr. Rohit Kumar
DEPARTMENT OF APPLIED MATHEMATICS
DELHI TECHNOLOGICAL UNIVERSITY
(FORMERLY Delhi College of Engineering)
Bawana Road, Delhi-110042

PROJECT REPORT
For the course of

MC-302 Database Management System


Submitted in complete fulfillment of the requirements for the award of the
degree of
Bachelor of Technology
In
MATHEMATICS AND COMPUTING
Submitted By:

TAUSIF NAWAZ (2K20/MC/145)


Submitted To: Worked under the supervision of
Mrs. Sumedha Mr. Rohit Kumar
Program – 5

AIM: Implement and perform Nested Queries along with Joins

a. Nested Queries

SELECT *
FROM purchase
WHERE item_id IN (
SELECT item_id FROM
bill WHERE biilno IN (
SELECT FROM
inventory
WHERE STATUS = 'ACTIVE'
)
);

b. Inner Join

SELECT inventory.item_name, purchase.item_id FROM inventory


INNER JOIN purchase ON inventory.item_id = purchase.item_id;
c. Left Join

SELECT Customers.First_Name, Customers.Last_Name,


Shopping_Cart.Cart_ID
FROM Customers
LEFT JOIN Shopping_Cart ON Customers.Customer_ID =
Shopping_Cart.Customer_ID;

d. Right Join

SELECT Publishers.Name, Books.Title


FROM Publishers
RIGHT JOIN Books ON Publishers.Publisher_ID =
Books.Publisher_ID;

e. Outer Join

SELECT Customers.Customer_ID, Shopping_Cart.Cart_ID


FROM Customers
LEFT OUTER JOIN Shopping_Cart ON Customers.Customer_ID =
Shopping_Cart.Customer_ID;
Program – 6

AIM: Create a PL/SQL block and implement the following:

a. Variables

b. Packages

CREATE OR REPLACE PACKAGE book_package AS


PROCEDURE get_book_details(p_ISBN NUMBER);
END book_package;

CREATE OR REPLACE PACKAGE BODY book_package AS


PROCEDURE get_book_details(p_ISBN NUMBER) AS
v_title books.title%TYPE;
v_price books.price%TYPE;
v_publisherpublishers.name%TYPE;
v_author authors.first_name%TYPE;
v_stock_count books.stock_count%TYPE;
BEGIN
SELECT title, price, name, first_name, stock_count
INTO v_title, v_price, v_publisher, v_author, v_stock_count
FROM books b
JOIN publishers p ON b.publisher_id = p.publisher_id
JOIN book_authors ba ON b.isbn = ba.isbn
JOIN authors a ON ba.author_id = a.author_id
WHERE b.isbn = p_ISBN;

DBMS_OUTPUT.PUT_LINE('Title: ' || v_title);


DBMS_OUTPUT.PUT_LINE('Price: $' || v_price);
DBMS_OUTPUT.PUT_LINE('Publisher: ' || v_publisher);
DBMS_OUTPUT.PUT_LINE('Author: ' || v_author);
DBMS_OUTPUT.PUT_LINE('Stock Count: ' || v_stock_count);
END get_book_details;
END book_package;

This package has a single procedure get_book_details that takes an


ISBN number as input and returns the book's title, price, publisher name,
author name, and stock count. It uses variables to store the result of a
SELECT statement that joins multiple tables.

c. Procedures

CREATE OR REPLACE PROCEDURE update_stock_count(p_isbn IN


books.isbn%TYPE, p_quantity IN NUMBER)
IS
v_stock_count NUMBER;
BEGIN
SELECT stock_count INTO v_stock_count FROM books WHERE
isbn = p_isbn;

IF (v_stock_count + p_quantity) < 0 THEN


RAISE_APPLICATION_ERROR(-20001,'Stock count cannot be
negative');
ELSE
UPDATE books SET stock_count = v_stock_count + p_quantity
WHERE isbn = p_isbn;
END IF;

COMMIT;

DBMS_OUTPUT.PUT_LINE('Stock count updated successfully');


END;
/

This procedure takes two parameters - the ISBN of the book to update
and the quantity by which to update the stock count. It first retrieves the
current stock count for the book, and then checks if the updated stock
count would be negative. If it would be, it raises an error. Otherwise, it
updates the stock count and commits the transaction.

d. Functions

DECLARE
FUNCTION get_stock_count(p_isbn IN Books.ISBN%TYPE)
RETURN Books.Stock_Count%TYPE
IS
v_stock_count Books.Stock_Count% TYPE;
BEGIN
SELECT Stock_Count INTO v_stock_count FROM Books WHERE ISBN =
p_isbn;
RETURN v_stock_count;
END;
v_stock_count Books.Stock_Count%TYPE;
BEGIN
v_stock_count := get_stock_count('1234567890');
DBMS_OUTPUT.PUT_LINE('Stock count for ISBN 1234567890: ' ||
v_stock_count);
END;
Program – 7

AIM: Perform Exception Handling in a PL/SQL block.

DECLARE
v_num1 NUMBER := 10;
v_num2 NUMBER := 0;
v_result NUMBER;
BEGIN
-- Perform division
v_result := v_num1 / v_num2;
DBMS_OUTPUT.PUT_LINE('Result: ' || v_result);
EXCEPTION
-- Handle division by zero exception
WHEN ZERO_DIVIDE THEN
DBMS_OUTPUT.PUT_LINE('Error: Division by zero.');
-- Handle other exceptions
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error: ' || SQLERRM);
END;
Program – 9

AIM: Implement Triggers in SQL

 Trigger to update stock count in the Books table when a


new entry is made in the Book_Warehouse table:

CREATE TRIGGER
update_stock_count AFTER INSERT
ON Book_Warehouse FOR EACH ROW
BEGIN
UPDATE Books
SET Stock_Count = Stock_Count + NEW.Quantity
WHERE ISBN = NEW.ISBN;
END;

 Trigger to prevent insertion of duplicate entries in


the Book_Authors table:

CREATE TRIGGER prevent_duplicate_book_authors


BEFORE INSERT ON Book_Authors
FOR EACH ROW
BEGIN
IF EXISTS(SELECT * FROM Book_Authors
WHERE ISBN = NEW.ISBN AND Author_ID
=
NEW.Author_ID) THEN
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT =
'Duplicate entry';
END IF;
END;
 Trigger to calculate total price of a shopping cart and
update the status in the Shopping_Cart table:

CREATE TRIGGER
update_shopping_cart AFTER INSERT
ON Cart_Items
FOR EACH ROW
BEGIN
UPDATE Shopping_Cart
SET Status = 'In
Progress',
Total_Price = (SELECT SUM(Price * NEW.Quantity) FROM
Books WHERE ISBN = NEW.ISBN)
WHERE Cart_ID =
NEW.Cart_ID; END;
Program – 10

AIM: Implement the following transactions statements –


a. Commit
b. Rollback
c. Savepoint

 Code –

START TRANSACTION;
SAVEPOINT before_insert;
INSERT INTO bill (biilno, item_name, item_id)
VALUES ('9', 'sulphate', 80');COMMIT;
-- If an error occurs before the COMMIT statement, you can use the
ROLLBACK statement to undo the changes:

START TRANSACTION;
INSERT INTO bill (biilno, item_name, item_id)
VALUES ('9', 'sulphate', 80');COMMIT;
ROLLBACK;
-- If you want to undo only part of the transaction, you can use the
SAVEPOINT and ROLLBACK TO statements:

START TRANSACTION;
INSERT INTO bill (biilno, item_name, item_id)
VALUES ('9', 'sulphate', 80');COMMIT;
SAVEPOINT before_update;
UPDATE bill SET item_id = 99 WHERE item_name ='sulphate';
ROLLBACK TO before_update;
-- This will undo the UPDATE statement, but keep the INSERT
statement:
COMMIT;

 In this example, we use the START TRANSACTION statement to


begin a transaction. We then use the SAVEPOINT statement to
create a savepoint named before_insert, which allows us to roll
back to this point if an error occurs later in the transaction.
 Next, we execute an INSERT statement to add a new row to the
Books table, and then use the COMMIT statement to commit the
transaction.
 In the second example, we intentionally cause an error by trying
to insert a duplicate row into the Books table. We then use the
ROLLBACK statement to undo the changes made by the
INSERT statement.
 Finally, we use a SAVEPOINT statement to create a savepoint
named before_update before executing an UPDATE statement. We
then use the ROLLBACK TO statement to undo only the
UPDATE statement and keep the changes made by the previous
INSERT statement. Finally, we use the COMMIT statement to
commit the transaction.

You might also like