SQL ํ™œ์šฉ ์™„๋ฒฝ ๊ฐ€์ด๋“œ

์ถœ์ œ ๋นˆ๋„: ์ƒ
๋นˆ์ถœ ํƒœ๊ทธ: CASCADE, RESTRICT, ์‚ญ์ œ/์กฐํšŒ/๊ถŒํ•œ ๋ถ€์—ฌ ๋ฌธ๋ฒ•

๋ฐ์ดํ„ฐ ์ •์˜์–ด (DDL)

CREATE

  • ๊ฐ์ฒด(DB, TABLE, INDEX, VIEW ๋“ฑ) ์ƒ์„ฑ
  • ๋ฉ”ํƒ€์–ธ์–ด ํ‘œํ˜„: CREATE <๊ฐ์ฒด์œ ํ˜•> <๊ฐ์ฒด๋ช…> <์˜ต์…˜>;
    • < >: ์ž…๋ ฅ์š”์†Œ (์ƒ๋žต๊ฐ€๋Šฅ)
    • [ ]: ์„ ํƒ์š”์†Œ
    • [, ...]: ๋ฐ˜๋ณต๊ฐ€๋Šฅ
    • |: ์„ ํƒ๊ฐ€๋Šฅ

ํ…Œ์ด๋ธ” ์ƒ์„ฑ ๋ฌธ๋ฒ•

CREATE TABLE <ํ…Œ์ด๋ธ”๋ช…> (
    <์ปฌ๋Ÿผ๋ช…> <๋ฐ์ดํ„ฐ์œ ํ˜•> [<์ œ์•ฝ์กฐ๊ฑด>] [, ...],
    [ํ…Œ์ด๋ธ” ์ œ์•ฝ์กฐ๊ฑด]
);

๋ฐ์ดํ„ฐ ์œ ํ˜•๊ณผ ์ œ์•ฝ์กฐ๊ฑด

  • ๋ฐ์ดํ„ฐ ์œ ํ˜•:
    • INT, DOUBLE
    • CHAR - ๊ณ ์ •ํฌ๊ธฐ ๋ฌธ์ž์—ด
    • VARCHAR(N) - ๊ฐ€๋ณ€ํฌ๊ธฐ ๋ฌธ์ž์—ด
    • DATE
    • UNIQUE - ์œ ์ผํ‚ค ์‹๋ณ„์ž
    • NOT NULL - ๊ณต๋ฐฑ ๋ถˆํ—ˆ
    • CHECK - ์กฐ๊ฑด์‹ ๋งŒ์กฑ๊ฐ’๋งŒ ํ—ˆ์šฉ
  • ์ œ์•ฝ์กฐ๊ฑด:
    • PRIMARY KEY (<์ปฌ๋Ÿผ๋ช…>[, ...]) - ๊ธฐ๋ณธํ‚ค
    • FOREIGN KEY (<์ปฌ๋Ÿผ๋ช…>[, ...]) REFERENCES (<์ปฌ๋Ÿผ๋ช…>[, ...])
    • ON UPDATE <์ฒ˜๋ฆฌ์˜ต์…˜>
    • ON DELETE <์ฒ˜๋ฆฌ์˜ต์…˜>

โญ์ฒ˜๋ฆฌ์˜ต์…˜โญ

  • NO ACTION: ์•„๋ฌด ์ž‘์—… ์•ˆํ•จ
  • SET DEFAULT: ๊ธฐ๋ณธ๊ฐ’ ์ง€์ •
  • SET NULL: ๊ณต๋ฐฑ ์ฒ˜๋ฆฌ
  • CASCADE: ๊ด€๋ จ ํŠœํ”Œ ๋ชจ๋‘ ์ฒ˜๋ฆฌ
  • RESTRICT: ๊ด€๋ จ ํŠœํ”Œ ์—†๋Š” ๊ฒฝ์šฐ๋งŒ ์ฒ˜๋ฆฌ
  • NULLIFY: NULL ๊ฐ’์œผ๋กœ ๋น„์›€

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

