-- 3 、追加相应的数据 , 见下页
insert into person (name, addr, class) value
( 'lucy' , 'beijing' , 'class_A' );
insert into person (name, addr, class) value
( 'peter' , 'tianjing' , 'class_B' );
insert into person (name, addr, class) value
( 'bob' , 'hebei' , 'class_A' );
insert into grade (id, score, year ) value ( 1 , 78 , '2013' );
insert into grade (id, score, year ) value ( 2 , 98 , '2013' );
insert into grade (id, score, year ) value ( 3 , 80 , '2013' );
insert into grade (id, score, year ) value ( 1 , 88 , '2014' );
-- 4 、打印所有学生的信息 : 包括 name,addr,class,score,year
select p.*,g .score ,g .year from person p,grade g where p .id = g .id ;
-- 5 、将打印所以学生信息的语句 , 创建视图 PersonsGrade, 方便后面使用
create view persongrade as select p.*,g .score ,g .year from person
p,grade g where p .id = g .id ;
-- 6 、查看最高分数是多少?
select max (score) from persongrade;
-- 7 、查看每个班的平均分,以及对应的班级名称和班级人数
select class 班级 , count ( distinct id) 人数 , avg (score) from
persongrade group by class;
-- 8 、在 persons 表中 id 列创建一个索引 persons_id_index
create index person_id_index on person (id);
-- 9 、查看 lucy 在 2013 年的成绩
select * from persongrade where name= 'lucy' and year = '2013' ;
-- 10 、 bob 要退学,必须要删除 bob 所有的信息 ( 包括成绩单 ), 通过设置触发器,自动删除bob 相关的所有数据,设置触发器 tg_delete
delimiter ||
create trigger tg_delete after delete on person for each row
begin
delete from grade where grade .id = old .id ;
end ||
delimiter ;
-- 11 、执行删除 bob 的操作
delete from person where name= 'bob' ;
-- 12 、查看所有人的信息,确定是否执行了删除操作
select * from person;
select * from grade;
select * from persongrade;