当前位置:   article > 正文

MySql-多表查询练习

三个表中查询w和t同时选修的课程

数据准备

  1. /*
  2. 数据导入:
  3. Navicat Premium Data Transfer
  4. Source Server : localhost
  5. Source Server Type : MySQL
  6. Source Server Version : 50624
  7. Source Host : localhost
  8. Source Database : sqlexam
  9. Target Server Type : MySQL
  10. Target Server Version : 50624
  11. File Encoding : utf-8
  12. Date: 10/21/2016 06:46:46 AM
  13. */
  14. SET NAMES utf8;
  15. SET FOREIGN_KEY_CHECKS = 0;
  16. -- ----------------------------
  17. -- Table structure for `class`
  18. -- ----------------------------
  19. DROP TABLE IF EXISTS `class`;
  20. CREATE TABLE `class` (
  21. `cid` int(11) NOT NULL AUTO_INCREMENT,
  22. `caption` varchar(32) NOT NULL,
  23. PRIMARY KEY (`cid`)
  24. ) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;
  25. -- ----------------------------
  26. -- Records of `class`
  27. -- ----------------------------
  28. BEGIN;
  29. INSERT INTO `class` VALUES ('1', '三年二班'), ('2', '三年三班'), ('3', '一年二班'), ('4', '二年九班');
  30. COMMIT;
  31. -- ----------------------------
  32. -- Table structure for `course`
  33. -- ----------------------------
  34. DROP TABLE IF EXISTS `course`;
  35. CREATE TABLE `course` (
  36. `cid` int(11) NOT NULL AUTO_INCREMENT,
  37. `cname` varchar(32) NOT NULL,
  38. `teacher_id` int(11) NOT NULL,
  39. PRIMARY KEY (`cid`),
  40. KEY `fk_course_teacher` (`teacher_id`),
  41. CONSTRAINT `fk_course_teacher` FOREIGN KEY (`teacher_id`) REFERENCES `teacher` (`tid`)
  42. ) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;
  43. -- ----------------------------
  44. -- Records of `course`
  45. -- ----------------------------
  46. BEGIN;
  47. INSERT INTO `course` VALUES ('1', '生物', '1'), ('2', '物理', '2'), ('3', '体育', '3'), ('4', '美术', '2');
  48. COMMIT;
  49. -- ----------------------------
  50. -- Table structure for `score`
  51. -- ----------------------------
  52. DROP TABLE IF EXISTS `score`;
  53. CREATE TABLE `score` (
  54. `sid` int(11) NOT NULL AUTO_INCREMENT,
  55. `student_id` int(11) NOT NULL,
  56. `course_id` int(11) NOT NULL,
  57. `num` int(11) NOT NULL,
  58. PRIMARY KEY (`sid`),
  59. KEY `fk_score_student` (`student_id`),
  60. KEY `fk_score_course` (`course_id`),
  61. CONSTRAIN
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/439288
推荐阅读
相关标签
  

闽ICP备14008679号