设为首页 收藏本站
查看: 1251|回复: 0

[经验分享] mysq基础笔记(sql语句)

[复制链接]

尚未签到

发表于 2018-10-23 10:22:37 | 显示全部楼层 |阅读模式
  1、数据库操作相关SQL ---- database
  创建数据库 create database 数据库名称; ------ 在sql后通过 character set 指定数据库本身字符集,如果没有指定将服务器默认
  * 服务器默认字符集 mysql安装目录/my.ini [mysqld] default-character-set
  查看当前有哪些数据库 show databases;
  修改数据库(修改数据库字符集) 数据库字符集存放mysql安装目录/data/数据库文件夹/db.opt
  alter database 数据库名称 character set 字符集;
  * collate 校对方式 ----- 用于数据库排序; 每一种字符集都存在一种默认校对方式(可以不用修改)
  删除数据库 drop database 数据库名称;
  切换数据库(设置当前使用数据库) use 数据库名称;
  * select database(); 查看当前使用数据库
  2、数据表操作相关SQL ---- table表结构
  创建数据表 create table 表名(列名 类型(长度) 约束,列名 类型(长度) 约束... ) ----- 在创建表之前必须指定数据库
  * 查看当前有哪些数据表 show tables;
  查看数据表表结构 desc table;
  修改表结构:

  修改列类型(长度) :>
  添加一个新列 :>
  修改列名称 :>
  删除列 :>  修改表名 : rename table 旧表名 to 新表名
  * table 在创建时 character set 指定表字符集,如果没有指定采用数据库默认字符集
  删除表 drop table 表名;
  3、数据记录增删改查  insert update delete select
  数据记录插入 insert into 表名(列,...)  values(值,...);
  * 值的顺序和列顺序一致,个数一致 , 在开发中经常省略列名,值按照表结构所有字段进行设值
  数据查看 select * from 表名;
  数据记录修改 update 表名 set 列名=值,列名= 值 where 条件语句
  数据记录删除 delete from 表名 where 语句
  * truncate 与 delete区别 ? truncate删除表,重新创建, delete from 逐行删除----- 性能truncate好于 delete,delete被事务控制,删除后回滚取消删除,truncate不可恢复
  select 语句
  S - F - W - G - H - O
  select ... from ... where ... group by ... having ... order by ... ; 顺序固定的
  1、from 指定查询数据表
  2、where 前置过滤条件 --- 将表数据过滤掉一部分
  3、group by 对where 过滤后数据进行分组
  4、having 对分组后结果添加条件过滤
  5、select 指定检索哪些字段
  6、order by 对检索结果排序
  4、数据库备份和恢复
  备份命令: mysqldump -u 用户名 -p 数据库名 > sql脚本位置 (回车输入密码)
  恢复命令: mysql -u 用户名 -p 数据库名 < sql脚本位置 (回车输入密码 )
  * 在mysql连接后,通过source 进行数据库恢复 source sql脚本位置
  5、数据库完整性约束 ----- 保证数据表中记录完整性
  主键约束 primary key : 用来指定数据表数据记录的唯一标识
  唯一约束 unique : 该字段取值唯一
  非空约束 not null ; 该字段值不能为null
  外键约束 foreign key : 当两个数据表存在关联时,添加外键约束,外键约束引用另一张表主键
  条件约束 check : mysql不支持 Oracle支持 check age  exists实现上面in 语句效果
  select name from student where exists (select * from studentcource where score < 60 and student.id = studentcource.student_id);
  select * from studentcource,student where score < 60 and student.id = studentcource.student_id;
  select name from student where exists (select * from studentcource where score < 60 and student.id = studentcource.student_id);
  * 在实际开发中 exists比 in效率要高
  ANY、SOME、ALL 用法
  SOME和ANY作用相同的  ----- 一些 >any(1,2,3) 大于任何一个都可以 等价于 >min
  ALL ---- 所有  >all(1,2,3) 必须同时大于三个值   等价于 >max
  查询获得最高分的学生学号
  select max(score) from studentcource; 最高学分
  select student_id from studentcource where score = (select max(score) from studentcource);
  * 自我比较
  select student_id from studentcource where score >=all(select score from studentcource);
  查询编号2课程比编号1课程成绩高所有学号
  select score from studentcource where cource_id = 2 and score > any(select score from studentcource where cource_id = 1);
  select score from studentcource where cource_id = 2; 课程2所有成绩
  select score from studentcource where cource_id = 1; 课程1所有成绩
  使用union将两个查询结果合并,union 排重重复数据 union all 不会排重重复数据
  * 合并时列名必须一致
  ------------------------------------------------------------------------------------------------------
  查询语文课程比数学课程成绩高的所有学生的学号
  mysql> select * from cource,studentcource where cource.id = studentcource.cource
  _id and cource.name='语文';
  +----+------+------------+------------+-----------+-------+

  |>  +----+------+------------+------------+-----------+-------+
  |  1 | 语文 |          1 |          1 |         1 |    80 |
  |  1 | 语文 |          1 |          3 |         1 |    71 |
  |  1 | 语文 |          1 |          5 |         1 |    60 |
  |  1 | 语文 |          1 |          6 |         1 |    76 |
  |  1 | 语文 |          1 |         10 |         1 |    77 |
  +----+------+------------+------------+-----------+-------+
  5 rows in set (0.02 sec)
  mysql> select * from cource,studentcource where cource.id = studentcource.cource
  _id and cource.name='数学';
  +----+------+------------+------------+-----------+-------+

  |>  +----+------+------------+------------+-----------+-------+
  |  2 | 数学 |          1 |          1 |         2 |    90 |
  |  2 | 数学 |          1 |          2 |         2 |    53 |
  |  2 | 数学 |          1 |          3 |         2 |    70 |
  |  2 | 数学 |          1 |          4 |         2 |    90 |
  |  2 | 数学 |          1 |          5 |         2 |    70 |
  |  2 | 数学 |          1 |          6 |         2 |    88 |
  |  2 | 数学 |          1 |          8 |         2 |    71 |
  |  2 | 数学 |          1 |          9 |         2 |    88 |
  |  2 | 数学 |          1 |         10 |         2 |    76 |
  +----+------+------------+------------+-----------+-------+
  9 rows in set (0.00 sec)
  select t1.student_id,t1.score 语文,t2.score 数学 from (select * from cource,studentcource where cource.id = studentcource.cource_id and cource.name='语文') t1,(select * from cource,studentcource where cource.id = studentcource.cource_id and cource.name='数学') t2 where t1.student_id = t2.student_id and t1.score > t2.score;
  查询平均成绩大于70分的同学的学号和平均成绩
  * 按人取平均成绩 ------ 分组
  select student_id,avg(score) from studentcource group by student_id having avg(score)>70;
  * 打印学生姓名
  select student.name,t.avgscore from student,(select student_id,avg(score) avgscore from studentcource group by student_id having avg(score)>70) t where student.id = t.student_id;
  查询所有同学的学号、姓名、选课数、总成绩
  *学生信息:select * from student;
  *选课数、总成绩 select student_id,count(*),sum(score) from studentcource group by student_id;
  select student.id 学号,student.name 姓名,t.courcenum 选课数, t.sumscore 总成绩 from student,(select student_id,count(*) courcenum,sum(score) sumscore from studentcource group by student_id) t where student.id = t.student_id;
  查询没学过关羽老师课的同学的学号、姓名
  * 关羽老师教什么课 select cource.id from teacher,cource where teacher.id = cource.teacher_id and teacher.name='关羽';
  * 选过关羽老师课 select distinct student_id from studentcource where cource_id in (select cource.id from teacher,cource where teacher.id = cource.teacher_id and teacher.name='关羽');

  select>  查询学过语文并且也学过数学课程的同学的学号、姓名

  * 语文和数据 课程编号  select>  * 学过语文的学生 select * from cource,studentcource where cource.id = studentcource.cource_id and cource.name='语文';
  * 学过数学的学生 select * from cource,studentcource where cource.id = studentcource.cource_id and cource.name='数学';
  select t1.student_id  from (select * from cource,studentcource where cource.id = studentcource.cource_id and cource.name='语文') t1, (select * from cource,studentcource where cource.id = studentcource.cource_id and cource.name='数学') t2 where t1.student_id = t2.student_id ;
  select student.id,student.name from student,(select t1.student_id  from (select * from cource,studentcource where cource.id = studentcource.cource_id and cource.name='语文') t1, (select * from cource,studentcource where cource.id = studentcource.cource_id and cource.name='数学') t2 where t1.student_id = t2.student_id) t where student.id = t.student_id;
  查询学过赵云老师所教的所有课的同学的学号、姓名
  查询没有学三门课以上的同学的学号、姓名
  查询至少有一门课与学号为“1”的同学所学相同的同学的学号和姓名
  查询和小李同学学习的课程完全相同的其他同学学号和姓名
  查询各科成绩最高和最低的分
  查询学生信息和平均成绩
  查询上海和北京学生数量
  查询不及格的学生信息和课程信息
  查询每门功成绩最好的前两名
  统计每门课程的学生选修人数(超过两人的进行统计)
  把成绩表中“关羽”老师教的课的成绩都更改为此课程的平均成绩


运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-625327-1-1.html 上篇帖子: 对每项物品,找出最贵价格的物品的经销商,sql优化分析 下篇帖子: apache tomcat + apache server
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表