Mysql集合笔记

  1 ----------------------------Mysql基本操作----------------------------
  2 
  3 desc 降序 
  4 
  5 ---------------例 desc 表 (查看表的描述)
  6 ---------------例 show create table 表(查看表怎么创建的)
  7 ---------------例 show index from 表 (查看表的索引)
  8 ---------------例 set profiling = 1;      SQL 执行语句     show profiles;(查看执行时间)
  9 
 10     
 11 
 12 create----创造
 13 
 14 defalut----默认
 15 
 16 engine----引擎
 17 
 18 charset----字符集
 19 
 20 
 21 ---------------例 create table tb1(nid int not null defalut 2,num int not null)engine=InnoDB default charset=utf8
 22 
 23 drop----放弃;停止 删除表
 24 
 25 ---------------例 drop table 表名
 26 
 27 alter----改变 改变表
 28 
 29 ---------------例 alter table 表名 add 列名 类型 (添加列)
 30 
 31 column----专栏;圆柱;纵队,列
 32 
 33 ---------------例 alter table 表名 drop column 列名(删除列)
 34 
 35 modify----修改;被修饰
 36 
 37 ---------------例 alter table 表名 modify column 列名 类型;  -- 类型(修改列)
 38 
 39 change----改变,变更;交换
 40 
 41 ---------------例 alter table 表名 change 原列名 新列名 类型; -- 列名,类型(修改列)
 42 
 43 primary----首要的,主要的
 44 
 45 ---------------例 alter table 表名 add primary key(列名);(添加主键)
 46 
 47 ---------------例 alter table 表名 drop primary key;(删除主键)
 48 
 49 ---------------例 alter table 表名  modify  列名 int, drop primary key;(删除主键)
 50 
 51 constraint----约束;限制;强制
 52 
 53 foreign----外国的 外
 54 
 55 references----参考引用
 56 
 57 ---------------例 alter table 从表 add constraint 外键名称(形如:FK_从表_主表) foreign key 从表(外键字段) references 主表(主键字段);(添
 58 
 59 加外键)
 60 
 61 ---------------例 alter table 表名 drop foreign key 外键名称(删除外键)
 62 
 63 default----设置默认值
 64 
 65 ---------------例 alter table 表 alter 列 set default 1000;(修改默认值)
 66 
 67 ---------------例 alter table 表 alter 列 drop default (删除默认值)
 68 
 69 insert----插入 
 70 
 71 into----方向
 72  
 73 ---------------例 insert into 表 (列名,列名...) values (值,值,值...)
 74 
 75 delete----删除 清空表
 76 
 77 ---------------例 delete from 表 where id=1 and name='alex'
 78 
 79 update----更新
 80 
 81 ---------------例 update 表 set name = 'alex' where id>1
 82 
 83 select----挑选出来的
 84 
 85 ---------------例 select * from 表 where id > 1
 86 
 87 like----类似的 喜欢的
 88 
 89 ---------------例 select * from 表 where name like 'ale%'  - ale开头的所有(多个字符串)
 90 
 91 limit----限制
 92 
 93 ---------------例 select * from 表 limit 4,5;          - 从第4行开始的5行
 94 
 95 count----计数
 96 
 97 sum---- 98 
 99 ---------------例 select count(列) from 表;
100 
101 ---------------例 select count(列),sum(列),max(列),min(列) from102 
103 order----要求 命令  秩序
104 
105 ---------------例 select * from 表 order by 列 asc     - 根据 “列” 从小到大排列
106 
107 ---------------例 select * from 表 order by 列 desc        - 根据 “列” 从大到小排列
108 
109 group----110 
111 group by----分组依据
112 
113 ---------------例 select num from 表 group by num          分组
114 
115 having----所有的
116 
117 ---------------例 select num from 表 group by num having max(id) > 10
118 
119 union----同盟,联盟;协会,工会;联合,团结
120 
121 ---------------例 select 列1 from 表1 union select 列2 from 表2(组合查看,自动处理重合)
122 
123 ---------------例 select 列1 from 表1 union all select 列2 from 表2(组合查看,不处理重合)
124 
125 http://www.cnblogs.com/cloniu/p/6393588.html
Mysql基本操作
 1 ----------------------------存储过程----------------------------
 2 
 3 procedures 程序---创建函数
 4 
 5 exists   存在---判断条件中使用
 6 
 7 declare  声明---声明变量
 8 
 9 default  默认---默认值设置
