一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

Mysql|Sql Server|Oracle|Redis|MongoDB|PostgreSQL|Sqlite|DB2|mariadb|Access|數(shù)據(jù)庫技術(shù)|

服務(wù)器之家 - 數(shù)據(jù)庫 - Mysql - 數(shù)據(jù)庫SQL小技巧大揭秘:IGNORE選項(xiàng)讓你的數(shù)據(jù)處理更從容

數(shù)據(jù)庫SQL小技巧大揭秘:IGNORE選項(xiàng)讓你的數(shù)據(jù)處理更從容

2023-12-08 01:01未知服務(wù)器之家 Mysql

在 MySQL 中,IGNORE 是一種在插入或更新數(shù)據(jù)時(shí)處理沖突的選項(xiàng)。具體來說,在 INSERT | UPDATE語句中,IGNORE 的作用是在插入或更新數(shù)據(jù)時(shí)忽略特定的錯(cuò)誤,而不導(dǎo)致整個(gè)操作失敗。另外,IGNORE選項(xiàng)還可以在非空約束、寫入的字段內(nèi)容超

數(shù)據(jù)庫SQL小技巧大揭秘:IGNORE選項(xiàng)讓你的數(shù)據(jù)處理更從容

在 MySQL 中,IGNORE 是一種在插入或更新數(shù)據(jù)時(shí)處理沖突的選項(xiàng)。具體來說,在 INSERT | UPDATE 語句中,IGNORE 的作用是在插入或更新數(shù)據(jù)時(shí)忽略特定的錯(cuò)誤,而不導(dǎo)致整個(gè)操作失敗。另外,IGNORE 選項(xiàng)還可以在非空約束、寫入的字段內(nèi)容超過字段長度時(shí)進(jìn)行截?cái)嗵幚淼?,下面是幾個(gè)具體的例子。

一、主鍵或唯一鍵沖突

1、初始化測試表并初始化數(shù)據(jù)

mysql> create table  test1(id int not null primary key,
card_no varchar(10)  not null,
name varchar(20) not null, 
c1 varchar(2) ,
unique key uq_card_no(card_no)
);
Query OK, 0 rows affected (0.05 sec)
mysql> insert into test1(id,card_no,name,c1) 
values(1,'1000000000','abc','a')
Query OK, 1 row affected (0.01 sec)
mysql> select * from test1;
+----+------------+------+------+
| id | card_no    | name | c1   |
+----+------------+------+------+
|  1 | 1000000000 | abc  | a    |
+----+------------+------+------+
1 row in set (0.00 sec)

數(shù)據(jù)庫SQL小技巧大揭秘:IGNORE選項(xiàng)讓你的數(shù)據(jù)處理更從容

2、主鍵沖突

插入一個(gè)表中已存在的主鍵數(shù)據(jù)時(shí),如果不添加ignore,則會報(bào)主鍵沖突。

mysql>  insert into test1(id,card_no,name,c1) values(1,'1000000001','abc','a');
ERROR 1062 (23000): Duplicate entry '1' for key 'test1.PRIMARY'

加上ignore選項(xiàng)后,結(jié)果如下:

mysql> select * from test1;
+----+------------+------+------+
| id | card_no    | name | c1   |
+----+------------+------+------+
|  1 | 1000000000 | abc  | a    |
+----+------------+------+------+
1 row in set (0.00 sec)
mysql>  insert ignore into test1(id,card_no,name,c1) values(1,'1000000001','abc','a'),
    -> (2,'1000000001','ttt','b');
Query OK, 1 row affected, 1 warning (0.01 sec)
Records: 2  Duplicates: 1  Warnings: 1
mysql> show warnings;
+---------+------+---------------------------------------------+
| Level   | Code | Message                                     |
+---------+------+---------------------------------------------+
| Warning | 1062 | Duplicate entry '1' for key 'test1.PRIMARY' |
+---------+------+---------------------------------------------+
1 row in set (0.00 sec)
mysql> select * from test1;
+----+------------+------+------+
| id | card_no    | name | c1   |
+----+------------+------+------+
|  1 | 1000000000 | abc  | a    |
|  2 | 1000000001 | ttt  | b    |
+----+------------+------+------+
2 rows in set (0.00 sec)

