前提
初質問なので不足情報あればぜひとも教えてください。
phpmyadminでmysqlを使用してテーブルを作成しようとしていますが、外部キー制約にエラーが出ます。
実現したいこと
- 複合主キーを外部キーとした複合主キーのテーブルを作る
発生している問題・エラーメッセージ
#1215 - Cannot add foreign key constraint
該当のソースコード
mysql
1CREATE TABLE customers( 2 customers_mail VARCHAR(191) PRIMARY KEY, 3 customers_pass VARCHAR(191) NOT NULL, 4 customers_nick VARCHAR(191) NOT NULL, 5 customers_ln VARCHAR(191) NOT NULL, 6 customers_fn VARCHAR(191) NOT NULL, 7 customers_postcode CHAR(7) NOT NULL, 8 customers_pref VARCHAR(191) NOT NULL, 9 customers_address VARCHAR(191) NOT NULL, 10 customers_subaddress VARCHAR(191) 11); 12CREATE TABLE makers( 13 makers_id INT PRIMARY KEY, 14 makers_name VARCHAR(191) NOT NULL 15); 16CREATE TABLE buys( 17 buys_id INT PRIMARY KEY, 18 buys_date DATE NOT NULL, 19 customers_mail VARCHAR(191) NOT NULL, 20 buys_payment INT NOT NULL, 21 FOREIGN KEY fkey1 (customers_mail) REFERENCES customers(customers_mail) 22); 23CREATE TABLE items( 24 items_id INT PRIMARY KEY, 25 makers_id INT NOT NULL, 26 items_name VARCHAR(191) NOT NULL, 27 items_description VARCHAR(191) NOT NULL, 28 items_price INT NOT NULL, 29 items_url VARCHAR(191) NOT NULL, 30 items_genre VARCHAR(191) NOT NULL, 31 items_material VARCHAR(191) NOT NULL, 32 items_color VARCHAR(191) NOT NULL, 33 FOREIGN KEY fkey2 (makers_id) REFERENCES makers(makers_id) 34); 35CREATE TABLE itemsinfo( 36 items_id INT, 37 itemsinfo_size VARCHAR(191), 38 itemsinfo_stock INT NOT NULL, 39 FOREIGN KEY fkey3 (items_id) REFERENCES items(items_id), 40 PRIMARY KEY(items_id, itemsinfo_size) 41); 42CREATE TABLE details( 43 buys_id INT, 44 items_id INT, 45 itemsinfo_size VARCHAR(191), 46 details_vol INT NOT NULL, 47 FOREIGN KEY fkey4 (buys_id) REFERENCES buys(buys_id), 48 FOREIGN KEY fkey5 (items_id) REFERENCES itemsinfo(items_id), 49 FOREIGN KEY fkey6 (itemsinfo_size) REFERENCES itemsinfo(itemsinfo_size), 50 PRIMARY KEY(buys_id, items_id, itemsinfo_size) 51);
試したこと
参照元が主キーである(=一意制約)ことを確認
参照元と参照先の型が同じであることを確認
補足情報(FW/ツールのバージョンなど)
lolipopのphpmyadminです。
xamppのphpmyadminで試した結果、次のエラーとログが出力されました。
#1005 - `webdb`.`details` テーブルが作れません.(errno: 150 "Foreign key constraint is incorrectly formed")
------------------------ LATEST FOREIGN KEY ERROR ------------------------ 2022-10-29 14:11:39 0x3600 Error in foreign key constraint of table `webdb`.`details`: FOREIGN KEY fkey6 (itemsinfo_size) REFERENCES itemsinfo(itemsinfo_size), PRIMARY KEY(buys_id, items_id, itemsinfo_size) ): Cannot find an index in the referenced table where the referenced columns appear as the first columns, or column types in the table and the referenced table do not match for constraint. Note that the internal storage type of ENUM and SET changed in tables created with >= InnoDB-4.1.12, and such columns in old tables cannot be referenced by such columns in new tables. Please refer to https://mariadb.com/kb/en/library/foreign-keys/ for correct foreign key definition. Create table `webdb`.`details` with foreign key constraint failed. There is no index in the referenced table where the referenced columns appear as the first columns near 'FOREIGN KEY fkey6 (itemsinfo_size) REFERENCES itemsinfo(itemsinfo_size), PRIMARY KEY(buys_id, items_id, itemsinfo_size) )'.
回答3件
あなたの回答
tips
プレビュー