简体   繁体   English

唯一约束,如何避免重复

[英]Unique constraint, how to avoid duplicates

According to my previous Query that post i have a table that looks like this: 根据我之前的查询我有一个看起来像这样的表:

|| *nid* || *language* ||
|| 8 || Chinese ||
|| 8 || Portuguese ||
|| 8 || German |

In which 'nid' and 'language' have a unique constraint. 其中“ nid”和“语言”具有唯一的约束。

With this setup how can i make sure that the there wont be any duplicate when i try to insert a new row ? 通过这种设置,当我尝试插入新行时,如何确保不会重复?

EDITED 已编辑

I am guessing I should try to make a query such as: 我猜我应该尝试进行查询,例如:

SELECT * FROM lang WHERE nid = $nid AND language = $lang

If this return FALSE then i know i can now insert my data. 如果返回FALSE,那么我知道我现在可以插入数据了。 Is this correct ? 这个对吗 ?

Enforce the unique constraint by creating a unique key: 通过创建唯一键来实施唯一约束:

ALTER TABLE the_table
ADD UNIQUE INDEX nid_language_unique (nid, language);

This constraint forbid two rows having the same nid and language. 此约束禁止两行具有相同的nid和语言。

Any query attempting to violate the constraint will fail. 任何试图违反约束的查询都将失败。

As you want to ignore errors (and still abort the query), you can use INSERT IGNORE and UPDATE IGNORE : 当您想忽略错误(并仍然中止查询)时,可以使用INSERT IGNOREUPDATE IGNORE

INSERT IGNORE INTO the_table (nid, language) VALUES (8, 'Chinese')
/* row not inserted and no error */

If you have actually established a unique constraint in the database then MySQL will not let you insert the second row. 如果您实际上已经在数据库中建立了唯一约束,那么MySQL将不允许您插入第二行。 The statement will raise an exception. 该语句将引发异常。 You can trap and ignore that in your code. 您可以在代码中捕获并忽略它。 If you're not interested in whether the row was added or not, you can use the IGNORE keyward in the MySQL INSERT INTO command and the row will either be added (if not there) or the command will complete without an error. 如果您对是否添加行不感兴趣,则可以在MySQL INSERT INTO命令中使用IGNORE键控,然后将添加该行(如果不存在)或该命令将完成而不会出现错误。

SELECT nid, language FROM lang WHERE nid = $nid AND language = $lang 在lang中选择nid,语言,而nid = $ nid和language = $ lang

If this return FALSE then i know i can now insert my data. 如果返回FALSE,那么我知道我现在可以插入数据了。 Is this correct ? 这个对吗 ?

Yes, but you need to write: 是的,但是您需要写:

$nid = mysql_real_escape_string($_POST['nid']);
$lang = mysql_real_escape_string($_POST['lang']);
$query = SELECT nid, language FROM lang WHERE nid = '$nid' AND language = '$lang'
// notice the quotes                                ^    ^                ^     ^

If you forget these your query give an error (and be at risk from SQL-injection). 如果您忘记了这些内容,则查询将给出错误(并可能受到SQL注入的威胁)。

If you have a unique constraint, you can just go ahead and insert the data, because MySQL will do the above test for you. 如果您有唯一的约束,则可以继续并插入数据,因为MySQL会为您进行上述测试。

You can use a counter in your code (not SQL, but the one you use to use SQL, like PHP, or else) 您可以在代码中使用一个计数器(不是SQL,但是可以使用一个计数器来使用SQL,例如PHP或其他)

You can use MySQL max function and add one (like max(nid)+1 (but don't remember about MySQL's max function)) 您可以使用MySQL的max函数并添加一个(例如max(nid)+1(但不记得MySQL的max函数))

You can use a random number with 10 characters (so you'd would have a really really low risk to go into an error) 您可以使用10个字符的随机数(这样您出错的风险就会非常低)

I used the first and last way many times. 我多次使用第一和最后一种方式。

And if you want to be sure that you won't have a duplicate, use the solutions from your last posts. 而且,如果您想确保不会重复,请使用最近发布的解决方案。 Stuff like UNIQUE constraint will prevent you to insert twice the same nid or language (thus, if you don't handle it, your program will crash). 像UNIQUE约束之类的东西将阻止您插入相同的nid或语言两次(因此,如果不处理,则程序将崩溃)。

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

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