基本sql语句练习(学生表)

深碍√TFBOYSˉ_ 2021-11-16 14:12 617阅读 0赞

基本sql语句练习

1.学生表的基本操作

老老实实把下面的打完,相信我。
在这里插入图片描述

  1. -- 创建表
  2. CREATE TABLE t_stu(
  3. -- 列名 类型 主键 自动增长 注释
  4. stu_id BIGINT PRIMARY KEY auto_increment COMMENT '学生id',
  5. -- 不允许为空约束
  6. stu_name VARCHAR(50) not null DEFAULT '马云' COMMENT '学生姓名',
  7. -- 学号 唯一性约束 不能为空
  8. stu_no VARCHAR(30) UNIQUE not null COMMENT '学号',
  9. -- 年龄
  10. stu_age int COMMENT '年龄',
  11. -- 生日
  12. stu_birth TIMESTAMP DEFAULT NOW() COMMENT '生日'
  13. )ENGINE=INNODB auto_increment=1000 charset=utf8 COMMENT '学生表';
  14. -- 插入数据 有默认值得不插入
  15. INSERT into t_stu(stu_no,stu_age) VALUES('20190101',25);
  16. -- 插入生日
  17. INSERT into t_stu(stu_no,stu_age,stu_birth) VALUES('20190102',25,'2000-10-10');
  18. -- 查询
  19. select * from t_stu;
  20. -- 插入多条数据
  21. INSERT into t_stu(stu_name,stu_no,stu_age)
  22. values('x1','20190108',25),('x3','20190104',26),('x6','20190107',20);
  23. -- 如果每列都插入值,可以省略列名的编写
  24. INSERT into t_stu value(2000,'x8','20190105',25,'2000-11-11');
  25. INSERT into t_stu(stu_no,stu_age) VALUES('20190110',30);
  26. -- 字符串作为主键
  27. CREATE table stu2(
  28. stu_no VARCHAR(30) not null,
  29. stu_name varchar(30) not null,
  30. -- 设置主键
  31. PRIMARY key(stu_no)
  32. );
  33. INSERT into stu2 values('1','a'),('2','b');
  34. SELECT stu_no,stu_name from stu2;
  35. -- 联合主键
  36. CREATE table stu3(
  37. stu_no VARCHAR(30) not null,
  38. stu_name varchar(30) not null,
  39. -- 设置主键
  40. PRIMARY key(stu_no,stu_name)
  41. );
  42. -- 主外键
  43. -- 创建课程表
  44. CREATE table course(
  45. course_id int auto_increment PRIMARY key,
  46. course_name VARCHAR(30) not null UNIQUE,
  47. -- 记录的时间
  48. update_time TIMESTAMP DEFAULT NOW()
  49. );
  50. -- 实现选课 学生和课程之间是一个多对多得关系
  51. -- 创建关系表
  52. CREATE table stu_course(
  53. stu_id BIGINT,
  54. cour_id int,
  55. -- 主键
  56. PRIMARY key(stu_id,cour_id),
  57. -- 外键
  58. CONSTRAINT FOREIGN key(stu_id) REFERENCES t_stu(stu_id),
  59. constraint foreign key(cour_id) REFERENCES course(course_id)
  60. );
  61. -- 查询学生表的信息
  62. select * from t_stu;
  63. -- 查询课程表
  64. select * from course;
  65. INSERT into course(course_id,course_name)
  66. values(200,'java'),(300,'html'),(400,'js')
  67. -- x1 选择类 java js课程 x3 选择了html js的课程
  68. insert INTO stu_course(stu_id,cour_id)
  69. values(1002,200),(1002,400),(1003,300),(1003,400)
  70. select * from stu_course
  71. -- 获取完整的信息需要3张出现在一个查询中
  72. select * from t_stu,stu_course,course WHERE t_stu.stu_id = stu_course.stu_id AND
  73. stu_course.cour_id = course.course_id
  74. -- 查询x1选择的所有课程的名称
  75. select stu_name,course_name from t_stu,stu_course,course WHERE t_stu.stu_id = stu_course.stu_id AND
  76. stu_course.cour_id = course.course_id and t_stu.stu_name = 'x1'
  77. -- 使用子查询的方式
  78. -- 获取 x1 id
  79. select stu_id from t_stu where stu_name = 'x1'
  80. -- 根据x1id查询课程的id
  81. select cour_id from stu_course where stu_id =
  82. (select stu_id from t_stu where stu_name = 'x1')
  83. -- 利用上面课程的id查询课程名称
  84. select course_name from course where course_id in (
  85. select cour_id from stu_course where stu_id =
  86. (select stu_id from t_stu where stu_name = 'x1')
  87. )
  88. -- 使用连接查询,stu_course sc ,course c
  89. select s.*,sc.*,c.* from t_stu s
  90. INNER JOIN stu_course sc ON s.stu_id = sc.stu_id
  91. INNER JOIN course c on sc.cour_id = c.course_id
  92. where s.stu_name = 'x1';
  93. -- 别名查询
  94. SELECT s.stu_id id, s.stu_name as name,s.stu_no as '学号' from t_stu s;
  95. -- 连接查询
  96. SELECT * from t_stu;
  97. select * from stu_course;
  98. -- 使用学生表左连接课程和学生的关系表 查询的数据以关键字left join 左边的表为准
  99. -- 如果没有数据与之关联,那么现实的null
  100. SELECT * from t_stu s LEFT JOIN stu_course sc ON s.stu_id = sc.stu_id
  101. SELECT * from stu_course sc LEFT JOIN t_stu s ON s.stu_id = sc.stu_id
  102. -- 右连接
  103. SELECT * from t_stu s right JOIN stu_course sc ON s.stu_id = sc.stu_id
  104. SELECT * from stu_course sc right JOIN t_stu s ON s.stu_id = sc.stu_id
  105. -- 修改语句
  106. select * from t_stu;
  107. -- 将年龄大于20并且小于26的人的年龄修改成25
  108. update t_stu set stu_age = 25 where stu_age >= 20 and stu_age < 26
  109. -- 删除
  110. DELETE from t_stu where stu_no = '20190110';
  111. select * from t_stu where 1=1 ;
  112. -- 统计记录条数
  113. select count(*) nums from t_stu where 1 = 1;
  114. -- 可以使用具体的列名
  115. select count(stu_id) nums from t_stu where 1 = 1;
  116. select count(stu_age) nums from t_stu where 1 = 1;
  117. -- 其他的聚合函数
  118. select count(*),MIN(stu_age),MAX(stu_age),SUM(stu_age),AVG(stu_age) from
  119. t_stu where 1 = 1;
  120. -- 一旦使用了聚合函数,那么要么使用group by,不然是不能查询单列
  121. select stu_id,count(*),MIN(stu_age),MAX(stu_age),SUM(stu_age),AVG(stu_age) from
  122. t_stu where 1 = 1;
  123. -- 分组
  124. select * from t_stu;
  125. select stu_id,count(*),MIN(stu_age),MAX(stu_age),SUM(stu_age),AVG(stu_age) from
  126. t_stu where 1 = 1 GROUP BY stu_age;
  127. -- 分组之后过滤 id 需要大于1001才做分组 分组之前数据
  128. select * from t_stu;
  129. select stu_id,count(*),MIN(stu_age),MAX(stu_age),SUM(stu_age),AVG(stu_age) from
  130. t_stu where 1 = 1 and stu_id > 1001 GROUP BY stu_age;
  131. -- 如果使用的having那么过滤的是分组之后的数据
  132. select * from t_stu;
  133. select stu_id,count(*),MIN(stu_age),MAX(stu_age),SUM(stu_age),AVG(stu_age) from
  134. t_stu where 1 = 1 GROUP BY stu_age HAVING stu_id > 1001;
  135. select * from t_stu;
  136. select stu_id,count(*),MIN(stu_age),MAX(stu_age),SUM(stu_age),AVG(stu_age) from
  137. t_stu where 1 = 1 GROUP BY stu_age HAVING count(*) > 1;
  138. -- 分组和排序
  139. select * from t_stu;
  140. select stu_id,count(*) nums,MIN(stu_age),MAX(stu_age),SUM(stu_age),AVG(stu_age) from
  141. t_stu where 1 = 1 and stu_id > 1001 GROUP BY stu_age ORDER BY nums ASC;
  142. -- select * from table where 条件 group by having ORDER BY asc/desc
  143. -- 查询选择了所有的课程的学生的姓名
  144. SELECT * from course;
  145. select * from stu_course;
  146. select * from t_stu;
  147. -- 查询总的课程数目
  148. select count(*) nums from course
  149. -- 按照学生的id分组统计课程和学生的关系表
  150. select stu_id ,count(*) from stu_course GROUP BY stu_id
  151. -- 过滤想要的id
  152. select stu_id ,count(*) from stu_course GROUP BY stu_id HAVING count(*) = (
  153. select count(*) nums from course
  154. )
  155. -- 根据学生表查询学生的姓名
  156. select stu_name from t_stu where stu_id in (
  157. select stu_id from stu_course GROUP BY stu_id HAVING count(*) = (
  158. select count(*) nums from course
  159. )
  160. )
  161. -- 连接查询
  162. select s.stu_id,s.stu_name,count(*) from t_stu s
  163. INNER JOIN stu_course sc on s.stu_id = sc.stu_id
  164. INNER JOIN course c on sc.cour_id = c.course_id
  165. GROUP BY s.stu_id,s.stu_name
  166. HAVING count(*) = (select count(*) nums from course)
  167. select *,CONCAT(stu_name,'-',stu_no) from t_stu;
  168. update t_stu set stu_name=CONCAT(stu_name,"123") where stu_id=1001
  169. update t_stu set stu_name=CONCAT(stu_name,stu_no) where stu_id=1001
  170. -- 插入日期可以是符合日期格式的字符串,也可以使用系统函数获取当前的日期
  171. SELECT * from t_stu;
  172. -- 日期
  173. INSERT into t_stu values(2002,'x5','20190705',30,'2019-07-05'),(2003,'x7','20190706',22,CURRENT_DATE());
  174. -- 日期和时间
  175. INSERT into t_stu values (2004,'x9','20190707',24,CURRENT_TIMESTAMP());
  176. -- 只有时间
  177. select CURRENT_TIME();
  178. -- 日期的抽取
  179. SELECT stu_birth, YEAR(stu_birth),month(stu_birth),day(stu_birth) from t_stu;
  180. -- 日期的增加和减少
  181. select stu_birth,DATE_SUB(stu_birth,INTERVAL 30 day) from t_stu
  182. select stu_birth,DATE_add(stu_birth,INTERVAL 5 day) from t_stu
  183. select stu_birth,DATE_add(stu_birth,INTERVAL 5 HOUR) from t_stu
  184. -- 日期相差
  185. select stu_birth,CURRENT_DATE(),DATEDIFF(CURRENT_DATE(),stu_birth) from t_stu
  186. -- 有多少岁 当前年 - 出生年
  187. select stu_birth,CURRENT_DATE(),YEAR(stu_birth),year(CURRENT_DATE()),(year(CURRENT_DATE())-YEAR(stu_birth)) b from t_stu
  188. -- 日期中某部分的抽取
  189. select stu_birth,EXTRACT(YEAR FROM stu_birth),EXTRACT(month FROM stu_birth),EXTRACT(day FROM stu_birth),
  190. EXTRACT(HOUR FROM stu_birth),EXTRACT(MINUTE FROM stu_birth),EXTRACT(SECOND FROM stu_birth) from t_stu;
  191. -- 数字的处理
  192. select 3*5 a;
  193. -- ROUND(X,D) 函数 CEIL(X)函数 FLOOR(X)
  194. select FLOOR(3.65),FLOOR(3.64),CEIL(3.65),CEIL(3.64),ROUND(3.654,2),ROUND(3.656,2)
  195. select * from t_stu
  196. -- 截取数字,不做四舍五入
  197. select TRUNCATE(3.656,2) ,TRUNCATE(3.654,2)
  198. show TABLES
  199. select * from stu2;
  200. select * from stu3;
  201. -- 删除数据
  202. delete from stu2;-- 000ms
  203. TRUNCATE table stu2;-- 时间: 0.040ms
  204. -- 创建一张表并且复制数据
  205. CREATE table copy_stu3 as select * from stu3;
  206. select * from copy_stu3
  207. CREATE table copy_stu31 as select stu_no from stu3;
  208. select * from copy_stu31
  209. -- 如果只是copy结构不要数据
  210. CREATE table copy_stu32 as select * from stu3 where 1=2;
  211. select * from copy_stu32;
  212. select * from copy_stu31;
  213. -- 查询表结构
  214. desc copy_stu32
  215. select * from copy_stu3;
  216. select * from copy_stu32;
  217. update copy_stu3 set stu_name = null where stu_no=2
  218. -- copy_stu3的数据插入到copy_stu32 如果数据是null 这用马云替换
  219. INSERT into copy_stu32(stu_no,stu_name) SELECT stu_no,IFNULL(stu_name,'马云') from copy_stu3
  220. -- 查询的时候null的处理
  221. select * from copy_stu3 where stu_name = null;
  222. select * from copy_stu3 where stu_name is null;
  223. select * from t_stu;
  224. select *,(
  225. CASE
  226. WHEN stu_age >=20 THEN '成年人'
  227. WHEN stu_age >=10 and stu_age < 20 THEN '未成年'
  228. ELSE '小孩'
  229. END
  230. )type from t_stu;
  231. -- 分页
  232. select * from t_stu;
  233. select count(*) from t_stu;
  234. -- 放入同一列
  235. select stu_id from t_stu UNION select count(*) from t_stu;
  236. -- 2张毫无关系的表数据放在一张表中 增加不同的列
  237. select stu_id,(select count(*) from t_stu) nums from t_stu ;
  238. select 5*3
  239. -- LIMIT
  240. select * from t_stu;
  241. -- 第一次查询 n = 1表示 0 = (n -1) * 3
  242. select * from t_stu LIMIT 0,3; -- 0表示第一条数据 3表示 查询记录条数
  243. -- 第二次任然查询3条记录 n = 2
  244. select * from t_stu LIMIT 3,3; -- 第一个3 如何计算
  245. -- 第三次查询任然查询3条记录 n = 3 6 = (3 -1) * 3
  246. select * from t_stu LIMIT 6,3;-- 6是如何计算
  247. -- 每次查询4条记录 ,获取 第二次查询的记录 (2 -1)* 4
  248. select * from t_stu LIMIT 4,4;
  249. -- 如果是网页 1 2 3 4 页码等同于 第几次查询
  250. -- 每次查询2条,如和计算要查询多少次可以把记录查询完毕
  251. -- 总的记录数是9
  252. --
  253. select count(*) from t_stu;
  254. select count(*),CEIL(count(*)/2) from t_stu;
  255. -- 如果在程序中计算 (9 + 2 -1 ) / 2 = 取整数 (总的记录条数 + 每次查询的记录数 - 1)/ 每次查询的记录数
  256. -- 每次查询3 获取第二次查询的数据 limit 3,3
  257. select s.stu_id,s.stu_name,count(*) from t_stu s
  258. INNER JOIN stu_course sc on s.stu_id = sc.stu_id
  259. INNER JOIN course c on sc.cour_id = c.course_id
  260. where s.stu_id > 1000
  261. GROUP BY s.stu_id,s.stu_name
  262. HAVING count(*) >=1
  263. limit 3,3
  264. SELECT t.stu_id,t.stu_name from
  265. (select s.stu_id,s.stu_name from t_stu s
  266. INNER JOIN stu_course sc on s.stu_id = sc.stu_id
  267. INNER JOIN course c on sc.cour_id = c.course_id
  268. where s.stu_id > 1000 limit 3,3) as t
  269. GROUP BY t.stu_id,t.stu_name
  270. HAVING count(*) >=1

