tags:

views:

26

answers:

2

Hi--

I have a mysqldump file created from an earlier version of a product that can't be imported into a new version of the product, since the db structure has changed slightly (mainly altering a column that was NOT NULL DEFAULT 0 to UNIQUE KEY DEFAULT NULL).

If I just import the old dump file, it will error out since the column that has default values of 0 now breaks the UNIQUE constraint.

It would be easy enough to either manually alter the mysqldump file, or import into a temp table and change it, then copy to the new table. However, is there a way to do this programatically, so it will be repeatable and not manual? (this will need to happen for many instances of this product).

I'm thinking something like disabling key constraints for the import, then setting all values that = 0 to NULL, then re-enabling the key constraints?

Is this possible? Any help appreciated.

A: 

You could just use sed and modify the dumpfile in an automated, repleatable way.

sed s/NOT NULL DEFAULT 0/UNIQUE KEY DEFAULT NULL/g

or something like that.

Joshua Smith
+1  A: 

Yes.

SET UNIQUE_CHECKS=0; Turns off Unique Key Constraints
SET FOREIGN_KEY_CHECKS=0; Turns off Foreign Key Constraints

Import file Update 0 to Null

SET UNIQUE_CHECKS=1 turns back on
SET FOREIGN_KEY_CHECKS=1 turns back on

Gary
thanks Gary-- I ended up dropping the index and then doing the import and transform, then re-applying the index-- I've read there's some odd behavior with the UNIQUE_CHECKS, and the statements as you'd written them didn't work. The theory seems to work however, so thanks for the input!
julio