zoukankan      html  css  js  c++  java
  • 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)

  • 相关阅读:
    FastReport合并多份报表为一份预览打印
    Delphi使用AcroPDF ActiveX显示PDF文件
    Delphi使用Zxing创建二维码
    TreeView和ListView数据库查询数据联动操作
    根据数据库查询结果动态创建控件(仿看板模式显示)
    GridView控件使用
    LayoutControl控件使用
    TreeListLookUpEdit控件使用
    Devexpress TreeList控件使用
    Asp.Net 5上传文件 (Core API方式)
  • 原文地址:https://www.cnblogs.com/monkeybron/p/10867919.html
Copyright © 2011-2022 走看看