外键约束错误
我有两个表环和样式即
CREATE TABLE IF NOT EXISTS `ring` ( `jewelry_id` int(11) NOT NULL auto_increment, `ring_id` varchar(50) NOT NULL, `gender` varchar(10) NOT NULL, `description` text NOT NULL, `image` varchar(100) NOT NULL, `type` text NOT NULL, PRIMARY KEY (`jewelry_id`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;
syle表是
CREATE TABLE IF NOT EXISTS `style` ( `style_id` int(11) NOT NULL AUTO_INCREMENT, `style` text NOT NULL, `jewelry_id` int(11) NOT NULL, PRIMARY KEY (`style_id`), KEY `jewelry_id` (`jewelry_id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;
当我向样式表添加外键时它给我错误即
ALTER TABLE `style` ADD CONSTRAINT `style_ibfk_1` FOREIGN KEY (`jewelry_id`) REFERENCES `ring` (`jewelry_id`) ON DELETE CASCADE ON UPDATE CASCADE;
错误是
#1005 - Can't create table './j_jewelry/#sql-2c3b_750.frm' (errno: 150) (Details...</a>)
i have two tables ring and style i.e
CREATE TABLE IF NOT EXISTS `ring` ( `jewelry_id` int(11) NOT NULL auto_increment, `ring_id` varchar(50) NOT NULL, `gender` varchar(10) NOT NULL, `description` text NOT NULL, `image` varchar(100) NOT NULL, `type` text NOT NULL, PRIMARY KEY (`jewelry_id`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;
the syle table is
CREATE TABLE IF NOT EXISTS `style` ( `style_id` int(11) NOT NULL AUTO_INCREMENT, `style` text NOT NULL, `jewelry_id` int(11) NOT NULL, PRIMARY KEY (`style_id`), KEY `jewelry_id` (`jewelry_id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;
when i add a foreign key to the style table it give me error i.e
ALTER TABLE `style` ADD CONSTRAINT `style_ibfk_1` FOREIGN KEY (`jewelry_id`) REFERENCES `ring` (`jewelry_id`) ON DELETE CASCADE ON UPDATE CASCADE;
the error is
#1005 - Can't create table './j_jewelry/#sql-2c3b_750.frm' (errno: 150) (Details...</a>)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
MyISAM 表不支持外键。将两个表都设为 InnoDB。
MyISAM tables dont support foreign keys. Make both tables InnoDB.
对于引用外键,表类型应为
INNODB
For foreign key referencing the table type should be
INNODB
InnoDB具有行级锁定。 MyISAM 仅具有完整的表级锁定。因此,InnoDB 支持引用完整性,从而支持外键和其他关系约束。
InnoDB has row-level locking. MyISAM only has full table-level locking. Consequently, InnoDB supports referential integrity and hence the foreign keys and other relationship constraints.