0

MySQL 5 を使用して 2 つのテーブルを作成しようとしています。以下に 2 つのテーブルを示します。

DROP TABLE IF EXISTS  `users` ; 
CREATE TABLE IF NOT EXISTS  `users` (
  `username` VARCHAR(50) not null ,
  `password` VARCHAR(50) not null,      
  `enabled` boolean not null,
  `accountNonExpired` boolean not null,
  `accountNonLocked` boolean not null,
  `credentialsNonExpired` boolean not null, 
  PRIMARY KEY (`username`)
  ) ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8;


DROP TABLE IF EXISTS  `authorities` ;
create table IF NOT EXISTS `authorities` (
`username` VARCHAR(50) not null ,
`authority` varchar(50) not null,
foreign key (`username`) references `users` (`username`),
unique index authorities_idx_1 (username, authority)
) engine = InnoDb;

このステートメントを実行しようとすると、users テーブルが作成されますが、エラーが発生します。

 Error Code: 1005 
 Can't create table 'dental.authorities' (errno: 150)

参照される 2 つの列が同一である場合に、この外部キーが失敗する理由がわかりません。ある

4

3 に答える 3

1

外部キーでは、両方のキーが同じ文字セットを持つ必要があります。追加

DEFAULT CHARACTER SET = utf8;

2 番目のテーブル CREATE 命令に追加します。

編集: ああ、私はパーティーに遅れたようです.

于 2013-07-12T12:04:07.953 に答える
0

次の点を確認してください。

DEFAULT CHARACTER SET = utf8;2番目のテーブルには提供されていないと思います

1. The two tables must be ENGINE=InnoDB. (can be others: ENGINE=MyISAM
    works too)
 2. The two tables must have the same charset.
 3. The PK column(s) in the parent table and the FK column(s) must be
    the same data type.
 4. The PK column(s) in the parent table and the FK column(s), if they
    have a define collation type, must have the same collation type;
 5. If there is data already in the foreign key table, the FK column
    value(s) must match values in the parent table PK columns.
 6. And the child table cannot be a temporary table.

お役に立てれば。

于 2013-07-12T12:03:22.430 に答える