如何修改表中外键的名称,如下面的外键名称,希望更改为“one”

xytpbqjk  于 2021-06-21  发布在  Mysql
关注(0)|答案(2)|浏览(345)

如何修改表中外键的名称,如外键名称为article\u ibfk\u 1,要改为'one'。

+--------------------+-------------------+-----------------+---------------+--------------+------------+-------------+------------------+-------------------------------+-------------------------+-----------------------+------------------------+
| CONSTRAINT_CATALOG | CONSTRAINT_SCHEMA | CONSTRAINT_NAME | TABLE_CATALOG | TABLE_SCHEMA | TABLE_NAME | COLUMN_NAME | ORDINAL_POSITION | POSITION_IN_UNIQUE_CONSTRAINT | REFERENCED_TABLE_SCHEMA | REFERENCED_TABLE_NAME | REFERENCED_COLUMN_NAME |
+--------------------+-------------------+-----------------+---------------+--------------+------------+-------------+------------------+-------------------------------+-------------------------+-----------------------+------------------------+
| def                | test              | PRIMARY         | def           | test         | article    | id          |                1 |                          NULL | NULL                    | NULL                  | NULL                   |
| def                | test              | article_ibfk_1  | def           | test         | article    | tag         |                1 |                             1 | test                    | tag                   | id                     |
+--------------------+-------------------+-----------------+---------------+--------------+------------+-------------+------------------+-------------------------------+-------------------------+-----------------------+------------------------+
2 rows in set (0.00 sec)
2uluyalo

2uluyalo1#

将其删除,然后使用新名称再次创建:

alter table article drop foreign key article_ibfk_1;

alter table article add constraint one foreign key (tag)
  references tag (id);
omhiaaxx

omhiaaxx2#

sp\u rename'article\u ibfk\u 1','one'

exec sp_rename@objname=n'article_ibfk_1',@newname=n'one';

相关问题