how do i insert multiple values in mysql and avoid duplicates(如何在mysql中插入多个值并避免重复)
问题描述
如何在以下架构中插入多行或值并避免重复.
How would I insert multiple rows or values and avoid duplicates in the following schema.
表架构是
id,subject1,subject2,subject3
id 会自动递增.
重复将是所有 subject1、subject2、subject3 已经以完全相同的顺序存在于记录中.
id is auto incremented.
A duplicate would be where all subject1,subject2,subject3 already exist in a record in the exact same order.
INSERT INTO "table_name" ("subject1","subject2","subject3")
VALUES ("cats", "dogs", "hamsters")
VALUES ("squirrels", "badgers", "minxes")
VALUES ("moose", "deer", "ocelots")
在表格中假设我已经有记录
In the table let's say I already have a record for
id,subject1,subject2,subject3
1,"cats", "dogs", "hamsters"
所以我希望它只插入
VALUES ("squirrels", "badgers", "minxes")
VALUES ("moose", "deer", "ocelots")
我已经看到有关避免单个项目重复的答案,但不是 3.
I've seen answers about avoiding duplicates for single items, but not for 3.
推荐答案
您想将 UNIQUE 约束添加到您的表中.如果您单独编写 UNIQUE 约束,如何将其应用于列的任意组合会变得更加清晰.
You want to add the UNIQUE constraint to your table. If you write the UNIQUE constraint out separately, it becomes clearer how to apply it to arbitrary combinations of columns.
CREATE TABLE table_name (
subject1 VARCHAR(30),
subject2 VARCHAR(30),
subject3 VARCHAR(30),
UNIQUE (subject1, subject2, subject3)
);
这篇关于如何在mysql中插入多个值并避免重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在mysql中插入多个值并避免重复
基础教程推荐
- 如何使用 mysql.connector 禁用查询缓存 2022-01-01
- SSMS 中的权限问题:“对象 'extended_properties'、数据库 'mssqlsystem_resource'、... 错误 229)上的 SELECT 权限被拒绝" 2022-01-01
- 在 SQL 中连接多个表 2021-01-01
- SQL Server 实例在登录协商期间返回无效或不受支持的协议版本 2021-01-01
- 是否可以执行按位分组功能? 2021-01-01
- 无法解决整理冲突 2021-01-01
- 需要 MySQL 5.1 中的抽象触发器来更新审计日志 2021-01-01
- SQL:使用来自具有相同列名的两个表中的数据... 2021-01-01
- SQL 效率:WHERE IN 子查询 vs. JOIN 然后 GROUP 2021-01-01
- 将 SQL Server DateTime 列迁移到 DateTimeOffset 2021-01-01
