MySQL 中 INSERTion 的顺序是否相关?
Is relevant the order of INSERTion in MySQL?
我有多个表,由多个外键关联,如下例所示:
Chefs(id_chef,name,stars)
- 其中 id_chef
是主键。
Recipes(id_recipe,name,calories,category)
- id_recipe
作为PK。
Ingredients(id_ing,name,type)
- id_ing
作为PK。
Contains(id_chef,id_recipe,quantity,unit)
- (id_chef,id_recipe)
作为 PK,作为 Recipes(id_recipe)
和 Chefs(id_chef)
. 的外键
Autorship(id_chef,id_recipe)
- 与之前相同。
Users(user_id,name)
- user_id
作为PK。
Evaluation(user_id,id_recipe,stars)
- (user_id,id_recipe)
作为 User(user_id)
和 Recipes(id_recipe)
的 PK 和外键。
问题是,现在我想用一个脚本在所有这些表中插入一些数据,我是否必须按照特定顺序执行并遵守某些条件,还是无所谓?
谢谢。
Alexander,您说得对,您不能引用尚不存在的内容。但是当您 运行 它时,您的脚本将需要一些计划,因此插入顺序会有些影响。您要确保在加载引用键之前加载所有引用的 tables(主键)。
当我计划我的加载脚本时,我安排我的 table 以便首先加载引用最多的脚本,然后再加载所有其他脚本。
我注意到 'Chefs' table 被引用了两次。在加载 'Contains' 和 'Autorship' table 之前,您需要先加载 table。这同样适用于 'Recipes' table.
我可能会使用我的脚本首先加载 Chefs、Autorship 和 Users,然后再加载其余部分。
希望对您有所帮助。
我有多个表,由多个外键关联,如下例所示:
Chefs(id_chef,name,stars)
- 其中id_chef
是主键。Recipes(id_recipe,name,calories,category)
-id_recipe
作为PK。Ingredients(id_ing,name,type)
-id_ing
作为PK。Contains(id_chef,id_recipe,quantity,unit)
-(id_chef,id_recipe)
作为 PK,作为Recipes(id_recipe)
和Chefs(id_chef)
. 的外键
Autorship(id_chef,id_recipe)
- 与之前相同。Users(user_id,name)
-user_id
作为PK。Evaluation(user_id,id_recipe,stars)
-(user_id,id_recipe)
作为User(user_id)
和Recipes(id_recipe)
的 PK 和外键。
问题是,现在我想用一个脚本在所有这些表中插入一些数据,我是否必须按照特定顺序执行并遵守某些条件,还是无所谓?
谢谢。
Alexander,您说得对,您不能引用尚不存在的内容。但是当您 运行 它时,您的脚本将需要一些计划,因此插入顺序会有些影响。您要确保在加载引用键之前加载所有引用的 tables(主键)。
当我计划我的加载脚本时,我安排我的 table 以便首先加载引用最多的脚本,然后再加载所有其他脚本。
我注意到 'Chefs' table 被引用了两次。在加载 'Contains' 和 'Autorship' table 之前,您需要先加载 table。这同样适用于 'Recipes' table.
我可能会使用我的脚本首先加载 Chefs、Autorship 和 Users,然后再加载其余部分。
希望对您有所帮助。