0
我們被要求創建一個數據庫,然後創建一個表格,然後填充它。 這裏是我想出迄今:Failed Constraint - 如何在sqlite3表中正確插入某些內容?
sqlite3 me5.db;
create table petID(pet_id int(3) PRIMARY KEY, pet_name varchar(10), pet_type varchar(10), pet_age int(3));
INSERT INTO petID(pet_id, pet_name, pet_type, pet_age) VALUES(246, 'ROVER', 'DOG', 12);
INSERT INTO petID(pet_id, pet_name, pet_type, pet_age) VALUES(298, 'SPOT', 'DOG', 2);
INSERT INTO petID(pet_id, pet_name, pet_type, pet_age) VALUES(341, 'MORRIS', 'CAT', 4);
INSERT INTO petID(pet_id, pet_name, pet_type, pet_age) VALUES(519, 'TWEEDY', 'BIRD', 2);
create table petowner(pet_id int(3), owner varchar(100) PRIMARY KEY, FOREIGN KEY(pet_id) REFERENCES petID(pet_id) ON DELETE CASCADE);
INSERT INTO petowner(pet_id, owner) VALUES(246, 'SAM COOK');
INSERT INTO petowner(pet_id, owner) VALUES(298, 'TERRY KIM');
然而,當我插入下面這兩個:
INSERT INTO petowner(pet_id, owner) VALUES(341, 'SAM COOK');
INSERT INTO petowner(pet_id, owner) VALUES(519, 'TERRY KIM');
我得到一個「約束失敗」的錯誤。任何想法如何解決這一問題?謝謝