little knowledge

 
Option 1: Use the ignore keyword
Option 2: Use replace into
Option 3: ON DUPLICATE KEY UPDATE 
Option 1: Use the ignore keyword
 
If the uniqueness of the record is distinguished by the primary key or the unique index unique, to avoid repeated insertion of records, you can use:
 
insert ignore into table_name(email,phone,user_id) values('[email protected]','99999','9999')
, so that when there are repeated records
 
The record will be ignored, and the number 0 will be returned after execution. Another application is to copy the table to avoid duplicate records:
 
insert ignore into table(name)  select  name from table2

 

 
Option 2: Use replace into
The syntax format of replace is:
 
1. replace into table_name(col_name, ...) values(...)

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

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

 

 
Algorithm Description:
 
REPLACE works much like INSERT, but if the old record has the same value as the new record, the old record is deleted before the new record is inserted, i.e.:
 
1. Attempt to insert a new row into the table 
 
2. When insert fails due to duplicate key error for primary key or unique key:
 
         Delete conflicting rows with duplicate key values ​​from table
 
        Try again to insert the new row into the table
 
旧记录与新记录有相同的值的判断标准就是:表有一个PRIMARY KEY或UNIQUE索引,否则,使用一个REPLACE语句没有意义
 
。该语句会与INSERT相同,因为没有索引被用于确定是否新行复制了其它的行。
 
返回值:
 
REPLACE语句会返回一个数,来指示受影响的行的数目。该数是被删除和被插入的行数的和。
 
受影响的行数可以容易地确定是否REPLACE只添加了一行,或者是否REPLACE也替换了其它行:检查该数是否为1(添加)或
 
更大(替换)。
 
示例:
 
eg:(phone字段为唯一索引)
 
replace  into table_name(email,phone,user_id) values('test569','99999','123')

 

 
另外:在 SQL Server 中可以这样处理:
 
if not exists (select phone from t where phone= '1') 

            insert into t(phone, update_time) values('1', getdate()) 

 else 

          update t set update_time = getdate() where phone= '1'

 

 
方案三:ON DUPLICATE KEY UPDATE 
如‍上所写,你也可以在INSERT INTO.....后面加上 ON DUPLICATE KEY UPDATE方法来实现。
 
如果您指定了ON DUPLICATE KEY UPDATE,并且插入行后会导致在一个UNIQUE索引
 
或PRIMARY KEY中出现重复值,
 
则执行旧行UPDATE。例如,如果列a被定义为UNIQUE,并且包含值1,则以下两个语句具有相
 
同的效果:
 
mysql>INSERT INTO table (a,b,c) VALUES (1,2,3)   

          ->ON DUPLICATE KEY UPDATE c=c+1; 

mysql>UPDATE table SET c=c+1 WHERE a=1;

 

 
如果行作为新记录被插入,则受影响行的值为1;如果原有的记录被更新,则受影响行的值为2。
 
注释:如果列b也是唯一列,则INSERT与此UPDATE语句相当:
 
mysql> UPDATE table SET c=c+1 WHERE a=1 OR b=2 LIMIT 1;

 

 
如果a=1 OR b=2与多个行向匹配,则只有一个行被更新。通常,您应该尽量避免对带有多个唯一关键字的表使用ON DUPLICATE KEY子句。
 
您可以在UPDATE子句中使用VALUES(col_name)函数从INSERT...UPDATE语句的INSERT部分引用列值。
 
换句话说,如果没有发生重复关键字冲突,则UPDATE子句中的VALUES(col_name)可以引用被插入的
 
col_name的值。本函数特别适用于多行插入。VALUES()函数只在INSERT...UPDATE语句中有意义,其它时候
 
会返回NULL。
 
mysql> INSERT INTO table (a,b,c) VALUES (1,2,3),(4,5,6)    

        -> ON DUPLICATE KEY UPDATE c=VALUES(a)+VALUES(b);

 

 
本语句与以下两个语句作用相同:
 
mysql> INSERT INTO table (a,b,c) VALUES (1,2,3)   

    -> ON DUPLICATE KEY UPDATE c=3;

mysql> INSERT INTO table (a,b,c) VALUES (4,5,6)   

         -> ON DUPLICATE KEY UPDATE c=9;

 

 
当您使用ON DUPLICATE KEY UPDATE时,DELAYED选项被忽略。
 
示例: 这个例子是我在实际项目中用到的:是将一个表的数据导入到另外一个表中,数据的重复性就得考虑(如下)。
 
唯一索引为:email
 
INSERT INTO table_name1(title,first_name,last_name,email,phone,user_id

,role_id,status,campaign_id)

SELECT '','','',table_name2.email,table_name2.phone,NULL,NULL,'pending',29

 FROM table_name2  

WHERE table_name2.status = 1 

ON DUPLICATE KEY UPDATE table_name1.status = 'pending'

 

 
语句的关键地方,都已高亮出来~
 
再贴一个例子:
 
insert into class select * from class1

ON DUPLICATE KEY UPDATE class.course = class1.course

 

 
其它关键:DELAYED  做为快速插入,并不是很关心失效性,提高插入性能。
 
IGNORE  只关注主键对应记录是不存在,无则添加,有则忽略。
 
 特别说明:在MYSQL中UNIQUE 索引将会对null字段失效,也就是说(a字段上建立唯一索引):
                    insert into test(a) values(null)

                    insert into test(a) values(null)

 

是可以重复插入的(联合唯一索引也一样)。

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327009166&siteId=291194637