Creating Tables in a certain order

When you have a “parent” and a “child” table, do you have to create the “parent” table before the “child” table?

It seems like you have to create the “parent” first so the “child” will have something to return to. :-/

Debbie

you must declare the referenced table before referencing it in a FOREIGN KEY declaration (in an engine which supports foreign keys)

i’m not sure whether disabling the foreign key check will disable it during the CREATE TABLE statement, though (disabling foreign key checking is usually done when loading large volumes of data, to minimize the overhead of checking individual rows)

still, it makes sense to create your tables in the intended order anyway

So that was a “Yes”?? :slight_smile:

Debbie

depending on what you mean by “return to” :slight_smile:

So the child table will have something to REFER (i.e. point) to.

Debbie

If you are using an engine that supports foreign key constraints and using them than yes, you will need to create the table referenced by the foreign key relationship first. If you are creating “conceptual” foreign keys, but not actually enforcing them with TRUE constraints than no, you don’t NEED to create the referenced table first but you should. Create a INNODB table with a foreign key constraint with a reference to a table that doesn’t exist. Than do the same thing with a MYISAM table minus the foreign key constraint and see what happens.