CREATE TABLE <ํ…Œ์ด๋ธ”๋ช…> AS <SELECT๋ฌธ>;

์˜ˆ์‹œ

CREATE TABLE ํ•™์ƒ(
    ํ•™๋ฒˆ INT PRIMARY KEY,
    ์ด๋ฆ„ CHAR(10) NOT NULL,
    ์—ฐ๋ฝ์ฒ˜ CHAR(15) DEFAULT '๋น„๊ณต๊ฐœ'
);

CREATE TABLE ๋„์„œ๋Œ€์—ฌ(
    ๋Œ€์—ฌ์ผ DATE NOT NULL,
    ํ•™๋ฒˆ INT,
    ๋„์„œ๋ช… CHAR(20) NOT NULL,
    FOREIGN KEY(ํ•™๋ฒˆ) REFERENCES ํ•™์ƒ(ํ•™๋ฒˆ)
        ON UPDATE CASCADE
        ON DELETE RESTRICT
);

ALTER

  • ์ปฌ๋Ÿผ ๋ณ€๊ฒฝ:
    ALTER TABLE <ํ…Œ์ด๋ธ”๋ช…> ADD <ํ•„๋“œ๋ช…> <๋ฐ์ดํ„ฐํƒ€์ž…> [<์œ„์น˜์˜ต์…˜>];
    ALTER TABLE <ํ…Œ์ด๋ธ”๋ช…> MODIFY <์ปฌ๋Ÿผ๋ช…> <๋ฐ์ดํ„ฐ์œ ํ˜•>;
    ALTER TABLE <ํ…Œ์ด๋ธ”๋ช…> RENAME COLUMN <์›๋ณธ์ปฌ๋Ÿผ๋ช…> TO <๋ณ€๊ฒฝ์ปฌ๋Ÿผ๋ช…>;
    
  • ์ปฌ๋Ÿผ ์‚ญ์ œ:
    ALTER TABLE <ํ…Œ์ด๋ธ”๋ช…> DROP <์ปฌ๋Ÿผ๋ช…>;
    
  • ์ œ์•ฝ์กฐ๊ฑด ๋ณ€๊ฒฝ:
    ALTER TABLE <ํ…Œ์ด๋ธ”๋ช…> ADD CONSTRAINT <์ œ์•ฝ์กฐ๊ฑด๋ช…> <์ œ์•ฝ์กฐ๊ฑด>;
    ALTER TABLE <ํ…Œ์ด๋ธ”๋ช…> ENABLE|DISABLE|DROP CONSTRAINT <์ œ์•ฝ์กฐ๊ฑด๋ช…>;
    

DROP

DROP <๊ฐ์ฒด์œ ํ˜•> <๊ฐ์ฒด๋ช…> [<์‚ญ์ œ์˜ต์…˜>];
TRUNCATE TABLE <ํ…Œ์ด๋ธ”๋ช…>;

๋ฐ์ดํ„ฐ ์กฐ์ž‘์–ด (DML)

INSERT

-- ๋ชจ๋“  ๊ฐ’ ์‚ฝ์ž…
INSERT INTO <ํ…Œ์ด๋ธ”๋ช…> VALUES (<๊ฐ’>[, ...]);

-- ํŠน์ • ์ปฌ๋Ÿผ ๊ฐ’ ์‚ฝ์ž…
INSERT INTO <ํ…Œ์ด๋ธ”๋ช…> (<์ปฌ๋Ÿผ๋ช…>[, ...]) VALUES (<๊ฐ’>[, ...]);

-- ๋‹ค๋ฅธ ํ…Œ์ด๋ธ”์—์„œ ๋ณต์‚ฌ
INSERT INTO <ํ…Œ์ด๋ธ”๋ช…> (<ํ•„๋“œ>[, ...]) <SELECT๋ฌธ>

UPDATE

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

DELETE

DELETE FROM <ํ…Œ์ด๋ธ”๋ช…> WHERE <์กฐ๊ฑด์‹>;
DELETE * FROM <ํ…Œ์ด๋ธ”๋ช…> WHERE <์กฐ๊ฑด์‹>;

SELECT

