数据库实现火车订票系统 代码
时间: 2023-11-18 11:05:46 浏览: 33
以下是一个简单的火车订票系统的数据库设计,包括表结构和示例代码:
1. 火车信息表(train)
| 字段名 | 数据类型 | 描述 |
| ------ | -------- | ---- |
| id | int | 火车编号(主键) |
| name | varchar | 火车名称 |
| start_station | varchar | 起始站 |
| end_station | varchar | 终点站 |
| start_time | datetime | 发车时间 |
| end_time | datetime | 到站时间 |
| duration | int | 行车时间(分钟) |
| price | decimal | 票价 |
示例代码:
CREATE TABLE train (
id INT PRIMARY KEY,
name VARCHAR(50),
start_station VARCHAR(50),
end_station VARCHAR(50),
start_time DATETIME,
end_time DATETIME,
duration INT,
price DECIMAL(8,2)
);
2. 座位信息表(seat)
| 字段名 | 数据类型 | 描述 |
| ------ | -------- | ---- |
| id | int | 座位编号(主键)|
| train_id | int | 火车编号(外键)|
| type | varchar | 座位类型(硬座、软座、硬卧、软卧等)|
| capacity | int | 座位总数 |
| available | int | 可用座位数 |
示例代码:
CREATE TABLE seat (
id INT PRIMARY KEY,
train_id INT,
type VARCHAR(20),
capacity INT,
available INT,
FOREIGN KEY (train_id) REFERENCES train(id)
);
3. 订单信息表(order)
| 字段名 | 数据类型 | 描述 |
| ------ | -------- | ---- |
| id | int | 订单编号(主键)|
| train_id | int | 火车编号(外键)|
| seat_id | int | 座位编号(外键)|
| user_id | int | 用户编号(外键)|
| order_time | datetime | 订单时间 |
| start_station | varchar | 起始站 |
| end_station | varchar | 终点站 |
| start_time | datetime | 发车时间 |
| end_time | datetime | 到站时间 |
| price | decimal | 票价 |
示例代码:
CREATE TABLE order (
id INT PRIMARY KEY,
train_id INT,
seat_id INT,
user_id INT,
order_time DATETIME,
start_station VARCHAR(50),
end_station VARCHAR(50),
start_time DATETIME,
end_time DATETIME,
price DECIMAL(8,2),
FOREIGN KEY (train_id) REFERENCES train(id),
FOREIGN KEY (seat_id) REFERENCES seat(id),
FOREIGN KEY (user_id) REFERENCES user(id)
);
4. 用户信息表(user)
| 字段名 | 数据类型 | 描述 |
| ------ | -------- | ---- |
| id | int | 用户编号(主键)|
| name | varchar | 用户名 |
| password | varchar | 密码 |
| email | varchar | 邮箱 |
| phone | varchar | 手机号 |
示例代码:
CREATE TABLE user (
id INT PRIMARY KEY,
name VARCHAR(50),
password VARCHAR(50),
email VARCHAR(50),
phone VARCHAR(20)
);
以上是一个简单的火车订票系统的数据库设计,可以根据实际需求进行修改和完善。同时,需要注意在进行数据操作时需要使用事务来保证数据的一致性和完整性。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)