10 
11 delimiter  定界符 向服务器说明以什么结束
12 
13 then  然后---多个语句判断添加
14 
15 http://www.cnblogs.com/cloniu/p/6396904.html
存储过程
1 ----------------------------自定义函数----------------------------
2 
3 http://www.cnblogs.com/cloniu/p/6398695.html
自定义函数
 1 ----------------------------触发器----------------------------
 2 
 3 trigger----触发
 4 
 5 before----在之前
 6 
 7 after----在之后
 8 
 9 for each row----对于每一行
10 
11 old----老的
12 
13 new----新的
14 
15 http://www.cnblogs.com/cloniu/p/6398207.html
触发器
1 ----------------------------视图----------------------------
2 
3 view----视图
4 
5 视图是一个虚拟表(非真实存在),其本质是【根据SQL语句获取动态的数据集,并为其命名】,用户使用时只需使用【名称】即可获取结果集,并可以将其
6 
7 当作表来使用。
8 
9 http://www.cnblogs.com/cloniu/p/6396287.html
视图
1 ----------------------------事务----------------------------
2 
3 
4 事务用于将某些操作的多个SQL作为原子性操作,一旦有某一个出现错误,即可回滚到原来的状态,从而保证数据库数据完整性。
5 
6 http://www.cnblogs.com/cloniu/p/6400753.html
事务
1 ----------------------------函数----------------------------
2 
3 
4 http://www.cnblogs.com/cloniu/p/6398695.html  (自定义函数)
5 
6 https://dev.mysql.com/doc/refman/5.7/en/string-functions.html (内置函数)
函数
 1 ----------------------------动态执行sql语----------------------------
 2 
 3 prepare 准备
 4 
 5 using   使用
 6 
 7 deallocate    释放
 8 
 9 execute   执行
10 
11 http://www.cnblogs.com/cloniu/p/6400238.html
动态执行sql语
 1 ----------------------------索引----------------------------
 2 
 3 普通索引:列,只能帮助查找
 4 
 5 唯一索引:单列,联合列,只能帮助查找,内容不允许重复
 6 
 7 主键索引:只能帮助查找,内容不允许重复,不允许NULL ,一张表只能有一个主键,可以让两列组合成为一个主键
 8 
 9 组合索引:多列共同组合一个索引
