这里加外键有什么问题:
mysql> create table notes (
id int (11) NOT NULL auto_increment PRIMARY KEY,
note_type_id smallint(5) NOT NULL,
data TEXT NOT NULL,
created_date datetime NOT NULL,
modified_date timestamp NOT NULL on update now())
Engine=InnoDB;
Query OK, 0 rows affected (0.08 sec)
mysql> create table notetypes (
id smallint (5) NOT NULL auto_increment PRIMARY KEY,
type varchar(255) NOT NULL UNIQUE)
Engine=InnoDB;
Query OK, 0 rows affected (0.00 sec)
mysql> alter table `notes` add constraint
foreign key(`note_type_id`) references `notetypes`.`id`
on update cascade on delete restrict;
ERROR 1005 (HY000): Can't create table './admin/#sql-43e_b762.frm' (errno: 150)
谢谢
日本
最佳答案
我认为 FK 需要命名。
尝试
ALTER TABLE `test`.`notes` ADD CONSTRAINT `note_type_id` FOREIGN KEY `note_type_id` (`note_type_id`)
REFERENCES `notetypes` (`id`)
ON DELETE RESTRICT
ON UPDATE CASCADE;
关于mysql外键问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4578611/