学生宿舍管理信息系统创建具体的sql语句
时间: 2024-07-04 20:01:14 浏览: 105
学生宿舍管理系统(C#+sql).rar.rar
5星 · 资源好评率100%
创建一个学生宿舍管理信息系统涉及到多个表和SQL语句,这里我会给出一些基本的示例,但请注意,实际系统可能需要更复杂的查询和设计。首先,假设我们有以下三个主要表:
1. **Students**(学生信息表)
2. **Rooms**(宿舍信息表)
3. **Reservations**(预定表)
**创建表(SQL语句):**
```sql
-- 学生信息表
CREATE TABLE Students (
ID INT PRIMARY KEY,
Name VARCHAR(100),
RoomID INT,
FOREIGN KEY (RoomID) REFERENCES Rooms(ID)
);
-- 宿舍信息表
CREATE TABLE Rooms (
ID INT PRIMARY KEY,
Capacity INT,
Location VARCHAR(100)
);
-- 预定表
CREATE TABLE Reservations (
ID INT AUTO_INCREMENT PRIMARY KEY,
StudentID INT,
RoomID INT,
StartDate DATE,
EndDate DATE,
FOREIGN KEY (StudentID) REFERENCES Students(ID),
FOREIGN KEY (RoomID) REFERENCES Rooms(ID)
);
```
**插入数据(示例):**
```sql
INSERT INTO Students (ID, Name, RoomID) VALUES (1, '张三', 1);
INSERT INTO Rooms (ID, Capacity, Location) VALUES (1, 4, 'A楼');
INSERT INTO Reservations (StudentID, RoomID, StartDate, EndDate) VALUES (1, 1, '2023-01-01', '2023-01-31');
```
**相关查询(SQL语句):**
1. 查询某个学生的住宿信息:
```sql
SELECT * FROM Students WHERE ID = [学生ID];
```
2. 获取某房间的当前居住学生:
```sql
SELECT Students.Name FROM Students JOIN Reservations ON Students.ID = Reservations.StudentID WHERE Reservations.RoomID = [房间ID] AND Reservations.EndDate > CURDATE();
```
3. 查看某个时间段内的所有预定:
```sql
SELECT * FROM Reservations WHERE StartDate >= [起始日期] AND EndDate <= [结束日期];
```
阅读全文