프로그래밍/DB

[ postgreSQL] 다른 테이블 복사하기

kugancity 2019. 9. 28. 15:44
반응형





PostgreSQL 사용법 


다른 테이블 구조 & 데이터 복사하기 








다른 테이블 구조와 데이터 복사하기 




CREATE TABLE newtable AS SELECT * FROM oldtable;






다른 테이블 구조만 복사하기



CREATE TABLE newtable ( LIKE oldtable ); 






데이터 & 인덱스 &  constraint 등의 정보 다 같이 복사하기 


아래 두개의 sql 문을 차례로 실행한다. 복사 속도는 위의 방식이 더 빠르지만 인덱스 정보가 같이 복사됨.



 


create table newtable (like "oldtable" including all);

insert into newtable ( select * from "oldtable");




다른 테이블의 일부 필드만 복사하기 





insert into items_ver(item_id, item_group, name)

select * from items where item_id=2;



insert into items_ver (item_id, name, item_group)

select item_id, name, item_group from items where item_id=2;












728x90
반응형