數(shù)據(jù)庫SQL小技巧大揭秘:IGNORE選項(xiàng)讓你的數(shù)據(jù)處理更從容

可以看到,有1條記錄沖突,但是進(jìn)行了warning提示,然后繼續(xù)進(jìn)行其他無沖突項(xiàng)的處理。

如果需查看warning信息,可以使用 show warnings 命令查看。

3、唯一鍵沖突

繼續(xù)以上的表,先正常方式插入一條唯一鍵已存在的記錄。

mysql> select * from test1;
+----+------------+------+------+
| id | card_no    | name | c1   |
+----+------------+------+------+
|  1 | 1000000000 | abc  | a    |
|  2 | 1000000001 | ttt  | b    |
+----+------------+------+------+
2 rows in set (0.00 sec)
mysql> insert  into test1(id,card_no,name,c1) values (4,'1000000000','ccccc','a');
ERROR 1062 (23000): Duplicate entry '1000000000' for key 'test1.uq_card_no'
mysql> select * from test1;
+----+------------+------+------+
| id | card_no    | name | c1   |
+----+------------+------+------+
|  1 | 1000000000 | abc  | a    |
|  2 | 1000000001 | ttt  | b    |
+----+------------+------+------+
2 rows in set (0.00 sec)

可見,因?yàn)閳?bào)錯(cuò),數(shù)據(jù)未插入。

數(shù)據(jù)庫SQL小技巧大揭秘:IGNORE選項(xiàng)讓你的數(shù)據(jù)處理更從容

下面通過添加ignore批量插入數(shù)據(jù)。

mysql> select * from test1;
+----+------------+------+------+
| id | card_no    | name | c1   |
+----+------------+------+------+
|  1 | 1000000000 | abc  | a    |
|  2 | 1000000001 | ttt  | b    |
+----+------------+------+------+
2 rows in set (0.00 sec)
mysql> insert ignore into test1(id,card_no,name,c1) values
    -> (4,'1000000000','ccccc','a'),
    -> (5,'1000000003','ccccabc','a');
Query OK, 1 row affected, 1 warning (0.00 sec)
Records: 2  Duplicates: 1  Warnings: 1
mysql> show warnings;
+---------+------+---------------------------------------------------------+
| Level   | Code | Message                                                 |
+---------+------+---------------------------------------------------------+
| Warning | 1062 | Duplicate entry '1000000000' for key 'test1.uq_card_no' |
+---------+------+---------------------------------------------------------+
1 row in set (0.00 sec)
mysql> select * from test1;
+----+------------+---------+------+
| id | card_no    | name    | c1   |
+----+------------+---------+------+
|  1 | 1000000000 | abc     | a    |
|  2 | 1000000001 | ttt     | b    |
|  5 | 1000000003 | ccccabc | a    |
+----+------------+---------+------+
3 rows in set (0.00 sec)
mysql>

數(shù)據(jù)庫SQL小技巧大揭秘:IGNORE選項(xiàng)讓你的數(shù)據(jù)處理更從容

可見,和主鍵沖突類似,有沖突的數(shù)據(jù)將會忽略告警而繼續(xù)進(jìn)行后續(xù)操作。

4、update操作

除了insert可以搭配ignore選項(xiàng),update也可以添加ignore選項(xiàng),例如:

更新主鍵:

mysql> select * from test1;
+----+------------+---------+------+
| id | card_no    | name    | c1   |
+----+------------+---------+------+
|  1 | 1000000000 | abc     | a    |
|  2 | 1000000001 | ttt     | b    |
|  5 | 1000000003 | ccccabc | a    |
+----+------------+---------+------+
3 rows in set (0.00 sec)
mysql> update test1 set id = id +1;
ERROR 1062 (23000): Duplicate entry '2' for key 'test1.PRIMARY'
mysql> update ignore test1 set id = id +1;
Query OK, 2 rows affected, 1 warning (0.01 sec)
Rows matched: 3  Changed: 2  Warnings: 1
mysql> select * from test1;
+----+------------+---------+------+
| id | card_no    | name    | c1   |
+----+------------+---------+------+
|  1 | 1000000000 | abc     | a    |
|  3 | 1000000001 | ttt     | b    |
|  6 | 1000000003 | ccccabc | a    |
+----+------------+---------+------+
3 rows in set (0.00 sec)
mysql>

