Repository navigation
Expand file tree
/
Copy pathdatabase.sql
More file actions
128 lines (101 loc) · 3.28 KB
/
Copy pathdatabase.sql
File metadata and controls
128 lines (101 loc) · 3.28 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
-- Create Customer table
USE mydatabase;
-- Create Admin table
CREATE TABLE IF NOT EXISTS Admin (
admin_id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL,
email VARCHAR(255) NOT NULL,
password VARCHAR(255) NOT NULL
);
CREATE TABLE IF NOT EXISTS Customer (
customer_id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL,
email VARCHAR(255) NOT NULL,
password VARCHAR(255) NOT NULL
);
-- Create Category table
CREATE TABLE IF NOT EXISTS Category (
category_id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL
);
CREATE TABLE IF NOT EXISTS Product (
product_id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL,
price DECIMAL(10,2) NOT NULL,
category_id INT,
FOREIGN KEY (category_id) REFERENCES Category(category_id)
);
-- Create ProductCategory table
CREATE TABLE IF NOT EXISTS ProductCategory (
category_id INT,
product_id INT,
FOREIGN KEY (category_id) REFERENCES Category(category_id),
FOREIGN KEY (product_id) REFERENCES Product(product_id)
);
-- Create CartProduct table
CREATE TABLE IF NOT EXISTS Cart (
customer_id INT,
product_id INT,
price DECIMAL,
FOREIGN KEY (customer_id) REFERENCES Customer(customer_id),
FOREIGN KEY (product_id) REFERENCES Product(product_id)
);
-- Create a function to calculate the total amount in the cart for a specific customer
DELIMITER //
CREATE FUNCTION CalculateCartTotalForCustomer(customer_id_param INT)
RETURNS DECIMAL(10, 2)
DETERMINISTIC
READS SQL DATA
BEGIN
DECLARE total_amount DECIMAL(10, 2);
SELECT SUM(price) INTO total_amount
FROM Cart
WHERE customer_id = customer_id_param;
RETURN COALESCE(total_amount, 0);
END //
DELIMITER ;
-- Create a trigger to check if the product is in the customer's cart before deletion
DELIMITER //
CREATE TRIGGER before_delete_product_from_cart
BEFORE DELETE ON Cart
FOR EACH ROW
BEGIN
DECLARE product_count INT;
-- Check if the product is in the customer's cart
SELECT COUNT(*)
INTO product_count
FROM Cart
WHERE customer_id = OLD.customer_id AND product_id = OLD.product_id;
-- If the product is not in the cart, prevent deletion
IF product_count = 0 THEN
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'Cannot delete product. Product is not in the customer''s cart.';
END IF;
END //
DELIMITER ;
-- Create Order table
-- Note: "Order" is a reserved keyword, consider using a different name for the table
-- CREATE TABLE IF NOT EXISTS Orders (
-- order_id INT PRIMARY KEY,
-- customer_id INT,
-- order_date DATE NOT NULL,
-- order_time TIME NOT NULL,
-- total_amt DECIMAL(10,2),
-- FOREIGN KEY (customer_id) REFERENCES Customer(customer_id)
-- );
-- Create Payment table
-- CREATE TABLE IF NOT EXISTS Payment (
-- payment_id INT PRIMARY KEY,
-- order_id INT,
-- customer_id INT, -- Assuming this is the foreign key for the customer
-- FOREIGN KEY (order_id) REFERENCES Orders(order_id)
-- );
-- Create Manage table
-- CREATE TABLE IF NOT EXISTS Manage (
-- admin_id INT,
-- product_id INT,
-- category_id INT,
-- FOREIGN KEY (admin_id) REFERENCES Admin(admin_id),
-- FOREIGN KEY (product_id) REFERENCES Product(product_id),
-- FOREIGN KEY (category_id) REFERENCES Category(category_id)
-- );