views:

38

answers:

3

I am trying to run this query:

ALTER TABLE table DROP PRIMARY KEY, ADD PRIMARY KEY( `CUSTNO` , `DEPTNO` , `PRODNO` , `DT` );

I get

Incorrect table definition; there can be only one auto column and it must be defined as a key
+2  A: 

You have to alter your pk column so that it hasn't auto_increment modifier anymore.

codymanix
@Anzeo: no, he doesn't. he just removes the primary-key-index.
oezi
@oezi, yup noticed I was incorrect, didn't noticed you already replied on my comment
Anzeo
A: 

First and foremost, if I'm correct you're defining a compound key? This is usually bad practice. It's better to have an extra ID column and to add a separate constraint to check you have a unique combination. And as codymanix suggested, you need to first alter the column to not have auto_increment anymore and then drop it.

Anzeo
to "This is usually bad practice": uhm... no? what about linking-tables for n-m relationships (just as one example)?
oezi
See http://stackoverflow.com/questions/1264248/database-design-composite-key-vs-one-column-primary-key/1264256#1264256 for an argumentation
Anzeo
A: 

you'll have to do this in 3 (or 4) steps:

  1. remove the "auto-incremet" attribute from your current primary key
  2. drop your primary key
  3. set the new primary key
  4. (reset "auto_incremet" to your orl primary-key-column)

EDIT: maybe setting a new primary isn't what you realy want to do. please take a look at unique indexes - i think thats waht you want to set on your other columns to make sure they don't occur more than once.

oezi