Thursday, 6 October 2016

mysql - ALTER TABLE to add a composite primary key



I have a table called provider. I have three columns called person, place, thing. There can be duplicate persons, duplicate places, and duplicate things, but there can never be a dupicate person-place-thing combination.




How would I ALTER TABLE to add a composite primary key for this table in MySQL with the these three columns?


Answer



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


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



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

No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...