当前位置:网站首页>MySQL insert ignore and replace into
MySQL insert ignore and replace into
2022-07-22 11:29:00 【Gan_ one thousand three hundred and fourteen】
MySQL Insert data , If the inserted data already exists in the table ( Primary key or unique key already exists ), Use insert ignore Syntax can ignore inserting duplicate data .
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 data
INSERT INTO users ( NAME, email, `password` ) VALUES ( 'gan', '[email protected]', 1 ),( 'gan', '[email protected]', 1 );
because email=‘[email protected]’ Already exists , So wrong reporting
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’
Time : 0.014s
INSERT INTO: Skip repeating lines , Insert remaining
increase IGNORE
, Successful implementation ( Skip existing data directly )
INSERT IGNORE INTO users ( NAME, email, `password` ) VALUES ( 'gan', '[email protected]', 1 ),( 'gan', '[email protected]', 1 );
If the business logic needs to insert duplicate data, it will be automatically ignored , Might as well try. MySQL Of insert ignore function .
REPLACEINTO: Delete duplicate lines , reinsert
REPLACE INTO users ( NAME, email, `password` ) VALUES ( 'gan1', '[email protected]', 1 ),( 'gan2', '[email protected]', 2 );
Why is the number of affected rows 4 That's ok . When inserting the first row, it is found that it already exists , So delete the existing data , Insert again ; When inserting the first row, it is found that it already exists , So delete the existing data , Insert again . Delete two lines , Insert two lines .
边栏推荐
猜你喜欢
随机推荐
[solution]vscode code code auto wrap bug without completion of input
炒股开户哪家证券好 网上开户安全吗
SYSTEMd manages redis exporter Linux
Introduction to nodes
基于torchvision对模型最后几层进行微调,用于训练自己的数据
【学习笔记】带你从0开始学习 01Trie
2021年全国职业院校技能大赛网络搭建与应用之虚拟化配置答案
榜样访谈——曾钰倬:从讲座中收获经验
Start jar package shell script
你为什么会做测试/开发程序员?各路伙伴描述......
Is it safe to open an account on tonghuashun online? How to buy national debt reverse repurchase
SYSTEMd management blackbox exporter
Is it safe for flush to open Huatai Securities account?
二分查找/折半查找
lc marathon 7.21
手动封装对象深拷贝方法
Memorize these interview questions, and you will be half successful in any technical aspect
flask 启动函数返回值的剖析
ICML 2022 lottery! Fudan University, Shanghai Jiaotong University and Xiamen University were selected for outstanding papers
Using virtual to implement hooks in solidity