오라클 SQL

오라클 SQL 10강

구자룡 2021. 3. 2. 15:59

10강

--DML? INSERT/UPDATE/DELETE
CREATE TABLE A_ENROL
AS
SELECT * FROM ENROL
WHERE STU_NO < 20150000;
SELECT * FROM A_ENROL;
DESC A_ENROL;
INSERT INTO A_ENROL(SUB_NO,STU_NO,ENR_GRADE)
VALUES(108,20151062,92);
SELECT * FROM A_ENROL;

INSERT INTO A_ENROL(SUB_NO,STU_NO)
VALUES (110,20152088);
SELECT * FROM A_ENROL;
INSERT INTO A_ENROL
VALUES(111,20153075,NULL);

--복수행 삽입하는 방법
SELECT * FROM ENROL;
SELECT * FROM ENROL
WHERE STU_NO LIKE '2015%';

INSERT INTO A_ENROL
SELECT * FROM ENROL
WHERE STU_NO LIKE '2015%';

SELECT * FROM A_ENROL;

--UPDATE문(수정작업)
--수강테이블(A_ENROL)에서 과목번호가 101인 경우에 각 점수(ENR_GRADE)를 5점씩 올려라
UPDATE A_ENROL
SET ENR_GRADE=ENR_GRADE+5
WHERE SUB_NO=101;
SELECT * FROM A_ENROL;

--과목이름(SUB_NAME)이 '시스템분석설계'를 수강하는 과목번호(SUB_NO)인 학생의 점수를 10점 올려라
UPDATE A_ENROL
SET ENR_GRADE=ENR_GRADE+10
WHERE SUB_NO=(SELECT SUB_NO FROM SUBJECT 
WHERE SUB_NAME='시스템분석설계');
SELECT * FROM A_ENROL;
SELECT SUB_NO FROM SUBJECT 
WHERE SUB_NAME='시스템분석설계';

--DML? DELETE()
DELETE FROM A_ENROL
WHERE STU_NO=2013001;
SELECT * FROM A_ENROL;
ROLLBACK;
SELECT * FROM A_ENROL;
--과목번호가'기계요소설계'인 학생의 과목번호인 테이블을 제거하라
DELETE A_ENROL
WHERE SUB_NO=(SELECT SUB_NO FROM SUBJECT
WHERE SUB_NAME='기계요소설계');

--다중행(튜플) 모두 삭제
SELECT * FROM ENROL;
DELETE FROM A_ENROL;
SELECT * FROM A_ENROL;
DESC A_ENROL;

--TCL(TRANSACTION CONTROL LANGUATE): COMMIT/ROLLBACK
SELECT * FROM B_STUDENT;
DELETE FROM B_STUDENT;
SELECT * FROM B_STUDENT;
ROLLBACK;

--DDL인 명령어를 사용하게되면.. 자동적으로 COMMIT이 되어버림
CREATE TABLE C_STUDENT (STU_NO NUMBER,
STU_NAME VARCHAR2(20));
ROLLBACK;
SELECT * FROM B_STUDENT;

SELECT * FROM A_STUDENT;
DELETE FROM A_STUDENT;
SELECT * FROM A_STUDENT;
ROLLBACK;

SELECT * FROM A_STUDENT;
INSERT INTO A_STUDENT(STU_NO,STU_NAME)
VALUES(10,'장');
SELECT * FROM A_STUDENT;
COMMIT;

--266p~
CREATE TABLE DEPT_TEMP
AS
SELECT * FROM DEPT;
SELECT * FROM DEPT_TEMP;

INSERT INTO DEPT_TEMP(DEPTNO,DNAME,LOC)
VALUES(50,'DATABASE','SEOUL');
SELECT * FROM DEPT_TEMP;

INSERT INTO DEPT_TEMP
VALUES(60,'NETWORK','BUSAN');
SELECT * FROM DEPT_TEMP;

INSERT INTO DEPT_TEMP(DEPTNO,DNAME,LOC)
VALUES(70,'WEB',NULL);
SELECT * FROM DEPT_TEMP;

INSERT INTO DEPT_TEMP(DEPTNO,DNAME,LOC)
VALUES(80,'MOBILE','');

INSERT INTO DEPT_TEMP(DEPTNO,LOC)
VALUES(90,'INCHEON');

CREATE TABLE EMP_TEMP
AS 
SELECT * FROM EMP
WHERE 1<>1;

SELECT * FROM EMP_TEMP;
DESC EMP_TEMP;

INSERT INTO EMP_TEMP(EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO)
VALUES (9999,'홍길동','PRESIDENT',NULL,TO_CHAR(TO_DATE('2001/01/01'),'YYYY-MM-DD'),5000,1000,10);

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 (2113,'이율곡','MANAGER',9999,SYSDATE,4000,NULL,20);

SELECT *FROM EMP_TEMP;

--봉급체계 1등급을 가진 사원들의 정보를 추가하시오
SELECT * FROM SALGRADE;

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;
SELECT *FROM EMP_TEMP;

CREATE TABLE DEPT_TEMP2
AS
SELECT *FROM DEPT;
SELECT *FROM DEPT_TEMP2;

UPDATE DEPT_TEMP2
SET LOC='SEOUL';

ROLLBACK;

UPDATE DEPT_TEMP2
SET DNAME='DATABASE',
    LOC='SEOUL'
WHERE DEPTNO=40;

UPDATE EMP
SET COMM = 50
WHERE SAL<=2500;
SELECT * FROM EMP;
ROLLBACK;

SELECT * FROM DEPT_TEMP2;
SELECT * FROM DEPT;
UPDATE DEPT_TEMP2
SET (DNAME,LOC)=(SELECT DNAME,LOC
FROM DEPT
WHERE DEPTNO=30)
WHERE DEPTNO=40;

SELECT* FROM DEPT_TEMP2;
ROLLBACK;

UPDATE DEPT_TEMP2
SET LOC='SEOUL'
WHERE DEPTNO=(SELECT DEPTNO
FROM DEPT_TEMP2
WHERE DNAME='OPERATIPNS');

--DML:DELETE?
CREATE TABLE EMP_TEMP2
AS
SELECT * FROM EMP;
SELECT * FROM EMP_TEMP2;

--WHERE절을 사용하여 데이터 일부분만 삭제하기
DELETE FROM EMP_TEMP2
WHERE JOB='MANAGER';
SELECT * FROM EMP_TEMP2;
ROLLBACK;

--WHERE절에 서브쿼리를 사용하여 데이터 일부만 삭제하기
--급여체계로 3등급이고 부서번호가 30번인 사원의 사원번호를 삭제하기
DELETE FROM EMP_TEMP2
WHERE EMPNO IN (SELECT E.EMPNO
FROM EMP E,SALGRADE S
WHERE E.SAL BETWEEN S.LOSAL AND S.HISAL
AND S.GRADE=3 AND DEPTNO=30);
SELECT * FROM EMP_TEMP2;
SELECT * FROM SALGRADE;
SELECT * FROM EMP
WHERE DEPTNO = 30;

DELETE FROM EMP_TEMP2;

'오라클 SQL' 카테고리의 다른 글

오라클 SQL 10강 연습문제  (0) 2021.03.02
오라클 SQL 11강  (0) 2021.03.02
오라클 SQL 9강  (0) 2021.02.26
오라클 SQL 8강  (0) 2021.02.26
오라클 SQL 7강 연습문제  (0) 2021.02.26