MYSQL Basic Learning

Operation in the case of mysql command line

 

First, the definition of variables

The first usage: set @num=1; or set @num:=1; //Here to use variables to save data, use the @num variable directly

The second usage: select @num:=1; or select @num:=field name from table name where  …

Such as:

1)set @t_error=0; select @t_error ;

2)select @num:=`name`  from   sys_area  where id=2  ; select @num;

 

2. Start the transaction and commit the transaction, roll back the transaction, if you plan to start the transaction, set the automatic commit to 0, start transaction; commit; 

# Value 1 auto commit 0 manual commit

select @@autocommit;  

#Set up manual submission

set @@autocommit=0;

start transaction ;

insert into dic(name ) values('1');

insert into dic(name ) values('2');

ROLLBACK;

commit;

 

Three, the three characteristics of the database: atomicity, consistency, isolation, persistence

Among them, isolation is the isolation principle between different things, and it is necessary to set the transaction isolation level to deal with it.

 

Fourth, the isolation level of the transaction (only used for the data of the business with transactions):

Read Uncommitted, Read Committed, Repeatable Read, Serializable 

1) View the current session isolation level

select @@tx_isolation;

2) View the current isolation level of the system

select @@global.tx_isolation;

3) Set the current session isolation level

set session transaction isolatin level repeatable read;

4) Set the current isolation level of the system

set global transaction isolation level repeatable read;

 

五、数据库的共享锁和排它锁

查询当前有多少事务,多少锁

SELECT * FROM information_schema.INNODB_TRX ;

SELECT * FROM information_schema.INNODB_LOCKS;

 

两者不同点在于是否允许另一个事务读取被锁住的数据

共享锁锁住了一条id=1的数据,则其他的事务是可以读取,但不能更改

排它锁锁住了一条id=1的数据,其他的事务不可以读取,也不能更改,直到这个事务完成

如果另一个查询没有显示添加锁,则他是可以读取数据,不受上面锁影响 

例子:

select @@autocommit;  

 

#设置手动提交

set @@autocommit=0;

 

set @t_error=0;

select @t_error ;

 

start transaction ;

select * from dic for update ;

insert into dic(name ) values('1');

insert into dic(name ) values('2');

ROLLBACK;

commit;

 

 

 

六、外键不能删除,必须先删除外键在删除相应的索引

 

 #查看表的外键以及外键名称

 show create table  表名

 #删除外键

 alter table 表名  drop FOREIGN KEY 外键名称  ;

 #显示所有的索引,并且删除需要删除的索引

 show index from  表名称 ;

 alter table  表名称 drop  index  FK_cv98jak92idoqj8rrlyhkghv0 ;

 

七、字段的添加与删除

 alter table   表名  drop column    cluster_num

 alter table   表名  add cluster_num int comment '数量';

 

八、NULL问题

 SELECT 1 = NULL, 1 <> NULL, 1 < NULL, 1 > NULL;

 

九、show  full  processlist (状态字段的意思)

或者(select * from  information_schema.processlist where  host like '%10.168.2.65%')

 

1)Sleep

通常代表资源未释放,如果是通过连接池,sleep状态应该恒定在一定数量范围内

(一般连接池会有一个参数initialSize,如果initialSize=10则初始化的时候会有10个状态为sleep连接被建立,并且一直存在),

例如:

数据查询时间为0.1秒,而网络输出需要1秒左右,原本数据连接在0.1秒即可释放,但是因为前端程序未执行close操作,

直接输出结果,那么在结果未展现在用户桌面前,该数据库连接一直维持在sleep状态

 

2)Locked

操作被锁定,通常使用innodb可以很好的减少locked状态的产生

 

3)Copy to tmp table

索引及现有结构无法涵盖查询条件时,会建立一个临时表来满足查询要求,产生巨大的i/o压力Copy to tmp table通常与连表查询有关,

建议减少关联查询或者深入优化查询语句,如果出现此状态的语句执行时间过长,会严重影响其他操作,此时可以kill掉该操作

4)Sending data

Sending data并不是发送数据,是从物理磁盘获取数据的进程,如果你的影响结果集较多,那么就需要从不同的磁盘碎片去抽取数据,

如果sending data连接过多,通常是某查询的影响结果集过大,也就是查询的索引项不够优化

 

5)Storing result to query cache

如果频繁出现此状态,使用set profiling分析,如果存在资源开销在SQL整体开销的比例过大(即便是非常小的开销,看比例),

则说明query cache碎片较多,使用flush query cache可即时清理,Query cache参数可适当酌情设置

 

 

 

十、mysql日期时间函数的处理

date_format(date, format) 函数,MySQL日期格式化函数date_format()

unix_timestamp( 时间日期) 函数 ,Mysql日期转换为unix时间戳

str_to_date(str, format) 函数 ,字符串转化日期

from_unixtime(unix_timestamp, format) 函数,MySQL时间戳格式化函数from_unixtime

实例

select DATE_FORMAT(now(),'%Y-%m-%d' ) 

select str_to_date('2017-12-08 00:00:00', '%Y-%m-%d %H:%i:%s' ) 

select unix_timestamp( DATE_FORMAT(now(),'%Y-%m-%d' )  )*1000  ;

select unix_timestamp( str_to_date('2017-12-08 00:00:00', '%Y-%m-%d %H:%i:%s' )  )*1000 ;

select from_unixtime( unix_timestamp( DATE_FORMAT(now(),'%Y-%m-%d' )  ),'%Y-%m-%d %H:%i:%s'  )  

 

 十一、in和exist区别

    exist先查询主表,遍历主表每一条,然后在子查询中查询,如果有数据返回,则主表的这条数据放到返回的查询结果中,主要子表索引起作用。子表数据多了,但因为索引查询,速度比较快。

    in:先查询子查询,得到查询结果,相当于查询结果or连接,然后使用主表索引,查询每个or条件的查询条件。那么子表查询结果集越多,最后查询越慢,子表越少查询越快

 

http://www.manongjc.com/article/981.html

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326067278&siteId=291194637