写出下列操作的SQL语句 S(学号SNO,姓名SNAME,年龄SAGE,性别SSEX) SC(学号SNO,课程号CNO,成绩GRADE) C(课程号CNO,课程名CNAME,任课教师TEACHER) 试用关系代数表达式表示下列查询语句。 (1)检索张三老师所授课程的课程号、课程名。(3分) (2)检索学号为003学生所学课程的课程名与任课教师名。(3分) (3)检索至少选修刘平所授课程中一门课程的女学生的姓名。(3分) (4)检索“王”同学不学的课程号。(3分) (5)检索全部学生都选修的课程的课程号与课程名。(4分)
时间: 2024-01-11 15:04:00 浏览: 492
(1) SQL语句:
```sql
SELECT CNO, CNAME FROM C WHERE TEACHER = '张三';
```
关系代数表达式:
$$\pi_{CNO, CNAME}(\sigma_{TEACHER='张三'}(C))$$
(2) SQL语句:
```sql
SELECT C.CNAME, C.TEACHER
FROM C, SC
WHERE C.CNO = SC.CNO AND SC.SNO = '003';
```
关系代数表达式:
$$\pi_{CNAME, TEACHER}(\sigma_{SNO='003'}(SC \Join C))$$
(3) SQL语句:
```sql
SELECT DISTINCT S.SNAME
FROM S, SC
WHERE S.SNO = SC.SNO AND SC.CNO IN (
SELECT CNO
FROM C
WHERE TEACHER = '刘平'
) AND S.SSEX = '女';
```
关系代数表达式:
$$\pi_{SNAME}(\sigma_{SSEX='女'}(S \Join SC \Join (\pi_{CNO}(C \Join \sigma_{TEACHER='刘平'}(C)))))$$
(4) SQL语句:
```sql
SELECT DISTINCT CNO
FROM SC
WHERE SNO = '王' AND CNO NOT IN (
SELECT CNO
FROM SC
WHERE SNO = '王'
);
```
关系代数表达式:
$$\pi_{CNO}(\sigma_{SNO='王'}(SC)) - \pi_{CNO}(\sigma_{SNO='王'}(SC) \Join SC)$$
(5) SQL语句:
```sql
SELECT C.CNO, C.CNAME
FROM C
WHERE NOT EXISTS (
SELECT *
FROM S
WHERE NOT EXISTS (
SELECT *
FROM SC
WHERE SC.SNO = S.SNO AND SC.CNO = C.CNO
)
);
```
关系代数表达式:
$$\pi_{CNO,CNAME}(C) - \pi_{CNO,CNAME}(\sigma_{\exists_{S} \forall_{SC}(SC.SNO=S.SNO \land SC.CNO=C.CNO)}(C \Join SC \Join S))$$
阅读全文