MySQL INSERT IGNORE語句的使用
在INSERT語句中,使用IGNORE關鍵字時,在INSERT語句執行過程中發生的錯誤將會被忽略。例如,當向建有UNIQUE索引或主鍵的欄位插入重複欄位時,會導致duplicate-key錯誤,執行的語句會失敗。當帶有IGNORE關鍵字時,這個錯誤會被忽略,只會產生警告。
當向分割槽表插入資料時,IGNORE關鍵字具有類似的效果。當沒有要插入的分割槽時,MySQL會忽略錯誤。
當資料轉換引發報錯時,可以透過IGNORE關鍵字來忽略錯誤,這時MySQL會產生警告而不退出語句的執行,無效的數值會被調整成接近的數值並被插入。
mysql> desc t10;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(10) | NO | PRI | 0 | |
| name | varchar(15) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
mysql> select * from t10;
+----+--------+
| id | name |
+----+--------+
| 10 | Neo |
| 20 | Lucy |
| 30 | Lily |
| 50 | Dingyi |
| 60 | Jones |
+----+--------+
5 rows in set (0.00 sec)
mysql> desc t7;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(10) | NO | PRI | 0 | |
| name | varchar(15) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
mysql> select * from t7;
+----+--------+
| id | name |
+----+--------+
| 50 | Dingyi |
| 60 | Jones |
+----+--------+
2 rows in set (0.00 sec)
mysql> insert into t7 select * from t10;
ERROR 1062 (23000): Duplicate entry '50' for key 'PRIMARY'
mysql> select * from t7;
+----+--------+
| id | name |
+----+--------+
| 50 | Dingyi |
| 60 | Jones |
+----+--------+
2 rows in set (0.00 sec)
mysql> select * from t10;
+----+--------+
| id | name |
+----+--------+
| 10 | Neo |
| 20 | Lucy |
| 30 | Lily |
| 50 | Dingyi |
| 60 | Jones |
+----+--------+
5 rows in set (0.00 sec)
mysql> insert ignore into t7 select * from t10;
Query OK, 3 rows affected, 1 warning (0.00 sec)
Records: 5 Duplicates: 2 Warnings: 1
當向分割槽表插入資料時,IGNORE關鍵字具有類似的效果。當沒有要插入的分割槽時,MySQL會忽略錯誤。
當資料轉換引發報錯時,可以透過IGNORE關鍵字來忽略錯誤,這時MySQL會產生警告而不退出語句的執行,無效的數值會被調整成接近的數值並被插入。
mysql> desc t10;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(10) | NO | PRI | 0 | |
| name | varchar(15) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
mysql> select * from t10;
+----+--------+
| id | name |
+----+--------+
| 10 | Neo |
| 20 | Lucy |
| 30 | Lily |
| 50 | Dingyi |
| 60 | Jones |
+----+--------+
5 rows in set (0.00 sec)
mysql> desc t7;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(10) | NO | PRI | 0 | |
| name | varchar(15) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
mysql> select * from t7;
+----+--------+
| id | name |
+----+--------+
| 50 | Dingyi |
| 60 | Jones |
+----+--------+
2 rows in set (0.00 sec)
mysql> insert into t7 select * from t10;
ERROR 1062 (23000): Duplicate entry '50' for key 'PRIMARY'
mysql> select * from t7;
+----+--------+
| id | name |
+----+--------+
| 50 | Dingyi |
| 60 | Jones |
+----+--------+
2 rows in set (0.00 sec)
mysql> select * from t10;
+----+--------+
| id | name |
+----+--------+
| 10 | Neo |
| 20 | Lucy |
| 30 | Lily |
| 50 | Dingyi |
| 60 | Jones |
+----+--------+
5 rows in set (0.00 sec)
mysql> insert ignore into t7 select * from t10;
Query OK, 3 rows affected, 1 warning (0.00 sec)
Records: 5 Duplicates: 2 Warnings: 1
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/26506993/viewspace-2124075/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 教你使用SQLite-insert語句SQLite
- MySQL:一個簡單insert語句的大概流程MySql
- 【Mysql】兩條insert 語句產生的死鎖MySql
- SQLite Insert 語句SQLite
- mysql insert語句錯誤問題解決MySql
- MySQL 5.5 INSERT ... ON DUPLICATE KEY UPDATE語句說明MySql
- SQLite中特殊的INSERT語句SQLite
- MySQL中explain語句的使用MySqlAI
- Oracle 使用一條insert語句完成多表插入Oracle
- 【SQL】使用一條INSERT語句完成多表插入SQL
- 【SQL】9 SQL INSERT INTO 語句SQL
- Sql Server系列:Insert語句SQLServer
- JDBC使用INSERT RETURN語句報錯ORA-439JDBC
- MySQL的語句MySql
- 快速執行大量 insert 語句的方法
- MySQL 的CASE WHEN 語句使用說明MySql
- 【SQL】14 UNION 操作符、SELECT INTO 語句、INSERT INTO SELECT 語句、CREATE DATABASE 語句、CREATE TABLE 語句SQLDatabase
- PostgreSQL的insert語句執行過程分析SQL
- 一個insert插入語句很慢的優化優化
- oracle dblink實現insert語句的同步Oracle
- SQLTest系列之INSERT語句測試SQL
- mysql的常用語句MySql
- mysql語句MySql
- 生成某一使用者下所有表資料的insert語句
- MySQL全文索引原始碼剖析之Insert語句執行過程MySql索引原始碼
- MySQL使用profile分析語句效能消耗MySql
- oracle-資料庫- insert 插入語句Oracle資料庫
- SQLite語句(二):INSERT DELETE UPDATE SELECTSQLitedelete
- 用merge 語句代替 insert 和deletedelete
- MySQL 的Rename Table語句MySql
- Mysql中的DQL語句MySql
- MySQL replace語句MySql
- mySql常用語句MySql
- MySQL語句大全MySql
- MYSQL語句集MySql
- MySQL DML語句MySql
- MySQL Dll語句MySql
- MySQL 預處理語句prepare、execute、deallocate的使用MySql