數(shù)據(jù)庫SQL小技巧大揭秘:IGNORE選項(xiàng)讓你的數(shù)據(jù)處理更從容

更新唯一鍵:

mysql> select * from test1;
+----+------------+---------+------+
| id | card_no    | name    | c1   |
+----+------------+---------+------+
|  1 | 1000000000 | abc     | a    |
|  3 | 1000000001 | ttt     | b    |
|  6 | 1000000003 | ccccabc | a    |
+----+------------+---------+------+
3 rows in set (0.00 sec)
mysql> show warnings;
Empty set (0.00 sec)
mysql> update test1 set card_no=card_no +1;
ERROR 1062 (23000): Duplicate entry '1000000001' for key 'test1.uq_card_no'
mysql> update ignore test1 set card_no=card_no +1;
Query OK, 2 rows affected, 1 warning (0.02 sec)
Rows matched: 3  Changed: 2  Warnings: 1
mysql> show warnings;
+---------+------+---------------------------------------------------------+
| Level   | Code | Message                                                 |
+---------+------+---------------------------------------------------------+
| Warning | 1062 | Duplicate entry '1000000001' for key 'test1.uq_card_no' |
+---------+------+---------------------------------------------------------+
1 row in set (0.00 sec)
mysql> select * from test1;
+----+------------+---------+------+
| id | card_no    | name    | c1   |
+----+------------+---------+------+
|  1 | 1000000000 | abc     | a    |
|  3 | 1000000002 | ttt     | b    |
|  6 | 1000000004 | ccccabc | a    |
+----+------------+---------+------+
3 rows in set (0.00 sec)
mysql>

數(shù)據(jù)庫SQL小技巧大揭秘:IGNORE選項(xiàng)讓你的數(shù)據(jù)處理更從容

二、忽略非空約束

1、列出字段賦值為null時(shí)

當(dāng)列出需賦值的字段,但是對其中的非空字段賦值為null時(shí),結(jié)果如下:

mysql> select * from test1;
+----+------------+---------+------+
| id | card_no    | name    | c1   |
+----+------------+---------+------+
|  1 | 1000000000 | abc     | a    |
|  3 | 1000000002 | ttt     | b    |
|  6 | 1000000004 | ccccabc | a    |
+----+------------+---------+------+
3 rows in set (0.00 sec)
mysql> insert into test1(id,card_no,name,c1) values
    -> (7,'1000000005',null,'aa');
ERROR 1048 (23000): Column 'name' cannot be null
mysql> insert ignore  into test1(id,card_no,name,c1) values (7,'1000000005',null,'aa');
Query OK, 1 row affected, 1 warning (0.01 sec)
mysql> select * from test1;
+----+------------+---------+------+
| id | card_no    | name    | c1   |
+----+------------+---------+------+
|  1 | 1000000000 | abc     | a    |
|  3 | 1000000002 | ttt     | b    |
|  6 | 1000000004 | ccccabc | a    |
|  7 | 1000000005 |         | aa   |
+----+------------+---------+------+
4 rows in set (0.00 sec)

數(shù)據(jù)庫SQL小技巧大揭秘:IGNORE選項(xiàng)讓你的數(shù)據(jù)處理更從容

結(jié)果為:會插入一個(gè)空字符串在表中而不會像正常SQL那樣因?yàn)榉强占s束而失敗。

有人疑惑,上面是空字符串么,驗(yàn)證結(jié)果如下:

mysql> select * from test1 where name='';
+----+------------+------+------+
| id | card_no    | name | c1   |
+----+------------+------+------+
|  7 | 1000000005 |      | aa   |
+----+------------+------+------+
1 row in set (0.00 sec)
mysql> select * from test1 where name is null;
Empty set (0.00 sec)

