2016-11-05 15 views
0

基本的な外部制約を作成しようとしていますが、構文エラーが発生しています。 #1005 - Can't create table 'my_database'.'#sql-334f_952bc' (errno: 150 "Foreign key constraint is incorrectly formed")外部キーを作成しようとしたときのエラー(1005)

まず、テーブルを作成してから、「alter table」メソッドを使用して外部制約を作成します。

テーブルの作成:誰もがここで私はそれを大幅に感謝し、問題を特定することができれば

ALTER TABLE `tbl_aircraft` 
ADD CONSTRAINT `fk_aircraft_id` FOREIGN KEY (`aircraft_id`) 
REFERENCES `my_database`.`tbl_flights` (`aircraft_id`) 
ON DELETE RESTRICT ON UPDATE CASCADE ; 

CREATE TABLE `tbl_flights` (
    `flight_id`  int(11)  NOT NULL AUTO_INCREMENT 
    `aircraft_id` int(11)  NOT NULL 
    `date`   date   NOT NULL 
    `auth_by`  varchar(255) NOT NULL 
    `auth_duration` time   NOT NULL 
    PRIMARY KEY (`flight_id`) 
) 
; 

CREATE TABLE `tbl_aircraft` (
    `aircraft_id` int(11) NOT NULL AUTO_INCREMENT 
    `registration` char(6) NOT NULL 
    `insurance`  date NOT NULL 
    `awrc`   date NOT NULL 
    PRIMARY KEY (`aircraft_id`) 
) 
; 

が外部キー/制約を作成します。

答えて

0

ベースにエラーメッセージ が見つかりませんでした。文の終わりに

CREATE TABLE `tbl_flights` (
    `flight_id`  int(11)  NOT NULL AUTO_INCREMENT 
    `aircraft_id` int(11)  NOT NULL 
    `date`   date   NOT NULL 
    `auth_by`  varchar(255) NOT NULL 
    `auth_duration` time   NOT NULL 
    PRIMARY KEY (`flight_id`) 
) 
; /* add this */ 

CREATE TABLE `tbl_aircraft` (
    `aircraft_id` int(11) NOT NULL AUTO_INCREMENT 
    `registration` char(6) NOT NULL 
    `insurance`  date NOT NULL 
    `awrc`   date NOT NULL 
    PRIMARY KEY (`aircraft_id`) 
) 
; /* add this*/ 

あなたがテーブル

ALTER TABLE `tbl_flights` 
ADD CONSTRAINT `fk_aircraft_id` FOREIGN KEY (`aircraft_id`) 
REFERENCES `my_database`.`tbl_aircraft` (`aircraft_id`) 
ON DELETE RESTRICT ON UPDATE CASCADE ; 

を反転しているか、あなたがテーブルの外部列を追加する必要があります可能性がありtbl_aircraft

+0

は私が追加、ありがとう ';'と今では別のエラーが発生しています:errno:150 "外部キー制約が正しく形成されていません" –

+0

これはエラーメッセージが解決されたことを意味します。+ – scaisEdge

+0

まだ問題があります。私は質問を更新しました。 –