InnoDB status, altering table by dropping foreign key column

So if you have a table, say Kinerja, that has a foreign key column, say NIP. And you need to alter that table by dropping the column, you can't just do:

alter table Kinerja drop column NIP;

It will spit errno 150 at ya.

You need to first drop the foreign key constraint pertinent to the said column, such as:

alter table Kinerja drop foreign key the_constraint_of_NIP_column_here;

After that is done, you can do:

alter table Kinerja drop column NIP;

But how to get the constraint? Well, just do:

alter table Kinerja drop column NIP;

first to get error. And then, look up the innoDB log by issuing:

show engine innoDB status;

In the log, find this paragraph:


------------------------
LATEST FOREIGN KEY ERROR
------------------------
121211  0:16:23 Error in foreign key constraint of table penilaian/kinerja:
there is no index in the table which would contain
the columns as the first columns, or the data types in the
table do not match the ones in the referenced table
or one of the ON ... SET NULL columns is declared NOT NULL. Constraint:
,
  CONSTRAINT "kinerja_ibfk_2" FOREIGN KEY ("NIP") REFERENCES "dosen" ("NIP")


There, you have the constraint! :p

Then, do delete the constraint and then the column.

0 komentar:

Posting Komentar