我们被要求创建一个数据库,然后是一个表,然后用givens填充它。 这是我到目前为止所提出的:
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');
我收到'约束失败'错误。任何想法如何解决这一问题?感谢
答案 0 :(得分:0)
您违反的约束是petowner
的主键;它会阻止'SAM COOK'
出现两次。
如果宠物可以拥有多个所有者,那么petowner
表的主键必须包含两个列:
CREATE TABLE petowner(
pet_id int(3),
owner varchar(100),
PRIMARY KEY(pet_id, owner),
FOREIGN KEY(pet_id) REFERENCES petID(pet_id) ON DELETE CASCADE
);