INSERT INTO dept01
(deptno, dname, loc)
VALUES
(10, 'accounting', 'new york');
SELECT * FROM dept01;
-- INSERT INTO dept01 (deptno, dname, lok) VALUES (10, 'accounting', 'new york');
-- INSERT INTO dept01 (deptno, dname, loc) VALUES (10, 'accounting');
-- INSERT INTO dept01 (deptno, dname, loc) VALUES (10, 'accounting', 'new york', 1);
-- INSERT INTO dept01 (deptno, dname, loc) VALUES (10, 'accounting', 12);
INSERT INTO dept01 VALUES (50, 'accounting', 'seoul');
INSERT INTO dept01 VALUES (50, 'accounting', 'seoul');
INSERT INTO dept01 (dname, loc, deptno) VALUES ('cloud', 'busan', 70);
SELECT * FROM dept01;
INSERT INTO dept01 VALUES (40, 'system', NULL);
INSERT INTO dept01 VALUES (70, NULL, '');
CREATE TABLE dept02
AS
SELECT * FROM dept WHERE 1=0;
SELECT * FROM dept02;
INSERT INTO dept02
SELECT * FROM dept;
SELECT * FROM dept02;
CREATE TABLE emp_hire
AS
SELECT empno, ename, hiredate
FROM emp WHERE 1=0;
SELECT * FROM emp_hire;
CREATE TABLE emp_mgr
AS
SELECT empno, ename, mgr
FROM emp WHERE 1=0;
SELECT * FROM emp_mgr;
INSERT ALL
INTO emp_hire VALUES (empno, ename, hiredate)
INTO emp_mgr VALUES (empno, ename, mgr)
SELECT empno, ename, hiredate, mgr
FROM emp
WHERE deptno=20;
SELECT * FROM emp_hire;
SELECT * FROM emp_mgr;
DROP TABLE emp01 PURGE;
CREATE TABLE emp01 AS SELECT * FROM emp;
SELECT * FROM emp01;
UPDATE emp01
SET deptno=30;
SELECT * FROM emp01;
UPDATE emp01
SET deptno=50
WHERE deptno=10;
SELECT * FROM emp01;
UPDATE emp01
SET deptno=40, job='MANAGER'
WHERE ename='SMITH';
SELECT * FROM emp01;
DROP TABLE dept01 PURGE;
CREATE TABLE dept01 AS SELECT * FROM dept;
SELECT * FROM dept01;
UPDATE dept01
SET loc=(SELECT loc
FROM dept01
WHERE deptno=40)
WHERE deptno=20;
SELECT * FROM dept01;
UPDATE dept01
SET (dname, loc)=(SELECT dname, loc
FROM dept01
WHERE deptno=30)
WHERE deptno=20;
SELECT * FROM dept01;
DELETE FROM dept01;
SELECT * FROM dept01;
DELETE FROM dept01
WHERE deptno=30;
SELECT * FROM dept01;
기존에 존재하는 행이 있으면 새로운 값으로 갱신(UPDATE) 하고,
존재하지 않으면 새로운 행으로 추가(INSERT) 됩니다
DROP TABLE emp01 PURGE;
CREATE TABLE emp01 AS SELECT * FROM emp;
SELECT * FROM emp01;
DROP TABLE emp02 PURGE;
CREATE TABLE emp02 AS SELECT * FROM emp WHERE job='MANAGER';
SELECT * FROM emp02;
UPDATE emp02
SET job='TEST';
SELECT * FROM emp02;
INSERT INTO emp02
VALUES (7000, 'ROBOT', 'AI', 7777, '2024/08/26', 2000, 200, 90);
데이터 추가 확인
SELECT * FROM emp02;
MERGE INTO emp01 -- 병합 테이블
USING emp02 -- 병합에 사용하는 데이터 테이블
ON(emp01.empno=emp02.empno) -- 테이블 연결 조건 : empno 값이 일치하는 행을 기준
WHEN MATCHED THEN -- emp01 테이블과 emp02 테이블에서 일치하는 empno 가 있을 경우 실행
UPDATE SET emp01.ename=emp02.ename,
emp01.job=emp02.job,
emp01.mgr=emp02.mgr,
emp01.hiredate=emp02.hiredate,
emp01.sal=emp02.sal,
emp01.comm=emp02.comm,
emp01.deptno=emp02.deptno
WHEN NOT MATCHED THEN -- emp02 테이블에는 있지만 emp01 테이블에는 없는 empno 의 경우 실행
INSERT VALUES (emp02.empno,
emp02.ename,
emp02.job,
emp02.mgr,
emp02.hiredate,
emp02.sal,
emp02.comm,
emp02.deptno);
![](https://velog.velcdn.com/images/smsee3/post/0a2242b5-01b9-4996-b309-b50f0dae6950/image.png)
SELECT * FROM emp01;
DROP TABLE TEST01 PURGE;
DROP TABLE TEST02 PURGE;
CREATE TABLE TEST01
AS
SELECT empno, ename, job, sal FROM emp WHERE 1=0;
SELECT * FROM TEST01;
INSERT INTO TEST01 VALUES (1000, 'manA', 'coder', 10000);
INSERT INTO TEST01 VALUES (1010, 'robot', 'ai', 20000);
INSERT INTO TEST01 VALUES (1020, 'manB', 'system', 30000);
SELECT * FROM TEST01;
INSERT INTO TEST01 VALUES (1030, 'manA', '', 40000);
INSERT INTO TEST01 VALUES (1040, 'manA', NULL, 50000);
SELECT * FROM TEST01;
INSERT INTO TEST01
SELECT empno, ename, job, sal
FROM emp
WHERE deptno=10;
SELECT * FROM TEST01;
UPDATE TEST01
SET sal=sal-3000
WHERE sal>=5000;
SELECT * FROM TEST01;
CREATE TABLE TEST02
AS
SELECT ename, sal, hiredate, deptno
FROM emp;
SELECT * FROM TEST02;
UPDATE TEST02
SET sal=sal+1000
WHERE deptno=(SELECT deptno
FROM dept
WHERE loc='DALLAS');
SELECT * FROM TEST02;
UPDATE TEST02
SET (sal, hiredate)=(SELECT sal, hiredate
FROM TEST02
WHERE ename='KING');
SELECT * FROM TEST02;
DELETE FROM TEST01
WHERE job IS NULL;
SELECT * FROM TEST01;
DELETE FROM TEST02
WHERE deptno=(SELECT deptno
FROM dept
WHERE dname='RESEARCH');
SELECT * FROM TEST02;