Mysql学习---SQL测试题之表结构

创建表结果和数据准备[直接执行即可]

  1 /*
  2 Navicat MySQL Data Transfer
  3
  4 Source Server         : ftl1012
  5 Source Server Version : 50617
  6 Source Host           : localhost:3306
  7 Source Database       : test_python
  8
  9 Target Server Type    : MYSQL
 10 Target Server Version : 50617
 11 File Encoding         : 65001
 12
 13 Date: 2017-12-30 13:12:57
 14 */
 15
 16 SET FOREIGN_KEY_CHECKS=0;
 17
 18 -- ----------------------------
 19 -- Table structure for class
 20 -- ----------------------------
 21 DROP TABLE IF EXISTS `class`;
 22 CREATE TABLE `class` (
 23   `cid` int(11) NOT NULL AUTO_INCREMENT,
 24   `caption` varchar(32) NOT NULL,
 25   PRIMARY KEY (`cid`)
 26 ) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;
 27
 28 -- ----------------------------
 29 -- Records of class
 30 -- ----------------------------
 31 INSERT INTO `class` VALUES (‘1‘, ‘三年二班‘);
 32 INSERT INTO `class` VALUES (‘2‘, ‘三年三班‘);
 33 INSERT INTO `class` VALUES (‘3‘, ‘一年二班‘);
 34 INSERT INTO `class` VALUES (‘4‘, ‘二年九班‘);
 35
 36 -- ----------------------------
 37 -- Table structure for course
 38 -- ----------------------------
 39 DROP TABLE IF EXISTS `course`;
 40 CREATE TABLE `course` (
 41   `cid` int(11) NOT NULL AUTO_INCREMENT,
 42   `cname` varchar(32) NOT NULL,
 43   `teacher_id` int(11) NOT NULL,
 44   PRIMARY KEY (`cid`),
 45   KEY `fk_course_teacher` (`teacher_id`),
 46   CONSTRAINT `fk_course_teacher` FOREIGN KEY (`teacher_id`) REFERENCES `teacher` (`tid`)
 47 ) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;
 48
 49 -- ----------------------------
 50 -- Records of course
 51 -- ----------------------------
 52 INSERT INTO `course` VALUES (‘1‘, ‘生物‘, ‘1‘);
 53 INSERT INTO `course` VALUES (‘2‘, ‘物理‘, ‘2‘);
 54 INSERT INTO `course` VALUES (‘3‘, ‘体育‘, ‘3‘);
 55 INSERT INTO `course` VALUES (‘4‘, ‘美术‘, ‘2‘);
 56
 57 -- ----------------------------
 58 -- Table structure for score
 59 -- ----------------------------
 60 DROP TABLE IF EXISTS `score`;
 61 CREATE TABLE `score` (
 62   `sid` int(11) NOT NULL AUTO_INCREMENT,
 63   `student_id` int(11) NOT NULL,
 64   `course_id` int(11) NOT NULL,
 65   `num` int(11) NOT NULL,
 66   PRIMARY KEY (`sid`),
 67   KEY `fk_score_student` (`student_id`),
 68   KEY `fk_score_course` (`course_id`),
 69   CONSTRAINT `fk_score_course` FOREIGN KEY (`course_id`) REFERENCES `course` (`cid`),
 70   CONSTRAINT `fk_score_student` FOREIGN KEY (`student_id`) REFERENCES `student` (`sid`)
 71 ) ENGINE=InnoDB AUTO_INCREMENT=53 DEFAULT CHARSET=utf8;
 72
 73 -- ----------------------------
 74 -- Records of score
 75 -- ----------------------------
 76 INSERT INTO `score` VALUES (‘1‘, ‘1‘, ‘1‘, ‘10‘);
 77 INSERT INTO `score` VALUES (‘2‘, ‘1‘, ‘2‘, ‘9‘);
 78 INSERT INTO `score` VALUES (‘5‘, ‘1‘, ‘4‘, ‘66‘);
 79 INSERT INTO `score` VALUES (‘6‘, ‘2‘, ‘1‘, ‘8‘);
 80 INSERT INTO `score` VALUES (‘8‘, ‘2‘, ‘3‘, ‘68‘);
 81 INSERT INTO `score` VALUES (‘9‘, ‘2‘, ‘4‘, ‘99‘);
 82 INSERT INTO `score` VALUES (‘10‘, ‘3‘, ‘1‘, ‘77‘);
 83 INSERT INTO `score` VALUES (‘11‘, ‘3‘, ‘2‘, ‘66‘);
 84 INSERT INTO `score` VALUES (‘12‘, ‘3‘, ‘3‘, ‘87‘);
 85 INSERT INTO `score` VALUES (‘13‘, ‘3‘, ‘4‘, ‘99‘);
 86 INSERT INTO `score` VALUES (‘14‘, ‘4‘, ‘1‘, ‘79‘);
 87 INSERT INTO `score` VALUES (‘15‘, ‘4‘, ‘2‘, ‘11‘);
 88 INSERT INTO `score` VALUES (‘16‘, ‘4‘, ‘3‘, ‘67‘);
 89 INSERT INTO `score` VALUES (‘17‘, ‘4‘, ‘4‘, ‘100‘);
 90 INSERT INTO `score` VALUES (‘18‘, ‘5‘, ‘1‘, ‘79‘);
 91 INSERT INTO `score` VALUES (‘19‘, ‘5‘, ‘2‘, ‘11‘);
 92 INSERT INTO `score` VALUES (‘20‘, ‘5‘, ‘3‘, ‘67‘);
 93 INSERT INTO `score` VALUES (‘21‘, ‘5‘, ‘4‘, ‘100‘);
 94 INSERT INTO `score` VALUES (‘22‘, ‘6‘, ‘1‘, ‘9‘);
 95 INSERT INTO `score` VALUES (‘23‘, ‘6‘, ‘2‘, ‘100‘);
 96 INSERT INTO `score` VALUES (‘24‘, ‘6‘, ‘3‘, ‘67‘);
 97 INSERT INTO `score` VALUES (‘25‘, ‘6‘, ‘4‘, ‘100‘);
 98 INSERT INTO `score` VALUES (‘26‘, ‘7‘, ‘1‘, ‘9‘);
 99 INSERT INTO `score` VALUES (‘27‘, ‘7‘, ‘2‘, ‘100‘);
