sql server create语句创建学生表,课程表,学生-课程表
时间: 2024-05-10 17:20:10 浏览: 75
创建学生表:
```
CREATE TABLE Students (
StudentID INT PRIMARY KEY,
Name VARCHAR(50) NOT NULL,
Age INT NOT NULL,
Gender CHAR(1) NOT NULL
);
```
创建课程表:
```
CREATE TABLE Courses (
CourseID INT PRIMARY KEY,
Name VARCHAR(50) NOT NULL,
CreditHours INT NOT NULL
);
```
创建学生-课程表:
```
CREATE TABLE StudentCourses (
StudentID INT,
CourseID INT,
Grade DECIMAL(5, 2),
PRIMARY KEY (StudentID, CourseID),
FOREIGN KEY (StudentID) REFERENCES Students(StudentID),
FOREIGN KEY (CourseID) REFERENCES Courses(CourseID)
);
```
相关问题
--作业2: --使用create table语句 创建学生成绩管理系统数据库6个表对象,逻辑结构见附图。 --1. 创建学院表 --2. 创建教师表 --3. 创建学生表 --4. 创建课程表 --5. 创建课堂表 --6. 创建上课表
创建学生成绩管理系统数据库六个表的对象通常涉及到数据库设计的基本步骤。以下是每个表的简单描述以及对应的SQL `CREATE TABLE` 语句示例:
1. **学院表 (Department)**
```sql
CREATE TABLE Department (
department_id INT PRIMARY KEY,
department_name VARCHAR(50) NOT NULL,
location VARCHAR(100)
);
```
2. **教师表 (Teacher)**
```sql
CREATE TABLE Teacher (
teacher_id INT PRIMARY KEY,
teacher_name VARCHAR(100),
department_id INT,
FOREIGN KEY (department_id) REFERENCES Department(department_id)
);
```
3. **学生表 (Student)**
```sql
CREATE TABLE Student (
student_id INT PRIMARY KEY,
student_name VARCHAR(100),
birth_date DATE,
gender CHAR(1),
department_id INT,
FOREIGN KEY (department_id) REFERENCES Department(department_id)
);
```
4. **课程表 (Course)**
```sql
CREATE TABLE Course (
course_id INT PRIMARY KEY,
course_name VARCHAR(100),
credit_hours INT,
department_id INT,
FOREIGN KEY (department_id) REFERENCES Department(department_id)
);
```
5. **课堂表 (Classroom)**
```sql
CREATE TABLE Classroom (
classroom_id INT PRIMARY KEY,
room_number VARCHAR(20),
capacity INT,
building_name VARCHAR(50)
);
```
6. **上课表 (Attendance)**
```sql
CREATE TABLE Attendance (
attendance_id INT PRIMARY KEY,
student_id INT,
teacher_id INT,
course_id INT,
classroom_id INT,
class_date DATE,
FOREIGN KEY (student_id) REFERENCES Student(student_id),
FOREIGN KEY (teacher_id) REFERENCES Teacher(teacher_id),
FOREIGN KEY (course_id) REFERENCES Course(course_id),
FOREIGN KEY (classroom_id) REFERENCES Classroom(classroom_id)
);
```
用sqlServer语句创建课程表,完成排课管理操作,并输出学生和教师的课表
好的,以下是创建课程表的 SQL Server 语句:
首先,创建一个课程表的表格:
```
CREATE TABLE CourseSchedule (
CourseID INT NOT NULL,
TeacherID INT NOT NULL,
StudentID INT NOT NULL,
CourseName VARCHAR(50) NOT NULL,
CourseTime VARCHAR(20) NOT NULL,
CONSTRAINT PK_CourseSchedule PRIMARY KEY (CourseID)
);
```
然后,向表格中插入一些数据:
```
INSERT INTO CourseSchedule (CourseID, TeacherID, StudentID, CourseName, CourseTime)
VALUES (1, 101, 1001, '数学', '周一 10:00-12:00'),
(2, 102, 1002, '语文', '周一 14:00-16:00'),
(3, 103, 1003, '英语', '周二 8:00-10:00'),
(4, 104, 1004, '物理', '周二 10:00-12:00'),
(5, 105, 1005, '化学', '周三 10:00-12:00');
```
接下来,我们可以根据需要进行各种排课管理操作,例如:
1. 查询某个学生的课表:
```
SELECT CourseName, CourseTime
FROM CourseSchedule
WHERE StudentID = 1001;
```
2. 查询某个老师的课表:
```
SELECT CourseName, CourseTime
FROM CourseSchedule
WHERE TeacherID = 101;
```
3. 查询某个时间段内的所有课程:
```
SELECT CourseName, TeacherID, StudentID
FROM CourseSchedule
WHERE CourseTime LIKE '周二%';
```
希望这些 SQL 语句能够帮助你完成排课管理操作,并输出学生和教师的课表。
阅读全文