1.分别创建class表和stuent表
class表
class_id | 主键、自增 |
class_name | char(20) |
teacher | char(20) |
student表
sid | int,主键,自增 |
sname | char(20) |
sage | int |
ssex | 男或者女 |
hometown | char(20) |
height | float |
class_id | class表外键 |
mysql> create table class (class_id int primary key auto_increment,class_name char(20),teacher char(20)) charset=utf8;
mysql> create table student(sid int primary key auto_increment,sname char(20),sage int,ssex enum('男','女'),hometown char(20),height float,class_id int,foreign key (class_id) references class(class_id)) charset=utf8;
2.插入数据
mysql> insert into class values(1,'一班','张三');
mysql> insert into class values(2,'二班','李四');
mysql> insert into class values(0,'三班','李四');
mysql> insert into class values(0,'四班','王五');
mysql> insert into class values(0,'五班','赵柳');
mysql> insert into class values(null,'六班','孙悟空');
mysql> insert into student values(0,'王子云',23,'2','河北',162,2);
mysql> insert into student values(0,'印小天',20,'1','天津',177,3);
mysql> insert into student values(0,'张无忌',25,'1','河南',176,4);
mysql> insert into student values(0,'赵敏',22,'2','广西',166,5);
mysql> insert into student values(0,'周芷若',23,'2','天津',162,6);
mysql> insert into student values(0,'张若昀',22,'1','河北',176,1),(0,'范闲',21,'1','北京',170,2),(0,'李沁',18,'2','北京',160,3),(0,'林婉儿',18,'2','北京',160,4),(0,'宋轶',19,'2','天津',166,5),(0,'范若若',18,'2','河北',166,6);
mysql> insert into student values(0,'海棠朵朵',18,'2','河北',167,1),(0,'辛芷蕾',24,'2','北京',166,2),(0,'陈道明',25,'1','北京',178,3),(0,'庆帝',25,'1','北京',180,4),(0,'吴刚',20,'1','河南',172,5),(0,'陈萍萍',24,'1','河南',177,6);
mysql> insert into student values(0,'司理理',17,'2','河北',167,1),(0,'李纯',24,'2','北京',166,2),(0,'叶灵儿',22,'2','广西',158,3),(0,'韩玖诺',22,'1','北京',170,4),(0,'吴刚',22,'1','河南',173,5),(0,'刘美彤',22,'2','天津',167,6);
3.数据库练习
3.1查询年龄18-22岁学生的信息。
3.2查询范闲的学号和班主任姓名
3.3计算每个老师学生的数量
3.4查询学生数量高于5人的老师的姓名和班级
3.5查询姓张或者姓李同学的身高,班主任姓名。
3.6查询来自河北的学生的全部信息
3.7查询来自河北学生的平均身高
3.8查询身高最高同学的老师的姓名
3.9查询学生平均身高高于170老师的姓名。
3.10删除一班身高最高学生的信息
3.11 修改学生sid 17的姓名为“张三丰”
3.12删除外键,重新添加。删除主键,重新添加。
3.1查询年龄18-22岁学生的信息。
select * from student,class where student.class_id=class.class_id and sage<=22 and sage>=18;
select * from student,class where student.class_id=class.class_id and sage between 18 and 22;
3.2查询范闲的学号和班主任姓名
select sid,sname,teacher from student,class where student.class_id=class.class_id and sname='范闲';
3.3计算每个老师学生的数量
select count(sname),teacher from class,student where student.class_id=class.class_id group by teacher;
3.4查询学生数量高于5人的老师的姓名和班级
select teacher,count(sname) from student,class where student.class_id=class.class_id group by teacher having count(*)>4;
select teacher,count(sname) from student,class where student.class_id=class.class_id group by teacher having count(sname)>4;
3.5查询姓张或者姓李同学的身高,班主任姓名。
select sname,height,teacher from class,student where class.class_id=student.class_id and (sname like "张%" or sname like "李%");
3.6查询来自河北的学生的全部信息
select * from student,class where student.class_id=class.class_id and hometown='河北';
3.7查询来自河北学生的平均身高
select avg(height),hometown from class,student where class.class_id=student.class_id and hometown='河北';
3.8查询身高最高同学的老师的姓名
select * from student,class where student.class_id=class.class_id and height=(select max(height) from student);
3.9查询学生平均身高高于170老师的姓名。
select teacher,avg(height) from student,class where student.class_id=class.class_id group by teacher having avg(height)>170;
3.10删除一班身高最高学生的信息
3.11 修改学生sid 17的姓名为“张三丰”
update student set sname='张三丰' where sid=17;
3.12删除外键,重新添加。删除主键,重新添加。
删除外键,查看创建表student的命令。
show create table student;
删除外键: alter table student drop foreign key (class_id) references class(class_id);
重新添加外键:
alter table student add foreign key (class_id) references class (class_id);