DBAtlas · 데이터베이스 운영 아틀라스

데이터를 INSERT 스크립트로 생성

Oracle

-- DBMS_METADATA for object DDL
SELECT DBMS_METADATA.GET_DDL('TABLE','CUSTOMER') FROM dual;

-- Generating row data as INSERTs is not built in;
-- use SQL Developer 'Export > Insert' or SQLcl 'SET SQLFORMAT INSERT':
-- SET SQLFORMAT INSERT
-- SELECT * FROM customer;

Oracle에는 데이터를 바로 INSERT 스크립트로 변환하는 SQL 명령이 없다. DDL은 DBMS_METADATA.GET_DDL을, 행 INSERT는 SQLcl(SET SQLFORMAT INSERT)이나 SQL Developer Export 마법사를 사용한다.

PostgreSQL

-- Data only, as INSERT statements
pg_dump --data-only --inserts --table=customer shop > insert.sql

-- With column names (more robust to schema differences)
pg_dump --data-only --column-inserts --table=customer shop > insert.sql

pg_dump --inserts는 각 행을 개별 INSERT로 쓰고, --column-inserts는 컬럼 이름도 추가한다(스키마 차이에 강함). 기본값(플래그 없음)은 COPY 형식을 생성하며 적재가 훨씬 빠르다.

MySQL

-- Data only (INSERTs), without the table definition
mysqldump --no-create-info shop customer > insert.sql

-- One INSERT per row (more readable)
mysqldump --no-create-info --skip-extended-insert shop customer > insert.sql

mysqldump는 기본적으로 INSERT 문을 생성한다. --no-create-info는 CREATE TABLE을 생략하고 데이터만 출력한다. --skip-extended-insert는 각 행을 개별 INSERT로 만든다(기본값은 여러 행을 한 문으로 묶음).

SQL Server

-- SSMS: right-click > Tasks > Generate Scripts >
--   Advanced > 'Types of data to script' = Data only (or Schema and data)

-- Command-line alternative (sqlpackage / mssql-scripter tool)
-- mssql-scripter -S server -d shop -t customer --data-only > insert.sql

SQL Server에는 이를 위한 기본 T-SQL 명령이 없다. 가장 일반적인 방법은 SSMS 'Generate Scripts' 마법사에서 Data only를 선택하는 것이다. 명령줄에서는 Microsoft의 mssql-scripter 도구가 --data-only로 INSERT를 생성한다.

MongoDB

# Produces a binary/BSON backup instead of INSERT SQL
mongodump --db=shop --collection=customer --out=./backup

# As JSON text (can be adapted to insertMany)
mongoexport --db=shop --collection=customer --jsonArray --out=customer.json

SQL INSERT 개념이 없다. 이식 가능한 백업에는 mongodump(BSON, mongorestore로 복원)를 사용한다. 코드에 넣을 insertMany용으로는 mongoexport --jsonArray 출력을 활용할 수 있다.

Redis

# Portable serialization of a single key
DUMP customer:1
# The output is restored with RESTORE:
# RESTORE customer:1 0 "<dump-data>"

# RDB snapshot for all data
redis-cli --rdb dump.rdb

SQL INSERT 스크립트는 생성되지 않는다. 키 단위로는 DUMP + RESTORE 직렬화를, 전체 데이터는 RDB(--rdb) 파일을 사용한다. 명령 목록이 필요하면 SCAN으로 순회하며 타입별로 적절한 SET/HSET 명령을 만든다.

Elasticsearch

# The INSERT equivalent is _bulk NDJSON; produce it with elasticdump
elasticdump --input=http://localhost:9200/customer --output=customer_bulk.json --type=data

# Manual: converting a _search result to _bulk NDJSON requires an external script

SQL INSERT는 생성되지 않는다. Elasticsearch의 적재 단위는 _bulk NDJSON이다. 기존 데이터를 재적재 가능한 형태로 추출하려면 elasticdump(type=data)를 사용한다. _search + PIT 출력은 외부 스크립트로 NDJSON으로 변환할 수 있다.

DBAtlas에서 열기 →