选修表的学号与学生表的学号一致(外键),课程号与课程表的课程号一致(外键),成绩如果是考试课则为0-100,如果是考查课则是“优、良、中、及格、不及格”SQL
时间: 2024-05-15 11:12:27 浏览: 97
创建选修表(Elective):
```sql
CREATE TABLE Elective (
id INT PRIMARY KEY,
student_id INT,
course_id INT,
score VARCHAR(10),
FOREIGN KEY (student_id) REFERENCES Student(id),
FOREIGN KEY (course_id) REFERENCES Course(id)
);
```
创建学生表(Student):
```sql
CREATE TABLE Student (
id INT PRIMARY KEY,
name VARCHAR(50),
age INT,
gender VARCHAR(10)
);
```
创建课程表(Course):
```sql
CREATE TABLE Course (
id INT PRIMARY KEY,
name VARCHAR(50),
teacher VARCHAR(50)
);
```
插入数据:
```sql
INSERT INTO Student (id, name, age, gender)
VALUES (1, '张三', 18, '男'),
(2, '李四', 19, '女'),
(3, '王五', 20, '男');
INSERT INTO Course (id, name, teacher)
VALUES (1, '数学', '张老师'),
(2, '语文', '李老师'),
(3, '英语', '王老师');
INSERT INTO Elective (id, student_id, course_id, score)
VALUES (1, 1, 1, '87'),
(2, 1, 2, '优'),
(3, 2, 1, '92'),
(4, 2, 3, '良'),
(5, 3, 2, '中'),
(6, 3, 3, '及格');
```
查询选修表(Elective):
```sql
SELECT * FROM Elective;
```
结果:
| id | student_id | course_id | score |
|----|------------|-----------|-------|
| 1 | 1 | 1 | 87 |
| 2 | 1 | 2 | 优 |
| 3 | 2 | 1 | 92 |
| 4 | 2 | 3 | 良 |
| 5 | 3 | 2 | 中 |
| 6 | 3 | 3 | 及格 |
查询学生表(Student):
```sql
SELECT * FROM Student;
```
结果:
| id | name | age | gender |
|----|------|-----|--------|
| 1 | 张三 | 18 | 男 |
| 2 | 李四 | 19 | 女 |
| 3 | 王五 | 20 | 男 |
查询课程表(Course):
```sql
SELECT * FROM Course;
```
结果:
| id | name | teacher |
|----|------|----------|
| 1 | 数学 | 张老师 |
| 2 | 语文 | 李老师 |
| 3 | 英语 | 王老师 |
阅读全文