Rectangle 27 0

mysql ALTER TABLE to add a composite primary key?


ALTER TABLE provider ADD PRIMARY KEY(person,place,thing);
ALTER TABLE provider DROP PRIMARY KEY, ADD PRIMARY KEY(person, place, thing);

Sign up for our newsletter and get our top new questions delivered to your inbox (see an example).

@David542 Of course you can - this is a composite primary key consisting of 3 fields. The combination of the 3 fields must be unique.

@David: it's a single primary key composed of multiple fields, aka a composite key.

If a primary key already exists then you want to do this

thanks for posting -- really got me out of battling with the ui

Note
Rectangle 27 0

mysql ALTER TABLE to add a composite primary key?


1) ALTER TABLE provider ADD PRIMARY KEY(person,place,thing);
and
2) ALTER TABLE provider ADD PRIMARY KEY(person,thing,place);
A) SELECT person, place, thing FROM provider WHERE person = 'foo' AND thing = 'bar';
B) SELECT person, place, thing FROM provider WHERE person = 'foo' AND place = 'baz';
C) SELECT person, place, thing FROM provider WHERE person = 'foo' AND place = 'baz' AND thing = 'bar';
D) SELECT person, place, thing FROM provider WHERE place = 'baz' AND thing = 'bar';
alter table parent  drop column id;
ERROR 1025 (HY000): Error on rename of
'./test/#sql-a04_b' to './test/parent' (errno: 150).

@Adrian Cornish's answer is correct. However, there is another caveat to dropping an existing primary key. If that primary key is being used as a foreign key by another table you will get an error when trying to drop it. In some versions of mysql the error message there was malformed (as of 5.5.17, this error message is still

A uses the first two fields in index 2 as a partial index. A can't use index 1 because it doesn't know the intermediate place portion of the index. It might still be able to use a partial index on just person though.

Also, when using composite keys, order is important. These

B can use the primary key index in ALTER statement 1 A can use the primary key index in ALTER statement 2 C can use either index D can't use either index

If you want to drop a primary key that's being referenced by another table, you will have to drop the foreign key in that other table first. You can recreate that foreign key if you still want it after you recreate the primary key.

are not the the same thing. They both enforce uniqueness on that set of three fields, however from an indexing standpoint there is a difference. The fields are indexed from left to right. For example, consider the following queries:

Note
Rectangle 27 0

mysql ALTER TABLE to add a composite primary key?


ALTER TABLE `MyDatabase`.`Provider`
    ADD CONSTRAINT CK_Per_Place_Thing_Unique UNIQUE (person,place,thing)
;

I typically use a surrogate key......then add a unique constraint. This way....if the "uniqueness" changes down the road, it is not much drama to tweak the constraint, vs messing with the primary-key. And if you have child tables that foreign-key references this table, you only have to FK the surrogate-key, not all 3 columns.

Thank you, a constraint is what I wanted, I didn't know what to ask for in this initial post. Thanks for adding this to the thread.

You may simply want a UNIQUE CONSTRAINT. Especially if you already have a surrogate key. (example: an AUTO_INCREMENT )

Note
Rectangle 27 0

mysql ALTER TABLE to add a composite primary key?


alter table table_name add primary key (col_name1, col_name2);
Note