100 INSERT INTO `score` VALUES (‘28‘, ‘7‘, ‘3‘, ‘67‘);
101 INSERT INTO `score` VALUES (‘29‘, ‘7‘, ‘4‘, ‘88‘);
102 INSERT INTO `score` VALUES (‘30‘, ‘8‘, ‘1‘, ‘9‘);
103 INSERT INTO `score` VALUES (‘31‘, ‘8‘, ‘2‘, ‘100‘);
104 INSERT INTO `score` VALUES (‘32‘, ‘8‘, ‘3‘, ‘67‘);
105 INSERT INTO `score` VALUES (‘33‘, ‘8‘, ‘4‘, ‘88‘);
106 INSERT INTO `score` VALUES (‘34‘, ‘9‘, ‘1‘, ‘91‘);
107 INSERT INTO `score` VALUES (‘35‘, ‘9‘, ‘2‘, ‘88‘);
108 INSERT INTO `score` VALUES (‘36‘, ‘9‘, ‘3‘, ‘67‘);
109 INSERT INTO `score` VALUES (‘37‘, ‘9‘, ‘4‘, ‘22‘);
110 INSERT INTO `score` VALUES (‘38‘, ‘10‘, ‘1‘, ‘90‘);
111 INSERT INTO `score` VALUES (‘39‘, ‘10‘, ‘2‘, ‘77‘);
112 INSERT INTO `score` VALUES (‘40‘, ‘10‘, ‘3‘, ‘43‘);
113 INSERT INTO `score` VALUES (‘41‘, ‘10‘, ‘4‘, ‘87‘);
114 INSERT INTO `score` VALUES (‘42‘, ‘11‘, ‘1‘, ‘90‘);
115 INSERT INTO `score` VALUES (‘43‘, ‘11‘, ‘2‘, ‘77‘);
116 INSERT INTO `score` VALUES (‘44‘, ‘11‘, ‘3‘, ‘43‘);
117 INSERT INTO `score` VALUES (‘45‘, ‘11‘, ‘4‘, ‘87‘);
118 INSERT INTO `score` VALUES (‘46‘, ‘12‘, ‘1‘, ‘90‘);
119 INSERT INTO `score` VALUES (‘47‘, ‘12‘, ‘2‘, ‘77‘);
120 INSERT INTO `score` VALUES (‘48‘, ‘12‘, ‘3‘, ‘43‘);
121 INSERT INTO `score` VALUES (‘49‘, ‘12‘, ‘4‘, ‘87‘);
122 INSERT INTO `score` VALUES (‘52‘, ‘13‘, ‘3‘, ‘87‘);
123
124 -- ----------------------------
125 -- Table structure for student
126 -- ----------------------------
127 DROP TABLE IF EXISTS `student`;
128 CREATE TABLE `student` (
129   `sid` int(11) NOT NULL AUTO_INCREMENT,
130   `gender` char(1) NOT NULL,
131   `class_id` int(11) NOT NULL,
132   `sname` varchar(32) NOT NULL,
133   PRIMARY KEY (`sid`),
134   KEY `fk_class` (`class_id`),
135   CONSTRAINT `fk_class` FOREIGN KEY (`class_id`) REFERENCES `class` (`cid`)
136 ) ENGINE=InnoDB AUTO_INCREMENT=17 DEFAULT CHARSET=utf8;
137
138 -- ----------------------------
139 -- Records of student
140 -- ----------------------------
141 INSERT INTO `student` VALUES (‘1‘, ‘男‘, ‘1‘, ‘理解‘);
142 INSERT INTO `student` VALUES (‘2‘, ‘女‘, ‘1‘, ‘钢蛋‘);
143 INSERT INTO `student` VALUES (‘3‘, ‘男‘, ‘1‘, ‘张三‘);
144 INSERT INTO `student` VALUES (‘4‘, ‘男‘, ‘1‘, ‘张一‘);
145 INSERT INTO `student` VALUES (‘5‘, ‘女‘, ‘1‘, ‘张二‘);
146 INSERT INTO `student` VALUES (‘6‘, ‘男‘, ‘1‘, ‘张四‘);
147 INSERT INTO `student` VALUES (‘7‘, ‘女‘, ‘2‘, ‘铁锤‘);
148 INSERT INTO `student` VALUES (‘8‘, ‘男‘, ‘2‘, ‘李三‘);
149 INSERT INTO `student` VALUES (‘9‘, ‘男‘, ‘2‘, ‘李一‘);
150 INSERT INTO `student` VALUES (‘10‘, ‘女‘, ‘2‘, ‘李二‘);
151 INSERT INTO `student` VALUES (‘11‘, ‘男‘, ‘2‘, ‘李四‘);
152 INSERT INTO `student` VALUES (‘12‘, ‘女‘, ‘3‘, ‘如花‘);
153 INSERT INTO `student` VALUES (‘13‘, ‘男‘, ‘3‘, ‘刘三‘);
154 INSERT INTO `student` VALUES (‘14‘, ‘男‘, ‘3‘, ‘刘一‘);
155 INSERT INTO `student` VALUES (‘15‘, ‘女‘, ‘3‘, ‘刘二‘);
156 INSERT INTO `student` VALUES (‘16‘, ‘男‘, ‘3‘, ‘刘四‘);
157
158 -- ----------------------------
159 -- Table structure for teacher
160 -- ----------------------------
161 DROP TABLE IF EXISTS `teacher`;
162 CREATE TABLE `teacher` (
163   `tid` int(11) NOT NULL AUTO_INCREMENT,
164   `tname` varchar(32) NOT NULL,
165   PRIMARY KEY (`tid`)
166 ) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;
167
168 -- ----------------------------
169 -- Records of teacher
170 -- ----------------------------
171 INSERT INTO `teacher` VALUES (‘1‘, ‘张磊老师‘);
172 INSERT INTO `teacher` VALUES (‘2‘, ‘李平老师‘);
173 INSERT INTO `teacher` VALUES (‘3‘, ‘刘海燕老师‘);
174 INSERT INTO `teacher` VALUES (‘4‘, ‘朱云海老师‘);
175 INSERT INTO `teacher` VALUES (‘5‘, ‘李杰老师‘);

