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