mysql - Check uniqueness for multiple attributes in SQL -
i having table of form id, mid,pid
. now, table id primary key. , might come across inserting same combination of mid,pid
, shouldn't add them. here id needs generated , can't obtained or crawled. there mechanism in sql check uniqueness of combination.
i thinking of generating id auto increment, being primary key can't here,another option thinking is, concatenate mid , pid , set them primary key. else check if data exists select command , same. these kind of defeats purpose, or least not elegant. there other methods?
will creating constraint
serve it?
yes, adding (composite) uniqueness constraint want:
alter table my_table add unique (mid, pid)
Comments
Post a Comment