学生公寓管理系统mysqlSQL语句
时间: 2023-07-06 07:26:36 浏览: 101
好的,以下是一些常用的学生公寓管理系统SQL语句:
1. 创建数据库:
```sql
CREATE DATABASE IF NOT EXISTS `student_apartment` DEFAULT CHARSET utf8 COLLATE utf8_general_ci;
```
2. 创建学生信息表:
```sql
CREATE TABLE IF NOT EXISTS `student` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '学生ID',
`name` varchar(20) NOT NULL COMMENT '学生姓名',
`gender` tinyint(1) NOT NULL DEFAULT '0' COMMENT '性别,0表示男,1表示女',
`age` tinyint(3) NOT NULL COMMENT '年龄',
`tel` varchar(11) DEFAULT NULL COMMENT '电话',
`email` varchar(50) DEFAULT NULL COMMENT '邮箱',
`department` varchar(50) DEFAULT NULL COMMENT '所属院系',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1001 DEFAULT CHARSET=utf8;
```
3. 创建公寓信息表:
```sql
CREATE TABLE IF NOT EXISTS `apartment` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '公寓ID',
`name` varchar(50) NOT NULL COMMENT '公寓名称',
`building` varchar(50) NOT NULL COMMENT '所属楼栋',
`floor` tinyint(3) NOT NULL COMMENT '所在楼层',
`room_num` tinyint(3) NOT NULL COMMENT '房间号',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1001 DEFAULT CHARSET=utf8;
```
4. 创建入住信息表:
```sql
CREATE TABLE IF NOT EXISTS `checkin` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '入住记录ID',
`student_id` int(11) NOT NULL COMMENT '学生ID',
`apartment_id` int(11) NOT NULL COMMENT '公寓ID',
`checkin_date` date NOT NULL COMMENT '入住时间',
`checkout_date` date DEFAULT NULL COMMENT '退房时间',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1001 DEFAULT CHARSET=utf8;
```
5. 查询所有学生信息:
```sql
SELECT * FROM `student`;
```
6. 查询所有公寓信息:
```sql
SELECT * FROM `apartment`;
```
7. 查询所有入住记录:
```sql
SELECT * FROM `checkin`;
```
8. 查询某个学生的入住记录:
```sql
SELECT c.*, a.`name` AS `apartment_name` FROM `checkin` AS c
LEFT JOIN `apartment` AS a ON c.`apartment_id` = a.`id`
WHERE c.`student_id` = 1001;
```
9. 查询某个公寓的入住情况:
```sql
SELECT c.*, s.`name` AS `student_name` FROM `checkin` AS c
LEFT JOIN `student` AS s ON c.`student_id` = s.`id`
WHERE c.`apartment_id` = 1001;
```
以上是一些常用的SQL语句,您可以根据具体需求进行修改。
阅读全文