MySQL 常用 SQL 语句

avatar 2018年01月06日10:25:37 7 1983 views
博主分享免费Java教学视频,B站账号:Java刘哥

数据库

  1. SET FOREIGN_KEY_CHECKS=0;
  2. -- ----------------------------
  3. -- Table structure for course
  4. -- ----------------------------
  5. DROP TABLE IF EXISTS `course`;
  6. CREATE TABLE `course` (
  7.   `Cno` int(11) NOT NULL,
  8.   `Cname` varchar(255) CHARACTER SET utf8 DEFAULT NULL,
  9.   `Cpno` int(11) DEFAULT NULL,
  10.   `Ccredit` int(11) DEFAULT NULL,
  11.   PRIMARY KEY (`Cno`)
  12. ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
  13. -- ----------------------------
  14. -- Records of course
  15. -- ----------------------------
  16. INSERT INTO `course` VALUES ('1', '数据库', '5', '4');
  17. INSERT INTO `course` VALUES ('2', '数学', null, '2');
  18. INSERT INTO `course` VALUES ('3', '信息系统', '1', '4');
  19. INSERT INTO `course` VALUES ('4', '操作系统', '6', '3');
  20. INSERT INTO `course` VALUES ('5', '数据结构', '7', '4');
  21. INSERT INTO `course` VALUES ('6', '数据处理', null, '2');
  22. INSERT INTO `course` VALUES ('7', 'PASCAL语言', '6', '4');
  23. -- ----------------------------
  24. -- Table structure for sc
  25. -- ----------------------------
  26. DROP TABLE IF EXISTS `sc`;
  27. CREATE TABLE `sc` (
  28.   `Sno` int(11) NOT NULL,
  29.   `Cno` int(11) NOT NULL,
  30.   `Grade` int(11) DEFAULT NULL,
  31.   KEY `FK_cno` (`Cno`),
  32.   KEY `FK_sno` (`Sno`),
  33.   CONSTRAINT `FK_cno` FOREIGN KEY (`Cno`) REFERENCES `course` (`Cno`) ON DELETE CASCADE ON UPDATE CASCADE,
  34.   CONSTRAINT `FK_sno` FOREIGN KEY (`Sno`) REFERENCES `student` (`Sno`) ON DELETE CASCADE ON UPDATE CASCADE
  35. ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
  36. -- ----------------------------
  37. -- Records of sc
  38. -- ----------------------------
  39. INSERT INTO `sc` VALUES ('201215121', '1', '97');
  40. INSERT INTO `sc` VALUES ('201215121', '2', '90');
  41. INSERT INTO `sc` VALUES ('201215121', '3', '93');
  42. INSERT INTO `sc` VALUES ('201215122', '2', '95');
  43. INSERT INTO `sc` VALUES ('201215122', '3', '85');
  44. -- ----------------------------
  45. -- Table structure for student
  46. -- ----------------------------
  47. DROP TABLE IF EXISTS `student`;
  48. CREATE TABLE `student` (
  49.   `Sno` int(11) NOT NULL,
  50.   `Sname` varchar(255) CHARACTER SET utf8 DEFAULT NULL,
  51.   `Ssex` varchar(255) CHARACTER SET utf8 DEFAULT NULL,
  52.   `Sage` int(11) DEFAULT NULL,
  53.   `Sdept` varchar(255) CHARACTER SET utf8 DEFAULT NULL,
  54.   PRIMARY KEY (`Sno`)
  55. ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
  56. -- ----------------------------
  57. -- Records of student
  58. -- ----------------------------
  59. INSERT INTO `student` VALUES ('201215121', '刘晨', '女', '19', 'CS');
  60. INSERT INTO `student` VALUES ('201215122', '罗琪', '女', '17', 'CS');
  61. INSERT INTO `student` VALUES ('201215123', '刘中江', '男', '18', 'MA');
  62. INSERT INTO `student` VALUES ('201215125', '曹志雄', '男', '18', 'CS');
  63. INSERT INTO `student` VALUES ('201215126', '刘琰', '男', '19', 'CS');
  64. INSERT INTO `student` VALUES ('201215127', '刘言曌', '男', '19', 'CS');
  65. INSERT INTO `student` VALUES ('201215128', '曹志雄', '男', '19', 'CS');
  66. INSERT INTO `student` VALUES ('201215207', '曹阿瞒', '男', '19', 'IS');
  67. INSERT INTO `student` VALUES ('201215208', '春日风', '女', '18', null);
   

代码如下

  1. #### SELECT 常用语句####
  2. #查询年龄在18-19的学生
  3. #select * from student where Sage between 18 and 19;
  4. #查询年龄不在18-19的学生
  5. #select * from student where Sage not between 18 and 19;
  6. #查询计算机科学系(CS系),数学系(MA系)和信息系(IS)学生
  7. #select * from student where Sdept in('CS','MA','IS');
  8. #查询既不是计算机系(IS系),也不是信息系(MA系)和信息系(IS系)学生
  9. #select * from student where Sdept not in('CS','MA','IS');
  10. #查询姓刘的学生
  11. #select * from student where Sname like '刘%';
  12. #查询姓欧阳,且全名为三个汉字的学生
  13. #select *from student where Sname like '欧阳_';
  14. #查询名字中第二个字为晨的学生
  15. #select * from student where Sname like '_晨';
  16. #查询不姓刘的学生
  17. #select * from student where Sname not like '刘%';
  18. #查询缺少成绩的学生的学号和相对的课程号
  19. #select sno,cno from sc where grade is null;
  20. #查询所有有成绩的学生的学号和课程号
  21. #select sno,cno from sc where grade is not null;
  22. #查询选修了3号课程的学生的学号,姓名和分数,按成绩降序排序
  23. #select sc.sno,student.sname,sc.grade from student,sc where cno=3 and student.sno=sc.sno order by grade desc;
  24. #查询全体学生,按所在系升序排序,同一个系按年龄降序
  25. #select * from student order by sdept asc,sage desc ;
  26. #查询学生总人数
  27. #select count(*) from student;
  28. #查询选修了课程的学生总人数
  29. #select count(distinct sno) from sc ;
  30. #计算3号课程的平均分
  31. #select avg(grade) from sc where cno=2;
  32. #查询学号201215121 选修课程的总分数
  33. #select sum(grade) from sc where sno='201215121';
  34. #查询每个学生及其选修课的情况
  35. #select * from student, sc where student.sno = sc.sno;
  36. #查询选修了2号课程且成绩在90分以上的所有学生
  37. #select * from sc where cno=2 and grade>90;
  38. #左外连接
  39. #select student.Sno,Sname,Ssex,Sage,Sdept,Cno,Grade from student left outer join sc on (student.sno = sc.sno);
  40. #多表连接
  41. #select student.sno,sname,cname,grade from student, sc, course where student.sno = sc.sno and course.cno = sc.cno
  42. #嵌套查询-查询选修了2号课程的学习姓名
  43. #select sname from student where sno in (select sno from sc where cno='2');
  44. #查询与刘晨在同一系学习的学生
  45. #select * from student where sdept = (select sdept from student where sname = '刘晨');
  46. #select * from student where sdept in (select sdept from student where sname = '刘晨');
  47. #select * from student s1,student s2 where s1.sdept = s2.sdept and s1.sname = '刘晨';
  48. #查询选修了信息系统的学生
  49. #select * from student,sc,course where sc.sno = student.sno and sc.cno = course.cno and course.cname = '信息系统'; ;
  50. #select * from student where sno in (select sno from sc where cno = ( select cno from course where cname  = '信息系统' ));
  51. #找出每个学生超过其选修平均成绩的课程号
  52. #select * from sc where grade > (select avg(grade) from sc where sno = sc.Sno);
  53. #查询非计算机科学系中比计算机科学系任意一个学生年龄小的学生
  54. #select * from student where sage < any (select sage from student) and sdept != 'cs';
  55. #select * from student where sage < (select max(sage) from student) and sdept != 'cs';
  56. #查询非计算机系中比计算机系所有年龄都要小的学生
  57. #select * from student where sage < all (select sage from student ) and sdept != 'cs';
  58. #select * from student where sage < (select min(sage) from student);
  59. #查询选修了2号课程的学生
  60. #select * from student where exists (select * from sc where cno = 2 and sc.sno = student.sno);
  61. #select * from student where sno in (select sno from sc where cno = 2);
  62. #查询没有选修2号课程的学生
  63. #select * from student where not exists (select * from sc where cno = 2 and sc.sno = student.sno);
  64. #select * from student where student.sno not in (select sc.sno from sc where sc.sno = 2);
  65. #查询选修了全部课程的学生
  66. -- select * from student where not exists 
  67. -- (
  68. --  select * from course where not exists  
  69. --  (
  70. --      select * from sc where sc.sno = student.sno and cno = course.cno
  71. --  )
  72. -- ) 
  73. -- select sno from 
  74. -- (
  75. --  select sno,count(*) as sum from sc group by sno  having sum = 
  76. --  (
  77. --      select count(*) from course
  78. --  )
  79. -- ) as A 
  80. # 集合查询 UNION (或,并集)
  81. #select * from student where Sname = '罗琪' UNION select * from student where Sname = '刘言曌';
  82. #select * from student where Sname = '罗琪' or Sname = '刘言曌';
  83. #找出每个学生超过自己选修课程平均成绩的课程号
  84. -- select * from sc,
  85. -- (
  86. --  select sno,avg(grade) as avg_grade from sc group by sno
  87. -- ) as A
  88. -- where A.sno = sc.sno and sc.grade > A.avg_grade;
  89. #### INSERT 常用语句 ####
  90. #将一个新学生插入到 Student 表中
  91. #insert into Student value(201215128, '曹志雄', '男', 20, 'MA');
  92. #insert into student(sno,sname,ssex,sage,sdept) values (201215207, '曹阿瞒','男',19, 'IS');
  93. #### UPDATE 常用语句 ####
  94. #修改某一个元组的值
  95. #将罗琪的年龄-1
  96. #update student set Sage = '18' where Sname = '罗琪'
  97. #修改多个元组的值
  98. #将所有的学生年龄-1
  99. #update Student set Sage = Sage - 1;
  100. #带子查询的修改语句
  101. #将计算机系的学生成绩+5
  102. -- update sc  
  103. -- set grade = grade + 5
  104. -- where sno in
  105. -- (select sno from student where sdept = 'CS');
  106. #### DELETE 常用语句 ####
  107. #先添加一条学生记录,然后删除他
  108. #insert into student value(201215130,'张三','男',19,'IS');
  109. #delete from student where sno = '201215130';
  110. #删除多条记录
  111. ##delete from sc;
  112. #### 空值的处理 ####
  113. #空值产生
  114. #添加一个学生,学院为NULL
  115. #insert into student value(201215208, '春日风','女',18,NULL);
  116. #将曹志雄的学院设置为NULL
  117. #update student set sdept = NULL where sname = '曹志雄';
  118. #空值判断
  119. #查询没有先行课的课程
  120. #select * from course where cpno is null;
  121. #查询有先行课的课程
  122. #select * from course where cpno is not null
  123. #### 视图常用语句 ####
  124. #创建一个男生视图
  125. -- create view is_boy 
  126. --  as 
  127. -- select * from student where ssex = '男';
  128. #查询视图
  129. #select * from is_boy where sage < 19
  130. #更新视图
  131. #update is_boy set sage = sage +1 where sname = '刘中江'
  132. #删除视图
  133. #drop view is_boy;
  本文地址:https://liuyanzhao.com/7131.html
  • 微信
  • 交流学习,有偿服务
  • weinxin
  • 博客/Java交流群
  • 资源分享,问题解决,技术交流。群号:590480292
  • weinxin
avatar

发表评论

avatar 登录者:匿名
匿名评论,评论回复后会有邮件通知

  

已通过评论:1   待审核评论数:0
  1. avatar 耐火砖

    感谢博主分享,纯干货,收藏了!