mysql functional test

 

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 uncommitted content), Read Committed (read committed content), Repeatable Read (rereadable), Serializable (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;

 

Five, shared locks and exclusive locks of the database

 

	Query how many transactions and how many locks there are currently
	SELECT * FROM information_schema.INNODB_TRX ;
	SELECT * FROM information_schema.INNODB_LOCKS;

	The difference between the two is whether another transaction is allowed to read the locked data
	If a shared lock locks a piece of data with id=1, other transactions can read it, but cannot change it
	An exclusive lock locks a piece of data with id=1, and other transactions cannot read or change it until the transaction is completed
	If another query does not show adding a lock, he can read the data without being affected by the above lock
	example:
	select @@autocommit;  

	#Set up manual submission
	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;

  6. The foreign key cannot be deleted, you must delete the foreign key first and then delete the corresponding index

 

 

 

#View the foreign key of the table and the foreign key name
 show create table  表名
 #delete foreign key
 alter table table name drop FOREIGN KEY foreign key name;
 #Display all indexes and delete the index that needs to be deleted
 show index from table name ;
 alter table table name drop index FK_cv98jak92idoqj8rrlyhkghv0 ;

 7. Add and delete fields

 

 

alter table   表名  drop column    cluster_num
 alter table table name add cluster_num int comment 'number';

 

 

Eight, NULL problem

 

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

 Nine, show full processlist (meaning of the status field)

 

  

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

  

1)Sleep
Usually means that the resource is not released. If it is through the connection pool, the sleep state should be constant within a certain range
(Generally, the connection pool will have a parameter initialSize. If initialSize=10, there will be 10 states where the sleep connection is established and exists during initialization),
E.g:
The data query time is 0.1 seconds, and the network output takes about 1 second. The original data connection can be released in 0.1 seconds, but because the front-end program does not perform the close operation,
If the result is output directly, the database connection remains in the sleep state until the result is not displayed on the user's desktop.

2)Locked
The operation is locked, usually using innodb can reduce the generation of locked state very well

3)Copy to tmp table
When the index and the existing structure cannot cover the query conditions, a temporary table will be established to meet the query requirements, resulting in huge I/O pressure. Copy to tmp table is usually related to table query.
It is recommended to reduce the associated query or optimize the query statement in depth. If the execution time of the statement in this state is too long, it will seriously affect other operations. At this time, you can kill the operation.
4)Sending data
Sending data is not a process of sending data, but a process of obtaining data from physical disks. If you affect a large number of result sets, then you need to extract data from different disk fragments.
If there are too many sending data connections, it is usually because the result set of a query is too large, that is, the index items of the query are not optimized enough.

5)Storing result to query cache
If this state occurs frequently, use set profiling analysis. If there is a resource overhead that accounts for a large proportion of the overall SQL overhead (even if it is a very small overhead, depending on the proportion),
It means that the query cache is more fragmented, and the flush query cache can be used to clear it immediately. The query cache parameter can be set as appropriate.

 

 

Ten, mysql date time function processing

 

 

date_format(date, format) function, MySQL date formatting function date_format()
unix_timestamp (time date) function, Mysql date is converted to unix timestamp
str_to_date(str, format) function, string conversion date
from_unixtime(unix_timestamp, format) function, MySQL timestamp format function from_unixtime
example
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'  )  ;

 11. Multi-table association update and delete

 

  

#Associate to update the data of a table
update user a    join  user_ext b on a.uid=b.id  
set  b.name=a.name, b.age=a.age
where ifNULL(a.business_ip,'')<>ifNULL(b.new_business_ip,'')   
 
 #Associate to delete the data of the h table
delete h.*
from user  g   join user_ext h on g.id=h.userId
where g.id=1

The difference between in and exist:
http://www.manongjc.com/article/981.html

 

 

 

 

Guess you like

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