经验: join, 临时表的学习[in 的使用,右边只能输出一个]

-- 查询平均成绩大于60分的同学的学号和平均成绩,按照平均成绩降序排序 操作score张表关联student查姓名

方案一[FTL]:
select * from (select student_id, avg(num) from score group by student_id having avg(num) > 60) as T left join student on student.sid = T.student_id;
方案二:
select score.student_id, student.sname, avg(num),max(num),min(num) from score LEFT JOIN student on student.sid = score.student_id
GROUP BY score.student_id having avg(num) > 60 order by avg(num) desc;

-- 查询所有同学的学号、姓名、选课数、总成绩;

SELECT  a.sid, a.sname,
SUM(s.num) as zongchengji, count(s.course_id) as xuankeshu from score s
LEFT JOIN student a on a.sid = s.student_id  GROUP BY s.student_id

-- 查询姓“李”的老师的个数;

方案一[FTL]:select count(tname) from teacher where tname like ‘李%‘;
方案二:select count(1) from teacher where tname like ‘李%‘;

-- 查询没学过“李平”老师课的同学的学号、姓名;

方案一[FTL]:局限性:不能查其他表中的内容
   -- 没学过  ==> not int ==> sid not in 成绩表
   -- 利用成绩表查找课程信息 ==>关联老师的信息
   -- 李平老师  ==> where tname like ‘李平‘