SELECT [ALL|DISTINCT] <์ปฌ๋Ÿผ๋ช…>[,...] FROM <ํ…Œ์ด๋ธ”๋ช…>
    [WHERE <์กฐ๊ฑด์‹>
    [GROUP BY <์ปฌ๋Ÿผ๋ช…> [HAVING <์กฐ๊ฑด์‹>]]
    [ORDER BY <์ปฌ๋Ÿผ๋ช…> [ASC | DESC]]];

SELECT ํ™œ์šฉ

AS (๋ณ„์นญ)

  • ์ง‘๊ณ„ํ•จ์ˆ˜:
    • COUNT(): ๋ ˆ์ฝ”๋“œ ๊ฐœ์ˆ˜
    • SUM()/AVG(): ํ•ฉ๊ณ„/ํ‰๊ท 
    • MAX()/MIN(): ์ตœ๋Œ€๊ฐ’/์ตœ์†Œ๊ฐ’
    • STDDEV(): ํ‘œ์ค€ํŽธ์ฐจ
    • VARIAN(): ๋ถ„์‚ฐ
SELECT SUM(์ ์ˆ˜) AS ํ•ฉ๊ณ„ FROM ํ•™์ƒ์ •๋ณด;
SELECT ์ด๋ฆ„ AS ํ•™์ƒ๋ช…, ์ ์ˆ˜-5 AS ์„ฑ์  FROM ํ•™์ƒ์ •๋ณด;

โญ์œˆ๋„์šฐ ํ•จ์ˆ˜โญ

  • ์ˆœ์œ„ํ•จ์ˆ˜:
    • RANK: ๋™์ผ ์ˆœ์œ„๋งŒํผ ๋‹ค์Œ ์ˆœ์œ„ ๊ฑด๋„ˆ๋œ€
    • DENSE_RANK: ๋™์ผ ์ˆœ์œ„๋ฅผ ํ•˜๋‚˜๋กœ ์ฒ˜๋ฆฌ
    • ROW_NUMBER: ์ˆœ์ฐจ์  ์ˆœ์œ„ ๋ถ€์—ฌ
  • ํ–‰์ˆœ์„œ ํ•จ์ˆ˜:
    • FIRST_VALUE: ์ตœ์†Œ๊ฐ’
    • LAST_VALUE: ์ตœ๋Œ€๊ฐ’
    • LAG: ์ด์ „ N๋ฒˆ์งธ ํ–‰์˜ ๊ฐ’
    • LEAD: ์ดํ›„ N๋ฒˆ์งธ ํ–‰์˜ ๊ฐ’

์กฐ๊ฑด์‹

AND

SELECT * FROM ์„ฑ์  WHERE ๊ตญ์–ด>=80 AND ์˜์–ด>=80;
-- BETWEEN ํ™œ์šฉ
SELECT * FROM ์„ฑ์  WHERE ์ˆ˜ํ•™ BETWEEN 80 AND 89;

OR

SELECT * FROM ์„ฑ์  WHERE ๋ฐ˜="1๋ฐ˜" OR ๋ฐ˜="3๋ฐ˜" OR ๋ฐ˜="5๋ฐ˜";
-- IN ํ™œ์šฉ
SELECT * FROM ์„ฑ์  WHERE ๋ฐ˜ IN("1๋ฐ˜", "3๋ฐ˜", "5๋ฐ˜");

IS NULL

SELECT * FROM ์„ฑ์  WHERE ๋ฒŒ์  IS NULL;

LIKE

SELECT * FROM ์„ฑ์  WHERE ์ด๋ฆ„ LIKE <ํŒจํ„ด>;
  • LIKE %๋™: ๋™์œผ๋กœ ๋๋‚˜๋Š” ๋ฌธ์ž์—ด
  • LIKE ํ™%: ํ™์œผ๋กœ ์‹œ์ž‘ํ•˜๋Š” ๋ฌธ์ž์—ด
  • LIKE %๊ธธ%: ๊ธธ์„ ํฌํ•จํ•˜๋Š” ๋ฌธ์ž์—ด
  • LIKE ๊ฐ•_: ๊ฐ•์œผ๋กœ ์‹œ์ž‘ํ•˜๋Š” 2๊ธ€์ž
  • LIKE _๊ฐ•_: ๊ฐ•์ด ๊ฐ€์šด๋ฐ์ธ 3๊ธ€์ž
  • LIKE ๊ฐ•__: ๊ฐ•์œผ๋กœ ์‹œ์ž‘ํ•˜๋Š” 3๊ธ€์ž

