views:

312

answers:

2

In mySQL 5, is there a way to drop all foreign key constraints on a table with one SQL statement without referring to them by name?

I'm writing a DB update script, and unfortunately some of the sites had constraints created with "wrong" names. I'm trying to avoid going in and getting the actual constraint names from the DB and inserting them back into SQL statements.

+3  A: 

You can surely select * the table to a temp table, drop and recreate it, then copy back.

Zed
+1  A: 

In your script you can always add SET FOREIGN_KEY_CHECKS=0 if you just want to get around the constraints.

Also, I have always deleted constraints on a per constraint basis using:

ALTER TABLE <table_name> DROP FOREIGN KEY <key_name>;

I don't think you can do all of them at once and I could not find any examples that show you can.

northpole