数据库习题
1. DDL语句以及插入数据练习
- 创建一张学生(Student)表,属性如下:
学生编号 SID 整数 主键 自增从1001开始
学生姓名 SNAME 字符串 长度为20 不为空
学生年龄 BIRTHDAY 日期
学生性别 SEX 字符串 长度为1
- 向学生表插入数据
+--------+-------+------------+-------+
| 1001 | 张三 | 1990-10-10 | 男 |
| 1002 | 李四 | 1981-10-10 | 男 |
| 1003 | 王五 | 1981-11-10 | 女 |
| 1004 | 赵六 | 1988-10-10 | 男 |
| 1005 | 孙七 | 1989-01-10 | 女 |
| 1006 | 周八 | 1990-10-10 | 男 |
| 1007 | 张三 | 1990-06-10 | 女 |
+---------+------+------------+-------+
- 创建教师表(Teacher)
教师编号 TID 整数 主键 自增
教师姓名 TNAME 字符串 长度为20 不为空
- 向教师表插入数据
+-----+--------+
| 1 | 叶平 |
| 2 | 王老师 |
| 3 | 张老师 |
| 4 | 李老师 |
| 5 | 孙老师 |
+-----+--------+
- 创建一张课程表(Course)
课程编号 CID 整数 主键 自增
课程名称 CNAME 字符串 长度为20 不为空
教师编号 TID 必须与教师表中的TID相符,不能为空
- 插入课程数据,注意课程与教师编号的对应关系
+----------+----------+----------+----------+
| 课程编号 | 课程名称 | 老师编号 | 老师名称 |
+----------+----------+----------+----------+
| 1 | 企业管理 | 1 | 叶平 |
| 2 | 马克思 | 2 | 王老师 |
| 3 | UML | 3 | 张老师 |
| 4 | 数据库 | 4 | 李老师 |
| 5 | 英语 | 5 | 孙老师 |
| 6 | 语文 | 1 | 叶平 |
| 7 | 数学 | 2 | 王老师 |
+----------+----------+----------+----------+
- 创建成绩表(SC)
学生编号 SID 必须与学生表中的学生编号相符,不为空
课程编号 CID 必须与课程表中的课程编号相符,不为空
成绩 SCORE 整数 不为空
课程编号与学生编号应该联合唯一
- 插入成绩数据
+----------+----------+----------+----------+------+
| 学生编号 | 学生姓名 | 课程编号 | 课程名称 | 成绩 |
+----------+----------+----------+----------+------+
| 1001 | 张三 | 1 | 企业管理 | 50 |
| 1001 | 张三 | 2 | 马克思 | 70 |
| 1001 | 张三 | 3 | UML | 80 |
| 1001 | 张三 | 4 | 数据库 | 90 |
| 1001 | 张三 | 5 | 英语 | 80 |
| 1001 | 张三 | 6 | 语文 | 80 |
| 1001 | 张三 | 7 | 数学 | 100 |
| 1002 | 李四 | 1 | 企业管理 | 90 |
| 1002 | 李四 | 4 | 数据库 | 55 |
| 1002 | 李四 | 6 | 语文 | 90 |
| 1002 | 李四 | 7 | 数学 | 80 |
| 1003 | 王五 | 1 | 企业管理 | 59 |
| 1003 | 王五 | 2 | 马克思 | 70 |
| 1004 | 赵六 | 1 | 企业管理 | 80 |
| 1004 | 赵六 | 2 | 马克思 | 70 |
| 1005 | 孙七 | 1 | 企业管理 | 50 |
| 1005 | 孙七 | 2 | 马克思 | 50 |
| 1005 | 孙七 | 3 | UML | 100 |
| 1005 | 孙七 | 4 | 数据库 | 30 |
| 1006 | 周八 | 1 | 企业管理 | 60 |
| 1007 | 张三 | 1 | 企业管理 | 100 |
| 1007 | 张三 | 4 | 数据库 | 58 |
| 1007 | 张三 | 6 | 语文 | 90 |
| 1007 | 张三 | 7 | 数学 | 80 |
+----------+----------+----------+----------+------+
2. 单表查询练习
drop table sc;
drop table student;
drop table course;
drop table teacher;
create table if not exists student(
sid int primary key auto_increment,
sname varchar(20) not null,
birthday date,
sex char(1)
) auto_increment=1001;
insert into student values(null,'张三','1990-10-10','男');
insert into student values(null,'李四','1981-10-10','男');
insert into student values(null,'王五','1981-11-10','女');
insert into student values(null,'赵六','1988-10-10','男');
insert into student values(null,'孙七','1989-01-10','女');
insert into student values(null,'周八','1990-10-10','男');
insert into student values(null,'张三','1990-06-10','女');
create table if not exists teacher(
tid int primary key auto_increment,
tname varchar(20) not null
);
insert into teacher(tname) values('叶平');
insert into teacher(tname) values('王老师');
insert into teacher(tname) values('张老师');
insert into teacher(tname) values('李老师');
insert into teacher(tname) values('孙老师');
create table if not exists course(
cid int primary key auto_increment,
cname varchar(20) not null,
tid int not null,
foreign key(tid) references teacher(tid)
);
insert into course values(null,'企业管理',1);
insert into course values(null,'马克思',2);
insert into course values(null,'UML',3);
insert into course values(null,'数据库',4);
insert into course values(null,'英语',5);
insert into course values(null,'语文',1);
insert into course values(null,'数学',2);
create table if not exists sc(
sid int not null,
cid int not null,
score tinyint not null,
foreign key(cid) references course(cid),
foreign key(sid) references student(sid),
primary key(cid,sid)
);
insert into sc values(1001,1,50);
insert into sc values(1001,2,70);
insert into sc values(1001,3,80);
insert into sc values(1001,4,90);
insert into sc values(1001,5,80);
insert into sc values(1001,6,80);
insert into sc values(1001,7,100);
insert into sc values(1002,1,90);
insert into sc values(1002,4,55);
insert into sc values(1002,6,90);
insert into sc values(1002,7,80);
insert into sc values(1003,1,59);
insert into sc values(1003,2,70);
insert into sc values(1004,1,80);
insert into sc values(1004,2,70);
insert into sc values(1005,1,50);
insert into sc values(1005,2,50);
insert into sc values(1005,3,100);
insert into sc values(1005,4,30);
insert into sc values(1006,1,60);
insert into sc values(1007,1,100);
insert into sc values(1007,4,58);
insert into sc values(1007,6,90);
insert into sc values(1007,7,80);
– 1. 查询姓“李”的老师的个数
SELECT COUNT(tname) FROM teacher where tname LIKE '李%';
– 2. 查询男女生人数个数
SELECT sex,COUNT(sex) from student GROUP BY sex;
– 3. 查询同名同姓学生名单,并统计同名人数
SELECT sname,COUNT(sname) FROM student GROUP BY sname;
– 4. 1981年出生的学生名单
SELECT sname from student WHERE EXTRACT(YEAR FROM birthday)=1981;
– 5. 查询平均成绩大于60分的同学的学号和平均成绩
SELECT sid,AVG(score) FROM sc GROUP BY sid HAVING AVG(score)>60;
– 6. 求选了课程的学生人数
SELECT COUNT(DISTINCT sid) FROM sc;
– 7. 查询至少选修两门课程的学生学号
SELECT sid FROM sc GROUP BY sid HAVING COUNT(cid)>=2;
– 8. 查询各科成绩最高和最低的分。以如下形式显示:课程ID,最高分,最低分
SELECT cid, MAX(score), MIN(score) FROM sc GROUP BY cid;
– 9. 统计每门课程的学生选修人数。要求输出课程号和选修人数,查询结果按人数降序排列,
– 若人数相同,按课程号升序排列
SELECT cid, COUNT(sid) FROM sc GROUP BY cid ORDER BY COUNT(sid) DESC ,cid ASC;
以下练习针对部门员工表,请导入scott.sql的数据
打印入职时间超过38年的员工信息
select * from emp where (extract(year from date(now())) - extract(year from hiredate))>38;
把hiredate列看做是员工的生日,求本月过生日的员工 (本月按照是7月)
select * from emp where (extract(month from hiredate))=7;
把hiredate列看做是员工的生日,求下月过生日的
员工(当前月是8月,下月就是9月)
select * from emp where extract(month from hiredate)=extract(month from date_add(now(), interval 1 month));求1980年下半年入职的员工
select * from emp where extract(year from hiredate)=1980 and extract(month from hiredate)>6;
请用两种的方式查询所有 名字长度为4 的员工的员工编号,姓名
select empno,ename from emp where char_length(ename)=4;
select empno,ename from emp having char_length(ename)=4 order by length(ename) asc;
select empno,ename from emp where ename like '____';
显示各种职位的最低工资
select job,min(sal) '最低工资' from emp group by job;
求1980年各个月入职的的员工个数
select extract(month from hiredate),count(empno) from emp group by extract(month from hiredate) asc;
查询每个部门的最高工资
select deptno,max(sal) from emp group by deptno;
查询每个部门,每种职位的最高工资
select deptno,job,max(sal) from emp group by deptno,job order by deptno;
查询各部门的总工资和平均工资
select deptno,sum(sal),avg(sal) from emp group by deptno;
查询10号部门,20号部门的平均工资(尝试用多种写法)
select deptno,avg(sal) from emp where deptno=10 or deptno=20 group by deptno order by deptno;
select deptno,avg(sal) from emp group by deptno having deptno=10 or deptno=20 order by deptno;
查询平均工资高于2000元的部门编号和平均工资
`select deptno,avg(sal) from emp group by deptno having avg(sal)>2000;
统计公司里经理的人数
select count(empno) from emp group by job having job='MANAGER';
查询工资最高的3名员工信息
select * from emp order by sal desc limit 3;
查询工资由高到低第五到第十的员工信息
select * from emp order by sal desc limit 5,5;