2013-04-24 103 views
3
create table Foo(
userId bigint(20) not null, 
KEY `Foo_userId` (`userId`), 
CONSTRAINT `Foo_userId` FOREIGN KEY (`userId`) REFERENCES `User` (`id`) 
); 

如何将键/约束名称从Foo_userId更改为Bar_userId,只更改名称。我知道他们可以先丢弃,然后重新创建它们。我正在寻找一个简单的方法,如mysql更改约束名称,如何?

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

在mysql中是否有这样的事情?谢谢。

回答

0

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

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

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