Bruce
Bruce

Reputation: 8849

MySQL Composite primary key uniqueness

How to make composite primary key in mySQL. In table1 i need to set id1,id2 as composite primary key. i used this query. But it makes each as primary key. it checks duplicates of each entry

ALTER TABLE `table1`
  DROP PRIMARY KEY,
   ADD PRIMARY KEY(
     `id1`,
     `id2`);

When i insert two rows

insert "a","b" 
insert "a","c"

1062 - Duplicate entry '1408181771009' for key 'pk_sentiments'

But

insert "a","c" 
insert "b","c"

works fine. I could not understand this behavior. How to resolve this?

Upvotes: 1

Views: 379

Answers (1)

cn0047
cn0047

Reputation: 17091

Maybe this:

ALTER TABLE `table1` ADD UNIQUE KEY `id1_id2` (`id1`, `id2`);

Upvotes: 0

Related Questions