ํ•˜์œ„ ์งˆ์˜ (Sub Query)

์„œ๋ธŒ ์ฟผ๋ฆฌ ๊ธฐ๋ณธ

  • ๋ฉ”์ธ ์ฟผ๋ฆฌ ์ด์ „์— ํ•œ๋ฒˆ๋งŒ ์‹คํ–‰
  • ๊ฒฐ๊ณผ๊ฐ’์€ ๋ฉ”์ธ์ฟผ๋ฆฌ์˜ ๋‚ด๋ถ€ ์š”์†Œ

์œ ์˜์‚ฌํ•ญ:

  • ๋น„๊ต์—ฐ์‚ฐ์ž ์˜ค๋ฅธ์ชฝ์— ๊ธฐ์ˆ 
  • ์†Œ๊ด„ํ˜ธ()๋กœ ๊ฐ์‹ธ๊ธฐ
  • ๋ฉ”์ธ์ฟผ๋ฆฌ๊ฐ€ ๊ธฐ๋Œ€ํ•˜๋Š” ํ–‰/์ปฌ๋Ÿผ ์ˆ˜์™€ ์ผ์น˜
  • ORDER BY ์‚ฌ์šฉ ๋ถˆ๊ฐ€

๋‹จ์ผ ํ–‰ ์„œ๋ธŒ์ฟผ๋ฆฌ

  • ํ•˜๋‚˜์˜ ํ–‰์œผ๋กœ ๊ฒฐ๊ณผ ๋ฐ˜ํ™˜
  • ์‚ฌ์šฉ ์—ฐ์‚ฐ์ž: =, <>, >, >=, <, <=
SELECT * FROM ์„ฑ์  
WHERE ํ•™๊ณผ=(SELECT ํ•™๊ณผ FROM ์„ฑ์  WHERE ์ด๋ฆ„="๊ถŒ์˜์„");

๋‹ค์ค‘ ํ–‰ ์„œ๋ธŒ์ฟผ๋ฆฌ

  • ์—ฌ๋Ÿฌ ํ–‰์œผ๋กœ ๊ฒฐ๊ณผ ๋ฐ˜ํ™˜
  • ์‚ฌ์šฉ ์—ฐ์‚ฐ์ž: IN, ANY, SOME, ALL, EXISTS
-- IN ์—ฐ์‚ฐ์ž ์˜ˆ์‹œ
SELECT ์„ฑ๋ช…, ํ•™๋…„ FROM ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค 
WHERE ํ•™๋ฒˆ IN (SELECT ์ธ๊ณต์ง€๋Šฅ.ํ•™๋ฒˆ FROM ์ธ๊ณต์ง€๋Šฅ);

-- EXISTS ์—ฐ์‚ฐ์ž ์˜ˆ์‹œ
SELECT ์„ฑ๋ช…, ํ•™๋…„ FROM ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค 
WHERE EXISTS (SELECT * FROM ์ธ๊ณต์ง€๋Šฅ 
              WHERE ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค.ํ•™๋ฒˆ = ์ธ๊ณต์ง€๋Šฅ.ํ•™๋ฒˆ);

์ •๋ ฌ๊ณผ ๊ทธ๋ฃนํ™”

์ •๋ ฌ (ORDER BY)

-- ๋‹จ์ผ ์ •๋ ฌ
SELECT * FROM ์„ฑ์  ORDER BY ์ ์ˆ˜ DESC;

-- ๋‹ค์ค‘ ์ •๋ ฌ
SELECT * FROM ์„ฑ์  ORDER BY ์„ฑ๋ณ„ ASC, ์„ฑ์  DESC;

