仅当外键(在本例中为模型)存在时,如何在具有外键引用的表中插入新行?
目前我有以下声明:
INSERT INTO furniture (model, type) VALUES (modelA, chair)
Run Code Online (Sandbox Code Playgroud)
如果FK不存在,请使用不返回任何内容的SELECT.
INSERT INTO furniture (model, type)
select 'modelA', 'chair'
where exists (select *
from model
where model.model = 'modelA');
Run Code Online (Sandbox Code Playgroud)
您没有告诉我们调用引用的表是什么.我认为是model- 你需要调整到真实姓名.