select student.sid, student.sname from student where sid not in
(
   select student_id from score LEFT JOIN
    course on score.course_id = course.cid
   LEFT JOIN teacher on teacher.tid = course.teacher_id
  where tname like ‘李平%‘
);

方案二:
思路:
    先查到“李平老师”老师教的所有课ID
    获取选过课的所有学生ID
    学生表中筛选
select * from student where sid not in (
    select DISTINCT student_id from score where score.course_id in (
        select cid from course left join teacher on course.teacher_id = teacher.tid where tname = ‘李平老师‘
    )
)

-- 查询学过“001”并且也学过编号“002”课程的同学的学号、姓名;

方案一[FTL]:
   -- 选出课程为1或者2的课 ==> 关联student表查询
SELECT sid,sname from student where sid in
(select DISTINCT student_id from score where score.course_id in
(select cid from course where cid in (1,2)))

方案二:
SELECT sid, sname from
(SELECT student_id, count(student_id) FROM
(SELECT student_id, course_id FROM score WHERE course_id = 1 OR course_id = 2) AS B
GROUP BY student_id HAVING count(student_id) > 1
) as C LEFT JOIN student on student.sid = C.student_id;

方案三:
select student_id,sname from
 (select student_id,course_id from score where course_id = 1 or course_id = 2) as B left join student on B.student_id = student.sid group by student_id HAVING count(student_id) > 1;

-- 查询没有学全所有课的同学的学号、姓名;

-- 查询没有学全所有课的同学的学号、姓名;
   -- 所有课  ==> count(1) from course
   -- 同学的学号、姓名 ==> sid, sname from student
   -- 没有学全所有课   ==> count(course_id)
