简体   繁体   中英

mysql change constraint name, how?

create table Foo(
 userId bigint(20) not null,
 KEY `Foo_userId` (`userId`),
 CONSTRAINT `Foo_userId` FOREIGN KEY (`userId`) REFERENCES `User` (`id`)
);

How to change the Key/constraint name from Foo_userId to Bar_userId, just change names only. I know that they can be dropped first, then re-create them. I am looking for a simple way like

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

Is there anything like this in mysql? thanks.

In order to change the constraint name or properties you can delete the old foreign key constraint and add a new one. This is the only solution that it really worked for me.

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

Here is another answer that helped me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM