需要用外键批量插入
Need to batch INSERT with foreign key
我目前正在使用 CockroachDB,需要批量插入商店类别代码。
这是我当前的 table 架构
CREATE TABLE storecategorycode(
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
isenabled BOOLEAN NOT NULL
);
CREATE TABLE storecategorycodeml(
id UUID PRIMARY KEY DEFAULT
gen_random_uuid(),
storecategorycode_id UUID NOT NULL,
category1 VARCHAR(255) NULL,
category2 VARCHAR(255) NULL,
category3 VARCHAR(255) NULL,
category4 VARCHAR(255) NULL,
category5 VARCHAR(255) NULL,
lang VARCHAR(25) NOT NULL,
CONSTRAINT storecategorycodeml_ibfk_1 FOREIGN KEY (storecategorycode_id)
REFERENCES storecategorycode(id) ON DELETE CASCADE
);
storecategorycodeml('ml' 代表 'multi-language')取决于 storecategorycode_id 外键。
如何插入这样的内容?
INSERT INTO storecategorycodeml
(storecategorycode_id, category1, category2, category3, category4, category5, lang)
VALUES (
INSERT INTO storecategorycode(isenabled) VALUES(true)
RETURNING id,'Restaurant','Restaurant-1','Cafe','In-store','takeout','en'
);
'RETURNING id' 是 cockroachDB 中用来 return 插入 id 的语法。
我可以创建一个脚本,首先在 storecategorycode 中手动创建记录,然后在 storecategorycodeml 中创建记录,但是我将插入数千条记录,我不想手动对这些进行编码。
仅供参考,我之所以有单独的 table 是为了支持多语言。例如,我可以有一个storecategorycode(001, true),001可以有多种语言,比如英文、韩文、中文等
我不知道您的 DBMS 是否支持这种链式 INSERT
语法,但值得一试:
WITH inserted AS (
INSERT INTO storecategorycode(isenabled) VALUES(true)
RETURNING id
)
INSERT INTO storecategorycodeml
(storecategorycode_id, category1, category2, category3, category4, category5, lang)
SELECT id, 'Restaurant','Restaurant-1','Cafe','In-store','takeout','en'
FROM inserted
;
这会先把INSERT
变成storecategorycode
,然后return变成id
值,让你在后面的INSERT
中引用它。
我目前正在使用 CockroachDB,需要批量插入商店类别代码。
这是我当前的 table 架构
CREATE TABLE storecategorycode(
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
isenabled BOOLEAN NOT NULL
);
CREATE TABLE storecategorycodeml(
id UUID PRIMARY KEY DEFAULT
gen_random_uuid(),
storecategorycode_id UUID NOT NULL,
category1 VARCHAR(255) NULL,
category2 VARCHAR(255) NULL,
category3 VARCHAR(255) NULL,
category4 VARCHAR(255) NULL,
category5 VARCHAR(255) NULL,
lang VARCHAR(25) NOT NULL,
CONSTRAINT storecategorycodeml_ibfk_1 FOREIGN KEY (storecategorycode_id)
REFERENCES storecategorycode(id) ON DELETE CASCADE
);
storecategorycodeml('ml' 代表 'multi-language')取决于 storecategorycode_id 外键。
如何插入这样的内容?
INSERT INTO storecategorycodeml
(storecategorycode_id, category1, category2, category3, category4, category5, lang)
VALUES (
INSERT INTO storecategorycode(isenabled) VALUES(true)
RETURNING id,'Restaurant','Restaurant-1','Cafe','In-store','takeout','en'
);
'RETURNING id' 是 cockroachDB 中用来 return 插入 id 的语法。
我可以创建一个脚本,首先在 storecategorycode 中手动创建记录,然后在 storecategorycodeml 中创建记录,但是我将插入数千条记录,我不想手动对这些进行编码。
仅供参考,我之所以有单独的 table 是为了支持多语言。例如,我可以有一个storecategorycode(001, true),001可以有多种语言,比如英文、韩文、中文等
我不知道您的 DBMS 是否支持这种链式 INSERT
语法,但值得一试:
WITH inserted AS (
INSERT INTO storecategorycode(isenabled) VALUES(true)
RETURNING id
)
INSERT INTO storecategorycodeml
(storecategorycode_id, category1, category2, category3, category4, category5, lang)
SELECT id, 'Restaurant','Restaurant-1','Cafe','In-store','takeout','en'
FROM inserted
;
这会先把INSERT
变成storecategorycode
,然后return变成id
值,让你在后面的INSERT
中引用它。