不能在mysql中创建表,因为它们都相互依赖?

时间:2022-09-09 14:12:56

I have one table Supplier that has foreign key RepID, and another called Rep that has foreign key SupplierID. How do I get around this? If I disable foreign key checking I get an error saying a foreign key constraint fails when trying to add data:

我有一个具有外键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");

2 个解决方案

#1


0  

You can disable the foregin key check with this:

您可以使用以下命令禁用foregin密钥检查:

SET foreign_key_checks = 0;

CREATE TABLES ......;

SET foreign_key_checks = 1;

#2


0  

Add the first table without foreign keys, then the second with all keys, then create the foreign key on the first table.

添加没有外键的第一个表,然后添加第二个包含所有键,然后在第一个表上创建外键。

#1


0  

You can disable the foregin key check with this:

您可以使用以下命令禁用foregin密钥检查:

SET foreign_key_checks = 0;

CREATE TABLES ......;

SET foreign_key_checks = 1;

#2


0  

Add the first table without foreign keys, then the second with all keys, then create the foreign key on the first table.

添加没有外键的第一个表,然后添加第二个包含所有键,然后在第一个表上创建外键。