DBMS Project
DBMS Project
One company that faced problems with accurately counting sales in retail
management is Walmart. Walmart has encountered various issues related to
inventory management and sales tracking, leading to discrepancies in their
financial reports and inventory levels.
Walmart is one of the world's largest retail chains, and like many retailers of its
scale, it has faced challenges in effectively counting and managing sales in its
numerous stores. Here's a more detailed description of some of the problems
Walmart has encountered and the strategies they've employed to address them:
Inventory Accuracy:
Walmart's vast inventory and the sheer volume of products sold in its stores
made it challenging to maintain accurate inventory counts.Inventory
inaccuracies could lead to stockouts, overstock situations, and difficulties in
tracking the sales of specific items.
FUNCTIONALITY:
Employee Training and Development
Loss Prevention
Automatic Calculation of amount
NORMALISATION
SUPPLIER’S TABLE:
DEPARTMENT_TABLE:
- Department_id (Primary Key)
- Department_name
18.TRUNCATE SALE_TABLE
TRUNCATE TABLE SALE_TABLE
Table truncated.
25. Create a view to get the total sales for each product:
CREATE VIEW TotalProductSales AS
SELECT p.Product_id, p.Product_Name, SUM(s.Sale_amount) AS Total_Sales
FROM PRODUCT_TABLE p
LEFT JOIN SALE_TABLE s ON p.Product_id = s.Product_id
GROUP BY p.Product_id, p.Product_Name;
View created.
29. Create a view to get the total revenue generated from sales:
CREATE VIEW TotalRevenue AS
SELECT SUM(Sale_amount) AS Revenue
FROM SALE_TABLE;
View created.
Customer Name
Gautam
Product_id
drs-hltr-0050
drs-blgn-0053
groc-vngr-0084
stat-shpn-0093
drs-sdrs-0054
drs-cctl-0052
cnpr-clgt-0001
groc-spcs-0087
drs-ctot-0059
groc-ktup-0082
stat-crtp-0099
groc-nuts-0077
stat-ersr-0090
groc-slt-0068
groc-ooil-0070
cnpr-arfr-0034
stat-pen-0088
drs-wrap-0046
cos-fmst-0019
groc-cerl-0065
groc-myns-0081
cnpr-tisu-0026
groc-bksd-0086
drs-tunc-0051
groc-rice-0063
cos-bbwp-0035
groc-flr-0066
drs-shrt-0047
stat-pcwp-0030
groc-teap-0078
cnpr-lnfr-0040
groc-cofep-0079
cnpr-hdsp-0021
cos-ssrn-0023
groc-butr-0072
groc-bkpw-0085
stat-alfl-0029
drs-bdcn-0044
groc-mstrd-0083
cos-lpbm-0024
drs-slp-0062
stat-rulr-0100
stat-mrkr-0092
cos-fcln-0013
cnpr-lndr-0015
cos-ctsw-0022
stat-hglt-0091
cos-bbpd-0039
drs-shth-0048
stat-cpncl-0094
55.Insert a new employee:
INSERT INTO EMPLOYEE_TABLE (Employee_id, Employee_name, Department_id,
Position, Hire_date, Salary)
VALUES ('E2', 'Jane Smith', 'D1', 'Manager', '01-may-2023', 60000);
1 row created.
UPDATE SUPPLIER_TABLE
SET Phone_number = 9999999999
WHERE Supplier_id = 'sup-002';
1 row updated
58.Delete a supplier:
DELETE FROM SUPPLIER_TABLE
WHERE Supplier_id = 'sup-003';
1 row deleted.
SUPPLIER_NAME EMAIL
-------------------------------------------------- -------------------------
Randeep [email protected]
Ashi [email protected]
Shaheer [email protected]
Dhruv [email protected]
PRODUCT_ID
--------------------------------------------------
stat-alfl-0029
stat-pcwp-0030
stat-zlbg-0031
stat-btrs-0032
stat-ltbl-0033
stat-pncl-0089
stat-pen-0088
stat-ersr-0090
stat-hglt-0091
stat-mrkr-0092
stat-shpn-0093
stat-cpncl-0094
stat-cryn-0095
stat-glpn-0096
stat-mcpn-0097
stat-blpn-0098
stat-crtp-0099
stat-rulr-0100
61.Select suuplier with a phone_number starting with a specific number:
SELECT * FROM SUPPLIER_TABLE WHERE Phone_number LIKE '863%';
SUPPLIER_ID SUPPLIER_NAME DEPARTMENT_ID EMAIL PHONE_NUMBER
------------------------------------------------------------------------------------------------------------
ADDRESS
-------------------------
sup-009 Dhruv cos [email protected] 8638768375
Bangalore
DATA INTEGRITY CONSTRAINT
62.NOT NULL:
create table product_table(product_id varchar(50) NOT NULL,product_name
varchar(100),department_id varchar(50),category varchar(100),price number,qty
varchar(50));
Table created
63.UNIQUE:
create table customer_table(bill_no varchar(25) unique,customer_id
varchar(25),customer_name varchar(25),product_id varchar(25),email varchar(25),
phone_number number,employee_id varchar(25),qty varchar(25),purchase_date date,amount
number);
Table created
64.CHECK:
create table employee_table(employee_id varchar(50) primary key,employee_name
varchar(50),department_id varchar(50),position varchar(50),hire_date date,salary number
CHECK(salary>0));
Table created
65.PRIMARY KEY:
create table supplier_table( supplier_id varchar(50) primary key,supplier_name
varchar(50),department_id varchar(25),email varchar(25),phone_number number,address
varchar(25));
Table created
66.FOREIGN KEY:
create table sale_table(sale_id varchar(25) ,product_id varchar(25),quantity_sold
varchar(25),sale_amount number,PRIMARY KEY(sale_id),FOREIGN KEY(product_id)
REFERENCES product_table(product_id));
Table created
AGGREGATE FUNCTIONS AND SORTING
AVG(AMOUNT)
-----------------------
880.647059
MIN(AMOUNT)
-----------------------
25
MAX(AMOUNT)
------------------------
4866
COUNT(BILL_NO)
----------------------------
51
71.Calculating the average amount in employee table
select avg(salary)from employee_table;
AVG(SALARY)
----------------------
24538.4615
MIN(SALARY)
---------------------
20000
COUNT(EMPLOYEE_ID)
--------------------------------------
26
UPPER(EMPLOYEE_NAME)
--------------------------------------------------
LOGI
NAVANI
AISHU
RENU
PRABHU
ABI
RAMYA
SASTRI
JANE SMITH
ARIYA
GOKILA
RAVI
KAVI
KANI
SIVA
NITHYA
RAM
ARUN
KAVIYA
PRAVEEN
KRISHNA
KANIKA
JANU
NISHA
THARA
NAKSHU
LOWER(EMPLOYEE_NAME)
--------------------------------------------------
logi
navani
aishu
renu
prabhu
abi
ramya
sastri
jane smith
ariya
gokila
ravi
kavi
kani
siva
nithya
ram
arun
kaviya
praveen
krishna
kanika
janu
nisha
thara
nakshu
AVG(PRICE)
------------------
433.36
MIN(PRICE)
-----------------
10
MAX(PRICE)
-------------------
5000
COUNT(PRODUCT_ID)
-----------------------------------
100
82.Calculating average price in sale_table:
select avg(sale_amount)from sale_table;
AVG(SALE_AMOUNT)
---------------------------------
880.352941
MIN(SALE_AMOUNT)
---------------------------------
0
MAX(SALE_AMOUNT)
----------------------------------
4866
COUNT(SALE_ID)
------------------------------
51
POSITION COUNT(*)
-------------------------------------------------- ----------
Manager 6
Supervisor 5
Salesperson 15
EMPLOYEE_ID COUNT(*)
------------------------- ----------------------------
drssal-0008 2
grocsal-0001 3
cnprsal-0014 5
drssal-0009 2
grocsal-0003 3
grocsal-0002 1
drssal-0007 4
cosma-024 3
grocsup-001 1
cnprsup-005 1
drssup-003 1
cnprma-025 2
cnprsal-0013 2
cnprsal-0015 3
cossal-0010 2
cossal-0012 1
cossal-0011 1
statsal-0006 3
statma-022 1
statsal-0004 2
statsal-0005 3
drsma-023 2
grocma-021 1
cossup-004 1
statsup-002 1
88.Find the number of products with a price greater than a specific value
SELECT COUNT(*) FROM PRODUCT_TABLE WHERE Price >3800;
COUNT(*)
--------------------
1
AVG(LENGTH(PHONE_NUMBER))
-------------------------
10
AVG(LENGTH(ADDRESS))
--------------------------------------
8.25
AVG(LENGTH(EMAIL))
------------------
16
CUSTOMER_ID SUM(QTY)
------------------------- ----------
cid-001 2
cid-002 1
cid-003 2
cid-004 3
cid-006 2
cid-007 4
cid-008 2
cid-009 2
cid-010 3
cid-011 3
cid-012 2
cid-013 3
cid-014 6
cid-015 3
cid-016 3
cid-017 2
cid-018 1
cid-019 2
cid-020 5
cid-021 2
cid-022 2
cid-023 1
cid-024 3
cid-025 6
cid-026 2
cid-027 2
cid-028 5
cid-030 3
cid-031 1
cid-032 2
cid-033 3
cid-034 2
cid-35 2
cid-036 1
cid037 3
cid-038 3
cid-039 2
cid-040 2
cid-041 1
cid-042 2
cid-043 2
cid-044 1
cid-045 1
cid-046 2
cid-047 1
AVG(PRICE)
------------------
169.380952
AVG(LENGTH(PRODUCT_NAME))
-------------------------
10.97
JOINS
PRODUCT_NAME PRICE
BILL_NO
---------------------------------------------------------------------------------------------------- ---------- -
------------------------
Little Black Dress 1800 bid-001
Pasta Sauce 99 bid-002
Dove_Soap 65 bid-003
Puff Sleeve Dress 1622 bid-004
Canned Tomatoes 100 bid-006
Cooking Oil 120 bid-007
Oats 195 bid-008
Shift Dress 999 bid-009
Tiered Dress 1199 bid-010
Canned Beans 110 bid-011
Dishwasher Detergent 600 bid-012
Skater Dress 1585 bid-013
Contact Lens Solution 241 bid-014
Peanut Butter 279 bid-015
Baby Shampoo 95 bid-016
A line Dress 380 bid-017
Colgate_Toothpaste 40 bid-018
Pepsodent_Toothpaste 97 bid-019
dabur_Toothpaste 112 bid-020
Indulekha_shampoo 204 bid-021
Nivea_Soap 145 bid-022
Body_Lotion 201 bid-023
Toilet_Paper 199 bid-024
Deodorant 265 bid-025
Facial_Cleanser 274 bid-026
Hand_Sanitizer 25 bid-027
Hand_Sanitizer 25 bid-028
Maxi Dress 699 bid-029
Facial Razor 199 bid-030
Dish Soap 75 bid-031
Conditioner 550 bid-032
Peplum Dress 899 bid-033
Baby Diaper 1200 bid-034
Crayon 10 bid-035
Gel Pen 180 bid-036
Batteries 270 bid-037
Light Bulbs 120 bid-038
Pencils 35 bid-039
Off Soulder 1700 bid-040
Sugar 80 bid-041
Mechanical Pencil 20 bid-042
Mechanical Pencil 20 bid-043
Ball Pen 100 bid-044
Midi Dress 2000 bid-045
Trash Bags 115 bid-046
Ziploc Bags 199 bid-047
Pepper 189 bid-048
Pasta 39 bid-049
Asymmetrical Dress 469 bid-050
Baby Lotion 138 bid-051
Light Bulbs 120 bid-052
Halter Dress 560
Ball Gown 2500
Vinegar 70
Lifebuoy_Soap 154
Cocktail Dress 1000
Sun Dress 670
Sharpener 70
Spices 100
CutOut Dress 1950
Ketchup 108
Correction Tape 125
Nuts 200
Erasers 40
Salt 40
Olive Oil 352
Air Freshner 275
Pens 50
Wrap Dress 2500
Face Moisturer 355
Cereal 152
Mayonnaise 185
Tissues 100
Tunic Dress 280
Baking Soda 24
Toothbrush 20
Baby Wipes 90
Rice 291
Flour 45
Shirt Dress 5000
Plastic Wrap 200
Tea pack 90
Laundry Fabric Softener 257
Coffee pack 216
Hand Soap 45
Sunscreen 499
Butter 56
Baking Powder 23
Clinic_Plus 105
Aluminium Foil 375
Bodycon Dress 800
Lip Balm 250
Mustard 30
Slip Dress 760
Ruler 10
Permanent Marker 100
Dove_Shampoo 599
Laundry_Detergent 230
Cotton Swabs 20
Highlighter 100
Baby Powder 88
Sheath Dress 720
Colour pencils 120
select product_table.product_id,product_table.product_name,
product_table.price,product_table.qty,customer_table.bill_no from product_table full join
customer_table on product_table.product_id=customer_table.product_id;
CURSOR:
112.To calculate the amount in customer_table for each customer bu
referencing product_table and then update the amount column in
customer_table.
SET SERVEROUTPUT ON;
DECLARE
v_customer_id customer_table.customer_id%TYPE;
v_product_id customer_table.product_id%TYPE;
v_price product_table.price%TYPE;
v_qty customer_table.qty%TYPE;
v_amount customer_table.amount%TYPE;
CURSOR customer_cursor IS
SELECT customer_id, product_id, qty
FROM customer_table;
BEGIN
OPEN customer_cursor;
FETCH customer_cursor INTO v_customer_id, v_product_id, v_qty;
WHILE customer_cursor%FOUND LOOP
SELECT price INTO v_price
FROM product_table
WHERE product_id = v_product_id;
v_amount := v_price * CAST(v_qty AS NUMBER);
UPDATE customer_table
SET amount = v_amount
WHERE customer_id = v_customer_id;
FETCH customer_cursor INTO v_customer_id, v_product_id, v_qty;
END LOOP;
CLOSE customer_cursor;
COMMIT;
END;
/
PL/SQL procedure successfully completed
TRIGGER:
116. Trigger to calculate the total amount for a customer's purchase when a
new purchase is made
CREATE OR REPLACE TRIGGER calculate_purchase_amount
BEFORE INSERT ON customer_table
FOR EACH ROW
DECLARE
purchase_amount NUMBER;
BEGIN
SELECT price * :new.qty
INTO purchase_amount
FROM product_table
WHERE product_id = :new.product_id;
:new.amount := purchase_amount;
Trigger created.
117.Trigger to calculate the difference of old salary and new salary after
updating the salary
CREATE OR REPLACE TRIGGER display_salary_changes
BEFORE DELETE OR INSERT OR UPDATE ON employee_table
FOR EACH ROW
WHEN (NEW.salary > 0)
DECLARE
sal_diff NUMBER;
BEGIN
sal_diff := :NEW.salary - :OLD.salary;
dbms_output.put_line('Old Salary : ' || :OLD.salary);
dbms_output.put_line('New Salary : ' || :NEW.salary);
dbms_output.put_line('Salary Difference : ' || sal_diff);
END;
/
Trigger created.
FOR cust_purchase IN (
SELECT Amount
FROM Customer_table
WHERE Customer_id = p_customer_id
) LOOP
v_total_amount := v_total_amount + cust_purchase.Amount;
END LOOP;
UPDATE Bill_table
SET Amount_to_be_paid = v_total_amount
WHERE customer_id = p_customer_id;
COMMIT;
EXCEPTION
WHEN OTHERS THEN
PACKAGE:
119.To create a package and inserting the values
Package created.
Package created.
CREATE OR REPLACE PACKAGE BODY department_salary_package AS
PROCEDURE calculate_total_salary(p_department_id VARCHAR2, p_total_salary OUT
NUMBER) IS
BEGIN
SELECT SUM(salary) INTO p_total_salary
FROM employee_table
WHERE department_id = p_department_id;
END calculate_total_salary;
END department_salary_package;
/
DECLARE
v_department_id VARCHAR2(50) := 'groc';
v_total_salary NUMBER;
BEGIN
department_salary_package.calculate_total_salary(v_department_id, v_total_salary);
DBMS_OUTPUT.PUT_LINE('Total Salary for Department ' || v_department_id || ': ' ||
v_total_salary);
END;
/
EXCEPTION HANDLING:
121.To print the name of a customer when the given customer_id matches:
DECLARE
c_id customer_table.customer_id%type := 'cid-045';
c_name customer_table.customer_name%type ;
BEGIN
SELECT customer_name INTO c_name FROM customer_table
WHERE customer_id = c_id ;
DBMS_OUTPUT.PUT_LINE ( 'Name : ' || c_name ) ;
EXCEPTION
WHEN no_data_found THEN
dbms_output.put_line ( ' No such customer! ' ) ; WHEN
others THEN
dbms_output.put_line ( ' Error! ' ) ;
END ;
/
Name : Charu
CONCLUSION
1. PRODUCT_TABLE: This table stores information about products, including their unique
IDs (Product_id), names, department IDs (Department_Id), categories, prices, and quantities
(Qty). It serves as a central repository for product data.
2. CUSTOMERS_TABLE: This table is dedicated to customer information. It includes details
such as customer IDs (Customer_id), names, email addresses, phone numbers, and the IDs of
the products they purchased. It also tracks the purchase quantity, date, and amount.
3. SALES_TABLE: The Sales table records sales transactions. It includes Sale_id as a unique
identifier, Product_id to link to products in the Product table, the quantity sold
(Quantity_sold), and the sale amount (Sale_amount).
https://www.scribd.com/document/388067179/308864997-RETAIL-STORE-
MANAGEMENT-SYSTEM
http://ignousupport.blogspot.com/p/supermarket-retail-management-
system.html?m=1