-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRetail store database design.sql
More file actions
83 lines (76 loc) · 2.85 KB
/
Retail store database design.sql
File metadata and controls
83 lines (76 loc) · 2.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# Create the database
CREATE DATABASE retail_store;
USE retail_store;
# CUSTOMERS TABLE
CREATE TABLE customer (
customer_id INT PRIMARY KEY AUTO_INCREMENT,
customer_name VARCHAR(100) NOT NULL,
phone_number VARCHAR(20),
address_line1 VARCHAR(100),
address_line2 VARCHAR(100),
city VARCHAR(50),
postcode VARCHAR(10),
loyalty_points_balance INT NOT NULL DEFAULT 0,
created_date DATETIME DEFAULT CURRENT_TIMESTAMP
);
# STAFF TABLE
CREATE TABLE staff (
staff_id INT PRIMARY KEY AUTO_INCREMENT,
staff_name VARCHAR(100) NOT NULL,
role VARCHAR(50),
created_date DATETIME DEFAULT CURRENT_TIMESTAMP
);
# PRODUCTS TABLE
CREATE TABLE product (
product_id INT PRIMARY KEY AUTO_INCREMENT,
barcode VARCHAR(30) NOT NULL UNIQUE,
product_name VARCHAR(100) NOT NULL,
product_category VARCHAR(50) NOT NULL,
product_subcategory VARCHAR(50),
product_description TEXT,
cost_price DECIMAL(10,2) NOT NULL,
selling_price DECIMAL(10,2) NOT NULL,
stock_quantity INT NOT NULL DEFAULT 0,
is_discontinued BOOLEAN NOT NULL DEFAULT FALSE,
created_date DATETIME DEFAULT CURRENT_TIMESTAMP
);
# SALES TABLE (one row per transaction)
CREATE TABLE sale (
sale_id INT PRIMARY KEY AUTO_INCREMENT,
sale_date DATE NOT NULL,
sale_time TIME NOT NULL,
customer_id INT NULL,
staff_id INT NOT NULL,
total_amount DECIMAL(10,2) NOT NULL,
total_discount DECIMAL(10,2) NOT NULL DEFAULT 0.00,
payment_method VARCHAR(20) NOT NULL,
created_date DATETIME NOT NULL DEFAULT NOW(),
FOREIGN KEY (customer_id) REFERENCES customer(customer_id),
FOREIGN KEY (staff_id) REFERENCES staff(staff_id)
);
# SALE ITEMS TABLE (one row per product in sale)
CREATE TABLE sale_item (
sale_item_id INT PRIMARY KEY AUTO_INCREMENT,
sale_id INT NOT NULL,
product_id INT NOT NULL,
quantity INT NOT NULL,
unit_selling_price DECIMAL(10,2) NOT NULL,
line_discount DECIMAL(10,2) NOT NULL DEFAULT 0.00,
line_total DECIMAL(10,2) NOT NULL,
created_date DATETIME NOT NULL DEFAULT NOW(),
FOREIGN KEY (sale_id) REFERENCES sale(sale_id) ON DELETE CASCADE,
FOREIGN KEY (product_id) REFERENCES product(product_id)
);
# LOYALTY TRANSACTIONS TABLE
CREATE TABLE loyalty_transaction (
loyalty_txn_id INT PRIMARY KEY AUTO_INCREMENT,
customer_id INT NOT NULL,
sale_id INT NULL,
points_change INT NOT NULL,
reason VARCHAR(100),
txn_datetime DATETIME NOT NULL DEFAULT NOW(),
FOREIGN KEY (customer_id) REFERENCES customer(customer_id),
FOREIGN KEY (sale_id) REFERENCES sale(sale_id)
);
# Verify tables created
SHOW TABLES;