mysql:insert ignore、insert和replace区别
指令 | 已存在 | 不存在 | 举例 |
---|---|---|---|
insert | 报错 | 插入 | insert into names(name, age) values(“小明”, 23); |
insert ignore | 忽略 | 插入 | insert ignore into names(name, age) values(“小明”, 24); |
replace | 替换 | 插入 | replace into names(name, age) values(“小明”, 25); |
表要求:有PrimaryKey,或者unique索引
结果:表id都会自增
测试代码
创建表
CREATE TABLE names(id INT(10) PRIMARY KEY AUTO_INCREMENT,name VARCHAR(255) UNIQUE,age INT(10)
)
插入数据
mysql> insert into names(name, age) values("小明", 24);
mysql> insert into names(name, age) values("大红", 24);
mysql> insert into names(name, age) values("大壮", 24);
mysql> insert into names(name, age) values("秀英", 24);mysql> select * from names;
+----+--------+------+
| id | name | age |
+----+--------+------+
| 1 | 小明 | 24 |
| 2 | 大红 | 24 |
| 3 | 大壮 | 24 |
| 4 | 秀英 | 24 |
+----+--------+------+
insert
插入已存在, id会自增,但是插入不成功,会报错
mysql> insert into names(name, age) values("小明", 23);ERROR 1062 (23000): Duplicate entry '小明' for key 'name'
replace
已存在替换,删除原来的记录,添加新的记录
mysql> replace into names(name, age) values("小明", 23);
Query OK, 2 rows affected (0.00 sec)mysql> select * from names;
+----+--------+------+
| id | name | age |
+----+--------+------+
| 2 | 大红 | 24 |
| 3 | 大壮 | 24 |
| 4 | 秀英 | 24 |
| 6 | 小明 | 23 |
+----+--------+------+
不存在替换,添加新的记录
mysql> replace into names(name, age) values("大名", 23);
Query OK, 1 row affected (0.00 sec)mysql> select * from names;
+----+--------+------+
| id | name | age |
+----+--------+------+
| 2 | 大红 | 24 |
| 3 | 大壮 | 24 |
| 4 | 秀英 | 24 |
| 6 | 小明 | 23 |
| 7 | 大名 | 23 |
+----+--------+------+
insert ignore
插入已存在,忽略新插入的记录,id会自增,不会报错
mysql> insert ignore into names(name, age) values("大壮", 25);
Query OK, 0 rows affected, 1 warning (0.00 sec)
插入不存在,添加新的记录
mysql> insert ignore into names(name, age) values("壮壮", 25);
Query OK, 1 row affected (0.01 sec)mysql> select * from names;
+----+--------+------+
| id | name | age |
+----+--------+------+
| 2 | 大红 | 24 |
| 3 | 大壮 | 24 |
| 4 | 秀英 | 24 |
| 6 | 小明 | 23 |
| 7 | 大名 | 23 |
| 10 | 壮壮 | 25 |
+----+--------+------+
参考:
INSERT IGNORE 与INSERT INTO的区别
mysql:insert ignore、insert和replace区别相关推荐
- INSERT INTO和INSERT IGNORE INTO 以及REPLACE INTO的区别
文章目录 关于INSERT INTO和INSERT IGNORE INTO 以及REPLACE INTO的区别 先创建一个数据表 CREATE TABLE plt_order_items(id int ...
- 语法:MySQL中INSERT IGNORE INTO和REPLACE INTO的使用
mysql中insert into和replace into以及insert ignore用法区别: mysql中常用的三种插入数据的语句: insert into表示插入数据,数据库会检查主键, ...
- MySQL中INSERT IGNORE INTO和REPLACE INTO的使用
mysql中insert into和replace into以及insert ignore用法区别: mysql中常用的三种插入数据的语句: insert into表示插入数据,数据库会检查主键, ...
- [数据库]---mysql 插入sql之 INSERT INTO和INSERT IGNORE INTO和REPLACE INTO和ON DUPLICATE KEY UPDATE比较应用
mysql插入一条数据方式有四种,接下来逐个说明验证: 前提: 有一个表,作为本次测试: CREATE TABLE `zs_test` (`id` int(11) NOT NULL AUTO_INCR ...
- INERT DELEYED、INSERT IGNORE replace into和insert区别
insert into表示插入数据,数据库会检查主键,如果出现重复会报错: replace into表示插入替换数据,需求表中有PrimaryKey,或者unique索引,如果数据库已经存在数据,则用 ...
- MYSQL INSERT IGNORE 与INSERT INTO的区别
mysql中常用的三种插入数据的语句: insert into表示插入数据,数据库会检查主键(PrimaryKey),如果出现重复会报错: replace into表示插入替换数据,需求表中有Prim ...
- insert into与insert ignore以及replace into的区别
insert ignore表示,如果表中已经存在相同的记录,则忽略当前新数据: INSERT INTO有无数据都插入,如果主键则不插入; REPLACE INTO 如果是主键插入则会替换以前的数据; ...
- MariaDB/MySQL防止重复插入相同记录:INSERT IGNORE或者REPLACE
MySQL防止重复插入相同记录有2种常用的方法. 1 使用 INSERT IGNORE 使用 INSERT IGNORE 可以在插入具有相同主键的数据时不做重复插入. mysql> SELECT ...
- mysql的几种插入语句_Mysql 几种常见的插入 Insert into,Replace Into,Insert ignore
简要说下三者的区别:insert into 最普遍的插入,如果表中存在主键相同的数据,执行会报错. replace into 如果表中存在主键相同的数据则根据主键修改当前主键的数据,反之则插入(存在就 ...
最新文章
- laravel blade模板
- 【Android 修炼手册】Gradle 篇 -- Android Gradle Plugin 主要流程分析
- java语言特点解释类_Java语言特点
- 整套的操作体系:三点看股法
- SpringCloud工作笔记040---zuul网关集合springsecurity_JWT 颁发token_但是Authorization无法传递到response的header中
- 游戏需求,猎豹启发!揭秘华为Mate 20 X石墨烯膜散热方案出台始末
- 【PL/SQL】处理数据
- XLN Audio Addictive Trigger for Mac(智能鼓音替换工具)
- 开源中文bi_odoo:免费开源ERP入门与实践
- 板邓:wordpress编辑器发布文章自动首行缩进
- npy文件的处理方式
- 最新的中国姓氏重新洗牌:快来看看你的姓排第几?
- 为什么网线接法要分交叉连接和直连连接两种方式
- db_create_file_dest
- C#未能加载文件或程序集
- 修复液晶显示器屏幕上的划痕
- 解决NoteExpress无法在Word中插入引用文献
- CentOS7如何设置开机自启动程序、开机自启动脚本?
- 解决客户之间的矛盾-生米煮成熟饭
- 双底形态识别又又又升级了!这次叠加了突破当日涨幅判断!