여기 데이터베이스에 test_center
이라는 4 개의 테이블이 있습니다. 그것들은 :세 개의 마스터 테이블이있는 동일한 하위 테이블에 세 개의 외래 키를 추가하는 방법은 무엇입니까?
test_user
-> = PKu_id
test_metadata
-> = PKtest_id
student_detail
-> PK =Student_id
test_records
-> = PKtest_record_id
(자식 테이블)
표 4는 하위 테이블이며 1, 2, 3은 마스터입니다.
test_records
테이블에 각 마스터 테이블에서 세 개의 외래 키가 있어야하지만 다음과 같이 몇 가지 오류가 표시됩니다.
ERROR 1005: Can't create table 'test_center.#sql-aa4_12' (errno: 121)
SQL 문 (I는 MySQL의 워크 벤치 5.0 MySQL 서버를 사용하고 있습니다) :
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
mydb를 기본 db로 선택하려면 먼저 'use mydb;'를 실행하십시오. – Johan