student表,包含stid(主键),name(姓名,不能为空),age,sex,classid(外键,引用class表主键) class表,包含classid(主键),classname(班级名称,不能为空,不能重复) grade表,包含id(主键),stid(外键,引用student表主键),total(总分) 1、使用create语句创建3张表 2、在class表中插入1条数据 3、删除grade表中总分低于300的数据 4、将学生张三的年龄改为17 5、查询班级编辑编号为11的学生的姓名,年龄,性别 6、查询每个班级的班级名称、学生人数 7、查询每个班级的名称,学生人数,班级的平均分 8、查询总分比张三高的学生的姓名,总分 9、查询每个班级总分最高的学生的姓名,总分 10、查询总分高于自己班级平均分的学生的姓名,总分
时间: 2023-06-09 21:05:49 浏览: 114
对不起,我作为AI语音助手,不能直接操作数据库。我只能提供相关的SQL语句供您参考:
1、使用create语句创建3张表:
```
CREATE TABLE student (
stid int primary key,
name varchar(50) NOT NULL,
age int,
sex varchar(10),
classid int,
foreign key (classid) references class(classid)
);
CREATE TABLE class (
classid int primary key,
classname varchar(50) NOT NULL UNIQUE
);
CREATE TABLE grade (
id int primary key,
stid int,
total int,
foreign key (stid) references student(stid)
);
```
2、在class表中插入1条数据:
```
INSERT INTO class (classid, classname)
VALUES (11, '一年级一班');
```
3、删除grade表中总分低于300的数据:
```
DELETE FROM grade
WHERE total < 300;
```
4、将学生张三的年龄改为17:
```
UPDATE student
SET age = 17
WHERE name = '张三';
```
5、查询班级编号为11的学生的姓名,年龄,性别:
```
SELECT student.name, student.age, student.sex
FROM student
WHERE student.classid = 11;
```
6、查询每个班级的班级名称、学生人数:
```
SELECT class.classname, COUNT(student.stid) AS student_num
FROM class
LEFT JOIN student ON class.classid = student.classid
GROUP BY class.classname;
```
7、查询每个班级的名称,学生人数,班级的平均分:
```
SELECT class.classname, COUNT(student.stid) AS student_num, AVG(grade.total) AS class_avg
FROM class
LEFT JOIN student ON class.classid = student.classid
LEFT JOIN grade ON student.stid = grade.stid
GROUP BY class.classname;
```
8、查询总分比张三高的学生的姓名,总分:
```
SELECT student.name, grade.total
FROM student
INNER JOIN grade ON student.stid = grade.stid
WHERE grade.total > (SELECT total FROM grade WHERE stid = (SELECT stid FROM student WHERE name = '张三'));
```
9、查询每个班级总分最高的学生的姓名,总分:
```
SELECT class.classname, student.name, MAX(grade.total) AS max_total
FROM class
LEFT JOIN student ON class.classid = student.classid
LEFT JOIN grade ON student.stid = grade.stid
GROUP BY class.classname;
```
10、查询总分高于自己班级平均分的学生的姓名,总分:
```
SELECT student.name, grade.total
FROM student
INNER JOIN grade ON student.stid = grade.stid
WHERE grade.total > (
SELECT AVG(g.total)
FROM grade g
INNER JOIN student s ON g.stid = s.stid
WHERE s.classid = student.classid
);
```
阅读全文