我目前正在阅读这个Yii应用程序电子书:http://www.packtpub.com/agile-web-application-development-yii11-and-php5/book - 我在将教程DDL / SQL语句输入PHPMyAdmin时遇到问题而没有抛出错误。
有人会对以下语法无效的原因有所了解吗?它可能很简单但我看不到它:
SQL声明:
ALTER TABLE tbl_issue
ADD CONSTRAINT FK_issue_project FOREIGN KEY (`project_id`)
REFERENCES tbl_project(`id`) ON DELETE CASCADE ON UPDATE RESTRICT;
ALTER TABLE tbl_issue
ADD CONSTRAINT `FK_issue_owner` FOREIGN KEY (`owner_id`)
REFERENCES tbl_user(`id`) ON DELETE CASCADE ON UPDATE RESTRICT;
ALTER TABLE tbl_issue
ADD CONSTRAINT `FK_issue_requester` FOREIGN KEY (`requester_id`)
REFERENCES tbl_user(`id`) ON DELETE CASCADE ON UPDATE RESTRICT;
ALTER TABLE tbl_project_user_assignment
ADD CONSTRAINT `FK_project_user` FOREIGN KEY (`project_id`)
REFERENCES tbl_project(`id`) ON DELETE CASCADE ON UPDATE RESTRICT;
ALTER TABLE tbl_project_user_assignment
ADD CONSTRAINT `FK_user_project` FOREIGN KEY (`user_id`)
REFERENCES tbl_user(`id`) ON DELETE CASCADE ON UPDATE RESTRICT;
INSERT INTO tbl_user (`email`, `username`, `password`)
VALUES
(`test1@notanaddress.com`,`Test_User_One`, MD5(`test1`)),
(`test2@notanaddress.com`,`Test_User_Two`, MD5(`test2`));
错误消息
Error SQL query: ALTER TABLE tbl_issue ADD CONSTRAINT FK_issue_project FOREIGN KEY ( `project_id` ) REFERENCES tbl_project( `id` ) ON DELETE CASCADE ON UPDATE RESTRICT ; MySQL said: #1005 - Can't create table 'trackstar_test.#sql-c78_127' (errno: 121) (<a href="server_engines.php?engine=InnoDB&page=Status& token=252c0553975923580ca430b6e98c4243">Details...</a>)
注意:
更新
答案 0 :(得分:0)
实际问题是:
当你在阅读本书后,在
上执行了一些插入/更新语句tbl_proect,
tbl_issue
比您尝试添加外键约束。在应用之前检查表数据。所以,这是实际的错误,可能是你的表包含一些违反外键约束的记录。因此phpmyadmin不允许您更改表并生成错误消息。
解决方案:
TRUNCATE TABLE `tbl_project`
TRUNCATE TABLE `tbl_issue`
只做一件事,清除所有表格。空表。 。现在解决了你的问题,phpmyadmin允许你运行命令。