0
這裏我在我的數據庫中有四個表名爲test_center
。這些是:如何爲三個主表添加三個外鍵給同一個子表?
test_user
- > PK =u_id
test_metadata
- > PK =test_id
student_detail
- > PK =Student_id
test_records
- > PK =test_record_id
(子表)
表4是子表,1,2,3是主表。
我想test_records
表有三個外鍵從每個主表,但它顯示如下錯誤。 (我使用MySQL工作臺5.0和MySQL服務器)
ERROR 1005: Can't create table 'test_center.#sql-aa4_12' (errno: 121)
SQL語句:
ALTER TABLE `test_center`.`test_records`
ADD CONSTRAINT `Student_id`
FOREIGN KEY (`Student_id`)
REFERENCES `test_center`.`student_detail` (`Student_id`)
ON DELETE CASCADE
ON UPDATE CASCADE,
ADD CONSTRAINT `test_id`
FOREIGN KEY (`test_id`)
REFERENCES `test_center`.`test_metadata` (`test_id`)
ON DELETE CASCADE
ON UPDATE CASCADE,
ADD CONSTRAINT `u_id`
FOREIGN KEY (`u_id`)
REFERENCES `test_center`.`test_user` (`u_id`)
ON DELETE CASCADE
ON UPDATE CASCADE
ERROR: Error when running failback script. Details follow. ERROR 1046: No database selected
SQL語句:
CREATE TABLE `test_records` (
`test_record_id` int(11) NOT NULL AUTO_INCREMENT,
`test_name` varchar(45) NOT NULL,
`Result` float NOT NULL,
`status` varchar(45) NOT NULL,
`Student_id` varchar(45) NOT NULL,
`u_id` int(11) NOT NULL,
`test_id` int(11) NOT NULL,`enter code here`
PRIMARY KEY (`test_record_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
執行'use mydb;'首先選擇mydb作爲默認數據庫。 – Johan