当前位置:网站首页>MySQL插入数据insert ignore和replace into
MySQL插入数据insert ignore和replace into
2022-07-21 20:32:00 【Gan_1314】
MySQL中插入数据,如果插入的数据在表中已经存在(主键或者唯一键已存在),使用insert ignore 语法可以忽略插入重复的数据。
CREATE TABLE `users` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,
`email` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,
`info` json DEFAULT NULL,
`email_verified_at` timestamp NULL DEFAULT NULL,
`password` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,
`remember_token` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
`phone` varchar(11) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci GENERATED ALWAYS AS (json_unquote(json_extract(`info`,_utf8mb4'$.phone_number'))) VIRTUAL,
PRIMARY KEY (`id`),
UNIQUE KEY `users_email_unique` (`email`),
UNIQUE KEY `idx_phone` (`phone`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
插入数据
INSERT INTO users ( NAME, email, `password` ) VALUES ( 'gan', '[email protected]', 1 ),( 'gan', '[email protected]', 1 );
因为email=‘[email protected]’已存在,所以报错
INSERT INTO users ( NAME, email,
password
) VALUES ( ‘gan’, ‘[email protected]’, 1 ),( ‘gan’, ‘[email protected]’, 1 )
1062 - Duplicate entry ‘[email protected]’ for key ‘users_email_unique’
时间: 0.014s
INSERT INTO:跳过重复行,插入其余
增加IGNORE
,执行成功(直接跳过已存在的数据)
INSERT IGNORE INTO users ( NAME, email, `password` ) VALUES ( 'gan', '[email protected]', 1 ),( 'gan', '[email protected]', 1 );
如果业务逻辑需要插入重复数据时自动忽略,不妨试试MySQL 的 insert ignore 功能。
REPLACEINTO:删除重复行,重新插入
REPLACE INTO users ( NAME, email, `password` ) VALUES ( 'gan1', '[email protected]', 1 ),( 'gan2', '[email protected]', 2 );
影响行数为什么是4行。插入第一行时发现已存在,故删除已存在数据,再插入;插入第一行时发现已存在,故删除已存在数据,再插入。删除两行,插入两行。
边栏推荐
- Creation and call of stored procedure based on Oracle Database
- Start jar package shell script
- SYSTEMd management blackbox exporter
- (PC+WAP)织梦模板防护口罩类网站
- 【红队】ATT&CK - Active Scanning(主动扫描)
- Fine tuning of model
- 标签平滑(LabelSmoothing)介绍与代码实现
- 调用“抱抱脸团队打造的Transformers pipeline API” && 通过预训练模型,快速训练和微调自己的模型
- imdecode、imencode、.tofile、fromfile 读取并保存 & 中文路径的中文名称的文件 & 一步一步解析并对比函数的结果
- Pyinstaller打包 && 关于Enum34的问题解决 && 降低版本
猜你喜欢
随机推荐
unordered_ Use of map
1million people are waiting in line! The public beta version of Dall · e is also charged
基于torchvision对模型最后几层进行微调,用于训练自己的数据
学生管理系统(文件版)
[shutter -- basic component] radio switch & Radio & checkbox
使用 Opencv and OS or pathlib.Path 获取路径名称和图像名称,并保存图像到指定路径
Meta最新图像生成工具火了,竟能把梦境画成现实!
深度剖析问题:Could not run ‘torchvision::nms‘ with arguments from the ‘CUDA‘ backend.
Introduction aux noeuds
什么是视频内容推荐引擎?
NASA「史上最强超算」投入使用,碾压老超算霸主Pleiades
Data analysis from 0 to 1 --- Matplotlib article
双指针(一)
The air conditioner in no man's land has poor refrigeration
Is it safe to open an account on flush? How to buy REITs fund
AcWing 1184. 欧拉回路 题解(欧拉回路)
Illustrate the beginning of NLP transfer learning by Bert, Elmo, etc
Responsive dream weaving template smart home website
向量化引擎对HTAP的价值与技术思考
力扣------统计有序矩阵中的负数