INSERT INTO table [( column [, column…] )]
VALUES (value [, value…] );
--(dept 테이블은 컬럼이 3개(deptno, dname, loc_code)인 테이블이다)
INSERT INTO dept ( deptno , dname)
VALUES (70, ‘MIS’)
이런 암시적 방법을 사용할 때, loc_code 컬럼에 not null 제약조건이 있을 경우 오류가 발생하고, 행이 삽입되지 않는다.
INSERT INTO dept ( deptno , dname,loc_code)
VALUES (70, ‘MIS’, NULL)
NULL은 '' 로도 쓸 수 있다. (스페이스 X)
INSERT INTO emp ( empno , ename, hiredate)
VALUES (7233, ‘PAUL’, SYSDATE);
INSERT INTO emp (empno, ename, sal)
VALUES (7234, user , 3400);
INSERT INTO emp (empno, ename, hiredate)
VALUES (7533 , 'ELVIS'
,TO_DATE('1997/02/05','YYYY/MM/DD') ) ;
INSERT INTO table [(column[, column…] )]
subquery;
--서브쿼리 사용시 VALUES 키워드는 쓰지 않는다.
INSERT INTO EMP_CP(ID, NAME, TITLE, DEPTNO)
SELECT EMPNO, ENAME, JOB, DEPTNO
FROM EMP
WHERE JOB = 'CLERK';
UPDATE TABLE
SET COLUMN = VALUE [, COLUMN = VALUE, ...]
[WHERE CONDITION];
UPDATE emp
SET deptno = ( SELECT deptno
FROM emp
WHERE ename = 'ALLEN')
WHERE empno = 7902;
UPDATE emp
SET job = ( SELECT job
FROM emp
WHERE empno = 7499)
,deptno = ( SELECT deptno
FROM dept
WHERE dname = 'SALES')
WHERE empno = 7698;
UPDATE emp
SET (job, deptno) = ( SELECT job, deptno
FROM emp
WHERE empno = 7499)
WHERE empno = 7698;
DELETE [FROM] TABLE
[WHERE CONDITION];
DELETE FROM emp ;
DELETE emp
WHERE empno = 7233;
DELETE emp
WHERE deptno = (SELECT deptno
FROM emp
WHERE ename = 'SCOTT');
DELETE emp
WHERE deptno = (SELECT deptno
FROM dept
WHERE dname = 'ACCOUNTING');
MERGE INTO 테이블명 ALIAS --target 테이블
USING [TABLE | VIEW | SUBQUERY] ALIAS -- source 테이블
ON (JOIN CONDITION)
WHEN MATCHED THEN
UPDATE SET 컬럼1(target) = 컬럼값1(source)
, 컬럼2(target) = 컬럼값2(source)
WHEN NOT MATCHED THEN
INSERT (column list) --target
VALUES (column values) ; --source
테이블명 alias를 필수로 작성해야한다.
MERGE INTO emp_master t
USING emp e
ON (t.empno = e.empno)
WHEN MATCHED THEN
UPDATE SET t.ename = e.ename
,t.sal = e.sal
,t.comm = e.comm
WHEN NOT MATCHED THEN
INSERT (empno, ename, sal, comm)
VALUES (e.empno, e.ename, e.sal, e.comm);
INSERT ALL
INTO sal_history VALUES (empid, hiredate, sal)
INTO mgr_history VALUES (empid, mgr, sal)
SELECT employee_id AS empid
,hire_date AS hiredate
,salary AS sal
,manager_id AS mgr
FROM employees
WHERE employee_id > 200
두 조건에 모두 해당되면 두 테이블에 모두 INSERT됨
INSERT ALL
WHEN sal > 6000
THEN INTO sal_history VALUES (empid, hiredate, sal)
WHEN mgr > 100
THEN INTO mgr_history VALUES (empid, mgr, sal)
SELECT employee_id empid
,hire_date hiredate
,salary sal
,manager_id mgr
FROM employees
WHERE employee_id > 200;
첫번째 조건에 해당되면 첫번째 테이블에만 INSERT되고
두번째 조건은 확인하지 않는다.
INSERT FIRST
WHEN sal > 6000
THEN INTO sal_history VALUES (empid, hiredate, sal)
WHEN mgr > 100
THEN INTO mgr_history VALUES (empid, mgr, sal)
SELECT employee_id empid
,hire_date hiredate
,salary sal
,manager_id mgr
FROM employees
WHERE employee_id > 200;
가로(COLUMN)로 기술되어있던 데이터를 세로(ROW)로 변경 = PIVOT
CREATE TABLE SALES_SOURCE_DATA(
EMPLOYEE_ID NUMBER(6),
WEEK_ID NUMBER(2), --주차별
SALES_MON NUMBER(8,2), --요일별 실적
SALES_TUE NUMBER(8,2),
SALES_WED NUMBER(8,2),
SALES_THU NUMBER(8,2),
SALES_FRI NUMBER(8,2));
INSERT INTO SALES_SOURCE_DATA
VALUES(1101,4,100,150,80,60,120);
INSERT INTO SALES_SOURCE_DATA
VALUES(1102,5,300,300,230,120,150);
-----------SALES_SOURCE_DATA 테이블을 PIVOTING 하여 복사
CREATE TABLE SALES_INFO(
EMPLOYEE_ID NUMBER(6),
WEEK_ID NUMBER(2), --주차별
DAY_ID NUMBER(1), --요일별
SALES NUMBER(8,2));
INSERT ALL
INTO SALES_INFO VALUES(EMPLOYEE_ID,WEEK_ID,1,SALES_MON)
INTO SALES_INFO VALUES(EMPLOYEE_ID,WEEK_ID,2,SALES_TUE)
INTO SALES_INFO VALUES(EMPLOYEE_ID,WEEK_ID,3,SALES_WED)
INTO SALES_INFO VALUES(EMPLOYEE_ID,WEEK_ID,4,SALES_THU)
INTO SALES_INFO VALUES(EMPLOYEE_ID,WEEK_ID,5,SALES_FRI)
SELECT EMPLOYEE_ID,WEEK_ID,SALES_MON,SALES_TUE
,SALES_WED,SALES_THU,SALES_FRI
FROM SALES_SOURCE_DATA;