ERROR 1025 (HY000): Error on rename of './MyDB/MyTable' to './MyDB/#sql2-e4a-ca3e' (errno: 152)
To avoid getting this error while trying to drop a foreign key, use the **constraint name** rather than the **column name** of the foreign key.
Summary:
99.999% of the time errors like this have nothing to do with the ability to create a temporary file and much to do with foreign key issues.
这是从这里引用的。老实说,我不确定约束的名称是什么。谁能举一个使用约束而不是列名的例子?
外键可以在创建表时使用CREATE table语句指定,也可以稍后使用ALTER table语句指定。
请看下面这些命令的简化语法:
CREATE TABLE: http://dev.mysql.com/doc/refman/5.7/en/create-table.html
CREATE TABLE tbl_name(
.....
[CONSTRAINT [symbol]] FOREIGN KEY
[index_name] (index_col_name,...) reference_definition
.....
)
ALTER TABLE: http://dev.mysql.com/doc/refman/5.1/en/alter-table.htmlALTER table
ADD [CONSTRAINT [symbol]]
FOREIGN KEY [index_name] (index_col_name,...)
reference_definition
这两种语法中都有可选的CONSTRAINT [symbol]
子句。此子句用于为约束提供显式名称。在SQL中,外键是约束。
必须使用以下语法来删除外键:
DROP外键: http://dev.mysql.com/doc/refman/5.1/en/alter-table.html
ALTER TABLE table_name
DROP FOREIGN KEY fk_symbol
where fk_symbol
是用于CREATE TABLE或ALTER TABLE ADD命令的约束
如果没有显式指定约束的名称,MySql会为约束生成自己的内部名称。
可以在INFORMATION_SCHEMA TABLE_CONSTRAINTS
视图中查看约束名称。例如:
create table aaa(
x int primary key
);
create table bbb(
y int,
constraint foreign key(y) references aaa(x)
);
select table_name, constraint_name, constraint_type
from INFORMATION_SCHEMA.TABLE_CONSTRAINTS
WHERE Table_name = 'bbb'
;
+ --------------- + -------------------- + -------------------- +
| table_name | constraint_name | constraint_type |
+ --------------- + -------------------- + -------------------- +
| bbb | bbb_ibfk_1 | FOREIGN KEY |
+ --------------- + -------------------- + -------------------- +
1 rows
在上面的例子中,我没有明确地给外键约束一个名字,MySql给这个外键分配了一个名字bbb_ibfk_1
我可以使用这个名称从表中删除(删除)外键:
ALTER TABLE bbb
DROP FOREIGN KEY bbb_ibfk_1 ;