码迷,mamicode.com
首页 > 数据库 > 详细

mysql为表添加外键完成性约束 报错Can't create table 'sfkbbs.#sql-513_25' (errno: 150)

时间:2016-10-03 08:55:48      阅读:1258      评论:0      收藏:0      [点我收藏+]

标签:

技术分享

代码

alter table sfk_son_module add constraint   
foreign key(father_module_id) references
sfk_father_module(id) on delete restrict on update restrict;

constraint 后面可以加上约束名字)

错误原因是之前两张表的id的类型不一样,一个时int,一个时bigint

解决办法时修改表,

alter table sfk_father_module modify id bigint;

修改之后就可以了

下面是mysql的一些资料

ALTER [ONLINE | OFFLINE] [IGNORE] TABLE tbl_name
    [alter_specification [, alter_specification] ...]
    [partition_options]

 ADD [CONSTRAINT [symbol]]
        FOREIGN KEY [index_name] (index_col_name,...)
        reference_definition

 

Topic: CONSTRAINT
SQL supports foreign keys, which let you cross-reference related data across tables, and foreign key constraints, which help keep this spread-out data consistent. The essential syntax for a foreign key constraint definition in a CREATE TABLE or ALTER TABLE statement looks like this:
[CONSTRAINT [symbol]] FOREIGN KEY
    [index_name] (index_col_name, ...)
    REFERENCES tbl_name (index_col_name,...)
    [ON DELETE reference_option]
    [ON UPDATE reference_option]

reference_option:
    RESTRICT | CASCADE | SET NULL | NO ACTION

 See also:  Online help create-table-foreign-keys

mysql为表添加外键完成性约束 报错Can't create table 'sfkbbs.#sql-513_25' (errno: 150)

标签:

原文地址:http://www.cnblogs.com/rocky-AGE-24/p/5928730.html

(0)
(2)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!