-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcashflow.sql
250 lines (224 loc) · 7.23 KB
/
cashflow.sql
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
use cashflow;
-- 用户表
CREATE TABLE Users (
userId BIGINT IDENTITY(1,1) PRIMARY KEY,
userName NVARCHAR(255) NOT NULL,
email NVARCHAR(255) NOT NULL UNIQUE,
passwordHash NVARCHAR(255) NOT NULL,
createdAt DATETIME NOT NULL,
updatedAt DATETIME NULL
);
SELECT * from Users;
-- 第三方登录表
CREATE TABLE UserOAuth (
userOAuthId BIGINT IDENTITY(1,1) PRIMARY KEY,
userId BIGINT NOT NULL,
providerName NVARCHAR(255) NOT NULL,
providerUserId NVARCHAR(255) NOT NULL,
createdAt DATETIME NOT NULL,
updatedAt DATETIME NULL,
FOREIGN KEY (userId) REFERENCES Users(userId)
);
-- 商品表
CREATE TABLE Products (
productId BIGINT IDENTITY(1,1) PRIMARY KEY,
productName NVARCHAR(255) NOT NULL,
productDescription NVARCHAR(MAX),
productPrice DECIMAL(18, 2) NOT NULL,
stockQuantity INT NOT NULL,
imageUrl NVARCHAR(MAX),
createdAt DATETIME NOT NULL,
updatedAt DATETIME NULL
);
-- 购物车表
CREATE TABLE Carts (
cartId BIGINT IDENTITY(1,1) PRIMARY KEY,
userId BIGINT NOT NULL,
createdAt DATETIME NOT NULL,
updatedAt DATETIME NULL,
FOREIGN KEY (userId) REFERENCES Users(userId)
);
-- 购物车明细表
CREATE TABLE CartItems (
cartItemId BIGINT IDENTITY(1,1) PRIMARY KEY,
cartId BIGINT NOT NULL,
productId BIGINT NOT NULL,
quantity INT NOT NULL,
createdAt DATETIME NOT NULL,
updatedAt DATETIME NULL,
FOREIGN KEY (cartId) REFERENCES Carts(cartId),
FOREIGN KEY (productId) REFERENCES Products(productId)
);
-- 订单表
CREATE TABLE Orders (
orderId BIGINT IDENTITY(1,1) PRIMARY KEY,
userId BIGINT NOT NULL,
totalAmount DECIMAL(18, 2) NOT NULL,
orderStatus NVARCHAR(255) NOT NULL,
createdAt DATETIME NOT NULL,
updatedAt DATETIME NULL,
FOREIGN KEY (userId) REFERENCES Users(userId)
);
-- 订单明细表
CREATE TABLE OrderItems (
orderItemId BIGINT IDENTITY(1,1) PRIMARY KEY,
orderId BIGINT NOT NULL,
productId BIGINT NOT NULL,
purchasePrice DECIMAL(18, 2) NOT NULL,
quantity INT NOT NULL,
createdAt DATETIME NOT NULL,
updatedAt DATETIME NULL,
FOREIGN KEY (orderId) REFERENCES Orders(orderId),
FOREIGN KEY (productId) REFERENCES Products(productId)
);
-- 支付信息表
CREATE TABLE Payments (
paymentId BIGINT IDENTITY(1,1) PRIMARY KEY,
orderId BIGINT NOT NULL,
paymentMethod NVARCHAR(255) NOT NULL,
paymentStatus NVARCHAR(255) NOT NULL,
createdAt DATETIME NOT NULL,
updatedAt DATETIME NULL,
FOREIGN KEY (orderId) REFERENCES Orders(orderId)
);
-- 类别表
CREATE TABLE Category (
categoryId BIGINT IDENTITY(1,1) PRIMARY KEY,
categoryName NVARCHAR(255) NOT NULL
);
-- 商品和类别的联结表
CREATE TABLE ProductCategory (
productId BIGINT NOT NULL,
categoryId BIGINT NOT NULL,
PRIMARY KEY (productId, categoryId),
FOREIGN KEY (productId) REFERENCES Products(productId),
FOREIGN KEY (categoryId) REFERENCES Category(categoryId)
);
-- 地址表
CREATE TABLE Address (
addressId BIGINT IDENTITY(1,1) PRIMARY KEY,
userId BIGINT NOT NULL,
fullAddress NVARCHAR(255),
FOREIGN KEY (userId) REFERENCES Users(userId)
);
-- 库存表
CREATE TABLE Stock (
stockId BIGINT IDENTITY(1,1) PRIMARY KEY,
productId BIGINT NOT NULL,
quantity INT NOT NULL,
reservedQuantity INT,
stockLocation NVARCHAR(255),
batchNo NVARCHAR(255),
stockStatus NVARCHAR(255),
stockCost DECIMAL(10,2),
stockValue DECIMAL(10,2),
createdAt DATETIME NOT NULL,
updatedAt DATETIME NULL,
FOREIGN KEY (productId) REFERENCES Products(productId)
);
-- 评论表
CREATE TABLE Review (
reviewId BIGINT IDENTITY(1,1) PRIMARY KEY,
content NVARCHAR(MAX),
rating INT,
createdAt DATETIME NOT NULL,
userId BIGINT NOT NULL,
productId BIGINT NOT NULL,
FOREIGN KEY (userId) REFERENCES Users(userId),
FOREIGN KEY (productId) REFERENCES Products(productId)
);
-- 心愿单表
CREATE TABLE WishlistItem (
wishlistItemId BIGINT IDENTITY(1,1) PRIMARY KEY,
userId BIGINT NOT NULL,
FOREIGN KEY (userId) REFERENCES Users(userId)
);
-- 心愿单和商品的联结表
CREATE TABLE WishlistProduct (
wishlistItemId BIGINT NOT NULL,
productId BIGINT NOT NULL,
PRIMARY KEY (wishlistItemId, productId),
FOREIGN KEY (wishlistItemId) REFERENCES WishlistItem(wishlistItemId),
FOREIGN KEY (productId) REFERENCES Products(productId)
);
-- 优惠券表
CREATE TABLE Coupon (
couponId BIGINT IDENTITY(1,1) PRIMARY KEY,
couponCode NVARCHAR(255) NOT NULL UNIQUE,
discount DECIMAL(10, 2) NOT NULL,
validFrom DATETIME NOT NULL,
validTo DATETIME NOT NULL,
userId BIGINT,
used BIT DEFAULT 0, -- 添加 used 字段,默认值为 0(表示未使用)
FOREIGN KEY (userId) REFERENCES Users(userId)
);
-- 产品推荐表
CREATE TABLE ProductRecommendation (
recommendationId BIGINT IDENTITY(1,1) PRIMARY KEY,
productId BIGINT NOT NULL,
recommendedProductId BIGINT NOT NULL,
FOREIGN KEY (productId) REFERENCES Products(productId),
FOREIGN KEY (recommendedProductId) REFERENCES Products(productId)
);
-- 促销表
CREATE TABLE Promotion (
promotionId BIGINT IDENTITY(1,1) PRIMARY KEY,
promotionDescription NVARCHAR(MAX) NOT NULL,
startDate DATETIME NOT NULL,
endDate DATETIME NOT NULL,
discount DECIMAL(18, 2) NOT NULL,
promotionType NVARCHAR(255) NOT NULL
);
-- 产品和促销活动的联结表
CREATE TABLE PromotionProduct (
promotionId BIGINT NOT NULL,
productId BIGINT NOT NULL,
PRIMARY KEY (promotionId, productId),
FOREIGN KEY (promotionId) REFERENCES Promotion(promotionId),
FOREIGN KEY (productId) REFERENCES Products(productId)
);
-- 用户和促销活动的联结表
CREATE TABLE PromotionUser (
promotionId BIGINT NOT NULL,
userId BIGINT NOT NULL,
PRIMARY KEY (promotionId, userId),
FOREIGN KEY (promotionId) REFERENCES Promotion(promotionId),
FOREIGN KEY (userId) REFERENCES Users(userId)
);
-- 类别和促销活动的联结表
CREATE TABLE PromotionCategory (
promotionId BIGINT NOT NULL,
categoryId BIGINT NOT NULL,
PRIMARY KEY (promotionId, categoryId),
FOREIGN KEY (promotionId) REFERENCES Promotion(promotionId),
FOREIGN KEY (categoryId) REFERENCES Category(categoryId)
);
-- 用户活动日志表
CREATE TABLE UserActivityLog (
userActivityLogId BIGINT IDENTITY(1,1) PRIMARY KEY,
userId BIGINT NOT NULL,
activityType NVARCHAR(255) NOT NULL,
activityDate DATETIME NOT NULL,
logDescription NVARCHAR(MAX),
FOREIGN KEY (userId) REFERENCES Users(userId)
);
-- 用户间的社交功能表
CREATE TABLE Follow (
followId BIGINT IDENTITY(1,1) PRIMARY KEY,
followerId BIGINT NOT NULL,
followingId BIGINT NOT NULL,
createdAt DATETIME NOT NULL,
FOREIGN KEY (followerId) REFERENCES Users(userId),
FOREIGN KEY (followingId) REFERENCES Users(userId)
);
-- SKU管理表
--SKU(庫存單位)管理是電子商務平台特別關注的一個方面,它涉及到產品的多個變體,例如大小、顏色等。
CREATE TABLE SKU (
skuId BIGINT IDENTITY(1,1) PRIMARY KEY,
productId BIGINT NOT NULL,
skuCode NVARCHAR(255) NOT NULL,
price DECIMAL(18, 2) NOT NULL,
stockQuantity INT NOT NULL,
attributes NVARCHAR(MAX),
FOREIGN KEY (productId) REFERENCES Products(productId)
);