22

I would like to rename a column in a table that is a foreign key to many tables. Apparently this is only possible if you delete the constraints, as I found out in this link.

I dont want to delete all the constratints manually is there a way to delete all the foreign key constraints in the database?

I have also tried SET FOREIGN_KEY_CHECKS=0; but I still cant rename the column.

Community
  • 1
  • 1
Borut Flis
  • 15,715
  • 30
  • 92
  • 119
  • 1
    If you don't know what you're deleting, how are you going to recreate all the FKs? – Augusto Jan 27 '13 at 10:31
  • What do you mean by all `foreign key constraints`? You have to consider if you have index on these columns as well.. check here http://stackoverflow.com/questions/1438654/how-do-i-drop-all-foreign-key-constraints-on-a-table-in-sql-server-2000 : it's for sql server, you may find a similar syntax for altering – bonCodigo Jan 27 '13 at 10:32
  • just all the foreign keys, I can recreate the FKeys because I use http://www.doctrine-project.org/ and have the models that will recreate the tables – Borut Flis Jan 27 '13 at 10:35

5 Answers5

39

You can use this SQL to generate ALTER TABLES (!!YOUR_SCHEMA_HERE!! needs to be replaced by your schema):

SELECT concat('alter table `',table_schema,'`.`',table_name,'` DROP FOREIGN KEY ',constraint_name,';')
FROM information_schema.table_constraints
WHERE constraint_type='FOREIGN KEY'
AND table_schema='!!YOUR_SCHEMA_HERE!!';

It will generate SQL like this:

alter table `viewpoint_test`.`answer_code` DROP FOREIGN KEY fk_answer_code_codebook_item1;
alter table `viewpoint_test`.`answer_code` DROP FOREIGN KEY fk_answer_code_questionary_answer1;
alter table `viewpoint_test`.`codebook` DROP FOREIGN KEY codebook_ibfk_1;
...

By "schema name", I mean your database name. It's the same thing.

Ryan Shillington
  • 23,006
  • 14
  • 93
  • 108
lopisan
  • 7,720
  • 3
  • 37
  • 45
1

You Can Try using As Like of Following ..

ALTER TABLE tableName
DROP FOREIGN KEY fieldName;
ADD FOREIGN KEY (newForignKeyFieldName);

Also you can try with Reference Key.As like .....

ALTER TABLE tableName
DROP FOREIGN KEY fieldName;
ADD FOREIGN KEY (newForignKeyFieldName)
REFERENCES anotherTableName(reference_id);
1

The following query will build the correct syntax automatically. Make sure you put your real DB schema name in the WHERE condition. Just execute each line returned and all your FOREIGN KEYS will be gone.

I leave the reverse (adding them back) as an exercise for you.

SELECT CONCAT("alter table ", TABLE_NAME," drop foreign key ", CONSTRAINT_NAME,"; ") AS runMe
FROM information_schema.key_column_usage 
WHERE TABLE_SCHEMA='MY_SCHEMA_NAME' AND CONSTRAINT_NAME <> 'PRIMARY';

If you need it to be schema independent, you can write: TABLE_SCHEMA=DATABASE() to get the current active DB name

boroboris
  • 1,548
  • 1
  • 19
  • 32
Dewey
  • 756
  • 6
  • 17
0

I was still having some issues after finding this thread, but I have seemed to find a workflow that works for me.

Database Deletion

If you are using MySQL Workbench, you will need to turn SAFE UPDATE off by doing the following 3 steps:

  1. MySql Workbench > Preferences > SQL EDITOR > (bottom of settings)
  2. turn the SAFE UPDATE off
  3. then log out of your connection and reconnect

Steps to start purge database and start new

  1. Delete all data from tables

(This MySQL script will create a DELETE FROM script for each table in your database.)

SELECT concat('DELETE FROM ',table_schema,'.',table_name,';') FROM information_schema.table_constraints WHERE table_schema='!!TABLE_SCHEMA!!';

Copy the output of this and run it. You might need this line-by-line.

  1. Delete all Foreign Keys from your tables

(This MySql script will create na ALTER TABLE _ DROP FOREIGN KEY script for each table in your database.)

SELECT concat('alter table ',table_schema,'.',table_name,' DROP FOREIGN KEY ',constraint_name,';') FROM information_schema.table_constraints WHERE constraint_type='FOREIGN KEY' AND table_schema='!!TABLE_SCHEMA!!';

Copy the output of this and run it. You might need this line-by-line.

After you've done this, you SHOULD be able to run your DROP DATABASE script.

MaylorTaylor
  • 4,671
  • 16
  • 47
  • 76
-4

Executing the following query

select * from information_schema.key_column_usage

will show you all the constraints (with the column name, constraint type, table and schema) that exist in your database. You'll notice these columns:

CONSTRAINT_CATALOG
CONSTRAINT_SCHEMA
CONSTRAINT_NAME
TABLE_CATALOG
TABLE_SCHEMA
TABLE_NAME
COLUMN_NAME
ORDINAL_POSITION
POSITION_IN_UNIQUE_CONSTRAINT
REFERENCED_TABLE_SCHEMA
REFERENCED_TABLE_NAME
REFERENCED_COLUMN_NAME

Then, if you're planning to delete each constraint you have referencing your column, you should consider the REFERENCED_* columns and run something like:

DELETE FROM information_schema.key_column_usage 
WHERE 
    REFERENCED_TABLE_SCHEMA='myschema'
    AND
    REFERENCED_TABLE_NAME='mytable'
    AND
    REFERENCED_COLUMN_NAME='mycolumn'

http://dev.mysql.com/doc/refman/5.1/en/key-column-usage-table.html

Augusto
  • 779
  • 5
  • 18
  • 7
    Sadly this will not work because `information_schema` tables are not writeable – SystemParadox Jan 31 '14 at 14:27
  • 1
    This kind of approach (even if the user had the permission, which it does not) will just left a gazzilion of trash values in so many tables. This solution just does not do the job – Guilherme Ferreira Feb 03 '16 at 19:45