简体   繁体   中英

MySQL dump restore is not restoring data for tables that contain only 1 row

I have a dump I took from a MySQL 5.5.62 database. The import into my 5.7 database works just fine for tables with more than 1 row. However, any tables containing only 1 row are not properly having the data inserted. Below is an example of one such table from the dump file:

--
-- Table structure for table `Tab2`
--
DROP TABLE IF EXISTS `Tab2`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `Tab2` (
  `Col2` char(36) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
  `ColT1` varchar(235) NOT NULL,
  `ColT2` varchar(256) DEFAULT NULL,
  PRIMARY KEY (`Col2`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */;

--
-- Dumping data for table `Tab2`
--

LOCK TABLES `Tab2` WRITE;
/*!40000 ALTER TABLE `Tab2` DISABLE KEYS */;
INSERT INTO `Tab2` VALUES ('094e5439-7afd-4fba-b934-476873a55c8b','/',NULL);
/*!40000 ALTER TABLE `Tab2` ENABLE KEYS */;
UNLOCK TABLES;

DROP TABLE IF EXISTS `Tab1`;
    /*!40101 SET @saved_cs_client     = @@character_set_client */;
    /*!40101 SET character_set_client = utf8 */;
    CREATE TABLE `Tab1` (
      `Col1` char(36) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
      `Col2` char(36) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
      `Col3` varchar(50) NOT NULL,
      `Col4` tinyint(1) NOT NULL,
      `Col5` datetime NOT NULL,
      PRIMARY KEY (`Col1`),
      KEY `Col2` (`Col2`),
      CONSTRAINT `Constraint1` FOREIGN KEY (`Col2`) REFERENCES `Tab2` (`Col2`) ON DELETE NO ACTION ON UPDATE NO ACTION
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
    /*!40101 SET character_set_client = @saved_cs_client */;

--
-- Dumping data for table `Tab1`
--

LOCK TABLES `Tab1` WRITE;
/*!40000 ALTER TABLE `Tab1` DISABLE KEYS */;
INSERT INTO `Tab1` VALUES ('xxx','xxx','xxx',0,'2019-07-24 15:29:06');
/*!40000 ALTER TABLE `Tab1` ENABLE KEYS */;
UNLOCK TABLES;

I'm not sure why this is happening and both the GCP import tool and MySQL Workbench do not give any errors.

There is a foreign key constraint in your table

CONSTRAINT `Constraint1` FOREIGN KEY (`Col2`) REFERENCES `Tab2` (`Col2`) ON DELETE NO ACTION ON UPDATE NO ACTION

Are you trying to execute this script before creating/inserting data in Tab2 table? To make this insert work, you need a row with the same value from Tab1.Col2 ('xxx') already inserted in a row in Tab2 with Tab2.Col2 = 'xxx'

You can get additional foreign key related errors for this type of issues calling:

show engine innodb status;

It was determined after running the dump manually via a MySQL Workbench session that it was in fact doing the inserts. I recalled looking at one of the tables and not seeing rows but I'm not sure what happened in that instance.

However, I was able to run all 3 methods again and they rows were inserted. The problem I had is I was relying on the count from INFORMATION_SCHEMA.TABLES. Once I did the research I determined the counts in this table/view is not necessarily accurate. I did an ANALYZE TABLE on all tables and then got the accurate count for tables with only 1 row.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM