insert
- 테이블에 새로운 행(row, record)을 추가할 때 사용하는 명령
- 테이블에 새로운 데이터를 입력(추가)하기 위한 데이터 조작어(DML)
1) insert into 테이블명(컬럼1, ... , 컬럼n) values(값1, ..., 값n);
2) insert into 테이블명 values(값1, ..., 값n);
--> 컬럼의 모든 값을 입력하는 경우 컬럼명 생략 가능
3) 서브쿼리를 이용해서 기존 테이블에 데이터를 추가하는 방법
insert into 테이블명 select 컬럼1, ..., 컬럼n from 테이블명 where ...
(컬럼과 값이 갯수, 순서, 데이터타입이 같은 경우에 사용 가능)
4) insert all
when 조건 then into
when 조건 then into
5) insert all
into 테이블명 values()
into 테이블명 values()
예) professor를 기준으로 prof_3과 prof_4 테이블을 생성
- 각각 profno number, name varchar(25)의 2개의 컬럼만 존재하는 테이블 생성
- prof_3에는 1000~1999번까지의 교수만
- prof_4에는 2000~2999번까지의 교수만 복사
create table prof_3 (profno number, name varchar2(25));
create table prof_4 as select * from prof_3 where 1=2;
뒤에 where 1=2를 붙이면 prof_3의 구조만 가져오고 내용은 가져오지 않음! (1=2는 false이기 때문에 where의 조건절에 맞지 않아서 내용은 가져오지 않음)
insert into prof_3 select profno, name from professor where profno between 1000 and 1999;
insert into prof_4 select profno, name from professor where profno between 2000 and 2999;
위의 내용을 insert all을 이용해서 한 문장으로 줄여봅시당
(prof_3과 prof_4 테이블을 drop하고 새로 create 했다는 가정하에)
insert all
when profno between 1000 and 1999 then into prof_3 values(profno, name)
when profno between 2000 and 2999 then into prof_4 values(profno, name)
select * from professor;
만약 동일자료를 각각 다른 테이블에 추가한다면~?
(prof_3과 prof_4 테이블을 drop하고 새로 create 했다는 가정하에)
- prof_3과 prof_4에 profno가 3000~3999번까지의 교수만 복사
insert all
into prof_3 values(profno, name)
into prof_4 values(profno, name)
select profno, name from professor
where profno between 3000 and 3999;
'DataBase' 카테고리의 다른 글
[DataBase] 누구인가? 누가 나의 데이터베이스를 가져갔는가 (0) | 2023.07.20 |
---|---|
[DB설계] 하나의 게시글에 여러 개의 태그를 등록하고 싶은데 왜 tag테이블이 두 개가 필요한가 (0) | 2023.07.05 |
[Oracle] PL/SQL 스토어드 프로시저(Stored Procedure) 간단 정리 + 내가 자주내는 오류 녀석들 (0) | 2023.04.03 |
[Oracle] Foreign key 제약사항 (0) | 2023.03.28 |
[Oracle] 급여에 따라 급여 인상율 다르게 적용하기 (decode, case, sign) (0) | 2023.03.23 |