How not to create foreign key constraint if table exists

Viewed 114

I am exporting a db with IF NOT EXISTS option. So all create table statements turn into CREATE TABLE IF NOT EXISTS. But I still have ALTER TABLE statements at the end of the sql file which add foreign key constraints. The thing is, if I have the tables already, I have the constraints set up already too. Hence I get "duplicate foreign key constraint name" error during import.

If I delete these alter table statements manually after export, the import works fine.

Is there a way to properly export or import it from phpmyadmin so that the foreign key constraints are created only if the table is created by the same script? Also, what is the point of creating these constraints if you haven't created the tables?

0 Answers
Related