简体   繁体   English

防止在MySql中插入匹配的行

[英]Prevent inserting matching rows in MySql

I have a table in MySql Database. 我在MySql数据库中有一个表。 I would like to prevent inserting matching rows in MySql. 我想防止在MySql中插入匹配的行。 Like I have 4 columns in a table. 就像我在一个表中有4列。 I would not like to insert any row which has matching values of these 4 columns. 我不想插入任何具有这4列匹配值的行。 I am trying to show that below 我想在下面显示

My table
----------
 product_name| product_sku |product_quantity| product_price
----------
 Computer    | comp_007    | 5              | 500

I would like to prevent to insert same row again. 我想防止再次插入同一行。 How can I do that using MySql Query ?? 如何使用MySql Query做到这一点?

UPDATE 更新

I would not like to insert again 我不想再次插入


 Computer    | comp_007    | 5              | 500

But I would like to insert below rows 但是我想在行下面插入


 mouse       | comp_007    | 5               | 500

 Computer    | comp_008    | 5               | 500

 Computer    | comp_007    | 50              | 500

 Computer    | comp_007    | 5               | 100

 mouse       | mou_007     | 5               | 500

Create a combined unique key / composite key on the columns in question: 在相关列上创建组合的唯一键/组合键:

ALTER TABLE `table` ADD UNIQUE (
`product_name` ,
`product_sku` ,
`product_quantity`,
`product_price`
);

Any attempts to insert duplicate rows will result in a MySQL error. 任何插入重复行的尝试都会导致MySQL错误。

If possible you should add a Unique Key to your columns: 如果可能,您应该在列中添加一个唯一键

ALTER TABLE `table_name` 
ADD UNIQUE INDEX `ix_name` (`product_name`, `product_sku`, `product_quantity`, `product_price`);

and then use INSERT IGNORE : 然后使用INSERT IGNORE

INSERT IGNORE INTO table_name (product_name, product_sku, product_quantity, product_price) VALUES (value1, value2, value3, value4);

If the record is unique MYSQL inserts it as usual, if the record is a duplicate then the IGNORE keyword discards the insert without generating an error. 如果记录是唯一的,则MYSQL照常插入它,如果记录是重复的,则IGNORE关键字放弃插入而不会产生错误。

SQLfiddle SQL小提琴

The simplest way would be to make your columns unique. 最简单的方法是使您的列唯一。 On undesired inserts, your MySQL driver should throw an exception then, which you can handle. 在不需要的插入上,MySQL驱动程序应引发一个异常,您可以处理该异常。

From a domain logic point of view, this is a bad practice, since exceptions should never handle expected behaviour. 从域逻辑的角度来看,这是一种不好的做法,因为异常永远不应该处理预期的行为。 By the way, your DB table could use a primary key. 顺便说一句,您的数据库表可以使用主键。

So, to have a better solution, your approach could be: - Define a unique field (the SKU seems suitable); 因此,为了有更好的解决方案,您的方法可能是:-定义唯一字段(SKU似乎合适); think about using this as the primary key as well - With MySQL, use a REPLACE statement: 考虑将其用作主键-对于MySQL,请使用REPLACE语句:

REPLACE INTO your_tablename
SET product_name='the name'
-- and so on for other columns
WHERE product_sku = 'whatever_sku';

REPLACE does the Job of trying to INSERT, and doing an UPDATE instead if the PK exists. 如果PK存在,则REPLACE执行尝试插入的工作,并执行UPDATE。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM