import mysql.connector from mysql.connector import errorcode DB_CONFIG = { 'host': 'localhost', 'user': 'root', 'password': '123456', # 替换为你的 MySQL 密码 'port': 3306 } DATABASE_NAME = 'tgst01' SQL_SCRIPT = """ SET NAMES utf8mb4; SET FOREIGN_KEY_CHECKS = 0; DROP TABLE IF EXISTS `coupons`; CREATE TABLE `coupons` ( `coupon_id` int NOT NULL AUTO_INCREMENT, `coupon_code` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL, `discount_type` enum('fixed','percentage') CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL, `discount_amount` decimal(10,2) DEFAULT NULL, `min_order_amount` decimal(10,2) DEFAULT NULL, `valid_from` date DEFAULT NULL, `valid_to` date DEFAULT NULL, `is_active` tinyint(1) DEFAULT '1', `created_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP, `updated_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, PRIMARY KEY (`coupon_id`), UNIQUE KEY `coupon_code` (`coupon_code`) ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin; DROP TABLE IF EXISTS `game_game_tags`; CREATE TABLE `game_game_tags` ( `game_tag_relation_id` int NOT NULL AUTO_INCREMENT, `game_id` int DEFAULT NULL, `tag_id` int DEFAULT NULL, PRIMARY KEY (`game_tag_relation_id`), KEY `game_id` (`game_id`), KEY `tag_id` (`tag_id`), CONSTRAINT `game_game_tags_ibfk_1` FOREIGN KEY (`game_id`) REFERENCES `games` (`game_id`), CONSTRAINT `game_game_tags_ibfk_2` FOREIGN KEY (`tag_id`) REFERENCES `game_tags` (`tag_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin; CREATE TABLE `game_groups` ( `group_id` int NOT NULL AUTO_INCREMENT, `user_id` int DEFAULT NULL, `group_name` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL, `description` text CHARACTER SET utf8mb4 COLLATE utf8mb4_bin, `start_date` date DEFAULT NULL, `start_time` datetime DEFAULT NULL, `end_time` datetime DEFAULT NULL, `max_members` int DEFAULT NULL, `group_status` enum('recruiting','full','completed','cancelled','pause') CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT 'recruiting', `created_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP, `updated_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, PRIMARY KEY (`group_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin; CREATE TABLE `game_tables` ( `table_id` int NOT NULL AUTO_INCREMENT COMMENT '服务器桌号', `game_table_number` text CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL COMMENT '物理桌号', `capacity` int NOT NULL COMMENT '承载人数', `price` decimal(10,2) NOT NULL COMMENT '价格倍率', PRIMARY KEY (`table_id`) ) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin; CREATE TABLE `announcement` ( `id` int NOT NULL AUTO_INCREMENT, `text` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL, `start_time` datetime NOT NULL, `end_time` datetime NOT NULL, `color` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin; CREATE TABLE `user_coupons` ( `user_coupon_id` int NOT NULL AUTO_INCREMENT, `user_id` int NOT NULL, `coupon_id` int NOT NULL, `obtained_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP, `used_at` timestamp NULL DEFAULT NULL, `is_used` tinyint(1) DEFAULT '0', `valid_from` date DEFAULT NULL, `valid_to` date DEFAULT NULL, PRIMARY KEY (`user_coupon_id`), UNIQUE KEY `unique_user_coupon` (`user_id`,`coupon_id`), FOREIGN KEY (`user_id`) REFERENCES `users` (`user_id`) ON DELETE CASCADE, FOREIGN KEY (`coupon_id`) REFERENCES `coupons` (`coupon_id`) ON DELETE CASCADE ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin; DROP TABLE IF EXISTS `game_tags`; CREATE TABLE `game_tags` ( `tag_id` int NOT NULL AUTO_INCREMENT, `tag_name` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL, PRIMARY KEY (`tag_id`), UNIQUE KEY `tag_name` (`tag_name`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin; CREATE TABLE `games` ( `game_id` int NOT NULL AUTO_INCREMENT, `game_name` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL, `game_type` int NOT NULL, `description` text CHARACTER SET utf8mb4 COLLATE utf8mb4_bin, `min_players` int DEFAULT NULL, `max_players` int DEFAULT NULL, `duration` int DEFAULT NULL, `price` decimal(10,2) DEFAULT NULL, `difficulty_level` int DEFAULT NULL, `is_available` tinyint(1) DEFAULT '1', `created_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP, `updated_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, `quantity` int DEFAULT NULL, `photo_url` varchar(255) COLLATE utf8mb4_bin DEFAULT NULL, PRIMARY KEY (`game_id`) ) ENGINE=InnoDB AUTO_INCREMENT=1001 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin; DROP TABLE IF EXISTS `group_members`; CREATE TABLE `group_members` ( `group_member_id` int NOT NULL AUTO_INCREMENT, `group_id` int DEFAULT NULL, `user_id` int DEFAULT NULL, `join_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP, `entry_status` int DEFAULT '1', PRIMARY KEY (`group_member_id`), KEY `group_id` (`group_id`), CONSTRAINT `group_members_ibfk_1` FOREIGN KEY (`group_id`) REFERENCES `game_groups` (`group_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin; DROP TABLE IF EXISTS `order_coupons`; CREATE TABLE `order_coupons` ( `order_coupon_id` int NOT NULL AUTO_INCREMENT, `order_id` int DEFAULT NULL, `coupon_id` int DEFAULT NULL, PRIMARY KEY (`order_coupon_id`), KEY `order_id` (`order_id`), KEY `coupon_id` (`coupon_id`), CONSTRAINT `order_coupons_ibfk_1` FOREIGN KEY (`order_id`) REFERENCES `orders` (`order_id`), CONSTRAINT `order_coupons_ibfk_2` FOREIGN KEY (`coupon_id`) REFERENCES `coupons` (`coupon_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin; DROP TABLE IF EXISTS `orders`; CREATE TABLE `orders` ( `order_id` int NOT NULL COMMENT '订单 id', `user_id` int NOT NULL COMMENT '下单用户 id', `game_table_id` int NOT NULL COMMENT '占用游戏桌 id', `game_id` int DEFAULT NULL COMMENT '使用游戏 id', `order_date` date NOT NULL COMMENT '下单时间', `start_datetime` datetime NOT NULL COMMENT '订单开始时间', `end_datetime` datetime DEFAULT NULL COMMENT '订单结束时间', `num_players` int NOT NULL COMMENT '游玩人数', `payable_price` decimal(10,2) DEFAULT NULL COMMENT '未优惠价格', `order_status` enum('pending','paid','in_progress','completed','cancelled') CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL DEFAULT 'pending' COMMENT '订单状态', `payment_method` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL COMMENT '优惠后价格', `coupon_id` int DEFAULT NULL COMMENT '优惠卷 id', `used_points` int DEFAULT NULL COMMENT '使用积分', `paid_price` decimal(10,2) DEFAULT '0.00' COMMENT '优惠后价格', `game_process_time` int DEFAULT NULL COMMENT '游戏时长', `settlement_time` datetime DEFAULT NULL, PRIMARY KEY (`order_id`), KEY `user_id` (`user_id`), KEY `game_table_id` (`game_table_id`), KEY `game_id` (`game_id`), KEY `coupon_id` (`coupon_id`), CONSTRAINT `orders_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `users` (`user_id`), CONSTRAINT `orders_ibfk_2` FOREIGN KEY (`game_table_id`) REFERENCES `game_tables` (`table_id`), CONSTRAINT `orders_ibfk_3` FOREIGN KEY (`game_id`) REFERENCES `games` (`game_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin; DROP TABLE IF EXISTS `points_history`; CREATE TABLE `points_history` ( `id` int NOT NULL AUTO_INCREMENT, `user_id` int NOT NULL, `change_amount` int NOT NULL, `reason` varchar(255) COLLATE utf8mb4_bin NOT NULL, `created_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (`id`), KEY `user_id` (`user_id`), CONSTRAINT `points_history_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `users` (`user_id`) ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin; DROP TABLE IF EXISTS `reviews`; CREATE TABLE `reviews` ( `review_id` int NOT NULL AUTO_INCREMENT, `user_id` int DEFAULT NULL, `game_id` int DEFAULT NULL, `rating` int DEFAULT NULL, `comment` text CHARACTER SET utf8mb4 COLLATE utf8mb4_bin, `created_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP, `updated_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, PRIMARY KEY (`review_id`), KEY `user_id` (`user_id`), KEY `game_id` (`game_id`), CONSTRAINT `reviews_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `users` (`user_id`), CONSTRAINT `reviews_ibfk_2` FOREIGN KEY (`game_id`) REFERENCES `games` (`game_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin; DROP TABLE IF EXISTS `users`; CREATE TABLE `users` ( `user_id` int NOT NULL AUTO_INCREMENT, `username` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL, `password` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL, `phone_number` varchar(15) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL, `email` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL, `user_type` enum('player','admin') CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT 'player', `gender` enum('male','female','other') CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL, `points` int DEFAULT '0', `created_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP, `updated_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, PRIMARY KEY (`user_id`), UNIQUE KEY `phone_number` (`phone_number`) ) ENGINE=InnoDB AUTO_INCREMENT=62 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin; SET FOREIGN_KEY_CHECKS = 1; """ def create_database_and_tables(): try: # 第一步:先连到 MySQL Server(不指定数据库) cnx = mysql.connector.connect(**DB_CONFIG) cursor = cnx.cursor() # 创建数据库(如果不存在则创建) cursor.execute(f"CREATE DATABASE IF NOT EXISTS {DATABASE_NAME} CHARACTER SET utf8mb4 COLLATE utf8mb4_bin;") print(f"Database {DATABASE_NAME} created or already exists.") cursor.close() cnx.close() # 第二步:重新连接到刚创建/已存在的数据库 cnx = mysql.connector.connect(database=DATABASE_NAME, **DB_CONFIG) cursor = cnx.cursor() # 由于脚本中有多条语句,需要手动分割执行 # 简单做法:按分号分割,然后逐条执行 # 注意如果脚本中有存储过程/触发器之类带有分号的场景,需要更复杂的处理。 statements = SQL_SCRIPT.split(';') for stmt in statements: # 去除前后空格 stmt = stmt.strip() if stmt: # 如果不为空,就执行 cursor.execute(stmt) cursor.close() cnx.close() print("All tables created successfully!") except mysql.connector.Error as err: if err.errno == errorcode.ER_ACCESS_DENIED_ERROR: print("连接数据库失败:用户名或密码错误。") elif err.errno == errorcode.ER_BAD_DB_ERROR: print("数据库不存在,且创建失败。") else: print(err) except Exception as e: print("执行过程中出现错误:", e) if __name__ == "__main__": create_database_and_tables()