I try to learn from book Professional PHP6 and in chapter 3 I need to create tables:
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
I check in original code for that book and there is sql file, which give me same error. ...is there anything wrong, or is something with my db in xampp? I try to create only tables and then create relation in designers, but I got program error...
I set InnoDB engine.
Thanks for any suggestion.
The type of entityid
has to be the same in both tables. SERIAL
is an alias for BIGINT UNSIGNED NOT NULL AUTO_INCREMENT UNIQUE
. So change
`entityid` int,
to
`entityid` BIGINT UNSIGNED NOT NULL,
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.