简体   繁体   English

PHP6第3章-#1215-无法添加外键约束

[英]PHP6 chapter 3 - #1215 - Cannot add foreign key constraint

I try to learn from book Professional PHP6 and in chapter 3 I need to create tables: 我尝试从Professional PHP6一书中学习,在第3章中,我需要创建表:

CREATE TABLE `entity` (
    `entityid` SERIAL PRIMARY KEY NOT NULL,
    `name1` varchar(100) NOT NULL,
    `name2` varchar(100) NOT NULL,
    `type` char(1) NOT NULL
);

and

CREATE TABLE `entityaddress` (
    `addressid` SERIAL PRIMARY KEY NOT NULL,
    `entityid` int,
    `saddress1` varchar(255),
    `saddress2` varchar(255),
    `scity` varchar(255),
    `cstate` char(2),
    `spostalcode` varchar(10),
    `stype` varchar(50),
    CONSTRAINT `fk_entityaddress_entityid`
        FOREIGN KEY (`entityid`) REFERENCES `entity`(`entityid`)
);

result is error: #1215 - Cannot add foreign key constraint 结果为错误: #1215-无法添加外键约束

I check in original code for that book and there is sql file, which give me same error. 我检入该书的原始代码,并且有sql文件,这给了我同样的错误。 ...is there anything wrong, or is something with my db in xampp? ...有什么问题吗,或者我的数据库在xampp中有问题吗? I try to create only tables and then create relation in designers, but I got program error... 我尝试仅创建表,然后在设计器中创建关系,但是出现程序错误...

I set InnoDB engine. 我设置了InnoDB引擎。

Thanks for any suggestion. 感谢您的任何建议。

The type of entityid has to be the same in both tables. 在两个表中, entityid的类型必须相同。 SERIAL is an alias for BIGINT UNSIGNED NOT NULL AUTO_INCREMENT UNIQUE . SERIALBIGINT UNSIGNED NOT NULL AUTO_INCREMENT UNIQUE的别名。 So change 所以改变

`entityid` int,

to

`entityid` BIGINT UNSIGNED NOT NULL,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM