10

I need to export a large database without foreign keys. What is the command to do this?

This is what I tried but I know this is incorrect.

mysqldump -u root -p DBNAME SET FOREIGN_KEY_CHECKS = 0; | gzip > database.sql.gzip
pb2q
  • 58,613
  • 19
  • 146
  • 147
user1452145
  • 103
  • 1
  • 1
  • 4
  • possible duplicate of [Can you automatically create a mysqldump file that doesn't enforce foreign key constraints?](http://stackoverflow.com/questions/2429655/can-you-automatically-create-a-mysqldump-file-that-doesnt-enforce-foreign-key-c) – Christopher Schultz Aug 19 '14 at 16:24
  • 5
    This is a very different question, not including the keys is different to disabling the checks on the keys. – ThomasRedstone Jan 28 '16 at 10:10

3 Answers3

15

you can use the --init-command flag on a per-session basis:

mysql --init-command="SET SESSION FOREIGN_KEY_CHECKS=0;" ... < dump.sql
codewandler
  • 536
  • 6
  • 9
12

This is ugly, but I ended up just modifying the schema file to remove the foreign keys.


Dump schema only, no foreign keys

mysqldump -uuser -ppassword --no-data dbname | sed '$!N;s/^\(\s*[^C].*\),\n\s*CONSTRAINT.*FOREIGN KEY.*$/\1/;P;D' | grep -v 'FOREIGN KEY' > dbname_schema_no_fk.sql

sed finds lines that precede foreign key constraint declarations and replaces those lines with themselves, minus the trailing comma. grep excludes the foreign key constraints.

Dump data only, no table creation

mysqldump -uuser -ppassword --no-create-info dbname > dbname_data.sql

Load schema first, then data

$ mysql -uuser -ppassword
> create database foo
> use foo
> source dbname_schema_no_fk.sql
> source dbname_data.sql

Tested with mysqldump Ver 10.13 Distrib 5.6.32-78.1, for Linux (x86_64).

MatrixManAtYrService
  • 8,023
  • 1
  • 50
  • 61
  • This is what I was looking for. The regular expression may need a fix but even with the current implementation it does the job. Thank you. – Nicolai Nita Apr 13 '23 at 15:59
11

From this SO thread:

Can you automatically create a mysqldump file that doesn't enforce foreign key constraints?

The mysqldump command included with MySQL 5.0.51 (and according to the change log versions since 4.1.1) does switch off foreign key checks. By default, mysqldump includes the following line at the top of the dump file:

/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;

The /*!40014 ... */ syntax is a conditional comment that will be executed on MySQL 4.0.14 and later. The old foreign key checks setting is restored at the end of the dump file:

/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;

Community
  • 1
  • 1
philwinkle
  • 7,036
  • 3
  • 27
  • 46
  • 14
    But is this the answer to the question? This exports all the foreign keys, just makes sure that they are not enforced during import. – Ivan P Jul 24 '15 at 20:17
  • 7
    It doesn't look like an answer to me. The question was to have a dump without FK constraints and not disabling it temporarily. The dump file still would contain FK constraints. – bakytn Jul 21 '16 at 04:58