0
votes

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.

1
By the way, the book you're using is badly outdated — PHP 6 was expected to be released shortly after its publication, but plans changed shortly thereafter, and PHP 6 doesn't exist. You may want to pick up something more recent.user149341
Thanks, but have you also some proposals? ...I want to learn OOP in PHP, so I read, that this book is good for OOP, but if it exist something better, I can try...PeterB

1 Answers

0
votes

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,