插入ignore / unique索引

时间:2011-10-15 02:23:01

标签: mysql

我无法理解这是怎么回事。 commentidcomment表中一行的id。 vote表有三列:idcommentid& username

我想要的是只有在最后两列(commentid和username)不存在时才插入vote表。 $commentids是来自$id表的多个comment的数组。如何添加唯一索引来实现此目的?

$commentids = explode(".",$id);

foreach($commentids as $value) {
  $insert = mysql_query("INSERT IGNORE INTO vote 
                           (id, commentid, username) 
                         VALUES 
                           ('','$value','$username')", $this->connect);
}

1 个答案:

答案 0 :(得分:2)

如果您希望(commentid,username)对唯一,那么:

alter table vote add constraint unique (commentid, username);

如果您希望commentidusername中的每一个在表格中全局唯一,那么:

alter table vote add constraint unique (commentid);
alter table vote add constraint unique (username);
相关问题