开发者

Insert in table only if two columns relationship is not duplicated

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

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

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.

How should i do to achieve that? Is t开发者_如何转开发here a type of query that would act as such, or should i first do check with PHP/MYSQL before inserting the row ?


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


You could just do a unique key index with 2 of those columns:

CREATE TABLE lang ( nid INT NOT NULL , language VARCHAR(45) NOT NULL , UNIQUE INDEX index_lang (nid ASC, language ASC) );

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