๊ทธ๋ฃนํ™” (GROUP BY)

-- ๊ธฐ๋ณธ ๊ทธ๋ฃนํ™”
SELECT ์„ฑ๋ณ„, COUNT(*) FROM ์„ฑ์  GROUP BY ์„ฑ๋ณ„;

-- HAVING ์ ˆ ์‚ฌ์šฉ
SELECT ์„ฑ๋ณ„, COUNT(*) FROM ์„ฑ์  
GROUP BY ์„ฑ๋ณ„ HAVING COUNT(*)<3;

โญ๊ทธ๋ฃน ํ•จ์ˆ˜โญ

  • ROLLUP(): ์ฒซ ์นผ๋Ÿผ์˜ ๊ฐ ๊ทธ๋ฃน ํ•ฉ๊ณ„์™€ ์ „์ฒด ํ•ฉ๊ณ„
  • CUBE(): ๋ชจ๋“  ์นผ๋Ÿผ์˜ ๊ฐ ๊ทธ๋ฃน ํ•ฉ๊ณ„์™€ ์ „์ฒด ํ•ฉ๊ณ„
  • GROUPING SETS(): ๊ฐ ๊ทธ๋ฃน๋ณ„ ์ด ํ•ฉ๊ณ„๋งŒ ํ‘œ์‹œ
  • GROUPING(): ์ง‘๊ณ„ ํ•จ์ˆ˜ ์ง€์›

์กฐ์ธ (JOIN)

INNER JOIN

SELECT * FROM <์™ผ์ชฝ ํ…Œ์ด๋ธ”> 
JOIN <์˜ค๋ฅธ์ชฝ ํ…Œ์ด๋ธ”> 
ON <์™ผ์ชฝ ํ…Œ์ด๋ธ”>.<์ปฌ๋Ÿผ๋ช…> = <์˜ค๋ฅธ์ชฝ ํ…Œ์ด๋ธ”>.<์ปฌ๋Ÿผ๋ช…>;

OUTER JOIN

SELECT * FROM <์™ผ์ชฝ ํ…Œ์ด๋ธ”> 
OUTER JOIN <์˜ค๋ฅธ์ชฝ ํ…Œ์ด๋ธ”> 
ON <์™ผ์ชฝ ํ…Œ์ด๋ธ”>.<์ปฌ๋Ÿผ๋ช…> = <์˜ค๋ฅธ์ชฝ ํ…Œ์ด๋ธ”>.<์ปฌ๋Ÿผ๋ช…>;

LEFT/RIGHT JOIN

-- LEFT JOIN
SELECT * FROM <์™ผ์ชฝ ํ…Œ์ด๋ธ”> 
LEFT JOIN <์˜ค๋ฅธ์ชฝ ํ…Œ์ด๋ธ”> 
ON <์™ผ์ชฝ ํ…Œ์ด๋ธ”>.<์ปฌ๋Ÿผ๋ช…> = <์˜ค๋ฅธ์ชฝ ํ…Œ์ด๋ธ”>.<์ปฌ๋Ÿผ๋ช…>;

-- RIGHT JOIN
SELECT * FROM <์™ผ์ชฝ ํ…Œ์ด๋ธ”> 
RIGHT JOIN <์˜ค๋ฅธ์ชฝ ํ…Œ์ด๋ธ”> 
ON <์™ผ์ชฝ ํ…Œ์ด๋ธ”>.<์ปฌ๋Ÿผ๋ช…> = <์˜ค๋ฅธ์ชฝ ํ…Œ์ด๋ธ”>.<์ปฌ๋Ÿผ๋ช…>;

๋ฐ์ดํ„ฐ ์ œ์–ด์–ด (DCL)

๊ถŒํ•œ ์ œ์–ด

GRANT

GRANT <๊ถŒํ•œ ์œ ํ˜•> TO <๋Œ€์ƒ>
    [WITH GRANT OPTION | WITH ADMIN OPTION];
  • WITH GRANT OPTION: ๊ถŒํ•œ ๋ถ€์—ฌ/ํšŒ์ˆ˜ ๊ฐ€๋Šฅ
  • WITH ADMIN OPTION: ๊ถŒํ•œ ๋ถ€์—ฌ๋งŒ ๊ฐ€๋Šฅ

REVOKE

REVOKE <๊ถŒํ•œ ์œ ํ˜•> FROM <๋Œ€์ƒ>;

ROLE

  • ์‚ฌ์šฉ์ž ๊ทธ๋ฃน ๊ด€๋ฆฌ
  • RBAC(Role Based Access Control) ๋ฐฉ์‹ ์‚ฌ์šฉ

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

ํŠธ๋žœ์žญ์…˜ ํŠน์ง•

  • ๋…ผ๋ฆฌ์  ์—ฐ์‚ฐ๋“ค(DML)์˜ ์ตœ์†Œ ๋‹จ์œ„
  • ํŠน์ง•: ์›์ž์„ฑ, ์ผ๊ด€์„ฑ, ๊ณ ๋ฆฝ์„ฑ, ์ง€์†์„ฑ

ํŠธ๋žœ์žญ์…˜ ์ƒํƒœ

  • ํ™œ๋™(Active)
  • ์™„๋ฃŒ(Committed)
  • ์‹คํŒจ(Failed)
  • ์ฒ ํšŒ(Aborted)
  • ์‹คํ–‰ ์ทจ์†Œ(Undo)
  • ๋‹ค์‹œ ์‹คํ–‰(Redo)

์ œ์–ด ๋ช…๋ น์–ด

  • COMMIT: ๋ณ€๊ฒฝ์‚ฌํ•ญ ์ตœ์ข… ๋ฐ˜์˜
  • ROLLBACK: ์ด์ „ ์ƒํ™ฉ์œผ๋กœ ๋ณต๊ตฌ
  • SAVEPOINT: ROLLBACK ์ง€์  ์ง€์ •

์ ˆ์ฐจํ˜• SQL

๊ธฐ๋ณธ ๊ตฌ์„ฑ์š”์†Œ

  • DECLARE: ์ •์˜ ์˜์—ญ
  • BEGIN~END: ์‹ค์ œ ๊ตฌํ˜„ ์˜์—ญ
  • OR REPLACE: ๊ธฐ์กด ์ฝ”๋“œ ๋ฎ์–ด์“ฐ๊ธฐ

ํ”„๋กœ์‹œ์ € (Procedure)

  • ํ˜ธ์ถœ์„ ํ†ตํ•ด ์‹คํ–‰
  • ๋ฐ˜ํ™˜๊ฐ’ ์—†์Œ
  • ๊ตฌ์„ฑ: CONTROL, EXCEPTION, SQL, TRANSACTION

์‚ฌ์šฉ์ž ์ •์˜ ํ•จ์ˆ˜

  • ํ˜ธ์ถœ์„ ํ†ตํ•ด ์‹คํ–‰
  • ๋ฐ˜ํ™˜๊ฐ’ ์กด์žฌ
  • ๊ตฌ์„ฑ: CONTROL, EXCEPTION, SQL, RETURN

โญํŠธ๋ฆฌ๊ฑฐ (Trigger)โญ

  • ์ด๋ฒคํŠธ ๋ฐœ์ƒ ์‹œ ์ž๋™ ํ˜ธ์ถœ
  • ์ž…์ถœ๋ ฅ/๋ฐ˜ํ™˜๊ฐ’ ์—†์Œ

๊ตฌ์„ฑ์š”์†Œ

  • EVENT: ์‹คํ–‰ ์กฐ๊ฑด (ํ•„์ˆ˜)
  • CONTROL: ์ˆœ์ฐจ/๋ถ„๊ธฐ/๋ฐ˜๋ณต ์ฒ˜๋ฆฌ
  • EXCEPTION: ์˜ˆ์™ธ ์ฒ˜๋ฆฌ
  • SQL: ์ฃผ๋กœ DML ์‚ฌ์šฉ

