用mysql设计一个学生宿舍管理系统,写出详细过程与代码
时间: 2024-05-28 10:08:40 浏览: 13
设计思路:
1. 数据库设计
- 学生表:包含学生的基本信息,包括学号、姓名、性别、年龄、所在宿舍号等字段;
- 宿舍表:包含宿舍的基本信息,包括宿舍号、楼栋号、可容纳人数、当前入住人数等字段;
- 入住表:记录学生入住宿舍的情况,包括学号、宿舍号、入住时间等字段。
2. 数据库操作
- 查询学生信息:根据学号、姓名等条件查询学生信息;
- 查询宿舍信息:根据宿舍号、楼栋号等条件查询宿舍信息;
- 查询入住信息:根据学号、宿舍号等条件查询入住信息;
- 录入学生信息:新增学生信息;
- 录入宿舍信息:新增宿舍信息;
- 学生入住:新增入住信息;
- 学生退宿:删除入住信息。
3. 实现代码
学生表
CREATE TABLE `student` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(20) NOT NULL,
`gender` varchar(10) NOT NULL,
`age` int(11) NOT NULL,
`dormitory` varchar(20) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
宿舍表
CREATE TABLE `dormitory` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`building` varchar(20) NOT NULL,
`dorm_num` varchar(20) NOT NULL,
`capacity` int(11) NOT NULL,
`current_num` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
入住表
CREATE TABLE `checkin` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`student_id` int(11) NOT NULL,
`dormitory_id` int(11) NOT NULL,
`checkin_time` datetime NOT NULL,
PRIMARY KEY (`id`),
KEY `student_id` (`student_id`),
KEY `dormitory_id` (`dormitory_id`),
CONSTRAINT `checkin_ibfk_1` FOREIGN KEY (`student_id`) REFERENCES `student` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `checkin_ibfk_2` FOREIGN KEY (`dormitory_id`) REFERENCES `dormitory` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
查询学生信息
SELECT * FROM student WHERE id=1;
查询宿舍信息
SELECT * FROM dormitory WHERE dorm_num='101';
查询入住信息
SELECT * FROM checkin WHERE student_id=1;
录入学生信息
INSERT INTO student (name, gender, age, dormitory) VALUES ('小明', '男', 18, '101');
录入宿舍信息
INSERT INTO dormitory (building, dorm_num, capacity, current_num) VALUES ('1号楼', '101', 4, 2);
学生入住
INSERT INTO checkin (student_id, dormitory_id, checkin_time) VALUES (1, 1, NOW());
学生退宿
DELETE FROM checkin WHERE student_id=1;
相关推荐
![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)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![java](https://img-home.csdnimg.cn/images/20210720083646.png)