๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Note for moment

SQL(DML,DDL,DCL,TCL)

by ๐Ÿ’œautumn 2020. 9. 7.

* SQL(Structured Query Language)

 : IBM, ๊ตญ์ œ ํ‘œ์ค€ DB์–ธ์–ด, RDBMS ์ง€์›, ๊ด€๊ณ„๋Œ€์ˆ˜์™€ ๊ด€๊ณ„ํ•ด์„์˜ ๊ธฐ์ดˆ 

 

1. DDL(Data Definition Language)

  : ์Šคํ‚ค๋งˆ, ๋„๋ฉ”์ธ, ํ…Œ์ด๋ธ”, ๋ทฐ, ์ธ๋ฑ์Šค๋ฅผ ์ •์˜, ๋ณ€๊ฒฝ, ์‚ญ์ œํ•˜๋Š” ๋ช…๋ น์–ด

  : ๋…ผ๋ฆฌ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ์™€ ๋ฌผ๋ฆฌ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ์˜ ์‚ฌ์ƒ ์ •์˜

  : DBA ๋˜๋Š” ์„ค๊ณ„์ž๋งŒ ์‚ฌ์šฉ โ‡จ ์‹ค์ œ๋กœ ๊ฐœ๋ฐœ์ž๋Š” DML์„ ์ฃผ๋กœ ์‚ฌ์šฉํ•˜๋ฉฐ, DDL์€ DB์„ค๊ณ„์ž๋“ค์ด ์ฃผ๋กœ ์‚ฌ์šฉ

     1) CREATE

CREATE SCHEMA ์Šค๋ฏธ์นด๋ช… AUTHORIZATION ์‚ฌ์šฉ์ž;
CREATE DOMAIN ๋„๋ฉ”์ธ๋ช… [AS] ๋ฐ์ดํ„ฐํƒ€์ž… [DEFAULT ๊ธฐ๋ณธ๊ฐ’] [CONSTRAINT ์ œ์•ฝ์กฐ๊ฑด];
-- CREATE TABLE ํ…Œ์ด๋ธ”๋ช… (์†์„ฑ๋ช… ๋ฐ์ดํ„ฐํƒ€์ž…, … FOREIGN KEY(์™ธ๋ž˜ํ‚ค์ ์šฉ์†์„ฑ๋ช…) REFERENCES ์ฐธ์กฐํ…Œ์ด๋ธ”๋ช…(๊ธฐ๋ณธํ‚ค) .. );
CREATE TABLE ํ•™์ƒ (ํ•™๋ฒˆ NUMBER(8), ์ด๋ฆ„ VARCHAR2 NOT NULL, ์ „๊ณต VARCHAR2, 
	PRAMARY KEY(ํ•™๋ฒˆ),
    FOREIGN KEY(์ „๊ณต) REFERENCESํ•™๊ณผ(ํ•™๊ณผ์ฝ”๋“œ)
    	ON DELETE SET NULL
        ON UPDATE CASCADE);

-- CREATE TABLE ํ…Œ์ด๋ธ”๋ช… AS SELECT ์†์„ฑ๋ช… FROM ๊ธฐ์กดํ…Œ์ด๋ธ”๋ช…;
CREATE TABLE ๊ณต๊ณผ๋Œ€ํ•™ AS SELECT ํ•™๋ฒˆ, ์ด๋ฆ„ FROM ๋Œ€ํ•™;
-- CREATE VIEW ๋ทฐ๋ช…(์†์„ฑ๋ช…, ..) AS SELECT๋ฌธ; โžก๏ธŽ ์„œ๋ธŒ์ฟผ๋ฆฌ(UNION, ORDER BY์‚ฌ์šฉ๋ถˆ๊ฐ€)์— ๋Œ€ํ•œ ๊ฒฐ๊ณผ๋กœ ๋ทฐ๋ฅผ ์ƒ์„ฑ
-- ์†์„ฑ๋ช… ์ƒ๋žต์‹œ ์ž๋™์œผ๋กœ ๊ธฐ์กด ์†์„ฑ๋ช… ์ ์šฉ
CREATE VIEW ๊ฐ•๋‚จ๊ตฌ๊ณ ๊ฐ(์ด๋ฆ„, ์ „ํ™”๋ฒˆํ˜ธ) AS SELECT ์„ฑ๋ช…, ํœด๋Œ€ํฐ FROM ๊ณ ๊ฐ WHERE ์ฃผ์†Œ1='๊ฐ•๋‚จ๊ตฌ';
-- CREATE [UNIQUE] INDEX ์ธ๋ฑ์Šค๋ช… ON ํ…Œ์ด๋ธ”๋ช…(์†์„ฑ DESC|ASC);
CREATE UNIQUE INDEX ๊ณ ๊ฐ๋ฒˆํ˜ธ_idx ON ๊ณ ๊ฐ(๊ณ ๊ฐ๋ฒˆํ˜ธ DESC);

     2) ALTER : ๋ณ€๊ฒฝ

