Postgres를 사용하여 한 번에 3 개의 테이블에 데이터 삽입
단일 쿼리로 3 개의 테이블에 데이터를 삽입하고 싶습니다.
내 테이블은 다음과 같습니다.
CREATE TABLE sample (
id bigserial PRIMARY KEY,
lastname varchar(20),
firstname varchar(20)
);
CREATE TABLE sample1(
user_id bigserial PRIMARY KEY,
sample_id bigint REFERENCES sample,
adddetails varchar(20)
);
CREATE TABLE sample2(
id bigserial PRIMARY KEY,
user_id bigint REFERENCES sample1,
value varchar(10)
);
모든 삽입에 대한 대가로 키를 얻고 다음 테이블에 해당 키를 삽입해야합니다.
내 질문은 다음과 같습니다.
insert into sample(firstname,lastname) values('fai55','shaggk') RETURNING id;
insert into sample1(sample_id, adddetails) values($id,'ss') RETURNING user_id;
insert into sample2(user_id, value) values($id,'ss') RETURNING id;
그러나 단일 쿼리를 실행하면 값을 반환하고 다음 쿼리에서 즉시 재사용 할 수 없습니다.
이것을 달성하는 방법?
사용 열팽창 계수를 데이터 - 수정 :
WITH ins1 AS (
INSERT INTO sample(firstname, lastname)
VALUES ('fai55', 'shaggk')
-- ON CONFLICT DO NOTHING -- optional addition in Postgres 9.5+
RETURNING id AS user_id
)
, ins2 AS (
INSERT INTO sample1 (user_id, adddetails)
SELECT user_id, 'ss' FROM ins1
-- RETURNING user_id -- only if used in turn
)
INSERT INTO sample2 (user_id, value) -- same here
SELECT user_id, 'ss' FROM ins1;
각각 INSERT
은 이전에 의존합니다. SELECT
대신 VALUES
이전에서 반환 된 행이없는 경우 보조 테이블에 아무것도 삽입되지 않도록합니다 INSERT
. (관련 : ON CONFLICT
Postgres 9.5+ 의 절)
또한이 방법은 조금 더 짧고 빠릅니다.
일반적으로 전체 데이터 행을 한 곳에서 제공하는 것이 더 편리합니다 .
WITH data(firstname, lastname, adddetails, value) AS (
VALUES -- provide data here
(text 'fai55', text 'shaggk', text 'ss', text 'ss2') -- see below
-- more? -- works for multiple input rows
)
, ins1 AS (
INSERT INTO sample (firstname, lastname)
SELECT firstname, lastname FROM data -- DISTINCT? see below
ON CONFLICT DO NOTHING -- requires UNIQUE constraint
RETURNING firstname, lastname, id AS sample_id
)
, ins2 AS (
INSERT INTO sample1 (sample_id, adddetails)
SELECT sample_id, adddetails
FROM data
JOIN ins1 USING (firstname, lastname)
RETURNING sample_id, user_id
)
INSERT INTO sample2 (user_id, value)
SELECT user_id, value
FROM data
JOIN ins1 USING (firstname, lastname)
JOIN ins2 USING (sample_id);
독립형 VALUES
표현식 에서 명시 적 유형 캐스트가 필요할 수 있습니다 . 에 VALUES
첨부 된 표현식 과 반대로 INSERT
, 데이터 유형은 대상 테이블에서 파생됩니다.
여러 행이 동일하게 올 수있는 경우 (firstname, lastname)
첫 번째에 대해 중복을 접어야 할 수 있습니다 INSERT
.
...
INSERT INTO sample (firstname, lastname)
SELECT DISTINCT firstname, lastname FROM data
...
CTE 대신 (임시) 테이블을 데이터 소스로 사용할 수 있습니다 data
.
관련, 자세한 내용 :
이 같은
with first_insert as (
insert into sample(firstname,lastname)
values('fai55','shaggk')
RETURNING id
),
second_insert as (
insert into sample1( id ,adddetails)
values
( (select id from first_insert), 'ss')
RETURNING user_id
)
insert into sample2 ( id ,adddetails)
values
( (select user_id from first_insert), 'ss');
삽입에서 생성 된 ID sample2
가 필요하지 않으므로 returning
마지막 삽입에서 절을 제거했습니다 .
Typically, you'd use a transaction to avoid writing complicated queries.
http://www.postgresql.org/docs/current/static/sql-begin.html
http://dev.mysql.com/doc/refman/5.7/en/commit.html
You could also use a CTE, assuming your Postgres tag is correct. For instance:
with sample_ids as (
insert into sample(firstname, lastname)
values('fai55','shaggk')
RETURNING id
), sample1_ids as (
insert into sample1(id, adddetails)
select id,'ss'
from sample_ids
RETURNING id, user_id
)
insert into sample2(id, user_id, value)
select id, user_id, 'val'
from sample1_ids
RETURNING id, user_id;
You could create an after insert trigger on the Sample table to insert into the other two tables.
The only issue i see with doing this is that you wont have a way of inserting adddetails it will always be empty or in this case ss. There is no way to insert a column into sample thats not actualy in the sample table so you cant send it along with the innital insert.
Another option would be to create a stored procedure to run your inserts.
You have the question taged mysql and postgressql which database are we talking about here?
참고URL : https://stackoverflow.com/questions/20561254/insert-data-in-3-tables-at-a-time-using-postgres
'UFO ET IT' 카테고리의 다른 글
IoC 컨테이너 및 종속성 주입 이해 (0) | 2020.12.11 |
---|---|
AngularJS의 순환 종속성 및 OOP 문제 (0) | 2020.12.11 |
Android 툴바 : 가로 모드의 작은 제목 텍스트 (0) | 2020.12.11 |
Microsoft.AspNet.Http.HttpRequest에서 원시 URL 가져 오기 (0) | 2020.12.11 |
설치된 웹팩 버전을 확인하는 방법 (0) | 2020.12.11 |