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

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

技术分享

代码

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)