2

Mysqlワークベンチで外部キーを設定しようとしています。関係を設定しようとしているテーブルの主キーと同じ名前を外部キーに使用しました。すでに1つのリレーションを別のテーブルにこのように設定していますが、このテーブルに変更を適用しようとすると、スクリプトでエラーが発生します。

エラー1005:テーブル'X.#sql-718_a'を作成できません(errno:121)

SQLステートメント:

ALTER TABLE `X`.`X_use`    
ADD CONSTRAINT `XyzID`   
FOREIGN KEY (`XyzID` )   REFERENCES `X`.`Xyz` (`XyzID` )   
ON DELETE NO ACTION   O
N UPDATE NO ACTION , 
ADD INDEX `XyzID` (`XyzID` ASC)  , 

ただし、外部キー名を「AbcID」に変更すると、外部キー関係の設定に問題はありません。それはなぜですか。また、あるテーブルの主キー名をこのテーブルの外部キーと同じにできないのはなぜですか。私は以前にそのような関係を設定しましたが、このテーブルではできません。

4

2 に答える 2

3

Constraint names have to be unique within the database.

That (errno: 121) in the error message means that MySQL encountered a duplicate key exception. The usual cause of this is that there is already a constraint of the same name in the database.

This "unique name" requirement is one reason why the normative pattern is to include the table name when constructing the name of the foreign key constraint. e.g. FK_table_cols e.g. FK_X_use_XyzID.

Why must the constraint name be unique within the database? That's a question for the dbms designers.

But consider this: when the database encounters a constraint violation, it throws an error that contains the name of the constraint. When that constraint name references only one constraint in the database, that makes locating the problem a bit easier.

于 2013-01-24T07:10:20.517 に答える
0

DBのACIDプロパティで説明されているように、データベース全体で同じ制約名を使用することはできません。

于 2013-01-24T07:13:33.807 に答える