简体   繁体   English

插入前MySQL检查2值已经存在

[英]MySQL check 2 values already exists before insert

My columns are like this. 我的专栏是这样的。 column "a" is primary and auto incremantal. “ a”列是主要的,并且是自动递增的。

a |  b |  x  |  y

When inserting new data, i need to check x and y columns shouldn't be exist together. 插入新数据时,我需要检查x和y列不应同时存在。

To clarify, imagine this row is at database with these values 为了澄清起见,假设此行位于数据库中并具有这些值

(2, "example.com" , "admin", "123456")

I should able to insert both of these columns 我应该可以同时插入这两列

(3, "example.com" , "user", "123456")
(4, "example2.com" , "admin", "123456")

But i shouldn't able to insert this column 但我不应该插入此列

(5, "example.com" , "admin", "5555555")

Because "example.com" and "admin" values already in database on a row. 因为“ example.com”和“ admin”值已经在数据库中连续存在。 It doesn't matter column "y" is same or not. 列“ y”相同与否无关紧要。

How can i do this? 我怎样才能做到这一点?

Create a composite unique index. 创建一个复合唯一索引。 This will allow any number of duplicates in the individual fields, but the combination needs to be unique. 这将允许在各个字段中进行任意数量的重复,但是组合必须是唯一的。 CREATE UNIQUE INDEX ix_uq ON tablename (b, x); 创建唯一索引ix_uq ON表名(b,x);

...and use INSERT IGNORE to insert if the unique index is not violated. ...并在不违反唯一索引的情况下使用INSERT IGNORE插入。 If it is, just ignore the insert. 如果是,则忽略插入。

INSERT IGNORE INTO test (a,b,x,y) VALUES (5, "example.com" , "admin", "5555555"); 插入IGNORE测试(a,b,x,y)值(5,“ example.com”,“ admin”,“ 5555555”);

If you want to insert unless there's a duplicate, and update if there is, you can also use INSERT INTO ... ON DUPLICATE KEY UPDATE; 如果要插入,除非有重复项,否则要进行更新,也可以使用INSERT INTO ... ON DUPLICATE KEY UPDATE;

Ref: MySQL only insert new row if combination of columns (which allow duplicates) is unique 参考: MySQL仅在列组合(允许重复)唯一时插入新行

You want to let the database do the work. 您想让数据库完成工作。 Although you can set up a condition within a query, that condition may not be universally true or someone might use another query. 尽管您可以在查询中设置条件,但该条件可能并非普遍成立,否则有人可能会使用其他查询。

The database can check this with a unique constraint or index. 数据库可以使用唯一约束或索引进行检查。 Actually, the unique constraint is implementing using a unique index: 实际上,唯一约束是使用唯一索引实现的:

create unique index unq_t_b_x on t(b, x);

(The columns can be in either order.) (列可以按任何顺序排列。)

The insert would then look like: 插入将如下所示:

insert into t(b, x, y)
    values ('example.com', 'admin', '5555555')
    on duplicate key update b = values(b);

Note that the auto-incremented value is not included in the update. 请注意,自动增加的值不包括在更新中。

The on duplicate key update just prevents the insert from generating an error. on duplicate key update仅可防止insert生成错误。 It is better than insert ignore because the latter will ignore all errors, and you just want to ignore the one caused by the duplicate key. 它比insert ignore更好,因为后者将忽略所有错误,而您只想忽略由重复键引起的错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在插入之前检查值,如果已经存在,请不要使用 php 在 mysql 上插入它 - Check value before insert and if already exists don't insert it on mysql using php MySQL 在插入前触发检查 email 是否存在 - MySQL trigger before insert check if email exists 在触发器中,如何将值插入到另一个表中,但检查值是否已存在 - In a trigger how to insert values into another table but check if values already exists MySQL-在插入之前触发+如果数据已经存在+枚举列 - MySQL - trigger on before insert + if data already exists + enum column PHP PDO在MySQL中插入经度/纬度,并在插入前检查是否存在 - PHP PDO Insert Longditude / latituded in Mysql and check if exists before insert 在更新 mysql 中已存在记录的列之前检查值是否存在 - Check if a value exists before updating a column for a already existing record in mysql MySQL:在尝试更新列之前检查值是否已存在 - MySQL: Check if a value already exists before trying to update a column MySQL-提交表单前检查用户名是否已存在 - MySQL - Check if username already exists before submitting the form Mysql INSERT IGNORE,如果特别是两列中的行值已经存在 - Mysql INSERT IGNORE if in particular row values in two columns already exists MySQL过程检查记录是否存在,然后插入不起作用 - MySQL Procedure check if record exists before insert not working
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM