如何修改表中外键的名称,例如下面的外键名称,想要更改为' one'

时间:2018-04-21 13:20:02

标签: mysql sql

如何修改表中外键的名称,例如外键名称为article_ibfk_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)

2 个答案:

答案 0 :(得分:0)

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

alter table article drop foreign key article_ibfk_1;

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

答案 1 :(得分:-1)

sp_rename'article_ibfk_1','one'

OR

EXEC sp_rename @objname = N'article_ibfk_1',@ newname = N'one';