2013-04-24 98 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是幫助我的另一個答案。