【问题标题】:mysql change constraint name, how?mysql更改约束名称,如何?
【发布时间】:2013-04-18 06:17:09
【问题描述】:
create table Foo(
 userId bigint(20) not null,
 KEY `Foo_userId` (`userId`),
 CONSTRAINT `Foo_userId` FOREIGN KEY (`userId`) REFERENCES `User` (`id`)
);

如何将 Key/constraint 名称从 Foo_userId 更改为 Bar_userId,只需更改名称即可。我知道可以先删除它们,然后重新创建它们。我正在寻找一种简单的方法,例如

alter table Foo rename KEY Foo_userId Bar_userId;
alter table Foo rename CONSTRAINT Foo_userId Bar_userId;

在mysql中有这样的东西吗?谢谢。

【问题讨论】:

    标签: mysql constraints rename


    【解决方案1】:

    要更改约束名称或属性,您可以删除旧的外键约束并添加一个新约束。这是唯一真正适合我的解决方案。

    alter table FOO 
    drop foreign key Foo_userId,
    add constraint Bar_userId foreign key (`userId`) references`User` (`id`)
    

    Here 是另一个帮助我的答案。

    【讨论】:

      猜你喜欢
      • 2018-12-31
      • 2016-12-24
      • 1970-01-01
      • 1970-01-01
      • 2012-10-26
      • 1970-01-01
      • 2015-10-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多