用mysql语句创建一个寝室数据库
时间: 2023-12-21 17:07:34 浏览: 120
好的,以下是一个简单的 MySQL 代码示例,用于创建一个寝室管理系统的数据库,你可以根据实际需求进行适当修改:
```
CREATE DATABASE dormitory_management; -- 创建一个名为 dormitory_management 的数据库
USE dormitory_management; -- 切换到该数据库
-- 创建学生表
CREATE TABLE student (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(50) NOT NULL,
gender ENUM('male', 'female') NOT NULL,
age INT NOT NULL,
phone VARCHAR(20) NOT NULL,
dormitory_id INT NOT NULL,
bed_number INT NOT NULL,
FOREIGN KEY (dormitory_id) REFERENCES dormitory(id)
);
-- 创建宿舍表
CREATE TABLE dormitory (
id INT PRIMARY KEY AUTO_INCREMENT,
dorm_number VARCHAR(20) NOT NULL,
building VARCHAR(50) NOT NULL,
capacity INT NOT NULL
);
-- 创建入住信息表
CREATE TABLE check_in (
id INT PRIMARY KEY AUTO_INCREMENT,
student_id INT NOT NULL,
dormitory_id INT NOT NULL,
bed_number INT NOT NULL,
check_in_time DATETIME NOT NULL,
FOREIGN KEY (student_id) REFERENCES student(id),
FOREIGN KEY (dormitory_id) REFERENCES dormitory(id)
);
-- 创建退宿信息表
CREATE TABLE check_out (
id INT PRIMARY KEY AUTO_INCREMENT,
student_id INT NOT NULL,
dormitory_id INT NOT NULL,
bed_number INT NOT NULL,
check_out_time DATETIME NOT NULL,
FOREIGN KEY (student_id) REFERENCES student(id),
FOREIGN KEY (dormitory_id) REFERENCES dormitory(id)
);
-- 创建维修信息表
CREATE TABLE maintenance (
id INT PRIMARY KEY AUTO_INCREMENT,
dormitory_id INT NOT NULL,
repairman VARCHAR(50) NOT NULL,
repair_content TEXT NOT NULL,
repair_time DATETIME NOT NULL,
FOREIGN KEY (dormitory_id) REFERENCES dormitory(id)
);
-- 创建报修信息表
CREATE TABLE repair (
id INT PRIMARY KEY AUTO_INCREMENT,
student_id INT NOT NULL,
dormitory_id INT NOT NULL,
bed_number INT NOT NULL,
repair_content TEXT NOT NULL,
repair_time DATETIME NOT NULL,
status ENUM('pending', 'completed') DEFAULT 'pending',
FOREIGN KEY (student_id) REFERENCES student(id),
FOREIGN KEY (dormitory_id) REFERENCES dormitory(id)
);
```
以上是一个简单的寝室管理系统数据库的创建示例,你可以根据具体需求进行修改和扩展。
阅读全文