Question

I'm having an error restoring tables which have Foreign Keys after lowercasing the contents of a mysql dump file. My goal is to lowercase all structure in a mysql database. I used vim to lowercase all contents of a mysqldump file when trying to restore the database, this is the error I get.

$  mysql -u [user] -p[password] dme < mysqldump.sql
    ERROR 1005 (HY000) at line 399: Can't create table 'dme.contacts' (errno: 121)

The restore is error'ing out on any table which has Foreign Key contstraints. If I remove lines after the Primary Key for each table, the entire restore works fine.

My question is, what is causing this and how can I keep my Foreign Keys in the process?

  1. Lowercased.sql example which results in the error (fyi prior to dme.contacts there are multiple tables which are created just fine. Only tables w/ Foreign Keys and Contstraints have issues.)

    drop table if exists `contacts`;
    /*!40101 set @saved_cs_client     = @@character_set_client */;
    /*!40101 set character_set_client = utf8 */;
    create table `contacts` (
      `contactid` int(11) not null auto_increment,
      `firstname` varchar(100) default null,
      `lastname` varchar(100) not null,
      `fullname` varchar(200) not null,
      `dob` datetime default null,
      `sex` char(1) default null,
      `voided` datetime default null,
      `vendorid` int(11) not null,
      `contacttypeid` int(11) default null,
      `comments` varchar(1500) default null,
      primary key (`contactid`),
      key `fk_contacts_vendors` (`vendorid`),
      key `fk_contacts_contacttype` (`contacttypeid`),
      constraint `fk_contacts_contacttype` foreign key (`contacttypeid`) references                 `contacttype` (`contacttypeid`) on delete no action on update no action,
      constraint `fk_contacts_vendors` foreign key (`vendorid`) references `vendors`         (`vendorid`) 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 `contacts`
    --
    
    lock tables `contacts` write;
    /*!40000 alter table `contacts` disable keys */;
    /*!40000 alter table `contacts` enable keys */;
    unlock tables;
    
  2. Original from the mysqldump.sql (No Error)

    --
    -- Table structure for table `Contacts`
    --
    
    DROP TABLE IF EXISTS `Contacts`;
    /*!40101 SET @saved_cs_client     = @@character_set_client */;
    /*!40101 SET character_set_client = utf8 */;
    CREATE TABLE `Contacts` (
      `ContactId` int(11) NOT NULL AUTO_INCREMENT,
      `FirstName` varchar(100) DEFAULT NULL,
      `LastName` varchar(100) NOT NULL,
      `FullName` varchar(200) NOT NULL,
      `DOB` datetime DEFAULT NULL,
      `Sex` char(1) DEFAULT NULL,
      `Voided` datetime DEFAULT NULL,
      `VendorID` int(11) NOT NULL,
      `ContactTypeID` int(11) DEFAULT NULL,
      `Comments` varchar(1500) DEFAULT NULL,
      PRIMARY KEY (`ContactId`),
      KEY `FK_Contacts_Vendors` (`VendorID`),
      KEY `FK_Contacts_ContactType` (`ContactTypeID`),
      CONSTRAINT `FK_Contacts_ContactType` FOREIGN KEY (`ContactTypeID`) REFERENCES `ContactType` (`ContactTYpeID`) ON DELETE NO ACTION ON UPDATE NO ACTION,
      CONSTRAINT `FK_Contacts_Vendors` FOREIGN KEY (`VendorID`) REFERENCES `Vendors` (`VendorId`) 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 `Contacts`
    --
    
    LOCK TABLES `Contacts` WRITE;
    /*!40000 ALTER TABLE `Contacts` DISABLE KEYS */;
    /*!40000 ALTER TABLE `Contacts` ENABLE KEYS */;
    UNLOCK TABLES;
    
Was it helpful?

Solution

Try removing the constrain name. Sample:

constraint `fk_contacts_contacttype` foreign key (`contacttypeid`) ....

To

constraint foreign key (`contacttypeid`) ...
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top