sql server create语句创建学生-课程表
时间: 2024-06-06 16:07:21 浏览: 8
CREATE TABLE student_course (
student_id INT NOT NULL,
course_id INT NOT NULL,
grade INT,
PRIMARY KEY (student_id, course_id),
FOREIGN KEY (student_id) REFERENCES students(id),
FOREIGN KEY (course_id) REFERENCES courses(id)
);
其中,students和courses为学生表和课程表的表名,id为主键。
相关问题
sql server create语句创建学生表,课程表,学生-课程表
创建学生表:
```
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)
);
```
sql server创建学生课程选修表
创建学生课程选修表的SQL Server语句如下所示:
CREATE TABLE student_course (
student_id INT NOT NULL,
course_id INT NOT NULL,
PRIMARY KEY (student_id, course_id),
FOREIGN KEY (student_id) REFERENCES student(id),
FOREIGN KEY (course_id) REFERENCES course(id)
);
上述SQL语句创建了一个名为“student_course”的表,该表包含两个列:student_id和course_id。这两个列的数据类型都是INT,且都不能为空(NOT NULL)。
PRIMARY KEY约束指定了这两列的组合作为主键,即每个学生只能选择一个课程,每门课程只能被一个学生选修。
FOREIGN KEY约束用于确保学生ID和课程ID在相应的学生和课程表中存在。这样可以防止在学生课程选修表中插入不正确的ID值。
注意:在创建此表之前,需要先创建学生和课程表。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)