Oacle 데이터베이스 [표 복사] insert into select from 과 create table as select * from 두 가지 표 복사 문 차이 점

3397 단어 Oracle
create table  as select * from 과 insert into select from 두 가지 표 복사 문 차이
create table targer_table as select * from source_table
insert into target_table(column1,column2) select column1,column2 from source_table

위 두 마디 는 모두 소스 소스 소스table 의 기록 을 목표 표 target 에 삽입 합 니 다.table, 그러나 두 마디 는 또 다르다.
첫 문장 (create table  as select * from) 요구 사항
목표 표 targettable 이 존재 하지 않 습 니 다. 삽입 할 때 자동 으로 생 성 되 기 때 문 입 니 다.
두 번 째 문장 (insert into select from) 요구
목표 표 targettable 이 존재 합 니 다. 목표 표 가 존재 하기 때문에 원본 표 source 를 삽입 하 는 것 을 제외 합 니 다.table 필드 외 에 상수 도 삽입 할 수 있 습 니 다. 예 를 들 어 sql 구문:
insert into target_table(column1,column2) select column1,5 from source_table
예 중의: 5;
create table 이 든  as select * from 또는 insert into select from, 뒤에 있 는 것 은 모두 원본 표 (source table) 입 니 다.
1 、 Insert into Select from 구문      문장 형식: Insert into targertable(field1,field2,...) select value1,value2,... from source_table           요구 목표 표 targertable 존재 해 야 합 니 다. 목표 표 targertable 이 이미 존재 하기 때문에 우 리 는 소스 를 삽입 하 는 것 을 제외 하고 sourcetable 필드 외 에 상수 도 삽입 할 수 있 습 니 다.예 는 다음 과 같다.
-- 1. 테스트 테이블 만 들 기:
CREATE TABLE Table1
(
  a varchar(10) PRIMARY KEY,
  b varchar(10),
  c varchar(10)
);
CREATE TABLE Table2
(
  a varchar(10) PRIMARY KEY,
  c varchar(10),
  d int
);

-- 2. 테스트 데이터 만 들 기:
Insert into Table1 values (' ', 'asds', '90');
Insert into Table1 values (' ', 'asds', '100');
Insert into Table1 values (' ', 'asds', '80');
Insert into Table1 values (' ', 'asds', null);

조회 목표 표:
select * from Table2
기록 없 음;
-- 3. INSERT INTO SELECT 구문 복사 표 데이터:
Insert into Table2(a, c, d) select a,c,5 from Table1

-- 4. 업데이트 결과 표시:
select * from Table2;
   	A	C	D
1	 	90	5
2	 	100	5
3	 	80	5
4	 		5
주의: D 필드 의 값 은 모두 상수 5 입 니 다.
-- 5. 테스트 테이블 삭제:
drop TABLE Table1
drop TABLE Table2

2、create table  as select * from 구문
문장 형식: create table targertable as select * from source_table;
대상 표 Table 2 가 존재 하지 않 습 니 다. 삽입 할 때 표 Table 2 를 자동 으로 만 들 고 Table 1 에서 지정 한 필드 데 이 터 를 Table 2 에 복사 하기 때 문 입 니 다.예 는 다음 과 같다.
 --1. 테스트 테이블 만 들 기:
CREATE TABLE Table1
(
  a varchar(10) PRIMARY KEY,
  b varchar(10),
  c varchar(10)
);

 --2. 테스트 데이터 생 성:
Insert into Table1 values (' ', 'asds', '90');
Insert into Table1 values (' ', 'asds', '100');
Insert into Table1 values (' ', 'asds', '80');
Insert into Table1 values (' ', 'asds', null);

  --3.create table  as select * from 구문 생 성 표 Table 2 및 데이터 복사:
create table TABLE2 as select * from TABLE1;

-- 4. 업데이트 결과 표시:
select * from Table2
   	A	B	C
1	 	asds	90
2	 	asds	100
3	 	asds	80
4	 	asds	

-- 5. 테스트 테이블 삭제:
drop TABLE Table1
drop TABLE Table2

첨부:
주의:
create table targer_table as select * from source_table 은 표 구조 + 표 데 이 터 를 복사 합 니 다.
그리고 table targertable as select * from source_table where 1=2;같은 표 구조 만 만 들 고 표 데 이 터 를 복사 하지 않 습 니 다.

좋은 웹페이지 즐겨찾기