2012-12-07 3 views
1

내 오류를 찾는데 도와주세요,하지만 난 오류 얻을하십시오 : 여기 내가 잘못 여기서 뭘하는지 모르겠어요이 MySQL의 코드

Error Code: 1005. Can't create table 'erm.section' (emo:150) 

코드입니다. '코스'테이블이 성공적으로 생성됩니다. course_number attritube의 이름을 'section'테이블에서 수정하려고 시도했지만 작동하지 않았습니다.

USE erm; 

    CREATE TABLE course 
    (
     course_name   VARCHAR(30)  NOT NULL, 
     course_number  VARCHAR(20)  NOT NULL, 
     credit_hours  INT    NOT NULL, 
     department   VARCHAR(10), 
     CONSTRAINT course_pk PRIMARY KEY (course_name) 
    ); 

    CREATE TABLE section 
    (
     section_identifier  INT     NOT NULL, 
     course_number   VARCHAR(20), 
     semester    VARCHAR(10)   NOT NULL, 
     school_year    VARCHAR(4)   NOT NULL, 
     instructor    VARCHAR(25), 
     CONSTRAINT section_pk PRIMARY KEY (section_identifier), 
     CONSTRAINT section_fk FOREIGN KEY (course_number) 
      REFERENCES course (course_number) 
      ON DELETE  SET NULL 
      ON UPDATE  CASCADE 
    ); 

답변

1

course_number는 테이블 코스의 기본 키가 아닙니다.

테이블 섹션의 외래 키는 다른 테이블의 기본 키를 참조해야합니다.

1

당신은 외래 키에 의해 참조되는 컬럼에 인덱스를 생성해야합니다 :

alter table course add index (course_number); 

(그것은 기본 키 인덱스 필요는 없습니다.)이 MySQL documentation에서 :

InnoDB permits a foreign key to reference any index column or group of columns. However, in the referenced table, there must be an index where the referenced columns are listed as the first columns in the same order.

0
A FOREIGN KEY in one table points to a PRIMARY KEY in another table. 

AFAIK, CONSTRAINT을 삭제 한 다음 이름을 바꾼 다음 CONSTRAINT을 다시 추가하는 것이 유일한 방법입니다. 먼저 백업!

는이

ALTER TABLE section 
ADD FOREIGN KEY (course_number) 
REFERENCES course (course_number) 
을 사용하여이

ALTER TABLE section 
    DROP FOREIGN KEY course_number 

를 사용하고 다시 추가 드롭합니다

관련 문제