๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
์ด๋ก /DB

[๋ฌด์กฐ๊ฑด ๋”ฐ๋ผํ•˜๊ธฐ] INSERT, UPDATE, DELECT

by 6161990 2021. 5. 26.

๐Ÿ“Œ DML(Data Manipulation Language(๋ฐ์ดํ„ฐ ์กฐ์ž‘์–ด))

  • INSERT : ํ…Œ์ด๋ธ”์— ์ƒˆ๋กœ์šด ๋กœ์šฐ๋ฅผ ์ถ”๊ฐ€ํ•˜๋Š” ๊ตฌ๋ฌธINSERT INTO ํ…Œ์ด๋ธ”๋ช… (์ปฌ๋Ÿผ๋ช…) VALUES (๊ฐ’)

 

INSERT INTO EMP01 (EMPNO, ENAME, JOB) VALUES(222,'๊น€์ œ๋‹ˆ','๊ฐœ๋ฐœ');

INSERT INTO EMP01 (EMPNO, ENAME, JOB) VALUES(223,'๋กœ์ œ','์ธ์‚ฌ');

INSERT INTO EMP01 (EMPNO, ENAME, JOB) VALUES(224,'์ง€์ˆ˜','์ƒ์‚ฐ');

 

--์ปฌ๋Ÿผ๋ชฉ๋ก์„ ์ƒ๋žตํ•˜๋Š” ๊ฒฝ์šฐ (๋ชจ๋“  ์ปฌ๋Ÿผ์— DATA ๋„ฃ์„๋•Œ๋งŒ!)

INSERT INTO EMP01 VALUES(225,'๋ฆฌ์‚ฌ','๊ฐœ๋ฐœ');

 

 

--์ปฌ๋Ÿผ ๋ชฉ๋ก์— ๋ชจ๋“  ์ปฌ๋Ÿผ์— ์žˆ์ง€ ์•Š์„ ๊ฒฝ์šฐ (JOB์— NULL ๊ฐ’์ด ๋“ค์–ด๊ฐ)

INSERT INTO EMP01 (EMPNO,ENAME) VALUES (226,'์‚ฌ๋‚˜');

 

 

 

--NULL์„ ๋ช…์‹œ์ ์œผ๋กœ ์ €์žฅํ•  ์ˆ˜๋„ ์žˆ์Œ

INSERT INTO EMP01 (EMPNO, ENAME, JOB) VALUES (227,'๋‚˜์—ฐ', NULL);

 


๐Ÿ“ ํ…Œ์ด๋ธ” ๊ตฌ์กฐ์™€ ๋ฐ์ดํ„ฐ ๋ณต์‚ฌ

--ํ…Œ์ด๋ธ” ๊ตฌ์กฐ ๋ณต์‚ฌ

CREATE TABLE EMP02 AS SELECT EMPNO, ENAME, JOB FROM EMP01 WHERE 1=0;

SELECT * FROM EMP02;

 

--ํ…Œ์ด๋ธ” ๋ฐ์ดํ„ฐ ๋ณต์‚ฌ (์ปฌ๋Ÿผ์ด๋ฆ„์ด ๊ฐ™์ง€ ์•Š์•„๋„๋จ, ์ˆœ์„œ๋งŒ ์ง€์ผœ์ฃผ๋ฉด OK)

INSERT INTO EMP02(EMPNO, ENAME, JOB) SELECT EMPNO, ENAME, JOB FROM EMP;

SELECT * FROM EMP02;

 

--๋ชจ๋“  ์ปฌ๋Ÿผ ๋ณต์‚ฌ์‹œ ๋ช…์‹œ X ๋„ ๊ฐ€๋Šฅ

INSERT INTO EMP02 SELECT EMPNO, ENAME, JOB FROM EMP01; SELECT * FROM EMP02;


๐Ÿ“ ์„œ๋ธŒ์ฟผ๋ฆฌ๋กœ ๋ฐ์ดํ„ฐ ์ €์žฅํ•˜๊ธฐ

INSERT INTO ํ…Œ์ด๋ธ”๋ช… ์„œ๋ธŒ์ฟผ๋ฆฌ INSERT ALL INTO ํ…Œ์ด๋ธ”๋ช… (์ปฌ๋Ÿผ๋ช…) VALUES (์ปฌ๋Ÿผ๋ช…) INTO ํ…Œ์ด๋ธ”๋ช…(์ปฌ๋Ÿผ๋ช…) VALUES(์ปฌ๋Ÿผ๋ช…) ์„œ๋ธŒ์ฟผ๋ฆฌ

 

--์„œ๋ธŒ์ฟผ๋ฆฌ ์ด์šฉํ•ด ํ•œ๊บผ๋ฒˆ์— DATA ์‚ฝ์ž…

INSERT ALL

INTO EMP03 ( EMPNO, ENAME, JOB) VALUES (EMPNO, ENAME, JOB)

INTO EMP04 ( EMPNO, ENAME, HIREDATE) VALUES (EMPNO, ENAME, HIREDATE)

SELECT EMPNO, ENAME, JOB, HIREDATE FROM EMP;

SELECT * FROM EMP03;

SELECT * FROM EMP04;

 

 


๐Ÿ“ UPDATE, ๋กœ์šฐ ๋‚ด์˜ ์ปฌ๋Ÿผ ๊ฐ’์„ ์ˆ˜์ •ํ•˜๋Š” ๊ตฌ๋ฌธ

UPDATE ํ…Œ์ด๋ธ”๋ช… SET ์ปฌ๋Ÿผ = ๊ฐ’, ์ปฌ๋Ÿผ = ๊ฐ’.. WHERE ์กฐ๊ฑด๋ฌธ

 

--์‚ฌ์›๋“ค์˜ ์ง๋ฌด๋ฅผ ๊ฐœ๋ฐœ๋กœ ๋ณ€๊ฒฝํ•œ๋‹ค.

UPDATE EMP01 SET JOB = '๊ฐœ๋ฐœ'; SELECT * FROM EMP01;

 

 

 

--์‚ฌ์›๋“ค์˜ ์‚ฌ์›๋ฒˆํ˜ธ, ์ด๋ฆ„, ์ง๋ฌด๋ฅผ ๋ณ€๊ฒฝํ•œ๋‹ค.

UPDATE EMP01 SET EMPNO = 777, ENAME ='์ œ๋‹ˆ' , JOB = '๊ฐœ๋ฐœ';

 

 

 

--์—ฐ๊ทนํŒ€ ๋ถ€์„œ์— ๊ทผ๋ฌดํ•˜๊ณ  ์žˆ๋Š” ์‚ฌ์›๋“ค์˜ ๊ธ‰์ณ๋ฅผ ์ „์ฒด ํ‰๊ท  ๊ธ‰์—ฌ๋กœ ์„ค์ •ํ•œ๋‹ค.

UPDATE EMP SET SAL = (SELECT TRUNC(AVG(SAL)) FROM EMP) WHERE JOB='์—ฐ๊ทนํŒ€';

 

 

 

 

--20๋ฒˆ ๋ถ€์„œ์— ๊ทผ๋ฌดํ•˜๊ณ  ์žˆ๋Š” ์‚ฌ์›๋“ค์˜ ์ง์†์ƒ๊ด€์„ KING ์œผ๋กœ ํ•˜๊ณ  ๊ธ‰์—ฌ๋ฅผ ์ „์ฒด ๊ธ‰์—ฌ์˜ ์ตœ๊ณ ์•ก์œผ๋กœ ์„ค์ •ํ•œ๋‹ค.

UPDATE EMP01 SET MGR = (), SAL = () WHERE DEPTNO =20; --ํ—ท๊ฐˆ๋ฆฌ๋Š” ๊ฒฝ์šฐ ์ด๋ ‡๊ฒŒ ๋งŒ๋“ค์–ด๋†“๊ณ 

SELECT EMPNO FROM EMP01 WHERE ENAME = 'KING'; --๊ด„ํ˜ธ์— ๋“ค์–ด๊ฐˆ ์ •๋ณด๋ฅผ ํ•˜๋‚˜์”ฉ ์ฑ„์šฐ๊ธฐ

SELECT MAX(SAL) FROM EMP01;

 

--์ง๋ฌด๊ฐ€ CLERK ์ธ ์‚ฌ์›๋“ค์˜ ์ง๋ฌด์™€ ๊ธ‰์—ฌ๋ฅผ ์ตœ๊ณ ์•ก์„ ๋ฐ›๋Š” ์‚ฌ์›์˜ ์ง๋ฌด์™€ ๊ธ‰์—ฌ์•ก์œผ๋กœ ๋ณ€๊ฒฝํ•œ๋‹ค.

--์ง๋ฌด์™€ ๊ธ‰์—ฌ ๋”ฐ๋กœ

UPDATE EMP01 SET JOB=(SELECT JOB FROM EMP01 WHERE SAL =(SELECT MAX(SAL) FROM EMP01)),

SAL=(SELECT SAL FROM EMP01 WHERE SAL =(SELECT MAX(SAL) FROM EMP01))

WHERE JOB = 'CLERK';

 

 

--์ง๋ฌด ๊ธ‰์—ฌ ํ•œ๊บผ๋ฒˆ์—

UPDATE UPDATE EMP01 SET(JOB,SAL) = (SELECT JOB, SAL FROM EMP01 WHERE SAL =(SELECT MAX(SAL) FROM EMP01))

WHERE JOB ='CLERK';

 

 


๐Ÿ“ DELETE, ํ…Œ์ด๋ธ” ๋‚ด์˜ ๋กœ์šฐ๋ฅผ ์‚ญ์ œํ•˜๋Š” ๊ตฌ๋ฌธ

DELETE FROM ํ…Œ์ด๋ธ”๋ช… WHERE ์กฐ๊ฑด๋ฌธ

 

--์กฐ๊ฑด์ ˆ์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š์œผ๋ฉด ํ…Œ์ด๋ธ”์˜ ๋ชจ๋“  ๋ฐ์ดํ„ฐ๊ฐ€ ์‚ญ์ œ๋œ๋‹ค.

DELETE FROM EMP01;

 

-- ์‚ฌ์›๋ฒˆํ˜ธ๊ฐ€ 7499์ธ ์‚ฌ์›์˜ ์ •๋ณด๋ฅผ ์‚ญ์ œํ•œ๋‹ค.

DELETE FROM EMP01 WHERE EMPNO = 7499;

 

--์‚ฌ์›์˜ ๊ธ‰์—ฌ๊ฐ€ ํ‰๊ท  ๊ธ‰์—ฌ ์ดํ•˜์ธ ์‚ฌ์›์˜ ์ •๋ณด๋ฅผ ์‚ญ์ œํ•œ๋‹ค.

DELETE FROM EMP01 WHERE SAL <= (SELECT AVG(SAL) FROM EMP01);

 

--์ปค๋ฏธ์…˜์„ ๋ฐ›์ง€ ์•Š๋Š” ์‚ฌ์›๋“ค์˜ ์ •๋ณด๋ฅผ ์‚ญ์ œํ•œ๋‹ค.

DELETE FROM EMP01 WHERE COMM IS NULL;