數(shù)據(jù)庫SQL小技巧大揭秘:IGNORE選項(xiàng)讓你的數(shù)據(jù)處理更從容

2、未列出字符串類型字段名

當(dāng)賦值時(shí)未在字段列表中加入有非空約束的字符串類型的字段時(shí),情況如下:

mysql> insert   into test1(id,name,c1) values (8,'aaa','aa');
ERROR 1364 (HY000): Field 'card_no' doesn't have a default value
mysql> insert  ignore  into test1(id,name,c1) values (8,'aaa','aa');
Query OK, 1 row affected, 1 warning (0.01 sec)
mysql> show warnings;
+---------+------+----------------------------------------------+
| Level   | Code | Message                                      |
+---------+------+----------------------------------------------+
| Warning | 1364 | Field 'card_no' doesn't have a default value |
+---------+------+----------------------------------------------+
1 row in set (0.00 sec)
mysql> select * from test1;
+----+------------+---------+------+
| id | card_no    | name    | c1   |
+----+------------+---------+------+
|  1 | 1000000000 | abc     | a    |
|  3 | 1000000002 | ttt     | b    |
|  6 | 1000000004 | ccccabc | a    |
|  7 | 1000000005 |         | aa   |
|  8 |            | aaa     | aa   |
+----+------------+---------+------+
5 rows in set (0.01 sec)

數(shù)據(jù)庫SQL小技巧大揭秘:IGNORE選項(xiàng)讓你的數(shù)據(jù)處理更從容

可見,字段未列出時(shí),也可以插入成功,也是將其插入一個(gè)空字符串。

3、未列整型字段時(shí)

當(dāng)賦值時(shí)未在字段列表中加入有非空約束的整型類型的字段時(shí),情況如下:

mysql> select * from test1;
+----+------------+---------+------+
| id | card_no    | name    | c1   |
+----+------------+---------+------+
|  1 | 1000000000 | abc     | a    |
|  3 | 1000000002 | ttt     | b    |
|  6 | 1000000004 | ccccabc | a    |
|  7 | 1000000005 |         | aa   |
|  8 |            | aaa     | aa   |
+----+------------+---------+------+
5 rows in set (0.01 sec)
mysql> insert  ignore  into test1(card_no,name,c1) values ('1000000006','bbb','aa');
Query OK, 1 row affected, 1 warning (0.00 sec)
mysql> show warnings;
+---------+------+-----------------------------------------+
| Level   | Code | Message                                 |
+---------+------+-----------------------------------------+
| Warning | 1364 | Field 'id' doesn't have a default value |
+---------+------+-----------------------------------------+
1 row in set (0.00 sec)
mysql> select * from test1;
+----+------------+---------+------+
| id | card_no    | name    | c1   |
+----+------------+---------+------+
|  0 | 1000000006 | bbb     | aa   |
|  1 | 1000000000 | abc     | a    |
|  3 | 1000000002 | ttt     | b    |
|  6 | 1000000004 | ccccabc | a    |
|  7 | 1000000005 |         | aa   |
|  8 |            | aaa     | aa   |
+----+------------+---------+------+
6 rows in set (0.00 sec)
mysql>

數(shù)據(jù)庫SQL小技巧大揭秘:IGNORE選項(xiàng)讓你的數(shù)據(jù)處理更從容

結(jié)果:此時(shí)插入了0 (整型的默認(rèn)值)。

三、字段超長

依舊進(jìn)行在上述的測試表上進(jìn)行測試

1、字符串超長

當(dāng)字符串類型超長時(shí),正常結(jié)果如下:

mysql> select * from test1;
+----+------------+---------+------+
| id | card_no    | name    | c1   |
+----+------------+---------+------+
|  0 | 1000000006 | bbb     | aa   |
|  1 | 1000000000 | abc     | a    |
|  3 | 1000000002 | ttt     | b    |
|  6 | 1000000004 | ccccabc | a    |
|  7 | 1000000005 |         | aa   |
|  8 |            | aaa     | aa   |
+----+------------+---------+------+
6 rows in set (0.00 sec)
mysql> insert into test1(id,card_no,name,c1) values(9,'1000000001','abc','a12345');
ERROR 1406 (22001): Data too long for column 'c1' at row 1
mysql> select * from test1;
+----+------------+---------+------+
| id | card_no    | name    | c1   |
+----+------------+---------+------+
|  0 | 1000000006 | bbb     | aa   |
|  1 | 1000000000 | abc     | a    |
|  3 | 1000000002 | ttt     | b    |
|  6 | 1000000004 | ccccabc | a    |
|  7 | 1000000005 |         | aa   |
|  8 |            | aaa     | aa   |
+----+------------+---------+------+
6 rows in set (0.00 sec)

結(jié)果:數(shù)據(jù)會因超長而未插入。

而使用ignore選項(xiàng)后,結(jié)果如下:

mysql> insert ignore into test1(id,card_no,name,c1) values(9,'1000000001','abc','a12345');
Query OK, 1 row affected, 1 warning (0.01 sec)
mysql> show warnings;
+---------+------+-----------------------------------------+
| Level   | Code | Message                                 |
+---------+------+-----------------------------------------+
| Warning | 1265 | Data truncated for column 'c1' at row 1 |
+---------+------+-----------------------------------------+
1 row in set (0.00 sec)
mysql> select * from test1;
+----+------------+---------+------+
| id | card_no    | name    | c1   |
+----+------------+---------+------+
|  0 | 1000000006 | bbb     | aa   |
|  1 | 1000000000 | abc     | a    |
|  3 | 1000000002 | ttt     | b    |
|  6 | 1000000004 | ccccabc | a    |
|  7 | 1000000005 |         | aa   |
|  8 |            | aaa     | aa   |
|  9 | 1000000001 | abc     | a1   |
+----+------------+---------+------+
7 rows in set (0.00 sec)
mysql> desc test1;
+---------+-------------+------+-----+---------+-------+
| Field   | Type        | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| id      | int         | NO   | PRI | NULL    |       |
| card_no | varchar(10) | NO   | UNI | NULL    |       |
| name    | varchar(20) | NO   |     | NULL    |       |
| c1      | varchar(2)  | YES  |     | NULL    |       |
+---------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
mysql>

數(shù)據(jù)庫SQL小技巧大揭秘:IGNORE選項(xiàng)讓你的數(shù)據(jù)處理更從容

結(jié)果:數(shù)據(jù)以截?cái)嗟姆绞讲迦氤晒α恕?/span>

2、整型數(shù)據(jù)超長

當(dāng)普通方式插入一個(gè)超過int類型最大值的數(shù)據(jù)時(shí),會直接因數(shù)據(jù)超過范圍而報(bào)錯(cuò)。例如:

mysql> select * from test1;
+----+------------+---------+------+
| id | card_no    | name    | c1   |
+----+------------+---------+------+
|  0 | 1000000006 | bbb     | aa   |
|  1 | 1000000000 | abc     | a    |
|  3 | 1000000002 | ttt     | b    |
|  6 | 1000000004 | ccccabc | a    |
|  7 | 1000000005 |         | aa   |
|  8 |            | aaa     | aa   |
|  9 | 1000000001 | abc     | a1   |
+----+------------+---------+------+
7 rows in set (0.00 sec)
mysql> insert  into test1(id,card_no,name,c1) values(999999999999999999999,'1000000003','abc','a2');
ERROR 1264 (22003): Out of range value for column 'id' at row 1
mysql> select * from test1;
+----+------------+---------+------+
| id | card_no    | name    | c1   |
+----+------------+---------+------+
|  0 | 1000000006 | bbb     | aa   |
|  1 | 1000000000 | abc     | a    |
|  3 | 1000000002 | ttt     | b    |
|  6 | 1000000004 | ccccabc | a    |
|  7 | 1000000005 |         | aa   |
|  8 |            | aaa     | aa   |
|  9 | 1000000001 | abc     | a1   |
+----+------------+---------+------+
7 rows in set (0.00 sec)