2.创表多对多关系,sql的基本操作、分组统计、聚合

  1. drop table student; -- 删除表
  2. -- 学生表
  3. create table student(
  4. stu_id int auto_increment PRIMARY key,
  5. stu_name VARCHAR(25) COMMENT '学生姓名',
  6. stu_no VARCHAR(25),
  7. stu_birth TIMESTAMP
  8. );
  9. -- 插入数据
  10. insert into student(stu_name,stu_no,stu_birth) values('lisi','2222',NOW());
  11. insert into student(stu_name,stu_no,stu_birth) values('zhangsan','3333',NOW());
  12. insert into student(stu_name,stu_no,stu_birth) values('wangwu','5555','2015-06-07');
  13. insert into student(stu_name,stu_no,stu_birth) values('zhaoliu','6666','2015-06-07');
  14. select * from student;
  15. -- 创建课程表
  16. drop table if EXISTS course; -- 如果存在就删除课程表
  17. create table course(
  18. cur_id int PRIMARY key,
  19. cur_name VARCHAR(25),
  20. create_time TIMESTAMP DEFAULT NOW() COMMENT '数据插入的时间'
  21. )
  22. -- 插入测试数据
  23. insert into course(cur_id,cur_name) values (100,'java');
  24. insert into course(cur_id,cur_name) values (200,'html');
  25. insert into course(cur_id,cur_name) values (300,'javascript');
  26. insert into course(cur_id,cur_name) values (400,'mysql');
  27. insert into course(cur_id,cur_name) values (500,'oracle');
  28. select * from course;
  29. -- 创建关系表
  30. create table stu_course(
  31. stu_id int,
  32. cur_id int,
  33. PRIMARY key(stu_id,cur_id), -- 联合组件
  34. CONSTRAINT FOREIGN key(stu_id) REFERENCES student(stu_id),
  35. CONSTRAINT FOREIGN key(cur_id) REFERENCES course(cur_id)
  36. );
  37. -- 插入测试数据
  38. INSERT into stu_course(stu_id,cur_id) values(1,100);
  39. INSERT into stu_course(stu_id,cur_id) values(1,200); -- lisi选择了2门课
  40. INSERT into stu_course(stu_id,cur_id) values(2,100); -- 张三选择了1门课
  41. INSERT into stu_course(stu_id,cur_id) values(3,100);
  42. INSERT into stu_course(stu_id,cur_id) values(3,200);
  43. INSERT into stu_course(stu_id,cur_id) values(3,300);
  44. INSERT into stu_course(stu_id,cur_id) values(3,400); -- 王五选择了4门课
  45. select * from stu_course;
  46. -- 查询lisi选择的所有课程的名称
  47. -- 李四的id
  48. select s.stu_id from student s where s.stu_name = 'lisi';
  49. -- 课程的id
  50. select sc.cur_id from stu_course sc ;
  51. -- 李四所选的课程的id
  52. select sc.cur_id from stu_course sc where sc.stu_id = (select s.stu_id from student s where s.stu_name = 'lisi');
  53. -- 查询课程的名称
  54. select c.cur_name from course c where c.cur_id in (
  55. select sc.cur_id from stu_course sc where sc.stu_id = (select s.stu_id from student s where s.stu_name = 'lisi')
  56. );
  57. -- 连接查询
  58. select s.*,sc.*,c.* from student s
  59. INNER JOIN stu_course sc on s.stu_id = sc.stu_id
  60. INNER join course c on sc.cur_id = c.cur_id
  61. where s.stu_name = 'lisi'
  62. select s.*,sc.* from student s
  63. inner JOIN stu_course sc on s.stu_id = sc.stu_id
  64. -- 左连接
  65. select s.*,sc.* from student s
  66. left JOIN stu_course sc on s.stu_id = sc.stu_id
  67. -- 右连接
  68. select c.*,sc.* from stu_course sc
  69. right JOIN course c on c.cur_id = sc.cur_id
  70. -- student表中那个添加age
  71. select * from student;
  72. ALTER table student add COLUMN age int;
  73. -- 聚合函数 统计记录条数 求平均值 求最大值 求最小值
  74. -- 统计年龄小于25学生有多少个 一般情况下聚合函数不能和单独的某列一起查询
  75. select count(*) '小于25的学生个数' from student where age <= 25;
  76. -- 聚合函数 统计中记录数 平均值 最大值 最小值 总和
  77. select count(*),avg(age),max(age),min(age),sum(age) from student;
  78. -- 排序 升序
  79. select * from student WHERE stu_id >= 1 ORDER BY age asc;
  80. select * from student WHERE stu_id >= 1 ORDER BY age desc; -- 降序
  81. -- 排序可以多个字段做排序
  82. select * from student WHERE stu_id >= 1 ORDER BY age asc,stu_name desc;
  83. -- 分组
  84. SELECT count(*), age from student GROUP BY age;
  85. -- 分组之后过滤
  86. SELECT count(*), age from student GROUP BY age HAVING age >18;
  87. SELECT count(*), age from student GROUP BY age HAVING max(age) > 25;
  88. -- 也可以放入where条件
  89. SELECT count(*), age from student where stu_id >= 2 GROUP BY age HAVING age >=18 ;
  90. -- 还可以排序
  91. SELECT count(*), age from student where stu_id >= 2 GROUP BY age HAVING age >=18 ORDER BY age desc;
  92. -- 查询选择了所有的课程的学生的姓名和学号
  93. -- 总共有多少门课程
  94. SELECT count(*) from course;
  95. -- 从关系表查询每个学生到底选择了多少门课
  96. select * from stu_course;
  97. select stu_id,count(*) from stu_course GROUP BY stu_id;
  98. -- 获取选择了所有的课程的人的id
  99. select stu_id,count(*) from stu_course GROUP BY stu_id HAVING count(*) = (SELECT count(*) from course);
  100. -- 显示选择了所有课程的学生的姓名
  101. select * from student where stu_id in(
  102. select stu_id from stu_course GROUP BY stu_id HAVING count(*) = (SELECT count(*) from course)
  103. );
  104. select s.*,t.* from student s INNER JOIN
  105. ( select stu_id,count(*) from stu_course GROUP BY stu_id HAVING count(*) = (SELECT count(*) from course) ) t
  106. on s.stu_id = t.stu_id;

发表评论

表情:
评论列表 (有 0 条评论,617人围观)

还没有评论,来说两句吧...

相关阅读

    相关 SQL语句练习

    一.[通过Sql实现根据分组合并指定列内容的查询 ][Sql_] 问题:   最近在做一个项目的时候,遇到这样一个要求,模拟要求如下:   ID SName

    相关 Oracle sql语句练习

    在网上找的 Oracle sql语句练习 终于磕磕绊  绊的做完了!!!! \--1、查询“c001”课程比“c002”课程成绩高的所有学生的学号; select