์ˆ˜ํ–‰ ์‹œ์ 

  • AFTER: ํ…Œ์ด๋ธ” ๋ณ€๊ฒฝ ํ›„ ์‹คํ–‰
  • BEFORE: ํ…Œ์ด๋ธ” ๋ณ€๊ฒฝ ์ „ ์‹คํ–‰
  • FOR EACH NOW: ๋ ˆ์ฝ”๋“œ๋งˆ๋‹ค ํŠธ๋ฆฌ๊ฑฐ ์ˆ˜ํ–‰
  • NEW: ์ƒˆ๋กœ์šด ๋ฐ์ดํ„ฐ
  • OLD: ๊ธฐ์กด ๋ฐ์ดํ„ฐ

์ธ๋ฑ์Šค์™€ ๋ทฐ

์ธ๋ฑ์Šค

  • ๋น ๋ฅธ ๊ฒ€์ƒ‰์„ ์œ„ํ•œ ์ž๋ฃŒ๊ตฌ์กฐ
  • ๊ธฐ๋ณธํ‚ค ์„ค์ • ์‹œ ์ž๋™ ์ƒ์„ฑ
  • ์ˆ˜์ •์ด ์žฆ์„์ˆ˜๋ก ๋น„ํšจ์œจ์ 

๊ตฌํ˜„

-- ์ƒ์„ฑ
CREATE [UNIQUE] INDEX <์ธ๋ฑ์Šค๋ช…> 
ON <ํ…Œ์ด๋ธ”๋ช…>(<์ปฌ๋Ÿผ๋ช…>[, ...]);

-- ๋ณ€๊ฒฝ
ALTER [UNIQUE] INDEX <์ธ๋ฑ์Šค๋ช…> 
ON <ํ…Œ์ด๋ธ”๋ช…>(<์ปฌ๋Ÿผ๋ช…>[, ...]);

-- ์กฐํšŒ
SHOW INDEX FROM <ํ…Œ์ด๋ธ”๋ช…>;

๋ทฐ (View)

  • ๋…ผ๋ฆฌ์  ๊ฐ€์ƒ ํ…Œ์ด๋ธ”
  • ์‹œ์Šคํ…œ ์นดํƒˆ๋กœ๊ทธ์— ์ €์žฅ
  • ์ข…์† ํ…Œ์ด๋ธ” ์ œ๊ฑฐ ์‹œ ํ•จ๊ป˜ ์ œ๊ฑฐ

์žฅ์ 

  • ๋…ผ๋ฆฌ์  ๋…๋ฆฝ์„ฑ ์œ ์ง€
  • ์ ‘๊ทผ ๋ฐฉ๋ฒ• ๋‹จ์ˆœํ™”
  • ๋ฐ์ดํ„ฐ ๋ณด์•ˆ ์œ ์ง€

๋‹จ์ 

  • ์ธ๋ฑ์Šค ์‚ฌ์šฉ ๋ถˆ๊ฐ€
  • ์ˆ˜์ •/๋ณ€๊ฒฝ ๋ถˆ๊ฐ€

๊ตฌํ˜„

-- ์ƒ์„ฑ
CREATE VIEW <๋ทฐ ์ด๋ฆ„>(<์ปฌ๋Ÿผ ๋ชฉ๋ก>) 
AS SELECT๋ฌธ [์˜ต์…˜];

-- ์‚ญ์ œ
DROP VIEW <๋ทฐ ์ด๋ฆ„>;

-- ์กฐํšŒ
SELECT * FROM <๋ทฐ ์ด๋ฆ„>;

์ฃผ์š” ์˜ต์…˜

  • REPLACE: ์žฌ์ƒ์„ฑ
  • FORCE: ๋ฌด์กฐ๊ฑด ์ƒ์„ฑ
  • NOFORCE: ์›๋ณธ ํ…Œ์ด๋ธ” ์กด์žฌ ์‹œ์—๋งŒ ์ƒ์„ฑ
  • WITH CHECK OPTION: ์กฐ๊ฑด ์ปฌ๋Ÿผ ์ˆ˜์ • ๋ถˆ๊ฐ€
  • WITH READ ONLY: ์ „์ฒด ์ˆ˜์ • ๋ถˆ๊ฐ€

