0

在小写 mysql 转储文件的内容后,我在还原具有外键的表时出错。我的目标是小写mysql数据库中的所有结构。尝试恢复数据库时,我使用 vim 将 mysqldump 文件的所有内容小写,这是我得到的错误。

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

在任何具有外键约束的表上,还原都会出错。如果我删除每个表的主键后的行,则整个还原工作正常。

我的问题是,是什么原因造成的,我怎样才能在这个过程中保留我的外键?

  1. 导致错误的 Lowercased.sql 示例(在 dme.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;
    
  2. 来自 mysqldump.sql 的原文(无错误)

    --
    -- 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;
    
4

1 回答 1

1

尝试删除约束名称。样本:

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

constraint foreign key (`contacttypeid`) ...
于 2013-07-16T15:31:49.260 回答