查看: 1364|回复: 0

[Mysql数据库] python之数据库

发表于 2018-3-9 08:00:02
  1. # 作业内容
  2. '''
  3. 1、查询所有的课程的名称以及对应的任课老师姓名
  4. 2、查询学生表中男女生各有多少人
  5. 3、查询物理成绩等于100的学生的姓名
  6. 4、查询平均成绩大于八十分的同学的姓名和平均成绩
  7. 5、查询所有学生的学号,姓名,选课数,总成绩
  8. 6、 查询姓李老师的个数
  9. 7、 查询没有报李平老师课的学生姓名
  10. 8、 查询物理课程比生物课程高的学生的学号
  11. 9、 查询没有同时选修物理课程和体育课程的学生姓名
  12. 10、查询挂科超过两门(包括两门)的学生姓名和班级
  13. '''
  14. # 创建库和库表,并插入数据
  15. '''
  16. 班级表:class
  17. 课程表:course
  18. 成绩表:score
  19. 学生表:student
  20. 老师表:teacher
  21. '''
  22. mysql> create database sqlexam;
  23. Query OK, 1 row affected (0.01 sec)
  24. mysql> use sqlexam
  25. Database changed
  26. mysql>
  27. mysql> SET NAMES utf8;
  28. Query OK, 0 rows affected (0.01 sec)
  29. mysql> SET FOREIGN_KEY_CHECKS = 0;
  30. Query OK, 0 rows affected (0.00 sec)
  31. mysql> DROP TABLE IF EXISTS `class`;
  32. Query OK, 0 rows affected, 1 warning (0.00 sec)
  33. mysql> CREATE TABLE `class` (
  34. -> `cid` int(11) NOT NULL AUTO_INCREMENT,
  35. -> `caption` varchar(32) NOT NULL,
  36. -> PRIMARY KEY (`cid`)
  37. -> ) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;
  38. Query OK, 0 rows affected (0.04 sec)
  39. mysql> INSERT INTO `class` VALUES ('1', '三年二班'), ('2', '三年三班'), ('3', ' 一年二班'), ('4', '二年九班');
  40. Query OK, 4 rows affected (0.00 sec)
  41. Records: 4 Duplicates: 0 Warnings: 0
  42. mysql> DROP TABLE IF EXISTS `course`;
  43. Query OK, 0 rows affected, 1 warning (0.00 sec)
  44. mysql> CREATE TABLE `course` (
  45. -> `cid` int(11) NOT NULL AUTO_INCREMENT,
  46. -> `cname` varchar(32) NOT NULL,
  47. -> `teacher_id` int(11) NOT NULL,
  48. -> PRIMARY KEY (`cid`),
  49. -> KEY `fk_course_teacher` (`teacher_id`),
  50. -> CONSTRAINT `fk_course_teacher` FOREIGN KEY (`teacher_id`) REFERENCES `teacher` (`tid`)
  51. -> ) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;
  52. Query OK, 0 rows affected (0.04 sec)
  53. mysql> INSERT INTO `course` VALUES ('1', '生物', '1'), ('2', '物理', '2'), ('3', '体育', '3'), ('4', '美术', '2');
  54. Query OK, 4 rows affected (0.01 sec)
  55. Records: 4 Duplicates: 0 Warnings: 0
  56. mysql> DROP TABLE IF EXISTS `score`;
  57. Query OK, 0 rows affected, 1 warning (0.00 sec)
  58. mysql> CREATE TABLE `score` (
  59. -> `sid` int(11) NOT NULL AUTO_INCREMENT,
  60. -> `student_id` int(11) NOT NULL,
  61. -> `course_id` int(11) NOT NULL,
  62. -> `num` int(11) NOT NULL,
  63. -> PRIMARY KEY (`sid`),
  64. -> KEY `fk_score_student` (`student_id`),
  65. -> KEY `fk_score_course` (`course_id`),
  66. -> CONSTRAINT `fk_score_course` FOREIGN KEY (`course_id`) REFERENCES `course` (`cid`),
  67. -> CONSTRAINT `fk_score_student` FOREIGN KEY (`student_id`) REFERENCES `student` (`sid`)
  68. -> ) ENGINE=InnoDB AUTO_INCREMENT=53 DEFAULT CHARSET=utf8;
  69. Query OK, 0 rows affected (0.02 sec)
  70. mysql> INSERT INTO `score` VALUES ('1', '1', '1', '10'), ('2', '1', '2', '9'), ('5', '1', '4', '66'), ('6', '2', '1', '8'), ('8', '2', '3', '68'), ('9', '2', '4', '99'), ('10', '3', '1', '77'), ('11', '3', '2', '66'), ('12', '3', '3', '87'), ('13', '3', '4', '99'), ('14', '4', '1', '79'), ('15', '4', '2', '11'), ('16', '4', '3', '67'), ('17', '4', '4', '100'), ('18', '5', '1', '79'), ('19', '5', '2', '11'), ('20', '5', '3', '67'), ('21', '5', '4', '100'), ('22', '6', '1', '9'), ('23', '6', '2', '100'), ('24', '6', '3', '67'), ('25', '6', '4', '100'), ('26', '7', '1', '9'), ('27', '7', '2', '100'), ('28', '7', '3', '67'), ('29', '7', '4', '88'), ('30', '8', '1', '9'), ('31', '8', '2', '100'), ('32', '8', '3', '67'), ('33', '8', '4', '88'), ('34', '9', '1', '91'), ('35', '9', '2', '88'), ('36', '9', '3', '67'), ('37', '9', '4', '22'), ('38', '10', '1', '90'), ('39', '10', '2', '77'), ('40', '10', '3', '43'), ('41', '10', '4', '87'), ('42', '11', '1', '90'), ('43', '11', '2', '77'), ('44', '11', '3', '43'), ('45', '11', '4', '87'), ('46', '12', '1', '90'), ('47', '12', '2', '77'), ('48', '12', '3', '43'), ('49', '12', '4', '87'), ('52', '13', '3', '87');
  71. Query OK, 47 rows affected (0.01 sec)
  72. Records: 47 Duplicates: 0 Warnings: 0
  73. mysql> DROP TABLE IF EXISTS `student`;
  74. Query OK, 0 rows affected, 1 warning (0.00 sec)
  75. mysql> CREATE TABLE `student` (
  76. -> `sid` int(11) NOT NULL AUTO_INCREMENT,
  77. -> `gender` char(1) NOT NULL,
  78. -> `class_id` int(11) NOT NULL,
  79. -> `sname` varchar(32) NOT NULL,
  80. -> PRIMARY KEY (`sid`),
  81. -> KEY `fk_class` (`class_id`),
  82. -> CONSTRAINT `fk_class` FOREIGN KEY (`class_id`) REFERENCES `class` (`cid`)
  83. -> ) ENGINE=InnoDB AUTO_INCREMENT=17 DEFAULT CHARSET=utf8;
  84. Query OK, 0 rows affected (0.02 sec)
  85. mysql> INSERT INTO `student` VALUES ('1', '男', '1', '理解'), ('2', '女', '1', '钢蛋'), ('3', '男', '1', '张三'), ('4', '男', '1', '张一'), ('5', '女', '1', '张二'), ('6', '男', '1', '张四'), ('7', '女', '2', '铁锤'), ('8', '男', '2', '李三'), ('9', '男', '2', '李一'), ('10', '女', '2', '李二'), ('11', '男', '2', '李四'), ('12', '女', '3', '如花'), ('13', '男', '3', '刘三'), ('14', '男', '3', '刘 一'), ('15', '女', '3', '刘二'), ('16', '男', '3', '刘四');
  86. Query OK, 16 rows affected (0.01 sec)
  87. Records: 16 Duplicates: 0 Warnings: 0
  88. mysql> DROP TABLE IF EXISTS `teacher`;
  89. Query OK, 0 rows affected, 1 warning (0.00 sec)
  90. mysql> CREATE TABLE `teacher` (
  91. -> `tid` int(11) NOT NULL AUTO_INCREMENT,
  92. -> `tname` varchar(32) NOT NULL,
  93. -> PRIMARY KEY (`tid`)
  94. -> ) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;
  95. Query OK, 0 rows affected (0.02 sec)
  96. mysql> INSERT INTO `teacher` VALUES ('1', '张磊老师'), ('2', '李平老师'), ('3', '刘海燕老师'), ('4', '朱云海老师'), ('5', '李杰老师');
  97. Query OK, 5 rows affected (0.00 sec)
  98. Records: 5 Duplicates: 0 Warnings: 0
  99. mysql> SET FOREIGN_KEY_CHECKS = 1;
  100. Query OK, 0 rows affected (0.00 sec)
  101. mysql> show tables;
  102. +-------------------+
  103. | Tables_in_sqlexam |
  104. +-------------------+
  105. | class |
  106. | course |
  107. | score |
  108. | student |
  109. | teacher |
  110. +-------------------+
  111. 5 rows in set (0.00 sec)
  112. # 1、查询所有的课程的名称以及对应的任课老师姓名
  113. mysql> SELECT
  114. -> course.cname,
  115. -> teacher.tname
  116. -> FROM
  117. -> course
  118. -> INNER JOIN teacher ON course.teacher_id = teacher.tid;
  119. +--------+-----------------+
  120. | cname | tname |
  121. +--------+-----------------+
  122. | 生物 | 张磊老师 |
  123. | 物理 | 李平老师 |
  124. | 美术 | 李平老师 |
  125. | 体育 | 刘海燕老师 |
  126. +--------+-----------------+
  127. 4 rows in set (0.01 sec)
  128. # 2、查询学生表中男女生各有多少人
  129. mysql> SELECT
  130. -> gender 性别,
  131. -> count(1) 人数
  132. -> FROM
  133. -> student
  134. -> GROUP BY
  135. -> gender;
  136. +--------+--------+
  137. | 性别 | 人数 |
  138. +--------+--------+
  139. | 女 | 6 |
  140. | 男 | 10 |
  141. +--------+--------+
  142. 2 rows in set (0.00 sec)
  143. # 3、查询物理成绩等于100的学生的姓名
  144. mysql> SELECT
  145. -> student.sname
  146. -> FROM
  147. -> student
  148. -> WHERE
  149. -> sid IN (
  150. -> SELECT
  151. -> student_id
  152. -> FROM
  153. -> score
  154. -> INNER JOIN course ON score.course_id = course.cid
  155. -> WHERE
  156. -> course.cname = '物理'
  157. -> AND score.num = 100
  158. -> );
  159. +--------+
  160. | sname |
  161. +--------+
  162. | 张四 |
  163. | 铁锤 |
  164. | 李三 |
  165. +--------+
  166. 3 rows in set (0.00 sec)
  167. # 4、查询平均成绩大于八十分的同学的姓名和平均成绩
  168. mysql> SELECT
  169. -> student.sname,
  170. -> t1.avg_num
  171. -> FROM
  172. -> student
  173. -> INNER JOIN (
  174. -> SELECT
  175. -> student_id,
  176. -> avg(num) AS avg_num
  177. -> FROM
  178. -> score
  179. -> GROUP BY
  180. -> student_id
  181. -> HAVING
  182. -> avg(num) > 80
  183. -> ) AS t1 ON student.sid = t1.student_id;
  184. +--------+---------+
  185. | sname | avg_num |
  186. +--------+---------+
  187. | 张三 | 82.2500 |
  188. | 刘三 | 87.0000 |
  189. +--------+---------+
  190. 2 rows in set (0.00 sec)
  191. # 5、查询所有学生的学号,姓名,选课数,总成绩
  192. mysql> SELECT
  193. -> student.sid,
  194. -> student.sname,
  195. -> t1.course_num,
  196. -> t1.total_num
  197. -> FROM
  198. -> student
  199. -> LEFT JOIN (
  200. -> SELECT
  201. -> student_id,
  202. -> COUNT(course_id) course_num,
  203. -> sum(num) total_num
  204. -> FROM
  205. -> score
  206. -> GROUP BY
  207. -> student_id
  208. -> ) AS t1 ON student.sid = t1.student_id;
  209. +-----+--------+------------+-----------+
  210. | sid | sname | course_num | total_num |
  211. +-----+--------+------------+-----------+
  212. | 1 | 理解 | 3 | 85 |
  213. | 2 | 钢蛋 | 3 | 175 |
  214. | 3 | 张三 | 4 | 329 |
  215. | 4 | 张一 | 4 | 257 |
  216. | 5 | 张二 | 4 | 257 |
  217. | 6 | 张四 | 4 | 276 |
  218. | 7 | 铁锤 | 4 | 264 |
  219. | 8 | 李三 | 4 | 264 |
  220. | 9 | 李一 | 4 | 268 |
  221. | 10 | 李二 | 4 | 297 |
  222. | 11 | 李四 | 4 | 297 |
  223. | 12 | 如花 | 4 | 297 |
  224. | 13 | 刘三 | 1 | 87 |
  225. | 14 | 刘一 | NULL | NULL |
  226. | 15 | 刘二 | NULL | NULL |
  227. | 16 | 刘四 | NULL | NULL |
  228. +-----+--------+------------+-----------+
  229. 16 rows in set (0.00 sec)
  230. # 6、 查询姓李老师的个数
  231. mysql> SELECT
  232. -> count(tid)
  233. -> FROM
  234. -> teacher
  235. -> WHERE
  236. -> tname LIKE '李%';
  237. +------------+
  238. | count(tid) |
  239. +------------+
  240. | 2 |
  241. +------------+
  242. 1 row in set (0.00 sec)
  243. # 7、 查询没有报李平老师课的学生姓名
  244. mysql> SELECT
  245. -> student.sname
  246. -> FROM
  247. -> student
  248. -> WHERE
  249. -> sid NOT IN (
  250. -> SELECT DISTINCT
  251. -> student_id
  252. -> FROM
  253. -> score
  254. -> WHERE
  255. -> course_id IN (
  256. -> SELECT
  257. -> course.cid
  258. -> FROM
  259. -> course
  260. -> INNER JOIN teacher ON course.teacher_id = teacher.tid
  261. -> WHERE
  262. -> teacher.tname = '李平老师'
  263. -> )
  264. -> );
  265. +--------+
  266. | sname |
  267. +--------+
  268. | 刘三 |
  269. | 刘一 |
  270. | 刘二 |
  271. | 刘四 |
  272. +--------+
  273. 4 rows in set (0.00 sec)
  274. # 8、 查询物理课程比生物课程高的学生的学号
  275. mysql> SELECT
  276. -> t1.student_id
  277. -> FROM
  278. -> (
  279. -> SELECT
  280. -> student_id,
  281. -> num
  282. -> FROM
  283. -> score
  284. -> WHERE
  285. -> course_id = (
  286. -> SELECT
  287. -> cid
  288. -> FROM
  289. -> course
  290. -> WHERE
  291. -> cname = '物理'
  292. -> )
  293. -> ) AS t1
  294. -> INNER JOIN (
  295. -> SELECT
  296. -> student_id,
  297. -> num
  298. -> FROM
  299. -> score
  300. -> WHERE
  301. -> course_id = (
  302. -> SELECT
  303. -> cid
  304. -> FROM
  305. -> course
  306. -> WHERE
  307. -> cname = '生物'
  308. -> )
  309. -> ) AS t2 ON t1.student_id = t2.student_id
  310. -> WHERE
  311. -> t1.num > t2.num;
  312. +------------+
  313. | student_id |
  314. +------------+
  315. | 6 |
  316. | 7 |
  317. | 8 |
  318. +------------+
  319. 3 rows in set (0.00 sec)
  320. # 9、 查询没有同时选修物理课程和体育课程的学生姓名
  321. mysql> SELECT
  322. -> student.sname
  323. -> FROM
  324. -> student
  325. -> WHERE
  326. -> sid IN (
  327. -> SELECT
  328. -> student_id
  329. -> FROM
  330. -> score
  331. -> WHERE
  332. -> course_id IN (
  333. -> SELECT
  334. -> cid
  335. -> FROM
  336. -> course
  337. -> WHERE
  338. -> cname = '物理'
  339. -> OR cname = '体育'
  340. -> )
  341. -> GROUP BY
  342. -> student_id
  343. -> HAVING
  344. -> COUNT(course_id) = 1
  345. -> );
  346. +--------+
  347. | sname |
  348. +--------+
  349. | 理解 |
  350. | 钢蛋 |
  351. | 刘三 |
  352. +--------+
  353. 3 rows in set (0.00 sec)
  354. # 10、查询挂科超过两门(包括两门)的学生姓名和班级
  355. mysql> SELECT
  356. -> student.sname,
  357. -> class.caption
  358. -> FROM
  359. -> student
  360. -> INNER JOIN (
  361. -> SELECT
  362. -> student_id
  363. -> FROM
  364. -> score
  365. -> WHERE
  366. -> num < 60
  367. -> GROUP BY
  368. -> student_id
  369. -> HAVING
  370. -> count(course_id) >= 2
  371. -> ) AS t1
  372. -> INNER JOIN class ON student.sid = t1.student_id
  373. -> AND student.class_id = class.cid;
  374. +--------+--------------+
  375. | sname | caption |
  376. +--------+--------------+
  377. | 理解 | 三年二班 |
  378. +--------+--------------+
  379. 1 row in set (0.00 sec)
复制代码


回复

使用道具 举报