์‹œ์Šคํ…œ ์นดํƒˆ๋กœ๊ทธ

ํŠน์ง•

  • DBMS๊ฐ€ ์ž๋™ ์ƒ์„ฑ
  • ์กฐํšŒ๋งŒ ๊ฐ€๋Šฅ (์ง์ ‘ ๋ณ€๊ฒฝ ๋ถˆ๊ฐ€)
  • DDL ์‹คํ–‰ ์‹œ ์ž๋™ ๊ฐฑ์‹ 

SQL ์ง€์›๋„๊ตฌ

  • PL/SQL: ํ™•์žฅ SQL๋„๊ตฌ
  • SQL*Plus: Oracle ์ œ๊ณต ๋„๊ตฌ
  • APM: ์„ฑ๋Šฅ ๋ชจ๋‹ˆํ„ฐ๋ง
  • TKPROF: SQL ์ถ”์ /๋ถ„์„
  • EXPLAIN PLAN: SQL ์‹คํ–‰๊ฒฝ๋กœ ๋ถ„์„

๋ณ‘ํ–‰์ œ์–ด

๋ฌธ์ œ์ 

  • ๋ถ„์‹ค๋œ ๊ฐฑ์‹ 
  • ๋ชจ์ˆœ์„ฑ
  • ์—ฐ์‡„ ๋ณต๊ท€
  • ๋น„์™„๋ฃŒ ์˜์กด์„ฑ

๋กœํ‚น (Locking)

  • ๋‹ค๋ฅธ ํŠธ๋žœ์žญ์…˜ ์ ‘๊ทผ ์ œํ•œ
  • ๋กœํฌ ๋‹จ์œ„๊ฐ€ ํด์ˆ˜๋ก:
    • ๋ณ‘ํ–‰์„ฑ ๋‚ฎ์Œ
    • ์˜ค๋ฒ„ํ—ค๋“œ ๊ฐ์†Œ
  • ๋กœํฌ ๋‹จ์œ„๊ฐ€ ์ž‘์„์ˆ˜๋ก:
    • ๋ณ‘ํ–‰์„ฑ ๋†’์Œ
    • ์˜ค๋ฒ„ํ—ค๋“œ ์ฆ๊ฐ€

๋กœํ‚น ๊ธฐ๋ฒ•

  • ํƒ€์ž„ ์Šคํƒฌํ”„: ์ง๋ ฌ ์ฒ˜๋ฆฌ
  • ๋‚™๊ด€์  ๋ณ‘ํ–‰ ์ œ์–ด: ์ข…๋ฃŒ ์‹œ ์ผ๊ด„ ๊ฒ€์‚ฌ
  • ๋‹ค์ค‘ ๋ฒ„์ „ ๋ณ‘ํ–‰ ์ œ์–ด: ํƒ€์ž„ ์Šคํƒฌํ”„ ๊ธฐ๋ฐ˜ ๋ฒ„์ „ ์„ ํƒ

ํšŒ๋ณต

  • ์žฅ์•  ๋ฐœ์ƒ ์ด์ „ ์ƒํƒœ๋กœ ๋ณต์›
  • ์—ฐ์‚ฐ์ž: Undo, Redo

ํšŒ๋ณต ๊ธฐ๋ฒ•

  • ๋กœ๊ทธ ์ด์šฉ:
    • ์ฆ‰์‹œ ๊ฐฑ์‹ : ์ฆ‰์‹œ ๋ฐ˜์˜ + Undo
    • ์ง€์—ฐ ๊ฐฑ์‹ : ์™„๋ฃŒ ์‹œ ๋ฐ˜์˜ + Redo
  • ๊ฒ€์‚ฌ ์‹œ์ :
    • CheckPoint ์ด์ „: Redo
    • CheckPoint ์ดํ›„: Undo
  • ๊ทธ๋ฆผ์ž ํŽ˜์ด์ง•: ๋ณต์‚ฌ๋ณธ ๊ธฐ๋ฐ˜ ํšŒ๋ณต