10 
11 profiling 分析   查看 执行过程用了多少时间
12 
13 profiles  简介
14 
15 ---------------例 set profiling = 1  SQL语句...     show profiles;
16 
17 explain 查看执行过程 
18 
19 ---------------例 explain select * from 表 (where 列=值 --可有可无);
20 
21 ---------------例 show index from 表 (查看表的索引)
22 
23 const 不变的  
24 
25 ---------------解:通过explain查看执行过程的时候 type 的类型 是最快的,
26 
27 
28 详情猛搓: http://www.cnblogs.com/cloniu/p/6401662.html
29 
30 
31 覆盖索引  索引合并  组合索引  概念
32 
33 小知识:导致索引没用的点;
34 
35 ---------------例 select * from tb1 where name like '%cn';(最左前缀----这种方式不走索引,%需要在 cn% 才OK)
36 
37 reverse----反转(内置函数)
38 
39 ---------------例 select * from tb1 where reverse(name) = 'wupeiqi';( 这样就走----name = reverse('wupeiqi'))
40 
41 ---------------例 select * from tb1 where name = 999;(如果列是字符串类型,传入条件是必须用引号引起来,不然...不走)
42 
43 ---------------例 select * from tb1 where name != 'alex'( != 特别的:如果是主键,则还是会走索引,其它不走索引)
44 
45 ---------------例 select * from tb1 where name > 'alex'( > 特别的:如果是主键或索引是整数类型,则还是会走索引,其它不走索引)
46 
47 ---------------例 select email from tb1 order by name desc;(当根据索引排序时候,选择的映射如果不是索引,则不走索引,特别的:如果对主键排序
48 
49 ,则还是走索引:)
50 
51 
52 index----索引(普通索引创建)
53 
54 ---------------例 show index from 表 (查看表的索引)
55 
56 ---------------例 create index 索引名 on 表(列名)  (创建普通索引)
57 
58 ---------------例 alter table 表 drop index 索引名 (删除普通索引)
59 
60 ---------------例 drop 索引名 on 表;(删除普通索引)
61 
62 
63 unique----唯一的(唯一索引创建)
64 
65 ---------------例 create unique index 索引名 on 表名(列名);(创建唯一索引)
66 
67 ---------------例 drop unique index 索引名 on 表名;(删除唯一索引)
68 
69 主键索引
70 
71 ---------------例 alter table 表 add primary key(列);(创建主键索引)
72 
73 ---------------例 alter table 表 drop primary key;(删除主键索引)
74 
75 ---------------例 alter table 表名  modify  列名 int, drop primary key;(删除主键索引)
76 
77 组合索引
78 
79 index(列1,列2) ---两列组合 --- 最左前缀
80 
81 ---------------例 create index ix_name_email on in3(name,email);(创建组合索引)
82 
83 ---------------例 select * from 表 where 列1 = 值   (走索引)
84 
85 ---------------例 select * from 表 where 列1 = 值 and 列2 = 值  (走索引)
86 
87 ---------------例 select * from 表 where 列2 = 值 (不走索引)
索引
 1 ----------------------------执行计划----------------------------
 2 
 3 profiling 分析   查看 执行过程用了多少时间
 4 
 5 profiles  简介
 6 
 7 ---------------例 set profiling = 1  SQL语句...     show profiles;
 8 
 9 ---------------例 show create table 表名(查看生成表的SQL)
10 
11 ---------------例 show index from  表名(查看索引)
12 
13 explain + 查询SQL - 用于显示SQL执行信息参数,根据参考信息可以进行SQL优化
14 
15 小知识:正确的使用索引 详情猛搓:http://www.cnblogs.com/wupeiqi/articles/5716963.html
执行计划
----------------------------设置全局变量----------------------------

variables----变量

---------------例 show global variables like '%query%' (查看全局变量)

---------------例 set global 变量名 = 值  (修改全局变量)

---------------例 slow_query_log = OFF(ON)  是否开启慢日志记录

---------------例 long_query_time = 2   时间限制,超过此时间,则记录

---------------例 slow_query_log_file = /usr/slow.log  日志文件

---------------例 log_queries_not_using_indexes = OFF(ON)  为使用索引的搜索是否记录

详情猛搓:http://www.cnblogs.com/wupeiqi/articles/5716963.html
设置全局变量
详情猛搓:http://www.cnblogs.com/cloniu/p/6395151.html
一对多,多对多
1 ---------------例 insert into 表1(列1,列2,列3) select 列a,列b,列c from 表a WHERE 列c> 80  可以通过查看另一个表的内从 往另一个表里面插入数据 
inset into 增加知识点
select 查看的是表 不是只针对文件
select 查看的结果 可以当作临时表 再次被查看处理
left join 链接的是表 而不是只有外键关联的表,外键只是一个对应关系,

case when 表.(列) > 60 then 1 else 0 end       如果大于60 等于1  否则 等于0; 

in 下的子查询不能有 limit 
    select ...from xx nid in (select id from tb2 limit5)  不支持
小只是补充
原文地址:https://www.cnblogs.com/cloniu/p/6400562.html