Home > Database > Mysql Tutorial > body text

MySQL optimization - sample code analysis of simple syntax

黄舟
Release: 2017-03-09 13:28:40
Original
1168 people have browsed it

MySQL optimization - sample code analysis of simple syntax:

1. Default constraints

    --mysql
    CREATE TABLE emp
    (
    id INT DEFAULT 12
    )
Copy after login

2. Set auto-increment columns

MYSQL's auto-increment column must be an indexed column, and the seed value must be set at the back of the table.

  --mysql
    -- 设置自增ID从N开始
    CREATE TABLE emp (
    ID INT  PRIMARY KEY AUTO_INCREMENT
    ) AUTO_INCREMENT = 100 ; --(设置自增ID从100开始)
Copy after login

Set the step size of the auto-increment column, which can be divided into global level and session level. If it is the session level, then when the user creates a new session, the step size returns to the global level. MySQL cannot be set to the table level step size! !
The mysql server maintains two types of mysql system parameters (system variables): global variables and session variables.
Their meanings and differences are as shown in their respective names. Session variables are at the session level, and changes to them will only affect this session; global variables are at the system level, and
changes to them will Affects all new sessions (existing sessions are not affected when changing) until the next restart of mysql server.
Note that the impact of its change cannot span restarts. If you want to use the new value when the mysql server restarts, you can only specify it by specifying variable options on the command line or changing the option file.
The change through SET is Cannot reach across restarts.
Each system variable has a default value, which is determined when compiling the mysql system.
To specify system variables, you can generally specify options on the command line when the server is started or through the option file
Of course, most system variables can be specified through the set command when the system is running. value.
View the system's current default auto-increment column seed value and step value

SHOW GLOBAL VARIABLES LIKE 'auto_incre%'; -- 全局变量
Copy after login

Question: If there is a table with an auto-increment primary key with the field id, when it has been added to the table After inserting 10 pieces of data, delete the data with IDs 8, 9, and 10, restart mysql, and insert another piece of data after
. What should the ID value of this piece of data be? Is it 8 or 11?
Answer: If the table type is MyISAM, then it is 11. If the type of table is InnoDB, the id is 8.
This is because the two types of storage engines store the maximum ID record in different ways. The MyISAM table records the maximum ID into the data file, and the maximum ID value of the auto-incremented primary key will not be lost when restarting mysql;
InnoDB records the maximum ID value in memory, so after restarting mysql or performing an OPTIMIZE operation on the table, the maximum ID value will be lost.
By the way, there are four ways for MYSQL to get the auto-increment value of the current table.

(1) SELECT MAX(id) FROM person   针对特定表
(2) SELECT LAST_INSERT_ID()  函数   针对任何表
(3) SELECT @@identity    针对任何表
Copy after login

@@identity means the latest data inserted into the table with the identity attribute (i.e. auto-increment column) The value of the corresponding auto-increment column is a system-defined global variable.
Generally, system-defined global variables start with @@, and user-defined variables start with @.
The prerequisite for using @@identity is that after performing the insert operation, the connection is not closed when executing select @@identity, otherwise the result will be a NULL value.

(4)  SHOW TABLE STATUS LIKE 'person'
Copy after login

If you are targeting a specific table, it is recommended to use this method
In the result, there is an Auto_increment field in the corresponding table name record, and the value of the next auto-increment ID in it is the current The maximum auto-increment ID of the table.

3. View the table definition

 DESC emp
Copy after login

4. Modify the table name

ALTER TABLE emp RENAME emp2
Copy after login

5. Modify the data type of the field

Change the int type of the id field to bigint

ALTER TABLE emp2 MODIFY id BIGINT
Copy after login

6. Modify the field name

When modifying the field name in MYSQL, you need to add the data type of the field, otherwise it will An error is reported, and CHANGE can also only modify the data type to achieve the same effect as MODIFY. The method is to set the "new field name" and "old field name" in the SQL statement to the same name, and only change the "data type". Change Data type, such as the example just now, change the id column to bigint data type

<p style="margin-bottom: 7px;">ALTER TABLE emp2 CHANGE id id BIGINT<br/></p>
Copy after login

7. Add fields

ALTER TABLE emp2 ADD NAME NVARCHAR(200)  NULL
Copy after login

8. Delete fields

MYSQL delete fields There is no need to add the COLUMN keyword

 ALTER TABLE emp2 DROP NAME
Copy after login

9. Delete foreign key constraints

If it is a foreign key constraint, you need to use DROP FOREIGN KEY. If it is a primary key constraint, you need to use DROP PRIMARY KEY.
--Delete foreign key constraints

 ALTER TABLE emp2 DROP FOREIGN KEY fk_emp_dept
Copy after login

--Delete primary key constraints

 ALTER TABLE emp2 DROP PRIMARY KEY pk_emp_dept
Copy after login

10. Delete table

DROP TABLE emp2
Copy after login

But if you want to delete it at the same time Please make a judgment first before deleting multiple tables or deleting

DROP TABLE IF EXISTS emp1 ,emp2
Copy after login

Supplement:

USE test;
-- myisam引擎
CREATE TABLE TEST(
ID int unsigned not null auto_increment,
name varchar(10) not null,
  key(name,id))engine=MYISAM auto_increment=100
;

-- innodb引擎
CREATE TABLE TESTIdentity(
ID int unsigned   not null auto_increment,
NID INT UNSIGNED ,
name varchar(10) not null,
  key(id))engine=INNODB auto_increment=100
;

--或者主键
CREATE TABLE TESTIdentity(
ID int unsigned   not null auto_increment,
NID INT UNSIGNED ,
name varchar(10) not null,
  key(id))engine=INNODB auto_increment=100
;

[Database4]
ErrorCode: -2147467259, Number: 1075
ErrorMessage: Incorrect table definition; there can be only one auto column and it must be defined as a key
alter table TESTIdentity modify column nid int auto_increment;
Copy after login

No matter the innodb engine or the MYISAM engine table, there can only be one auto-increment column, and the auto-increment column The column must be an index column, whether it is a secondary index or a primary key index

The above is the detailed content of MySQL optimization - sample code analysis of simple syntax. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!