desc / explain:慢性分析工具,查询优化的时候就是desc根据分析,看索引信息和查询条件的约束。
delete会清除表中记录并且不能恢复,drop可以恢复。
增:
注意:插入属性值个数一定要和所写属性名的个数相匹配。
向表中插入一条内容(根据所写属性插入属性对应的值):
-
insert into 表名 values (属性值,属性值,属性值) ;
-
insert into 表名(属性名,属性名) values (属性值,属性值) ;
向表中批量插入n条内容:
- insert into 表名(属性名,属性名) values (属性值,属性值),(属性值,属性值) ,(属性值,属性值);
删:
注意:主键只会记住他身后的值,就算清空表中内容,主键还会从下一个值开始。
删除表中的一条内容:
- delete from 表名 where 条件;
删除表中的所有内容:
- delete from 表名 ;
改:
修改表中的数据:
- update 表名 set 属性 = 值,属性 = 值 where 条件;
查:
select uuid():其实select就是将结果打印出来,select可以不加from,可以直接加函数,并将函数查出来的值打印出来。
单表查询:
查询属性列:
- select 属性 from 表名;
基本条件查询属性列:
- select 属性 from 表名 where 条件;
查询出来的属性名会用别名代替:
- select 属性 别名 , 属性 别名 from 表名;
- select name 姓名 , pwd 密码 from 表名;
有的客户看不懂英文,所以可以用别名设置查出来的结果用中文进行展示。
多表联查:
根据条件从表一和表二中查询所需要的字段:
- select 表一.属性,表二.属性 from 表一名,表二名 where 表一.属性 = 表二.属性;
- select name, mname from tb_user, tb_mar where name = uname;
其实,where后面的是表达式,当结果为true时,就会执行。
条件查询:
- in在离散的范围之内:select * from tb_user where id in (10, 2, 6);
- between在a和b之间(连续范围):select * from tb_user where id between 7 and 10 ;
- 与:and
- 或 :or
- 非:not
- like :模糊查询 %:代表任意值:select * from tb_user where name like ‘%a%’ ;
联合查询:
join,不join时先生成临时表,然后根据where的条件判断,最后生成。用join时在根据where的条件判断,然后在生成表。
-
select * from tb_user join tb_mar on name = uname;
-
左外联查询:select * from 左表 left join 右表 on 左属性 = 右属性
-
右外联查询:select * from 左表 right join 右表 on 左属性 = 右属性
-
desc select * from 左表 right join 右表 on 左属性 = 右属性\G 6查询优化,检查查询过程中的执行过程。(查询优化时要做的)
子查询:
where的条件是最后查出来的值。