數(shù)據(jù)庫SQL小技巧大揭秘:IGNORE選項(xiàng)讓你的數(shù)據(jù)處理更從容

而使用ignore選項(xiàng)后,可以插入數(shù)據(jù),例如:

mysql> select * from test1;
+----+------------+---------+------+
| id | card_no    | name    | c1   |
+----+------------+---------+------+
|  0 | 1000000006 | bbb     | aa   |
|  1 | 1000000000 | abc     | a    |
|  3 | 1000000002 | ttt     | b    |
|  6 | 1000000004 | ccccabc | a    |
|  7 | 1000000005 |         | aa   |
|  8 |            | aaa     | aa   |
|  9 | 1000000001 | abc     | a1   |
+----+------------+---------+------+
7 rows in set (0.00 sec)
mysql> insert ignore  into test1(id,card_no,name,c1) values(999999999999999999999,'1000000003','abc','a2');
Query OK, 1 row affected, 2 warnings (0.01 sec)
mysql> show warnings;
+---------+------+---------------------------------------------+
| Level   | Code | Message                                     |
+---------+------+---------------------------------------------+
| Warning | 1264 | Out of range value for column 'id' at row 1 |
| Warning | 1264 | Out of range value for column 'id' at row 1 |
+---------+------+---------------------------------------------+
2 rows in set (0.00 sec)
mysql> select * from test1;
+------------+------------+---------+------+
| id         | card_no    | name    | c1   |
+------------+------------+---------+------+
|          0 | 1000000006 | bbb     | aa   |
|          1 | 1000000000 | abc     | a    |
|          3 | 1000000002 | ttt     | b    |
|          6 | 1000000004 | ccccabc | a    |
|          7 | 1000000005 |         | aa   |
|          8 |            | aaa     | aa   |
|          9 | 1000000001 | abc     | a1   |
| 2147483647 | 1000000003 | abc     | a2   |
+------------+------------+---------+------+
8 rows in set (0.00 sec)
mysql>

數(shù)據(jù)庫SQL小技巧大揭秘:IGNORE選項(xiàng)讓你的數(shù)據(jù)處理更從容

結(jié)果: 會以截?cái)嗟姆绞讲迦耄╥nt的最大值)

四、結(jié)語

總的來說,IGNORE 提供了一種在插入或更新時(shí)處理主鍵、唯一鍵沖突、非空約束字段未賦值、字段超長等異常時(shí)內(nèi)部自動(dòng)處理的方法,使得操作不因?yàn)槟骋恍械臎_突而中斷,而是繼續(xù)處理。但也因?yàn)槠涮攸c(diǎn),會導(dǎo)致結(jié)果與預(yù)期不符的情況。在實(shí)際操作中還是建議使用正常的方式進(jìn)行處理,以免出現(xiàn)不必要的故障。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 草草影院在线 | 成人影院在线看 | 亚洲人成绝费网站色ww | 男女男精品网站免费观看 | 国产一区二区三区在线看片 | 国产天天在线 | 操老妇| 四虎1515hhc0m| 日本不卡视频免费的 | 97就去干 | 五月丁香啪啪. | 3p文两男一女办公室高h | 国产精品亚洲综合第一区 | 亚洲国产成人在线视频 | 四虎影院网址大全 | 成人在线免费播放 | 国产成人成人一区二区 | 女暴露狂校园裸露小说 | 国产午夜精品久久理论片 | 亚洲精品中文字幕第一区 | 色老板影视| 青春草在线观看精品免费视频 | 国产亚洲精品美女久久久 | 特黄一级大片 | 成人国产精品 | 女高h| 青草视频在线观看免费资源 | 色琪琪久久草在线视频 | 久久热这里只有 精品 | 成人aaaa| 村上里沙40分钟在线观看 | 午夜伦理电影在线观免费 | voyeur多毛厕所 | 九色PORNY真实丨国产大胸 | 国产成人a v在线影院 | 婷婷丁香色综合狠狠色 | 黑人艹逼 | 清纯漂亮女友初尝性过程 | 国产一区在线播放 | 免费人成在线观看视频播放 | 好妈妈7在线观看高清 |