MySQL第一周周测

周测自己做的

/*
2.创建两张表,关系如下
Class表
        cid         数值型,主键,自增长
    cname       班级名称,字符型,非空,默认值空串
Student表
        id          id,数值型  主键,自增长
        name        姓名,字符型  非空,默认值空串
        math        数学,小数,7位,小数点后两位,默认值0,非空
        chinese     语文,小数,7位,小数点后两位,默认值0,非空
        english     英语,小数,7位,小数点后两位,默认值0,非空
        cno         班级编号,外键,关联class表的cid

3.向班级和学生表中分别插入数据
班级
1java1班
2java2班
学员
    1001    张三  78.5        90      85      1
    1002    李四  70      87.5        90      2
    1003    王五  72      67.5        0       1
1004    赵六  70      87.5        88      2
4.查询数学成绩70分以上的学员,并按照数学成绩降序排序
5.查询姓名中包含“三”或姓“王”的同学信息
6.查询2班所有学员的平均成绩
7.查询出1班学员中英语前5名的同学信息
8.查询平均成绩高于70分的学员信息
*/

create table Class(
cid int(9) auto_increment,
cname varchar(100),
primary key(cid)
)
create table Student(
id int(9) auto_increment,
name varchar(50) default '' not null,
math decimal(7,2) default 0 not null,
chinese decimal(7,2) default 0 not null,
english decimal(7,2) default 0 not null,
cno int(9),
primary key(id),
foreign key (cno) references Class (cid)
)

insert into class  values (1,'java1班');
insert into class  values (2,'java2班');

insert into student  values(1001,'张三',78.5,90,85,1);
insert into student  values(1002,'李四',70,87.5,90,2);
insert into student  values(1003,'王五',72.5,67.5,0,1);
insert into student  values(1004,'赵六',70.5,87.5,88,2);

select * from student where math >=70 order by math desc;
select * from student where name like ('%王%') or name like ('%三%')
-- 查询2班所有学员的平均成绩
select name,(math+english+chinese)/3 平均成绩 from student where cno = 2
select * from student where cno = 1 order by english desc limit 0,5
select * from student where (math+english+chinese)/3 > 70

你可能感兴趣的:(MySQL第一周周测)