简体   繁体   中英

PHP / MySQL - Foreign Key Issue

Im following a tutorial at the moment and learning how to build something into my existing site.

The tutorial tells me that i need to run this against my database

ALTER TABLE posts ADD FOREIGN KEY(post_by) REFERENCES users(user_id) ON DELETE RESTRICT ON UPDATE CASCADE;

and im receiving an error saying it cant create table.

the 'users' table is from my existing database and everything else is new.

The tutorial gives me a few alter commands to run and they all go perfect, except when im trying to use the 'users' table.

Im totally stuck on this, any help would be greatly appreciated.

Cheers

Check your table type. It should be InnoDB .

Take a look to http://dev.mysql.com/doc/refman/5.1/en/alter-table.html , there is very clear what options are available to each engine.

Take a look to the section that describes:

The FOREIGN KEY and REFERENCES clauses are supported by the InnoDB storage engine

And:

For other storage engines, the clauses are parsed but ignored.

And as h2ooooooo said you can change your engine with:

ALTER TABLE table ENGINE = INNODB

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.

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