开发者

MySQL - Correct way to use the UNIQUE KEY?

开发者 https://www.devze.com 2023-01-22 23:26 出处:网络
Is the proper way to use the UNIQUE KEY in my MySQL table? MySQL data. idpid aid 123 232 334 MySQL table. CREATE TABLE ab (

Is the proper way to use the UNIQUE KEY in my MySQL table?

MySQL data.

id  pid aid
1   2   3
2   3   2
3   3   4   

MySQL table.

CREATE TABLE ab ( 
id INT UNSIGNED NOT NULL AUTO_INCREMENT, 
pid INT UNSIGNED NOT NULL DEFAULT 0, 
ai开发者_运维技巧d INT UNSIGNED NOT NULL DEFAULT 0,  
PRIMARY KEY (id), 
UNIQUE KEY (pid, aid)
);


The unique index you have created is a valid index. It creates an index which allows duplicate values in pid and also in aid but does not allow duplicates of any pair (pid, aid). For example, this insert would fail if your table already contains the data from your example because it conflicts with the second row:

INSERT INTO ab (pid, aid) VALUES (3, 2)
0

精彩评论

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

关注公众号