使用一个SQL查询将数据插入多个表

时间:2016-05-12 13:13:56

标签: sql postgresql postgresql-9.3 postgresql-9.2

我想使用这些表将数据存储到PostgreSQL

CREATE TABLE SETTING
    (`id` int, `description` varchar(16)
     , `constrained` varchar(5), `data_type` varchar(12)
     , `min_value` varchar(6) NULL , `max_value` varchar(6) NULL)
;

INSERT INTO SETTING
    (`id`, `description`, `constrained`, `data_type`, `min_value`, `max_value`)
VALUES
    (10, 'Favourite Colour', 'true', 'alphanumeric', NULL, NULL),
    (11, 'Item Max Limit', 'false', 'integer', '0', '9001'),
    (12, 'Item Min Limit', 'false', 'integer', '0', '9000')
;

CREATE TABLE ALLOWED_SETTING_VALUE
    (`id` int, `setting_id` int, `item_value` varchar(7)
     , `caption` varchar(6))
;

INSERT INTO ALLOWED_SETTING_VALUE
    (`id`, `setting_id`, `item_value`, `caption`)
VALUES
    (123, 10, '#0000FF', 'Blue'),
    (124, 10, '#FFFF00', 'Yellow'),
    (125, 10, '#FF00FF', 'Pink')
;

CREATE TABLE USER_SETTING
    (`id` int, `user_id` int, `setting_id` int
     , `allowed_setting_value_id` varchar(6) NULL
     , `unconstrained_value` varchar(6) NULL)
;

INSERT INTO USER_SETTING
    (`id`, `user_id`, `setting_id`, `allowed_setting_value_id`, `unconstrained_value`)
VALUES
    (5678, 234, 10, '124', NULL),
    (7890, 234, 11, NULL, '100'),
    (8901, 234, 12, NULL, '1')
;

我可以使用一个SQL查询将数据插入到所有表中吗?例如,带有一个SQL查询的多个SQL插入语句?

这在PostgreSQL中是否可行?

1 个答案:

答案 0 :(得分:1)

数据与级联插入太不相似,但如果它更规则(例如,如果您的颜色对于每个设置而言是一个而不是相同设置中的三个,那么有一个用户设置为每个设置)然后它会更有意义。但是,可以这样做(虽然这很愚蠢):

CREATE temp TABLE SETTING (id int, description varchar(16), constrained varchar(5), data_type varchar(12), min_value varchar(6) NULL , max_value varchar(6) NULL);
CREATE TABLE ALLOWED_SETTING_VALUE (id int, setting_id int, item_value varchar(7), caption varchar(6));
CREATE TABLE USER_SETTING (id int, user_id int, setting_id int, allowed_setting_value_id varchar(6) NULL, unconstrained_value varchar(6) NULL);

-- the following is a single statement that inserts all 9 records
with
  allowed AS (
    INSERT INTO ALLOWED_SETTING_VALUE (id, setting_id, item_value, caption)
    VALUES (123, 10, '#0000FF', 'Blue'), (124, 10, '#FFFF00', 'Yellow'), (125, 10, '#FF00FF', 'Pink')
    RETURNING *),
  data(id, description, constrained, data_type, min_value, max_value, us_id, user_id, allowed_setting_value_id, unconstrained_value) as (
    VALUES
        (10, 'Favourite Colour', 'true', 'alphanumeric', NULL, NULL, 5678, 234, (select id::text from allowed where caption = 'Yellow'), NULL),
        (11, 'Item Max Limit', 'false', 'integer', '0', '9001', 7890, 234, NULL, '100'),
        (12, 'Item Min Limit', 'false', 'integer', '0', '9000', 8901, 234, NULL, '1')),
  settings as (
    INSERT INTO SETTING (id, description, constrained, data_type, min_value, max_value)
    SELECT id, description, constrained, data_type, min_value, max_value
    FROM data
    RETURNING *)
INSERT INTO USER_SETTING (id, user_id, setting_id, allowed_setting_value_id, unconstrained_value)
SELECT d.us_id, d.user_id, s.id, d.allowed_setting_value_id, d.unconstrained_value
FROM settings s
JOIN data d ON (d.id = s.id);

让整个事物像单个语句一样运行的一种方法是将它包含在DO命令中:

DO $do$ BEGIN
  ... the whole script here ...
END; $do$;