mysql - 将 MySQL 转储文件中的所有文本小写后无法创建表

标签 mysql database-restore

在小写 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;
    

最佳答案

尝试删除约束名称。示例:

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

constraint foreign key (`contacttypeid`) ...

关于mysql - 将 MySQL 转储文件中的所有文本小写后无法创建表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17680067/

相关文章:

mysql - 有没有办法忽略 INSERT 中不存在的列?

php - 在 CodeIgniter 的 Active Record 查询中删除/禁用变量上的反引号

mysql - 使用 .gz 格式的 Unicode 字符(阿拉伯语和库尔德语)恢复 MySQL 数据库

mysql - 如何将 bzip 的输出通过管道传输到 mysql,以将数据直接从 bzip 压缩文件恢复到数据库中

mysql - 无需任何形式的备份即可回滚对 MySQL 数据库的更改

mysql 恢复数据库脚本与create 子句?

mysql - 在mySQL中删除/复制具有特定条件的数据库中的数据

php - 这是 Mysqli 中的错误 - 部分结果

php - 限制在回收 View 中显示的项目

c# - 使用 mysql 和 c# 恢复功能不起作用