• INSERT IGNORE


    INSERT IGNORE与INSERT INTO的区别

    就是INSERT IGNORE会忽略数据库中已经存在 的数据,如果数据库没有数据,就插入新的数据,如果有数据的话就跳过这条数据。这样就可以保留数据库中已经存在数据,达到在间隙中插入数据的目的。

    mysql> create table testtb(
        ->   id int not null primary key,
        ->   name varchar(50),
        ->   age int
        -> );
    Query OK, 0 rows affected (0.01 sec)
    
    mysql> 
    mysql> select * from testtb;
    Empty set (0.01 sec)
    
    mysql> insert into testtb(id,name,age)values(1,"bb",13);
    Query OK, 1 row affected (0.00 sec)
    
    mysql> insert ignore into testtb(id,name,age)values(1,"aa",13);
    Query OK, 0 rows affected, 1 warning (0.01 sec)                                 ---因为 ignore 插入,虽然没有报错,但是因为主键没插入成功。
    
    mysql> show warnings;
    +---------+------+---------------------------------------+
    | Level   | Code | Message                               |
    +---------+------+---------------------------------------+
    | Warning | 1062 | Duplicate entry '1' for key 'PRIMARY' |
    +---------+------+---------------------------------------+
    1 row in set (0.00 sec)
    
    mysql> 
    mysql> 
    mysql> 
    mysql> select * from testtb;
    +----+------+------+
    | id | name | age  |
    +----+------+------+
    |  1 | bb   |   13 |
    +----+------+------+
    1 row in set (0.00 sec)

    mysql> insert ignore into testtb(id,name,age)values(2,"bb",13);    --主键不冲突,插入成功。
    Query OK, 1 row affected (0.00 sec)

    mysql> select * from testtb;
    +----+------+------+
    | id | name | age |
    +----+------+------+
    | 1 | bb | 13 |
    | 2 | bb | 13 |
    +----+------+------+
    2 rows in set (0.00 sec)

  • 相关阅读:
    SqlServer数据库同步方案详解(包括跨网段)
    makefile 和shell文件相互调用
    处理百万级以上的数据处理
    makefile Template(添加多个lib)
    Linux下如何删除非空目录
    makefile Template
    g++ 编译和链接
    gcc include路径
    C++ XML解析之TinyXML篇
    利用Lucene.net搭建站内搜索(2)分词技术
  • 原文地址:https://www.cnblogs.com/monkeybron/p/10867919.html
Copyright © 2020-2023  润新知