方案二:
SELECT student.sid, sname from student LEFT JOIN score on score.student_id = student.sid
group by student_id HAVING count(course_id) = (select count(1) from course)

-- 查询学过“李平老师“所教的所有课的同学的学号、姓名;

-- 查询学过“李平老师“所教的所有课的同学的学号、姓名;
-- 找到李平老师教过的课程ID
方案一[FTL]:
SELECT sid, sname FROM student where sid in (SELECT student_id FROM score WHERE course_id IN (SELECT cid FROM course LEFT JOIN teacher ON teacher_id = tid WHERE teacher.tname LIKE ‘%李平%‘ ))

方案二:
select sid, sname from (select student_id from score where course_id in (select cid from course left join teacher on course.teacher_id = teacher.tid  where teacher.tname=‘李平老师‘) group by student_id)
as B left JOIN student on student.sid = B.student_id

-- 查询所有课程成绩小于60分的同学的学号、姓名;

-- 查询所有课程成绩小于60分的同学的学号、姓名;
方案一[FTL]:
select * from (
select student_id, num from score LEFT JOIN student on student.sid = score.student_id  where num < 60 ORDER BY num asc ) as B LEFT JOIN student on B.student_id = student.sid

-- 查询至少有一门课与学号为“001”的同学所学相同的同学的学号和姓名

-- 查询至少有一门课与学号为“001”的同学所学相同的同学的学号和姓名
方案一[FTL_有错误,未解决]
select student.sid, sname from
(SELECT * from score where course_id in (
	select course_id from score where student_id = 1
)) as B LEFT JOIN student on B.sid = student.sid
方案二:
   思路:
        获取 001 同学选择的所有课程
        获取课程在其中的所有人以及所有课程
        根据学生筛选,获取所有学生信息
        再与学生表连接,获取姓名
select student_id, sname, count(course_id) from score
LEFT JOIN student on student.sid = score.student_id where student_id != 1 and course_id in (
select course_id from score where course_id = 1) GROUP BY student_id

-- 查询至少学过学号为“001”同学所有课数目相同的其他同学学号和姓名

-- 查询至少学过学号为“001”同学所有课数目相同的其他同学学号和姓名
  -- 001 所学习的课程
  -- 其他人的课 >= 001里面的课
方案一[FTL]:
select sid, sname from
(select student_id, course_id from score where course_id in
(select course_id from score where student_id = 1) group by student_id
HAVING count(1) = (select count(1) from score WHERE student_id = 1) ) as B
LEFT JOIN student on student.sid = B.student_id GROUP BY student_id

-- 查询和“002”号的同学学习的课程完全相同的其他同学学号和姓名;【高难度】

-- 查询和“002”号的同学学习的课程完全相同的其他同学学号和姓名;【高难度】
   -- 002号学习的课程
   -- count(1) == 002的课程count(1)
   -- left JOIN student 拼接姓名和学号
-- 数量相同学生ID
select student_id, course_id, count(1) from score GROUP BY student_id having count(1) =
(select count(1) from score where student_id = 2 )
-- 课程相同
select student_id, course_id from score where course_id in
(select DISTINCT course_id from score where student_id = 2) GROUP BY student_id
-- 综合完成
 select student_id,sname from score left join student on score.student_id = student.sid where student_id in (select student_id from score  where student_id != 1 group by student_id HAVING count(course_id) = (select count(1) from score where student_id = 1)) and course_id in (select course_id from score where student_id = 1) group by student_id HAVING count(course_id) = (select count(1) from score where student_id = 1)

-- 删除学习“叶平”老师课的score表记录;

delete from score where course_id in (
select cid from course left join teacher on course.teacher_id = teacher.tid where teacher.name = ‘叶平‘) 

[更多参考] http://www.cnblogs.com/wupeiqi/articles/5729934.html  

[更多参考] http://www.cnblogs.com/wupeiqi/articles/5748496.html  

原文地址:https://www.cnblogs.com/ftl1012/p/9385161.html

时间: 2024-10-10 02:21:03

Mysql学习---SQL测试题之表结构的相关文章

SQL Server查看表结构及视图,适合开发者使用,简单易用

SELECT * FROM INFORMATION_SCHEMA.TABLES SELECT * FROM INFORMATION_SCHEMA.COLUMNS 查看执行结果 SQL Server查看表结构及视图,适合开发者使用,简单易用,布布扣,bubuko.com

Oracle与Sql Server复制表结构及数据

1.Oracle create table 新表名 AS SELECT * FROM 源表名 2.Sql Server SELECT * into 新表名 from 源表名 Oracle与Sql Server复制表结构及数据

sql server 读取表结构

SELECT 表名=case when a.colorder=1 then d.name else '' end, 字段序号=a.colorder, 主键=case when exists(SELECT 1 FROM sysobjects where xtype='PK' and name in ( SELECT name FROM sysindexes WHERE indid in( SELECT indid FROM sysindexkeys WHERE id = a.id AND coli

SQL脚本修改表结构

SQL脚本修改表结构 新建表:create table [表名]([自动编号字段] int IDENTITY (1,1) PRIMARY KEY ,[字段1] nVarChar(50) default '默认值' null ,[字段2] ntext null ,[字段3] datetime,[字段4] money null ,[字段5] int default 0,[字段6] Decimal (12,4) default 0,[字段7] image null ,) 删除表:Drop table

MySQL学习14:操作数据表中的记录(二)

四查询记录 MySQL数据库中对于数据表中的记录最常用的就是记录的查询,操作数据表中的记录大都是记录的查询.查找 记录的语法结构为: SELECT select_expr [,select_expr ...] [ FROM table_refereneces [WHERE where_condition] [GROUP BY {col_name | position } [ASC | DESC],...] [HAVING where_condition] [ORDER BY {col_name

MySQL导出数据库、数据库表结构、存储过程及函数【用】

一.导出数据库 我的mysql安装目录是D:\Program Files\MySQL\MySQL Server 5.5\bin\,导出文件预计放在D:\sql\ 在mysql的安装目录执行命令: mysqldump -hhostname -uusername -ppassword databasename > d:\sql\databasename.sql 在mysql5.7的时候报出一个警告,但是数据导出成功: 现在换一种方式解决上述问题: 创建一my.cnf文件,我放在与mysqldump.

【转】Mysql学习---SQL的优化

[原文]https://www.toutiao.com/i6594314336913588743/ mysql如何处理亿级数据,第一个阶段--优化SQL语句 1.应尽量避免在 where 子句中使用!=或<>操作符,否则将引擎放弃使用索引而进行全表扫描. 2.对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引. 3.应尽量避免在 where 子句中对字段进行 null 值判断,否则将导致引擎放弃使用索引而进行全表扫描,如: select id

MySQL不同库名相同表结构实现主从配置

数据库版本 5.6.16 在两个服务器上,存在不同名字的数据库,但是两个数据库中的所有表结构相同,实现主从复制. 主库服务器的数据库名为yoon,从库服务器的库名为hank 在从库的my.cnf配置文件中添加如下参数,并重启数据库replicate-rewrite-db = yoon -> hank 设置主从:change master to master_host='172.16.9.243',master_port=3306,master_user='master',master_passw

MySQL学习7:操作数据表(一)

数据表 数据表(或称为表)是数据库最重要的组成部分之一,是其它对象的基础. 先来解决上一篇博客末尾说到的进入数据库验证的问题: 验证当前数据库是哪一个的MySQL命令为: SELECT DATABASE(); 例子: SHOW DATABASES; USE t1; SELECT DATABASE(); 一创建数据表 MySQL语法中创建数据表的语法格式为: CREATE TABLE [IF NOT EXISTS] table_name( column_name data_type, .... )