When inserting data mysql insert Some Applications IGNORE, ON DUPLICATE KEY UPDATE, insert ignore into replace into MySQL is, replace into other summary

 

turn:

When several operations DELAYED mysql insert, IGNORE, the difference between the ON DUPLICATE KEY UPDATE

zccst finishing

a, DELAYED used

using a delay insert

DELAYED adjusted operator is used and REPLACE INSERT statement. When the operation reaches the DELAYED inserted, the server the data row into a queue, and immediately returned to the client a status information, so that the client can continue to operate until the data is actually inserted into the recording sheet. If the read data are read from the data table, the data in the queue will be maintained, until there is no readers so far. Then the server begins inserting delay data rows (delayed-row) rows of data in the queue. While the insertion operation, the server also checks whether there is a new read request and waiting for arrival. If so, the delay line data queue was suspended, allowing the operation to continue the reading. When those who do not read when the server started again insert delayed rows. This process continues until the queue is empty so far.

To a few precautions:
· INSERT DELAYED should be used only for INSERT statement specifies a list of values. Ignore server for DELAYED INSERT DELAYED ... SELECT statements.
The server ignored for INSERT DELAYED ... ON DUPLICATE UPDATE statement DELAYED.
· Because the front row is inserted, the statement returns immediately, so you can not use LAST_INSERT_ID () to get the AUTO_INCREMENT value. AUTO_INCREMENT value may be generated by the statement.
• For SELECT statements, DELAYED rows are not visible until the lines were actually inserted so far.
· DELAYED in slave replication server is ignored, because DELAYED no primary server is not the same data on a slave server.

Note that each line in the queue is currently only stored in the memory until they are inserted into the table so far. This means that if you are forced to abort the mysqld (for example, using kill -9) or if mysqld unexpectedly stopped, all the lines are not written to disk will be lost.



Two, IGNORE use

IGNORE is a MySQL extension to standard SQL. If there are duplicate keys in the new table, or when the warning after STRICT mode is activated, use the ALTER TABLE IGNORE control operation. If you do not specify IGNORE, when a duplicate key error occurs, the copy is aborted return to the previous step. If IGNORE is specified, then for rows have duplicate keywords, only the first line, the other lines of conflict are removed. And, on the error value is corrected to make it as close to the correct value.

insert ignore into tb (...) value (...)

so do not check whether there is, and there is ignored, no add



three, ON DUPLICATE KEY UPDATE using
MySQL since version 4.1 started supporting INSERT ... ON DUPLICATE KEY UPDATE syntax, making an already need to perform three SQL statements (SELECT, INSERT, UPDATE), reduced to a sentence to complete.

E.g. ipstats table structure as follows:
references
the CREATE TABLE ipstats (
  IP VARCHAR (15) the NOT NULL UNIQUE,
  Clicks SMALLINT (. 5) UNSIGNED the NOT NULL the DEFAULT '0'
);

otherwise need to perform three SQL statement, as follows:

The IF (the FROM ipstats the SELECT * = the WHERE IP '192.168.0.1') {
the UPDATE ipstats the SET. 1 Clicks = Clicks + IP = the WHERE '192.168.0.1';
} {the else
the INSERT the INTO ipstats (IP, Clicks) the VALUES ( '192.168.0.1 ', 1);
}

now just below an SQL statement to complete:

the INSERT the INTO ipstats the VALUES (' 192.168.0.1 ', 1) the ON KEY the DUPLICATE the UPDATE Clicks = Clicks + 1;

note that for this statement, provided that the table must have a unique index or primary key.

MySQL> Create Table I (UNIQUE ID int, int CO.'s);
MySQL> INSERT INTO I values (1,1), (2,1);
MySQL> INSERT INTO values I (1,1) ON = CO.'s Duplicate Key Update CO.'s + 1'd;
MySQL> SELECT * from I;
+ ------ + ------ +
| ID | CO.'s |
+ ------ + ------ +
|. 1 | 2 |
| 2 | 1 |
+ ------ + ------ +

// If we perform a insert into i values (1,1) on duplicate key update co = co + 1; the result is:
MySQL> SELECT * from I;
+ ------ + ------ +
| the above mentioned id | CO |
+ ------ + ------ +
| 1 | 3 |
| 2 | 1 |
+ ------ + ------ +

// tested when id primary key is also possible, as when the UNIQUE
the Create the Table i (id int AUTO_INCREMENT Primary key, CO int);

Fourth, the difference between the three
DELAYED as quickly insert, it is not very concerned about the failure, improve insert performance.

ignore only concerned with the primary key of the corresponding record does not exist, no add, there is ignored.

ON DUPLICATE KEY UPDATE when adding operation, attention non-primary key column, note the difference between the ignore. Updates the designated column there is no add.

 

----------------------

turn:

insert ignore into MySQL in, replace into some uses such as summary

MySQL replace into three forms:

1. replace into tbl_name(col_name, ...) values(...)

2. replace into tbl_name(col_name, ...) select ...

3. replace into tbl_name set col_name=value, ...

 

1.insert ignore into

When inserting data, such as when an error occurs, such as the duplicate data will not return an error, it returns only as a warning. So be sure to use ignore the statement itself is no problem, otherwise it will be ignored. E.g:

INSERT IGNORE INTO books (name) VALUES ('MySQL Manual')

2.on duplicate key update

When the primary or unique repeated update statement is executed, as described later update is unnecessary statements, such as id = id, the same as a function, but the error can not be ignored. For example, to achieve repeatable name data insertion is not being given, may be used at statement:

INSERT INTO books (name) VALUES ('MySQL Manual') ON duplicate KEY UPDATE id = id

3.insert … select … where not exist

The conditions for determining whether the select inserted, can only be determined by the primary and UNIQUE, but also through other conditions. E.g:

INSERT INTO books (name) SELECT 'MySQL Manual' FROM dual WHERE NOT EXISTS (SELECT id FROM books WHERE id = 1)

4.replace into

If the same primary or unique records exist, the first deleted. Then insert a new record.

REPLACE INTO books SELECT 1, 'MySQL Manual' FROM books

MySQL replace into three forms:

1. replace into tbl_name(col_name, ...) values(...)

2. replace into tbl_name(col_name, ...) select ...

3. replace into tbl_name set col_name=value, ...

 

1.insert ignore into

When inserting data, such as when an error occurs, such as the duplicate data will not return an error, it returns only as a warning. So be sure to use ignore the statement itself is no problem, otherwise it will be ignored. E.g:

INSERT IGNORE INTO books (name) VALUES ('MySQL Manual')

2.on duplicate key update

When the primary or unique repeated update statement is executed, as described later update is unnecessary statements, such as id = id, the same as a function, but the error can not be ignored. For example, to achieve repeatable name data insertion is not being given, may be used at statement:

INSERT INTO books (name) VALUES ('MySQL Manual') ON duplicate KEY UPDATE id = id

3.insert … select … where not exist

The conditions for determining whether the select inserted, can only be determined by the primary and UNIQUE, but also through other conditions. E.g:

INSERT INTO books (name) SELECT 'MySQL Manual' FROM dual WHERE NOT EXISTS (SELECT id FROM books WHERE id = 1)

4.replace into

If the same primary or unique records exist, the first deleted. Then insert a new record.

REPLACE INTO books SELECT 1, 'MySQL Manual' FROM books

Guess you like

Origin www.cnblogs.com/libin6505/p/11390327.html