2016-06-22 30 views
0

外部キーを適用する際に「外部キー制約を追加できません」というエラーが表示されています。mysqlに外部キー制約を追加できません

それ以外の場合、クエリは完全に正常に機能します。これまでMySQLクエリの構文にこれまでどおり何の問題もないようです。

 //Department Table 
       query = "CREATE TABLE IF NOT EXISTS department " 
        + "(" 
        + "dept_id INT PRIMARY KEY AUTO_INCREMENT," 
        + "dept_name VARCHAR(40) NOT NULL" 
        + ")ENGINE=InnoDB"; 
       command = new MySqlCommand(query, connection); 
       command.ExecuteNonQuery(); 

       //Designation Table 
       query = "CREATE TABLE IF NOT EXISTS designation " 
        + "(" 
        + "desig_id INT PRIMARY KEY AUTO_INCREMENT," 
        + "designation VARCHAR(40) NOT NULL" 
        + ")ENGINE=InnoDB"; 
       command = new MySqlCommand(query, connection); 
       command.ExecuteNonQuery(); 

       // PayNature Table 
       query = "CREATE TABLE IF NOT EXISTS paynature " 
        + "(" 
        + "paynature_id INT PRIMARY KEY AUTO_INCREMENT," 
        + "pay_nature VARCHAR(40) NOT NULL" 
        + ")ENGINE=InnoDB"; 
       command = new MySqlCommand(query, connection); 
       command.ExecuteNonQuery(); 

       // Employee Type 
       query = "CREATE TABLE IF NOT EXISTS employeetype " 
        + "(" 
        + "emptype_id INT PRIMARY KEY AUTO_INCREMENT," 
        + "emp_type VARCHAR(40) NOT NULL" 
        + ")ENGINE=InnoDB"; 
       command = new MySqlCommand(query, connection); 
       command.ExecuteNonQuery(); 

       // Pay Scale Table 
       query = "CREATE TABLE IF NOT EXISTS payscale " 
        + "(" 
        + "payscale_id INT PRIMARY KEY AUTO_INCREMENT," 
        + "payscale VARCHAR(40) NOT NULL, " // Optional 
        + "basic_salary DEC(10,3) NOT NULL" 
        + ")ENGINE=InnoDB"; 
       command = new MySqlCommand(query, connection); 
       command.ExecuteNonQuery(); 

       // Employee Table 
       query = "CREATE TABLE IF NOT EXISTS employee " 
        + "(" 
        + "emp_id INT PRIMARY KEY AUTO_INCREMENT," 
        + "emp_name VARCHAR(40) NOT NULL," 
        + "emptype_id_fk VARCHAR(40)," //Optional 
        + "dept_id_fk INT," //Optional 
        + "desig_id_fk INT," //Optional 
        + "payscale_id_fk INT NOT NULL, " 
        + "INDEX(emptype_id_fk, dept_id_fk, desig_id_fk, payscale_id_fk)," 

        //Employee - R - EmployeeType 
        + "FOREIGN KEY(emptype_id_fk)" 
        + "REFERENCES employeetype(emptype_id)" 
        + "ON UPDATE CASCADE," 

        // Employee - R - Department 
        + "FOREIGN KEY(dept_id_fk)" 
        + "REFERENCES department(dept_id)" 
        + "ON DELETE SET NULL ON UPDATE CASCADE," 


        // Employee - R- Designation 
        + "FOREIGN KEY(desig_id_fk)" 
        + "REFERENCES designation(desig_id)" 
        + "ON DELETE SET NULL ON UPDATE CASCADE ," 

        // Employee - R - Payscale 
        + "FOREIGN KEY(payscale_id_fk)" 
        + "REFERENCES payscale(payscale_id)" 
        + "ON DELETE CASCADE ON UPDATE CASCADE" 
        + ")ENGINE=InnoDB"; 

       command = new MySqlCommand(query, connection); 
       command.ExecuteNonQuery(); 
+0

あなたの投稿にエラーが含まれていますか?見えない。 – totalfreakingnoob

+0

こんにちは、テーブルの支払いを作成し、外部キーを定義するときにpayscaleテーブルを参照するようです。それらが同じテーブルであるはずかどうか分からない? – Gilles

答えて

1

ちょうど最初のものを見ました。

"emptype_id_fk VARCHAR(40)," 
..... 
+ "FOREIGN KEY(emptype_id_fk)" 
+ "REFERENCES employeetype(emptype_id)" 
.... 

しかしemployeetypeテーブルのemptype_idフィールドはINT(11)として宣言されて

おそらくあなたはemployeeテーブルのフィールドemptype_id_fkはINTになりたいです。
この変更を行った後、クエリは機能します

+0

はい、まさに私はばかです! ありがとうございます –

関連する問題