-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmysql.sql
78 lines (71 loc) · 2.99 KB
/
mysql.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
CREATE DATABASE IF NOT EXISTS `second_hand_trading_system`;
USE `second_hand_trading_system`;
CREATE TABLE `user` (
`user_id` int NOT NULL AUTO_INCREMENT,
`name` varchar(50) NOT NULL,
`contact_info` varchar(100) DEFAULT NULL,
`email` varchar(100) DEFAULT NULL,
`password` varchar(255) NOT NULL,
`role` enum('buyer','seller') NOT NULL,
`registration_date` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`user_id`),
UNIQUE KEY `email_UNIQUE` (`email`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
CREATE TABLE `product` (
`product_id` int NOT NULL AUTO_INCREMENT,
`name` varchar(100) NOT NULL,
`description` text,
`price` decimal(10,2) NOT NULL,
`category` varchar(100) DEFAULT NULL,
`upload_date` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
`status` enum('available','sold') DEFAULT 'available',
`user_id` int DEFAULT NULL,
PRIMARY KEY (`product_id`),
KEY `seller_id_idx` (`user_id`),
CONSTRAINT `fk_Product_1_user_id` FOREIGN KEY (`user_id`) REFERENCES `User` (`user_id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
CREATE TABLE `orders` (
`order_id` int NOT NULL AUTO_INCREMENT,
`buyer_id` int DEFAULT NULL,
`seller_id` int DEFAULT NULL,
`product_id` int DEFAULT NULL,
`order_date` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
`status` enum('completed','pending','cancelled') DEFAULT 'pending',
PRIMARY KEY (`order_id`),
KEY `buyer_id` (`buyer_id`),
KEY `seller_id` (`seller_id`),
KEY `product_id` (`product_id`),
CONSTRAINT `Order_ibfk_3` FOREIGN KEY (`product_id`) REFERENCES `Product` (`product_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
CREATE TABLE `product_images` (
`imageid` int NOT NULL AUTO_INCREMENT,
`productid` int NOT NULL,
`image_path` varchar(255) NOT NULL,
`created_at` datetime DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`imageid`),
KEY `productid` (`productid`),
CONSTRAINT `product_images_ibfk_1` FOREIGN KEY (`productid`) REFERENCES `Product` (`product_id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
CREATE TABLE `private_message` (
`id` int NOT NULL,
`uid_from` int NOT NULL,
`uid_to` int NOT NULL,
`message_content` text CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
CREATE TABLE `message` (
`id` int unsigned NOT NULL AUTO_INCREMENT,
`user_id` int NOT NULL,
`message_content` text CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
`date` timestamp NOT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
# 创建触发器
DELIMITER //
CREATE TRIGGER after_insert_order
AFTER INSERT ON orders
FOR EACH ROW
BEGIN
UPDATE product SET product.status = "pending" WHERE product.product_id = NEW.product_id;
END//
DELIMITER ;