day02.sql 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. #插入数据
  2. -- 删除stu表
  3. drop table if exists stu;
  4. -- 创建stu表
  5. CREATE TABLE stu (
  6. id int, -- 编号
  7. name varchar(20), -- 姓名
  8. age int, -- 年龄
  9. sex varchar(5), -- 性别
  10. address varchar(100), -- 地址
  11. math double(5,2), -- 数学成绩
  12. english double(5,2), -- 英语成绩
  13. hire_date date -- 入学时间
  14. );
  15. -- 添加数据
  16. INSERT INTO stu(id,name,age,sex,address,math,english,hire_date)
  17. VALUES
  18. (1,'马运',55,'男','杭州',66,78,'1995-09-01'),
  19. (2,'马花疼',45,'女','深圳',98,87,'1998-09-01'),
  20. (3,'马斯克',55,'男','香港',56,77,'1999-09-02'),
  21. (4,'柳白',20,'女','湖南',76,65,'1997-09-05'),
  22. (5,'柳青',20,'男','湖南',86,NULL,'1998-09-01'),
  23. (6,'刘德花',57,'男','香港',99,99,'1998-09-01'),
  24. (7,'张学右',22,'女','香港',99,99,'1998-09-01'),
  25. (8,'德玛西亚',18,'男','南京',56,65,'1994-09-02');
  26. # 查询name、age两列
  27. select name ,age from stu;
  28. # 查询所有列的数据,列名的列表可以使用*替代
  29. select * from stu;
  30. # 加注释
  31. select
  32. name , -- 姓名
  33. age
  34. from stu;
  35. # 别名
  36. # 查询姓名、数学成绩、英语成绩。并通过as给math和english起别名(as关键字可以省略)
  37. select
  38. math as 数学 , english as 英语
  39. from stu;
  40. # as 省略 表名 别名
  41. select
  42. math 数学 , english 英语
  43. from stu s;
  44. # s. 表名.字段名
  45. select s.math ,s.english from stu s;
  46. # 去重复
  47. select distinct address from stu;
  48. select * from stu;
  49. -- 条件查询练习
  50. # 查询年龄大于20岁的学员信息
  51. select * from stu where age > 20;
  52. #查询年龄大于等于20岁的学员信息
  53. select * from stu where age >= 20;
  54. # 查询年龄大于等于20岁 并且 年龄 小于等于 30岁 的学员信息
  55. # and 链接
  56. select * from stu where age >= 20 and age <= 30 ;
  57. # between and
  58. select * from stu where age between 20 and 30; # 小 大
  59. # 查询入学日期在'1998-09-01' 到 '1999-09-01' 之间的学员信息
  60. select * from stu where hire_date >= '1998-09-01' and hire_date <= '1999-09-01' ;
  61. select * from stu where hire_date between '1998-09-01' and '1999-09-01' ;
  62. # 查询年龄等于18岁的学员信息
  63. select * from stu where age = 18;
  64. # 查询年龄不等于18岁的学员信息
  65. select * from stu where age != 18;
  66. select * from stu where age <> 18;
  67. # 查询年龄等于18岁 或者 年龄等于20岁 或者 年龄等于22岁的学员信息
  68. select * from stu where age = 18 or age = 20 or age = 22;
  69. select * from stu where age in ( 18,20,22 );
  70. # 查询英语成绩为 null的学员信息,null值的比较不能使用 = 或者 != 。需要使用 is 或者 is not
  71. select * from stu where english is null ;
  72. select * from stu where english is not null ;
  73. # <=>
  74. select * from stu where english <=> null ;
  75. # 查询姓'马'的学员信息
  76. select * from stu where name like '马%';
  77. # 查询第二个字是'花'的学员信息
  78. select * from stu where name like '_花%';
  79. # 查询名字中包含 '德' 的学员信息
  80. select * from stu where name like '%德%';
  81. #all
  82. select * from stu where age > all();
  83. -- 排序
  84. # 查询学生信息,按照年龄升序排列
  85. select * from stu order by age;
  86. select * from stu order by age ASC ;
  87. # 查询学生信息,按照数学成绩降序排列
  88. select * from stu order by math DESC ;
  89. # 查询学生信息,按照数学成绩降序排列,如果数学成绩一样,再按照英语成绩升序排列
  90. select * from stu order by math DESC , english ASC ;
  91. -- 聚合函数
  92. # 统计班级一共有多少个学生
  93. select count(id) from stu;
  94. select count(1) from stu;
  95. # 不统计 null
  96. select count(english) from stu;
  97. # 查询数学成绩的最高分
  98. SELECT MAX(math) FROM stu;
  99. # 查询数学成绩的最低分
  100. SELECT MIN(math) from stu;
  101. # 查询数学成绩的总分
  102. SELECT SUM(math) from stu;
  103. # 查询数学成绩的平均分
  104. SELECT AVG(math) from stu;
  105. select * from stu;
  106. -- 分组查询
  107. # 查询男同学和女同学各自的数学平均分
  108. # 字段 必须是 分组的条件 其他字段 没有意义
  109. select avg(math) , sex from stu group by sex;
  110. # 查询男同学和女同学各自的数学平均分,以及各自人数
  111. select avg(math),count(sex),sex from stu group by sex;
  112. # 查询男同学和女同学各自的数学平均分,以及各自人数,要求:分数低于70分的不参与分组
  113. select avg(math),count(sex),sex from stu where math > 70 group by sex;
  114. # 查询男同学和女同学各自的数学平均分,以及各自人数,
  115. # 要求:分数低于70分的不参与分组,分组之后人数大于2个的
  116. select
  117. avg(math),count(sex),sex
  118. from stu
  119. where
  120. math > 70
  121. group by
  122. sex
  123. having
  124. count(sex) > 2;
  125. -- 分页
  126. # 从0开始查询,查询3条数据
  127. select * from stu limit 0 , 3;
  128. # 每页显示3条数据,查询第1页数据
  129. # 每页显示3条
  130. select * from stu limit 0 , 3;
  131. # 每页显示3条数据,查询第2页数据
  132. select * from stu limit 3 , 3;
  133. # 每页显示3条数据,查询第3页数据
  134. select * from stu limit 6 , 3;
  135. # 根据 页数计算 起始位置
  136. # 当前页(第一页) (1 - 1) * 条数
  137. # 当前页(第二页) (2 - 1) * 条数
  138. # 当前页(第三页) (3 - 1) * 条数
  139. # (当前页-1) 条数
  140. Select * from stu;
  141. -- 约束
  142. # 非空约束
  143. create table person1 (
  144. name varchar(20) not null
  145. );
  146. #验证
  147. insert into person1(name) values (null);
  148. insert into person1(name) values ('kunkun');
  149. # 唯一约束
  150. create table person2 (
  151. id int auto_increment unique ,
  152. name varchar(20) not null
  153. );
  154. insert into person2(id,name) values (1,'kunkun');
  155. insert into person2(id,name) values (2,'kunkun');
  156. insert into person2(name) values ('kunkun');
  157. create table person3 (
  158. id int auto_increment ,
  159. name varchar(20) not null,
  160. unique (id)
  161. );
  162. insert into person3(name) values ('kunkun');
  163. insert into person3(id,name) values (1,'kunkun');
  164. # 主键约束
  165. create table person4 (
  166. id int primary key auto_increment ,
  167. name varchar(20) not null
  168. );
  169. insert into person4(name) values ('kunkun');
  170. insert into person4(id,name) values (1,'kunkun');
  171. # 默认约束
  172. create table person5 (
  173. id int primary key auto_increment ,
  174. name varchar(20) not null,
  175. age int(3) default 0 -- 默认值
  176. );
  177. insert into person5(name) values ('kunkun' );
  178. # 给出null 就是 null 不是默认值
  179. insert into person5(name,age) values ('kunkun',null );
  180. -- 外键约束
  181. # 部门表
  182. create table dept(
  183. id int auto_increment primary key ,
  184. name varchar(50) not null unique
  185. );
  186. # 员工表
  187. create table emp(
  188. id int auto_increment primary key ,
  189. name varchar(50) not null unique,
  190. dept_id int ,
  191. foreign key fk_id (dept_id) references dept(id)
  192. );
  193. # 先添加主表内容
  194. # 部门表内容
  195. insert into dept( name ) values ('开发部门');
  196. insert into emp( name , dept_id ) values ('kunk',1);
  197. # 删除 主表中从表有数据的 不可以
  198. delete from dept where id = 2;