我有一个包含外键RepID的表供应商,另一个名为具有外键供应商ID的代表。我该如何解决这个问题?如果我禁用外键检查,我会在尝试添加数据时收到错误,说明外键约束失败:
INSERT INTO Supplier VALUES
(44, "PENCO", "43", "Brown road", "Penville", "P14 5AA", "07816272182"),
(13, "PAPERCO", "1", "Old road", "Paperville", "P34 5NA", "07111232289"),
(24, "CALCCO", "12", "London road", "Calcville", "CA4 1XX", "07615282615"),
(8, "BAGCO", "10", "New road", "Bagville", "BA1 1AM", "07817827131");
INSERT INTO Item VALUES
(1, "Ballpoint pen", 1.00, "A nice pen", "~/Pictures/Pen", 20, 44),
(2, "Notepad", 3.20, "A nice notepad", "~/Pictures/Notepad", 21, 13),
(3, "Calculator", 4.50, "A nice calculator", "~/Pictures/Calculator", 22, 24),
(4, "Backpack", 10.00, "A nice backpack", "~/Pictures/Backpack", 23, 8);
INSERT INTO Rep VALUES
(41, "John", "Repman", "07019827336", 44, "JR@gmail.com", "JR@msn.com", "JRMAN", "1", "Potato street", "Patatoville", "P01 1FX"),
(11, "Fake", "Name", "07817001992", 13, "FN@gmail.com", "FN@msn.com", "FNMAN", "3", "Fake street", "Fakeville", "FA3 9KA"),
(1, "Good", "Name", "07911876678", 24, "GN@gmail.com", "GN@msn.com", "GNMAN", "12", "Good street", "Goodville", "GO0 1DD"),
(666, "John", "Smith", "07918822191", 8, "JS@gmail.com", "JS@msn.com", "JSMAN", "666", "Boring street", "Boringville", "BO1 1LM");
答案 0 :(得分:0)
您可以使用以下方法禁用外观密钥检查:
SET foreign_key_checks = 0;
CREATE TABLES ......;
SET foreign_key_checks = 1;
答案 1 :(得分:0)
添加没有外键的第一个表,然后添加第二个包含所有键的表,然后在第一个表上创建外键。