CREATE TABLE DEPT_TEMP
AS SELECT * FROM DEPT;
DROP TABLE 테이블 이름;
INSERT INTO 테이블 이름[(열1, 열2, ..., 열N)]
VALUES(열1에 들어갈 데이터, 열2에 들어갈 데이터, ..., 열N에 들어갈 데이터);
INSERT INTO DEPT_TEMP (DEPTNO, DNAME, LOC)
VALUES (50, 'DATABASE', 'SEOUL');
INSERT INTO DEPT_TEMP
VALUES(60, 'NETWORK', 'BUSAN');
INSERT INTO DEPT_TEMP
VALUES(70, 'WEB', NULL);
INSERT INTO DEPT_TEMP
VALUES(80, 'MOBILE', '');
INSERT INTO DEPT_TEMP(DEPTNO, LOC)
VALUES (90, 'INCHON');
CREATE TABLE EMP_TEMP
AS SELECT * FROM EMP
WHERE 1<> 1;
INSERT INTO EMP_TEMP(EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
VALUES (9999, '홍길동', 'PRESIDENT', NULL, '2001/01/01',
5000, 1000, 10);
INSERT INTO EMP_TEMP(EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
VALUES( 1111, '성춘향', 'MANAGER', 9999, '2001-01-05', 4000, NULL, 20);
INSERT INTO EMP_TEMP(EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
VALUES( 2111, '이순신', 'MANAGER', 9999, '07/01/2001', 4000, NULL, 20);
오류가 발생하는 이유: 오라클은 설치되어 있는 운영체제(OS)의 종류나 사용하는 기본 언어군에 따라 날짜 표기방식이 다르기 때문이다.
⇒ 날짜 데이터를 INSERT문으로 입력할 때는 문자열로 날짜를 입력하지 않고, 다음 예제와 같이 TO_DATE 함수를 사용하는 것이 좋다.
TO_DATE 함수를 사용하여 날짜 데이터 입력하기
INSERT INTO EMP_TEMP(EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
VALUES( 2111, '이순신', 'MANAGER', 9999, TO_DATE('07/01/2001', 'DD/MM/YYYY'), 4000, NULL, 20);
INSERT INTO EMP_TEMP(EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
VALUES( 3111, '심청이', 'MANAGER', 9999, SYSDATE, 4000, NULL, 30);
INSERT INTO EMP_TEMP(EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
SELECT E.EMPNO, E.ENAME, E.JOB, E.MGR, E.HIREDATE, E.SAL, E.COMM, E.DEPTNO
FROM EMP E, SALGRADE S
WHERE E.SAL BETWEEN S.LOSAL AND S.HISAL
AND S.GRADE = 1;
CREATE TABLE DEPT_TEMP2
AS SELECT * FROM DEPT;
UPDATE [변경할 테이블]
SET [변경할 열1]=[데이터], [변경할 열2]=[데이터], ..., [변경할 열N]=[데이터]
[WHERE 데이터를 변경할 대상 행을 선별하기 위한 조건];
UPDATE DEPT_TEMP2
SET LOC = 'SEOUL';
ROLLBACK;
UPDATE DEPT_TEMP2
SET DNAME = 'DATABASE',
LOC = 'SEOUL'
WHERE DEPTNO = 40;
SELECT * FROM DEPT_TEMP2;
UPDATE DEPT_TEMP2
SET(DNAME, LOC) = (SELECT DNAME, LOC FROM DEPT WHERE DEPTNO=40)
WHERE DEPTNO=40;
UPDATE DEPT_TEMP2
SET DNAME = (SELECT DNAME FROM DEPT WHERE DEPTNO=40),
LOC = (SELECT LOC FROM DEPT WHERE DEPTNO=40)
WHERE DEPTNO=40;
UPDATE DEPT_TEMP2
SET LOC = 'SEOUL'
WHERE DEPTNO = (SELECT DEPTNO FROM DEPT_TEMP2 WHERE DNAME='OPERATIONS');
SELECT * FROM DEPT_TEMP2;
UPDATE DEPT_TEMP2
SET DNAME = 'DATABASE',
LOC = 'SEOUL'
WHERE DEPTNO = 40;
SELECT *
FROM DEPT_TEMP2
WHERE DEPTNO=40;
반드시, UPDATE문과 DELETE문을 실행하기 전에,
SELECT문으로 WHERE절의 조건식이 정확한지 확인하는 습관을 길러야한다.
CREATE TABLE EMP_TEMP3
AS SELECT * FROM EMP;
DELETE [FROM] [테이블 이름]
[WHERE 삭제할 대상 행을 선별하기 위한 조건식];
DELETE EMP_TEMP3
WHERE JOB = 'MANAGER';
SELECT * FROM EMP_TEMP3;
DELETE FROM EMP_TEMP3
WHERE EMPNO
IN (SELECT E.EMPNO
FROM EMP_TEMP2 E, SALGRADE S
WHERE E.SAL BETWEEN S.LOSAL
AND S.HISAL
AND S.GRADE=3
AND DEPTNO=30);
DELETE FROM EMP_TEMP3;
Q1.
INSERT INTO DPET2
(DEPTNO, DNAME, LOC)
VALUES(50, 'ORACLE', 'BUSAN');
INSERT INTO DPET2
(DEPTNO, DNAME, LOC)
VALUES(60, 'SQL', 'ILSAN');
INSERT INTO DPET2
(DEPTNO, DNAME, LOC)
VALUES(70, 'SELECT', 'INCHEON');
INSERT INTO DPET2
(DEPTNO, DNAME, LOC)
VALUES(80, 'DML', 'BUNDANG');
SELECT * FROM DPET2;
Q2.
INSERT INTO EMP2 (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
VALUES(7201, 'TEST_USER1', 'MANAGER', 7788, '2016-01-02', 4500, NULL, 50);
INSERT INTO EMP2 (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
VALUES(7202, 'TEST_USER2', 'CLERK', 7201, '2016-02-21', 1800, NULL, 50);
INSERT INTO EMP2 (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
VALUES(7203, 'TEST_USER3', 'ANALYST', 7201, '2016-04-11', 3400, NULL, 60);
INSERT INTO EMP2 (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
VALUES(7204, 'TEST_USER4', 'SALESMAN', 7201, '2016-05-31', 2700, 300, 60);
INSERT INTO EMP2 (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
VALUES(7205, 'TEST_USER5', 'CLERK', 7201, '2016-07-20', 2600, NULL, 70);
INSERT INTO EMP2 (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
VALUES(7206, 'TEST_USER6', 'CLERK', 7201, '2016-09-08', 2600, NULL, 70);
INSERT INTO EMP2 (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
VALUES(7207, 'TEST_USER7', 'LECTURER', 7201, '2016-10-28', 2300, NULL, 80);
INSERT INTO EMP2 (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
VALUES(7208, 'TEST_USER8', 'STUDENT', 7201, '2016-03-09', 1200, NULL, 80);
Q3.
UPDATE EMP2
SET DEPTNO = 70
WHERE SAL > (SELECT AVG(SAL) FROM EMP2 WHERE DEPTNO = 50 );
Q4.
UPDATE EMP2
SET SAL = SAL*1.1,
DEPTNO = 80
WHERE HIREDATE > (SELECT MIN(HIREDATE) FROM EMP2 WHERE DEPTNO = 60);
Q5.
DELETE FROM EMP2 E1
WHERE E1.EMPNO IN (SELECT E.EMPNO
FROM EMP2 E, SALGRADE2 S
WHERE E.SAL BETWEEN S.LOSAL AND S.HISAL
AND S.GRADE = 5);