-- ADD : ์—ด ์ถ”๊ฐ€
ALTER TABLE ํ•™์ƒ ADD ์ฃผ์†Œ VARCHAR2(100);
-- ALTER : ์ปฌ๋Ÿผ ๊ธฐ๋ณธ๊ฐ’ ๋ณ€๊ฒฝ
ALTER TABLE ํ•™์ƒ ALTER ํ•™๋ฒˆ NUMBER(8) NOT NULL;
-- DROP : ์—ด ์‚ญ์ œ
ALTER TABLE ํ•™์ƒ DROP ์ทจ๋ฏธ;

     3) DROP : ์‚ญ์ œ (TRUNCATE๋Š” ๋ฐ์ดํ„ฐ๋งŒ ๋‚ ๋ฆฌ๋Š”๊ฒƒ)

DROP SCHEMA|DOMAIN|TABLE|VIEW|INDEX|CONSTRAINT ๋ช…์นญ [CASCADE|RESTRICTED];
-- CASCADE : ์ฐธ์กฐํ•˜๋Š” ๋ชจ๋“  ๊ฐœ์ฒด๋ฅผ ์—ฐ์‡„์ ์œผ๋กœ ์‚ญ์ œ์ฒ˜๋ฆฌ
-- RESTRICTED : ์ฐธ์กฐ์ค‘์ธ ๊ฒฝ์šฐ ์ œ๊ฑฐ ๋ช…๋ น์„ ์ทจ์†Œ

   

 

 

2. DML(Data Manipulation Language)

  : ํ•œ๋ฒˆ์— ํ•œ ํ…Œ์ด๋ธ”๋งŒ ์กฐ์ž‘ ๊ฐ€๋Šฅ

  : DB์‚ฌ์šฉ์ž์™€ DBMS๊ฐ„ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ œ๊ณตํ•˜๋ฉฐ ์‹ค์งˆ์ ์ธ ๋ฐ์ดํ„ฐ ๊ด€๋ฆฌ ๋ช…๋ น์–ด

     1) INSERT

-- INSERT INTO ํ…Œ์ด๋ธ”๋ช…(์†์„ฑ๋ช…,...) VALUES(๊ฐ’,...);
INSERT INTO STUDENT(NAME, NUM, SCORE) VALUES('๊ธธ๋™์ด',30203940, 'A');
INSERT INTO ๋ฐฉ์†ก๋ถ€์›(์ด๋ฆ„, ํ•™๋…„, ์ „ํ™”๋ฒˆํ˜ธ) SELECT ์ด๋ฆ„, ํ•™๋…„, ์ „ํ™”๋ฒˆํ˜ธ FROM ํ•™์ƒ WHERE ๋ถ€์„œ='๋ฐฉ์†ก๋ถ€';

     2) UPDATE

-- UPDATE ํ…Œ์ด๋ธ”๋ช… SET ์†์„ฑ๋ช…=๊ฐ’,... WHERE ์กฐ๊ฑด์‹;
UPDATE ์ˆ˜ํ•™๊ณผ SET ์ฃผ์†Œ='์„œ์šธ์‹œ ์†กํŒŒ๊ตฌ' WHERE ์ด๋ฆ„='๋™๋™์ด';
-- ์ˆ˜์‹์‚ฌ์šฉ ๊ฐ€๋Šฅ
UPDATE ์‚ฌ์› SET ์ง๊ธ‰='๋Œ€๋ฆฌ', ์›”๊ธ‰=์›”๊ธ‰+500000 WHERE ์‚ฌ์›๋ฒˆํ˜ธ=93820;

     3) DELETE

-- DELETE FROM ํ…Œ์ด๋ธ”๋ช… [WHERE ์กฐ๊ฑด์‹];
DELETE FROM ์‚ฌ์› WHERE ๋ถ€์„œ='์˜์—…3';
-- ์กฐ๊ฑด์‹ ์ƒ๋žต์‹œ ํ•ด๋‹น ํ…Œ์ด๋ธ”์˜ ๋ชจ๋“  ํŠœํ”Œ ์ œ๊ฑฐ
DELETE FROM ์˜ˆ์•ฝ๋ช…๋‹จ;

     4) SELECT

-- SELECT ์†์„ฑ๋ช… .. FROM ํ…Œ์ด๋ธ”๋ช… [WHERE ์กฐ๊ฑด์‹] [GROUP BY ์†์„ฑ๋ช…] [HAVING ์กฐ๊ฑด] [ORDER BY ์†์„ฑ๋ช… [DESC|ASC]];
SELECT ์ด๋ฆ„ FROM ํ•™์ƒ WHERE ์ฃผ์†Œ LIKE '์„œ์šธ์‹œ'% AND ํ•™๋…„ IN(3,4);
-- DISTINCT : ์ค‘๋ณต์ œ๊ฑฐ
SELECT DISTINT ์ฃผ์†Œ FROM ์ง€์ ;
-- BETWEEN : ์‚ฌ์ด๊ฐ’(ํฌํ•จ)
SELECT ์‚ฌ์›๋ช… FROM ์‚ฌ์› WHERE ์ƒ๋…„์›”์ผ BETWEEN '90/01/01' AND '99/12/31';
-- IS NULL : ๋„๊ฐ’ ๋น„๊ต
SELECT ์ด๋ฆ„ FROM ํ•™์ƒ WHERE ์ ์ˆ˜ IS NULL;
SELECT ์ด๋ฆ„ FROM ํ•™์ƒ WHERE ์ ์ˆ˜ IS NOT NULL;
-- ๋ณต์ˆ˜ํ…Œ์ด๋ธ” ๊ฒ€์ƒ‰ : ํ…Œ์ด๋ธ”.์ปฌ๋Ÿผ๋ช…์œผ๋กœ ์ ‘๊ทผ
SELECT ์‚ฌ์›.์ด๋ฆ„, ์‚ฌ์›.๋ถ€์„œ, ์—ฌ๊ฐ€ํ™œ๋™.์ทจ๋ฏธ, ์—ฌ๊ฐ€ํ™œ๋™.๊ฒฝ๋ ฅ FROM ์‚ฌ์›, ์—ฌ๊ฐ€ํ™œ๋™ WHERE ์—ฌ๊ฐ€ํ™œ๋™.๊ฒฝ๋ ฅ>=10 AND ์—ฌ๊ฐ€ํ™œ๋™.์ด๋ฆ„=์‚ฌ์›.์ด๋ฆ„;

 

 

3. DCL(Data Control Language)

  : ๋ฐ์ดํ„ฐ ์ ‘๊ทผ ๊ถŒํ•œ์„ ๋ถ€์—ฌํ•˜๋Š” ๋ช…๋ น์–ด

  : ๋ณด์•ˆ, ๋ฌด๊ฒฐ, ํšŒ๋ณต, ๋ณ‘ํ–‰์ œ์–ด ๋“ฑ ๋ฐ์ดํ„ฐ ๊ด€๋ฆฌ๊ฐ€ ๋ชฉ์ 

    # ์‚ฌ์šฉ์ž ๋“ฑ๊ธ‰ : DBA(DB๊ด€๋ฆฌ์ž), Resource(DB๋ฐ ํ…Œ์ด๋ธ” ์ƒ์„ฑ ๊ฐ€๋Šฅ์ž), Connect(๋‹จ์ˆœ์‚ฌ์šฉ์ž) 

     1) GRNAT : ๊ถŒํ•œ๋ถ€์—ฌ

-- ์‚ฌ์šฉ๋“ฑ๊ธ‰ ๊ถŒํ•œ ๋ถ€์—ฌ : GRANT ์‚ฌ์šฉ์ž๋“ฑ๊ธ‰ TO ์‚ฌ์šฉ์žID;
GRANT RESOURCE TO IRENE;
-- ํ…Œ์ด๋ธ” ๊ถŒํ•œ ๋ถ€์—ฌ : GRANT ๊ถŒํ•œ ON ๊ฐœ์ฒด TO ์‚ฌ์šฉ์ž [WITH GRANT OPTION];
GRANT UALL ON ๊ณ ๊ฐ TO NATE WITH GRANT OPTION;

     2) REVOKE : ๊ถŒํ•œํšŒ์ˆ˜

-- ์‚ฌ์šฉ์ž ๊ถŒํ•œ ๋ฐ•ํƒˆ : REVOKE ๋“ฑ๊ธ‰ FROM ์‚ฌ์šฉ์žID; 
REVOKE DBA FROM AMY; 
-- ํ…Œ์ด๋ธ” ๊ถŒํ•œ ๋ฐ•ํƒˆ : REVOKE [GRANT OPTION FOR] ๊ถŒํ•œ ON ๊ฐœ์ฒด FROM ์‚ฌ์šฉ์ž [CASCADE];
REVOKE GRANT OPTION FOR UPDATE ON ๊ณ ๊ฐ FROM JIMMY CASCADE;

 

 

4. TCL(Transaction control Language)

   : ํŠธ๋žœ์žญ์…˜ ์ œ์–ด์–ด

 

     1) COMMIT

     : ํŠธ๋žœ์žญ์…˜์ด ์„ฑ๊ณต์ ์œผ๋กœ ์ข…๋ฃŒ๋œ ๊ฒฝ์šฐ ์ผ๊ด€์„ฑ์„ ์œ ์ง€ํ•˜๊ธฐ ์œ„ํ•ด ์ „์ฒด DB์— ๋ฐ˜์˜ํ•˜๋Š” ๋ช…๋ น์–ด

     2) ROLLBACK

     : ์•„์ง COMMIT๋˜์ง€ ์•Š์€ ๋ณ€๊ฒฝ๋œ ๋ชจ๋“  ๋‚ด์šฉ์„ ์ทจ์†Œํ•˜๊ณ  DB๋ฅผ ์ด์ „์ƒํƒœ๋กœ ๋˜๋Œ๋ฆฌ๋Š” ๋ช…๋ น์–ด

     3) SAVEPOINT

     : ROLLBACK ์ง€์ ์„ ์ง€์ •ํ•˜๋Š” ๋ช…๋ น์–ด โ‡จ SAVEPOINT S1;

 

 

 

๋Œ“๊ธ€