仅当两列的关系不重复时才插入表中 [英] Insert in table only if two columns relationship is not duplicated

查看:75
本文介绍了仅当两列的关系不重复时才插入表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个语言表,其中包含一个"node_id"和一个语言"列.

I have a language table that contains a 'node_id' and a 'language' column.

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

我想在其中插入新的语言,但前提是该特定nid尚无类似的语言条目.因此,根据上述示例,添加nid> 8和language>中文将无效.

I'd like to insert new languages into it, but only if there isn't already a similar language entry for that particular nid. So according to exemple above, adding nid>8 and language> Chinese would have no effect.

我应该怎么做才能做到这一点?有没有可以执行这种查询的类型,或者我应该在插入行之前先进行PHP/MYSQL检查吗?

How should i do to achieve that? Is there a type of query that would act as such, or should i first do check with PHP/MYSQL before inserting the row ?

推荐答案

只需在MySQL数据库中的一对列上设置UNIQUE约束即可.任何尝试添加重复行的查询都将失败.

Just put a UNIQUE constraint on the pair of columns in the MySQL database. Any query trying to add a duplicate row will fail.

这篇关于仅当两列的关系不重复时才插入表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