teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

1

回答を追記

2017/01/23 18:32

投稿

KiyoshiMotoki
KiyoshiMotoki

スコア4791

answer CHANGED
@@ -29,4 +29,23 @@
29
29
  ```sql
30
30
  mysql> SELECT * FROM ;
31
31
  ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1
32
- ```
32
+ ```
33
+
34
+ # 追記
35
+
36
+ > Cannot add foreign key constraint
37
+
38
+ 参照するカラム(`reservable_room.room_id`)と参照されるカラム(`meeting_room.room_id`)のデータ型が異なっているためです。
39
+ [https://dev.mysql.com/doc/refman/5.6/ja/create-table-foreign-keys.html](https://dev.mysql.com/doc/refman/5.6/ja/create-table-foreign-keys.html)
40
+ > 外部キー内の対応するカラムと、参照されるキーは同様のデータ型を持っている必要があります。整数型のサイズと符号が同じである必要があります。
41
+
42
+ `meeting_room.room_id`カラムのデータ型を INT4 に変更するか、`reservable_room.room_id`カラムのデータ型を BIGINT UNSIGNED に変更してください。
43
+ [https://dev.mysql.com/doc/refman/5.6/ja/numeric-type-overview.html](https://dev.mysql.com/doc/refman/5.6/ja/numeric-type-overview.html)
44
+ > SERIAL は BIGINT UNSIGNED NOT NULL AUTO_INCREMENT UNIQUE のエイリアスです。
45
+
46
+ ---
47
+ ちなみに
48
+ > なぜ”Cannot add foreign key constraint”になるのかが不明です。
49
+
50
+ とのことですが、たいていの場合、エラーメッセージで検索すればすぐに原因と解決策を見つけられます。
51
+ [https://www.google.com/search?q=Cannot%20add%20foreign%20key%20constraint](https://www.google.com/search?q=Cannot%20add%20foreign%20key%20constraint)