Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mysqldump and the order of the tables

I copy my database with mysqldump - uroot -p pagesjaunes > E:\db.sql
But when I try to execute mysql pagesjaunes < db.sql in my home pc I got error because mysqldump didn't put the tables with the right order in the file db.sql, tables without foreign key must be first for example.

DROP TABLE IF EXISTS `fonction`;

CREATE TABLE `fonction` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `nom` varchar(20) NOT NULL,
  `id_qualite` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `fonction_qualite_fk` (`id_qualite`),
  CONSTRAINT `fonction_qualite_fk` FOREIGN KEY (`id_qualite`) REFERENCES `qualite` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=41 DEFAULT CHARSET=utf8;

DROP TABLE IF EXISTS `qualite`;
CREATE TABLE `qualite` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `nom` varchar(40) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=25 DEFAULT CHARSET=utf8;

I have to put qualite table first and it works but I have a lot of tables and there will be a lot of manual ordering.

So how can I handle this ?

like image 771
Hayi Avatar asked Feb 01 '14 12:02

Hayi


People also ask

Can Mysqldump lock tables?

By default, the mysqldump utility, which allows to back a MySQL database, will perform a lock on all tables until the backup is complete.

Does order of columns matter in MySQL?

Yes, column order does matter.

How do I change the order of data in MySQL?

An "ALTER TABLE ORDER BY" statement exist in the syntaxes accepted by MySQL. According to the documentation, this syntax: - only accept *one* column, as in "ALTER TABLE t ORDER BY col;" - is used to reorder physically the rows in a table, for optimizations.


2 Answers

i found the solution HERE i was making a mistake, in file E:\db.sql generated i deleted the lines like that

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

i was thinking that it's just a comment but in reality they are instructions too.

like image 165
Hayi Avatar answered Nov 15 '22 18:11

Hayi


You can't use --compact parameter of mysqldump, because it removes the information (comments) about foreign key dependency needed by mysql to recreate the tables later in the correct order.

like image 27
Ivan Apolonio Avatar answered Nov 15 '22 17:11

Ivan Apolonio