如何在此表创建脚本中添加删除级联? id_fk来自mytable表。
CREATE TABLE IF NOT EXISTS `ip` (
`ip_id` int(11) NOT NULL AUTO_INCREMENT,
`id_fk` int(11) DEFAULT NULL,
`ip_add` varchar(40) DEFAULT NULL,
PRIMARY KEY (`ip_id`),
KEY `id_fk` (`id_fk`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;
答案 0 :(得分:2)
试试这个:
ALTER TABLE `ip`
ADD CONSTRAINT `FK_mytable` FOREIGN KEY (`id_fk`) REFERENCES `mytable` (`id`)
ON DELETE CASCADE ON UPDATE CASCADE;
希望这有帮助!
答案 1 :(得分:0)
添加foreign key constraint并指定要执行的操作和时间。
ALTER TABLE ip
ADD CONSTRAINT constraint_name
FOREIGN KEY index_name(id_fk)
REFERENCES other_table(other_column_name)
ON DELETE CASCADE
ON UPDATE CASCADE;
您可以直接在表定义中包含约束:
FOREIGN KEY index_name(id_fk)
REFERENCES other_table(other_column_name)
ON DELETE CASCADE
ON UPDATE CASCADE;