CREATE DATABASE if NOT EXISTS xskc CHARSET UTF8 COLLATE UTF8_GENERAL_CI; use xskc; CREATE TABLE STUDENT ( Sno char(9)primary key, Sname char(20), Ssex char(2)check(Ssex in('男','女')) , Sage smallint check(Sage>=10 and Sage<=60), Sdept char(20) ); insert into STUDENT(Sno,Sname,Ssex,Sage,Sdept)values(201215121,'李勇','男',20,'CS'); insert into STUDENT(Sno,Sname,Ssex,Sage,Sdept)values(201215122,'刘晨','女',19,'IS'); insert into STUDENT(Sno,Sname,Ssex,Sage,Sdept)values(201215123,'王敏','男',18,'MA'); insert into STUDENT(Sno,Sname,Ssex,Sage,Sdept)values(201215124,'张立','男',19,'CS'); select * from STUDENT; CREATE TABLE COURSE ( Cno char(4)primary key, Cname char(40), Cpno char(4), foreign key(Cpno)references COURSE(Cno), Ccredit smallint ); insert into COURSE(Cno,Cname,Cpno,Ccredit)values(1,'数据库',NULL,4); insert into COURSE(Cno,Cname,Cpno,Ccredit)values(2,'数学',NULL,2); insert into COURSE(Cno,Cname,Cpno,Ccredit)values(3,'信息系统',1,4); insert into COURSE(Cno,Cname,Cpno,Ccredit)values(4,'操作系统',2,3); insert into COURSE(Cno,Cname,Cpno,Ccredit)values(5,'数据结构',3,4); insert into COURSE(Cno,Cname,Cpno,Ccredit)values(6,'数据分析',NULL,4); insert into COURSE(Cno,Cname,Cpno,Ccredit)values(7,'PASCAL语言',6,4); select * from COURSE; CREATE TABLE SC ( Sno char(9), Cno char(4), Grade smallint check(Grade>=10 and Grade<=100), primary key(Sno,Cno), foreign key(Sno) references STUDENT(Sno), foreign key(Cno) references COURSE(Cno) ); insert into SC(Sno,Cno,Grade)values(201215121,1,92); insert into SC(Sno,Cno,Grade)values(201215121,2,85); insert into SC(Sno,Cno,Grade)values(201215121,3,88); insert into SC(Sno,Cno,Grade)values(201215122,2,90); insert into SC(Sno,Cno,Grade)values(201215122,3,80);
时间: 2023-10-11 13:09:36 浏览: 262
好的,我看到您创建了三张表:`STUDENT`、`COURSE` 和 `SC`,并向它们插入了一些数据。表 `STUDENT` 存储了学生的基本信息,表 `COURSE` 存储了课程的信息,表 `SC` 存储了学生选课的成绩信息。接下来,您可以使用 SQL 语句来查询学生的成绩信息,如下:
1. 查询学生 `201215121` 的选课情况和成绩:
```
SELECT c.Cname, sc.Grade
FROM SC sc
JOIN COURSE c ON sc.Cno = c.Cno
WHERE sc.Sno = 201215121;
```
执行该 SQL 语句后,将会返回学号为 `201215121` 的学生选课的课程名称和成绩信息。
2. 查询课程 `数据库` 的选课学生情况和成绩:
```
SELECT s.Sname, sc.Grade
FROM SC sc
JOIN STUDENT s ON sc.Sno = s.Sno
JOIN COURSE c ON sc.Cno = c.Cno
WHERE c.Cname = '数据库';
```
执行该 SQL 语句后,将会返回选修了 `数据库` 课程的学生的姓名和成绩信息。
3. 查询所有学生的选课情况和成绩:
```
SELECT s.Sno, s.Sname, c.Cname, sc.Grade
FROM SC sc
JOIN STUDENT s ON sc.Sno = s.Sno
JOIN COURSE c ON sc.Cno = c.Cno;
```
执行该 SQL 语句后,将会返回所有学生的选课情况和成绩信息。
阅读全文