CS_DB_2๏ธโƒฃ ๐Ÿ™

์ด์ •๋นˆยท2023๋…„ 11์›” 15์ผ
0

CS์Šคํ„ฐ๋””

๋ชฉ๋ก ๋ณด๊ธฐ
2/10

์•„์˜ค! ๋‚ ์”จ ๊ฐœ์ถฅ๋‹ค ใ…Žใ…Ž.. ํ™”์ดํŒ…ํ•ด์„œ ์ ์–ด๋ด…์‹œ๋‹ค.

๐Ÿ“Œ SQL์— ๋Œ€ํ•ด์„œ ์„ค๋ช…ํ•ด์ฃผ์„ธ์š”. C์–ธ์–ด์™€ ๊ฐ™์€ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด์™€ ์–ด๋–ค์ฐจ์ด๊ฐ€ ์žˆ๋‚˜์š”?

SQL(Structured Query Language)

SQL์€ Structured Query Language๋ฆฌ๋Š” ๋ง์— ๋งž๊ฒŒ ํด๋ผ์ด์–ธํŠธ์˜ ์š”์ฒญ(Query)๋ฅผ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์—์„œ ์‰ฝ๊ฒŒ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด ๋งŒ๋“ค์–ด์ง„ ์–ธ์–ด์ž…๋‹ˆ๋‹ค. ์‰ฝ๊ฒŒ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด ๋‹ค๋ฅธ ์–ธ์–ด์ธ Java, c++ ๋“ฑ์— SQL ๋ฌธ์„ ์‚ฝ์ž…ํ•˜์—ฌ Query๋ฅผ ์ฒ˜๋ฆฌํ•˜๊ณ  DB๋ฅผ ์กฐ์ž‘ํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ฅผ ๋‹ค๋ฃจ๋Š”๋ฐ ์žˆ์–ด ํ•„์ˆ˜์ ์ธ ์š”์†Œ๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

โœ… SQL๊ณผ C์–ธ์–ด์™€ ๊ฐ™์€ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด๋Š” ๊ฐœ๋ฐœ์˜ ๋ชฉ์  ์ž์ฒด๊ฐ€ ๋‹ค๋ฆ…๋‹ˆ๋‹ค. C์–ธ์–ด์™€ ๊ฐ™์€ ํ”„๋กœ๊ทธ๋žจ ์–ธ์–ด๋Š” ์‹œ์Šคํ…œ ์†Œํ”„ํŠธ์›จ์–ด๋‚˜ ์‘์šฉ ์†Œํ”„ํŠธ์›จ์–ด ๊ฐœ๋ฐœ์„ ๋ชฉ์ ์œผ๋กœ ๋งŒ๋“ค์–ด์ง„ ์–ธ์–ด์ด๋‚˜ SQL์€ ์ฃผ๋กœ Query๋ฅผ ํ†ตํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์™€ ์ƒํ˜ธ์ž‘์šฉํ•˜์—ฌ ์‚ฌ์šฉ์ž๊ฐ€ ์›ํ•˜๋Š” ์ •๋ณด๋ฅผ ๊ฒ€์ƒ‰ํ•˜๊ณ  ์กฐ์ž‘ํ•˜๊ธฐ ์œ„ํ•œ ๋ชฉ์ ์„ ๊ฐ๊ฐ์˜ ์–ธ์–ด๊ฐ€ ๊ฐ€์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.


๐Ÿ”Ž ์–ด๋–ป๊ฒŒ SQL ํ…Œ์ด๋ธ”์„ ์ •์˜ํ•˜๊ณ  CRUD์˜ ์ž‘์—…์„ ์ง„ํ–‰ํ• ๊นŒ?

  • Create
CREATE TABLE ์ œํ’ˆ (

์ œํ’ˆ๋ฒˆํ˜ธ CHAR(30) NOT NULL,
์ œํ’ˆ๋ช… VARCHAR(20),
์žฌ๊ณ ๋Ÿ‰ INT,
๋‹จ๊ฐ€ INT,
์ œ์กฐ์—…์ฒด VARCHAR(20),
PRIMARY KEY(์ œํ’ˆ๋ฒˆํ˜ธ),
CHECK (์žฌ๊ณ ๋Ÿ‰ ใ€‰= 0 AND ์žฌ๊ณ ๋Ÿ‰ ใ€ˆ= 1000)
);

CREATE TABLE ์ฃผ๋ฌธ (

์ฃผ๋ฌธ๋ฒˆํ˜ธ CHAR(30) NOT NULL,
์ฃผ๋ฌธ๊ณ ๊ฐ VARCHAR(20),
์ฃผ๋ฌธ์ œํ’ˆ CHAR(30),
์ˆ˜๋Ÿ‰ INT,
๋ฐฐ์†ก์ง€ VARCHAR(30),
์ฃผ๋ฌธ์ผ์ž DATETIME,
PRIMARY KEY(์ฃผ๋ฌธ๋ฒˆํ˜ธ),
FOREIGN KEY(์ฃผ๋ฌธ๊ณ ๊ฐ) REFERENCES ๊ณ ๊ฐ(๊ณ ๊ฐ์•„์ด๋””),
FOREIGN KEY(์ฃผ๋ฌธ์ œํ’ˆ) REFERENCES ์ œํ’ˆ(์ œํ’ˆ๋ฒˆํ˜ธ)

);
  • Insert new attribute
ALTER TABLE ํ…Œ์ด๋ธ”_์ด๋ฆ„

ADD ์†์„ฑ_์ด๋ฆ„ ๋ฐ์ดํ„ฐ_ํƒ€์ž… [NOT NULL] [DEFAULT ๊ธฐ๋ณธ_๊ฐ’];

ALTER TABLE ๊ณ ๊ฐ ADD ๊ฐ€์ž…๋‚ ์งœ DATETIME;
  • Delete attribute
ALTER TABLE ํ…Œ์ด๋ธ”_์ด๋ฆ„ DROP ์†์„ฑ_์ด๋ฆ„ CASCADE | RESTRICT;

ALTER TABLE ๊ณ ๊ฐ DROP ๋“ฑ๊ธ‰ CASCADE;

์œ„์˜ Cascade์™€ Restrict์˜ ์ฐจ์ด๋Š” ๊ด€๋ จ ์ฐธ์กฐํ•˜๋Š” ์†์„ฑ ํ˜น์€ ์ œ์•ฝ์กฐ๊ฑด ๊นŒ์ง€ ๊ฐ™์ด ์‚ญ์ œํ•˜๋Š”์ง€ ์•„๋‹Œ์ง€ ์—ฌ๋ถ€์— ๋”ฐ๋ผ ์šฐ๋ฆฌ๊ฐ€ ๊ธฐํ˜ธ์— ๋งž๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

  • Insert new Constraint
ALTER TABLE ํ…Œ์ด๋ธ”_์ด๋ฆ„ ADD CONSTRAINT ์ œ์•ฝ์กฐ๊ฑด_์ด๋ฆ„ ์ œ์•ฝ์กฐ๊ฑด_๋‚ด์šฉ;
ALTER TABLE ๊ณ ๊ฐ ADD CONSTRAINT CHK_AGE CHECK(๋‚˜์ด ใ€‰= 20);
  • Delete Constraint
ALTER TABLE ํ…Œ์ด๋ธ”_์ด๋ฆ„ DROP CONSTRAINT ์ œ์•ฝ์กฐ๊ฑด_์ด๋ฆ„;
ALTER TABLE ๊ณ ๊ฐ DROP CONSTRAINT CHK_AGE;
  • Delete Table
DROP TABLE ํ…Œ์ด๋ธ”_์ด๋ฆ„ CASCADE | RESTRICT;
DROP TABLE ๊ณ ๊ฐ RESTRICT;

๋ณธ๊ฒฉ์ ์œผ๋กœ ๋‘๋ฒˆ์งธ ์„ธ๋ฒˆ์งธ ์งˆ๋ฌธ์œผ๋กœ ๋„˜์–ด๊ฐ€๊ธฐ ์ด์ „์— ๊ธฐ๋ณธ์ ์ธ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์—์„œ ๋ฐ์ดํ„ฐ ์กฐ์ž‘ ๊ธฐ๋Šฅ์— ๋Œ€ํ•ด์„œ ์„ค๋ช…ํ•˜๊ณ  ๋‹ค์Œ์œผ๋กœ ๋„˜์–ด ๊ฐ€๊ฒ ์Šต๋‹ˆ๋‹ค!


๐Ÿ™‹๐Ÿป ๋ฐ์ดํ„ฐ์˜ ๊ฒ€์ƒ‰ (SELECT)

SELECT๋ฌธ์€ ๋‹ค์–‘ํ•œ ๊ฒ€์ƒ‰ ์œ ํ˜•์„ ์ง€์›ํ•˜๋ฉฐ, ์ผ๋ฐ˜ ์‚ฌ์šฉ์ž๋“ค์ด ๊ฐ€์žฅ ๋งŽ์ด ์‚ฌ์šฉํ•˜๋ฏ€๋กœ ๊ผผ๊ผผํžˆ ์‚ดํ‘œ๋ณด์•„์•ผ ํ•ฉ๋‹ˆ๋‹ค!

๐Ÿ”Ž ๊ธฐ๋ณธ๊ฒ€์ƒ‰

๊ธฐ๋ณธ์ ์œผ๋กœ ๊ฒ€์ƒ‰์„ ์œ„ํ•œ SELECT์˜ ์‚ฌ์šฉ ๋ฐฉ๋ฒ•์€ ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.

SELECT [ALL|DISTINCT] ์†์„ฑ_๋ฆฌ์ŠคํŠธ FROM ํ…Œ์ด๋ธ”_๋ฆฌ์ŠคํŠธ;

SELECT ๊ณ ๊ฐ์•„์ด๋””, ๊ณ ๊ฐ์ด๋ฆ„, ๋“ฑ๊ธ‰ FROM ๊ณ ๊ฐ;
SELECT * FROM ๊ณ ๊ฐ;

๋ชจ๋“  ์†์„ฑ ๊ฒ€์ƒ‰

ํŠน์ • ์†์„ฑ๋งŒ ๊ฒ€์ƒ‰

๋‘๋ฒˆ์งธ SQL๋ฌธ์—์„œ ์•Œ ์ˆ˜ ์žˆ๋Š” ๋‚ด์šฉ์€ ๊ณ ๊ฐ์ด๋ผ๋Š” ํฐ ํ…Œ์ด๋ธ”์—์„œ ๊ณ ๊ฐ ์•„์ด๋””, ๊ณ ๊ฐ ์ด๋ฆ„, ๋“ฑ๊ธ‰์ด๋ผ๋Š” Attribute ๊ฐ’์„ ๊ฒ€์ƒ‰ํ•œ ๊ฒƒ ์ž…๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๊ทธ ๋‹ค์Œ์œผ๋กœ ์˜ค๋Š” Query์—์„œ๋Š” *๋ฅผ ๋„ฃ์—ˆ๋Š”๋ฐ ์ด ๊ฒƒ์€ ๊ณ ๊ฐ์ด๋ผ๋Š” ํ…Œ์ด๋ธ”์— ๋ชจ๋“  ์†์„ฑ ๊ฐ’์„ ๊ฒ€์ƒ‰ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋‘๋ฒˆ์งธ ํŠน์ • ์†์„ฑ๋งŒ ๊ฒ€์ƒ‰ ํ–ˆ์„ ๊ฒฝ์šฐ ์•Œ ์ˆ˜ ์žˆ๋Š” ์ฐจ์ด๋Š” ์ผ๋ฐ˜ ๋ฆด๋ ˆ์ด์…˜๊ณผ๋Š” ๋‹ค๋ฅด๊ฒŒ ์ค‘๋ณต๊ฐ’์ด ๋งŽ์ด ๋‚˜์˜ค๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ SELECT๋ฅผ ์‚ฌ์šฉํ–ˆ์„๋•Œ ์ค‘๋ณต๊ฐ’์„ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๐Ÿ”Ž ์ค‘๋ณต ์ œ๊ฑฐ ๊ฒ€์ƒ‰

๋งŒ์•ฝ ์œ„ ๊ฐ™์€ ์ค‘๋ณต์„ ์—†์• ๊ณ  ์‹ถ์€ ๊ฒฝ์šฐ๋Š” DISTINCT๋ฅผ ์‚ฝ์ž…ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

SELECT DISTINCT ์ œ์กฐ์—…์ฒด FROM ์ œํ’ˆ;

์ค‘๋ณต ์ œ๊ฑฐ ํ…Œ์ด๋ธ” ์œ„์˜ `DISTINCT`๋Š” ์‹ค์ œ ํ•ด๋‹น ์œ„์น˜์˜ ๊ฐ’์ด ๋ณ€ํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹Œ ์ˆœ์ˆ˜ํ•˜๊ฒŒ ์ค‘๋ณต์ด ์ œ๊ฑฐ๋œ ํ›„์— ๋‚จ์€ ๊ฐ’๋“ค์„ ๋ณด์—ฌ์ฃผ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

๐Ÿ”Ž ํŠน๋ณ„ ๊ฒ€์ƒ‰

SELECT ์ œํ’ˆ๋ช…, ๋‹จ๊ฐ€ AS ๊ฐ€๊ฒฉ FROM ์ œํ’ˆ;

์œ„ ๊ฐ™์ด ์ฟผ๋ฆฌ๋ฅผ ์ž‘์„ฑํ•˜๊ณ  ์—”ํ„ฐ๋ฅผ ๋ˆ„๋ฅด๊ฒŒ ๋  ๊ฒฝ์šฐ ๋‹จ๊ฐ€๋ผ๋Š” ์†์„ฑ๊ฐ’์ด ๊ฐ€๊ฒฉ์œผ๋กœ ๋ฐ”๋€ ํ›„์˜ ํ…Œ์ด๋ธ”์„ ๊ฒ€์ƒ‰ํ•ด์„œ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๐Ÿ“Ž ๋ฐ์ดํ„ฐ ์‚ฝ์ž…

INSERT INTO ํ…Œ์ด๋ธ”_์ด๋ฆ„[(์†์„ฑ_๋ฆฌ์ŠคํŠธ)] VALUES (์†์„ฑ๊ฐ’_๋ฆฌ์ŠคํŠธ);
INSERT
INTO ๊ณ ๊ฐ(๊ณ ๊ฐ์•„์ด๋””, ๊ณ ๊ฐ์ด๋ฆ„, ๋‚˜์ด, ๋“ฑ๊ธ‰, ์ง์—…, ์ ๋ฆฝ๊ธˆ)
VALUES ('strawberry', '์ตœ์œ ๊ฒฝ', 30, 'vip', '๊ณต๋ฌด์›', 100);

SELECT * FROM ๊ณ ๊ฐ;

Insert with value query

์œ„์— ์‚ฌ์ง„์—์„œ ๋ณด์ด๋“ฏ์ด ๋ฆด๋ ˆ์ด์…˜ ์ธ์Šคํ„ด์Šค๋ฅผ ์ถ”๊ฐ€ ํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•˜๋Š” sql ๋ฌธ์ด INSERT [TABLE NAME](Attributes) VALUES(Relation Attributes and instance);์ด๋‹ค.

โ—๏ธNULL ๊ฐ’์ด ํ—ˆ์šฉ๋˜๋Š” ๊ฒฝ์šฐ

INSERT
INTO ๊ณ ๊ฐ(๊ณ ๊ฐ์•„์ด๋””, ๊ณ ๊ฐ์ด๋ฆ„, ๋‚˜์ด, ๋“ฑ๊ธ‰, ์ ๋ฆฝ๊ธˆ)
VALUES ('tomato', '์ •์€์‹ฌ', 36, 'gold', 4);

SELECT * FROM ๊ณ ๊ฐ;

ํ•ด๋‹น ํ…Œ์ด๋ธ”์„ ์„ ์–ธํ•˜๋Š” ๋‹จ๊ณ„์—์„œ ์ž…๋ ฅ๊ฐ’์— Not Null์ด๋ผ๋Š” ๊ฒƒ์„ ๊ฐ™์ด ์„ ์–ธํ•˜์ง€ ์•Š์€ Attribute์— ๋Œ€ํ•ด์„œ๋Š” null ๊ฐ’์ด ๋“ค์–ด๊ฐ€๋„ ๋ฌธ์ œ๊ฐ€ ์ƒ๊ธฐ์ง€ ์•Š๋Š”๋‹ค.

๐Ÿ› ๏ธ ๋ฐ์ดํ„ฐ ์ˆ˜์ •

UPDATE [TABLE_NAME] SET ATTRIBUTE_NAME1 = VAL1, ATTRIBUTE_NAME2 = VAL2, [WHERE]; 

Update๋ฌธ์€ ๋ฐ์ด๋ธ”์— ์ €์žฅ๋œ ํŠœํ”Œ์—์„œ ํŠน์ • ์†์„ฑ์˜ ๊ฐ’์„ ์ˆ˜์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์–ด๋–ป๊ฒŒ ์ˆ˜์ •ํ• ์ง€์— ๋Œ€ํ•œ ์ •๋ณด๋Š” SET ๋’ค์— ์ž…๋ ฅํ•œ ๋‚ด์šฉ์œผ๋กœ ์ˆ˜์ •์„ ์ง„ํ–‰ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ๋งŒ์•ฝ WHERE๋ฌธ์„ ์ƒ๋žตํ•˜๊ฒŒ ๋œ๋‹ค๋ฉด ๋ชจ๋“  ํŠœํ”Œ์— ๋Œ€ํ•ด์„œ ๋‚ด์šฉ์ด ์ˆ˜์ •์ด ๋˜๊ธฐ ๋•Œ๋ฌธ์— ์ฃผ์˜ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

UPDATE ์ฃผ๋ฌธ
SET ์ˆ˜๋Ÿ‰ = 5
WHERE ์ฃผ๋ฌธ๊ณ ๊ฐ IN (SELECT ๊ณ ๊ฐ์•„์ด๋””
                 FROM ๊ณ ๊ฐ
                 WHERE ๊ณ ๊ฐ์ด๋ฆ„ = '์ •์†Œํ™”');

SELECT * FROM ์ฃผ๋ฌธ;

๐Ÿ—‘๏ธ ๋ฐ์ดํ„ฐ ์‚ญ์ œ

DELETE FROM [TABLE_NAME] [WHERE];

DELETE
FROM ์ฃผ๋ฌธ
WHERE ์ฃผ๋ฌธ์ผ์ž = '2013-05-22';

SELECT * FROM ์ฃผ๋ฌธ;

์ด DELETE ๋ฌธ๋„ ๊ทธ ์ „์˜ UPDATE์™€ ๊ฐ™์ด WHERE๋ฌธ์„ ์ƒ๋žตํ•˜๊ฒŒ ๋  ๊ฒฝ์šฐ ๋ชจ๋“  ํ…Œ์ด๋ธ”์˜ ๊ฐ’์— ๋Œ€ํ•ด ์ง„ํ–‰์ด ๋  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์œ ์˜ํ•ด์•ผํ•œ๋‹ค.


๐Ÿ“Œ ๊ฐœ๋ฐœ์ž๊ฐ€ ์ž‘์„ฑํ•œ SQL์ด ์–ด๋–ค ๊ณผ์ •์„ ํ†ตํ•ด ์‹คํ–‰ ๋˜๋Š”์ง€ ์„ค๋ช…ํ•ด์ฃผ์„ธ์š”.

SQL ํ”„๋กœ์„ธ์‹ฑ ๊ณผ์ •

โœ… ์ž‘์„ฑํ•œ SQL ์ฟผ๋ฆฌ๊ฐ€ ์‹คํ–‰๋˜๋Š” ๊ณผ์ •์€ ํฌ๊ฒŒ ํŒŒ์‹ฑ(parse), ์ปดํŒŒ์ผ, ์ตœ์ ํ™”(execute), ์‹คํ–‰(fetch)์œผ๋กœ ๋‚˜๋‰ฉ๋‹ˆ๋‹ค. ์šฐ์„  ์‚ฌ์šฉ์ž๊ฐ€ ์ž‘์„ฑํ•œ ์ฟผ๋ฆฌ๋Š” ๋ฌธ๋ฒ•์ ์œผ๋กœ ๋ถ„์„๋˜๊ณ , ๊ทธ ํ›„ ์ปดํŒŒ์ผ๋Ÿฌ์— ์˜ํ•ด ๊ธฐ๊ณ„์—์— ๊ฐ€๊นŒ์šด ํ˜•ํƒœ๋กœ ๋ณ€ํ™˜ ๋ฉ๋‹ˆ๋‹ค. ์ตœ์ ํ™”๋Š” ์‹คํ–‰ ๊ณ„ํš์„ ํšจ์œจ์ ์œผ๋กœ ๋งŒ๋“ค๊ธฐ ์œ„ํ•œ ๋‹จ๊ณ„์ด๋ฉฐ, ๋งˆ์ง€๋ง‰์œผ๋กœ ์ตœ์ ํ™”๋œ ๊ณ„ํš์— ๋”ฐ๋ผ ์‹ค์ œ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์—์„œ ์ฟผ๋ฆฌ๊ฐ€ ์‹คํ–‰๋ฉ๋‹ˆ๋‹ค.

โ“ What is SQL Parsing?
SQL Parsing์€ SQL ์‹คํ–‰ ๊ณผ์ •์— ํ•˜๋‚˜๋กœ์จ, ๊ฐ๊ฐ์˜ ๋‹ค๋ฅธ ๋ฃจํ‹ด์—์„œ ์‹คํ–‰์ด ๋  ์ˆ˜ ์žˆ๊ฒŒ SQL๋ฌธ์„ ์กฐ๊ฐ ์กฐ๊ฐ ๋‚˜๋ˆ„๋Š” ๋‹จ๊ณ„๋ฅผ ๋งํ•ฉ๋‹ˆ๋‹ค. ์‹ค์งˆ์ ์œผ๋กœ SQL Statement๊ฐ€ ์‹คํ–‰์ด ๋˜๋Š”๊ฒƒ์€ ๊ทธ๋ ‡๊ฒŒ ํŒŒ์‹ฑ๊ณผ์ •์—์„œ ์ชผ๊ฐœ์ง€๊ณ  ์ „์†ก๋œ Application์—์„œ ์ง„ํ–‰์ด ๋ฉ๋‹ˆ๋‹ค.

  • Application ์—์„œ SQL ๋ฐœํ–‰
  • Parse Call to Database for statement excution
    โžก๏ธ Parse call์€ ํŒŒ์‹ฑ๋œ SQL statement์™€ ๋‹ค๋ฅธ ์ฒ˜๋ฆฌ์ค‘์ธ ์ •๋ณด๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” private SQL ์˜์—ญ์„ ๋“ค์–ด๊ฐ€ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•˜์—ฌ ํ•ด๋‹น ์˜์—ญ์— ์ปค์„œ๋ฅผ ํ˜•์„ฑํ•œ๋‹ค.
    โžก๏ธ ์ด๋Ÿฌํ•œ Parse Call์„ ํ†ตํ•ด์„œ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋Š” Statement ์‹คํ–‰์ „์— ๋ฐœ๊ฒฌ ๋  ์ˆ˜ ์žˆ๋Š” ์˜ค๋ฅ˜๋“ค์„ ์‹๋ฒฝํ•˜๋Š” ํ™•์ธ์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค.

โ“ What is Syntax Check & Sementic Check & Shared Pool Check?

  • Syntax Check๋Š” ๋‹จ์ˆœํ•˜๊ฒŒ ๋‚ด๊ฐ€ ์ž…๋ ฅํ•œ SQL Statement์— ์˜ค๋ฅ˜๊ฐ€ ์žˆ๋Š”์ง€ ํ™•์ธ ํ•˜๋Š” ์ž‘์—…์„ ๋งํ•ฉ๋‹ˆ๋‹ค.
  • Sementic Check๋Š” ๋‚ด๊ฐ€ ์ž…๋ ฅํ•œ SQL Statement๊ฐ€ ์–ด๋–ค ๊ฒƒ์„ ์˜๋ฏธํ•˜๋Š”์ง€ ํ•ด์„ํ•˜๋Š” ์ž‘์—…์ž…๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ๋“ค์–ด ์ œ๊ฐ€ ์ •์˜๋˜์–ด ์žˆ์ง€ ์•Š์€ ํ…Œ์ด๋ธ”์„ ์‹ค์ˆ˜๋กœ SELECT๋ฅผ ํ• ๋•Œ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•˜๊ฒŒ ๋˜๋Š”๋ฐ ์ด๋ ‡๊ฒŒ ๋ฌธ๋ฒ•์ ์ธ ์‹ค์ˆ˜๊ฐ€ ์•„๋‹Œ ์ œ๊ฐ€ ์ž…๋ ฅํ•œ ๊ทธ Statement๊ฐ€ ์‹คํ–‰๋˜๊ธฐ ์œ„ํ•ด์„œ ์˜๋ฏธ๋ฅผ ํ•ด์„ํ•˜๋Š” ๊ณผ์ •์— ์žˆ์–ด ๋ฌธ์ œ๊ฐ€ ์žˆ๋Š”์ง€ ํŒŒ์•…์„ ํ•ฉ๋‹ˆ๋‹ค.
  • Shared pool check๋Š” ํŒŒ์‹ฑํ•˜๋Š” ๋™ํ•œ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์—์„œ ๋ฆฌ์†Œ์Šค๋ฅผ ๋งŽ์ด ์‚ฌ์šฉํ•˜๋Š”(์ฒ˜๋ฆฌ ์‹œ๊ฐ„์ด ๊ธธ์–ด์ง€๋Š”) ๋‹จ๊ณ„์— ๋Œ€ํ•ด์„œ ์Šคํ‚ต์ด ๊ฐ€๋Šฅํ•œ์ง€๋ฅผ ๊ฒฐ์ •ํ•˜๊ธฐ ์œ„ํ•œ ํ™•์ธ ๋‹จ๊ณ„๋ฅผ ์‹ค์‹œํ•ฉ๋‹ˆ๋‹ค.
  • ์œ„ ๊ณผ์ •์„ ๊ฑฐ์ณ ๋ฐ์ดํ„ฐ๋ฒ ์‹œ์Šค๋Š” ๋ชจ๋“  SQL Statement์— ๋Œ€ํ•˜์—ฌ Hash ๊ฐ’์„ ๋งŒ๋“œ๋Š” Hashing ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ํ•ด์‹ฑ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ๊ฑฐ์น˜๋ฉด ํ•ด๋‹น SQL์— ID๊ฐ€ ๋ถ€์—ฌ๋ฉ๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ƒˆ๋กœ์šด ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ SQL Statement๊ฐ€ ์ž…๋ ฅ์ด ๋ ๋•Œ ์œ„์˜ ๊ณผ์ •์„ ๋˜ ๊ฑฐ์น˜๋ฉด์„œ ๊ธฐ์กด์— ์ €์žฅ๋˜์—ˆ๋˜ SQL ID์˜ ์œ ๋ฌด๋ฅผ ํ™•์ธํ•˜์—ฌ ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ ๊ณผ์ •์— ๋Œ€ํ•œ ๊ฒฐ์ •์„ ๋‚ด๋ฆฌ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

๐Ÿ“Œ DML(Data Manipulation Language)์€ ๋ฌด์—‡์ธ๊ฐ€์š”? ์–ด๋–ค ๊ตฌ๋ฌธ์ด ์žˆ๋Š”์ง€๋„ ์„ค๋ช…ํ•ด์ฃผ์„ธ์š”.

โœ… DML์€ ๋ฐ์ดํ„ฐ๋ฅผ ์กฐ์ž‘ํ•˜๊ธฐ ์œ„ํ•œ ์–ธ์–ด์ž…๋‹ˆ๋‹ค. ์ฃผ์š” ๊ตฌ๋ฌธ์œผ๋กœ๋Š” SELECT, INSERT, UPDATE, DELETE๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ธฐ๋ณธ์ ์œผ๋กœ ๋ฐ์ดํ„ฐ ์กฐ์ž‘์–ด(DML)์„ ํ†ตํ•ด์„œ ๋ฐ์ดํ„ฐ์˜ ๊ฒ€์ƒ‰, ์‚ฝ์ž…, ์ˆ˜์ •, ์‚ญ์ œ์˜ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค.

๐Ÿ“Œ DDL(Data Definition Language)์€ ๋ฌด์—‡์ธ๊ฐ€์š”? ์–ด๋–ค ๊ตฌ๋ฌธ์ด ์žˆ๋Š”์ง€๋„ ์„ค๋ช…ํ•ด์ฃผ์„ธ์š”.

โœ… DDL์€ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์˜ ๊ตฌ์กฐ๋ฅผ ์ •์˜ํ•˜๊ธฐ ์œ„ํ•œ ์–ธ์–ด์ž…๋‹ˆ๋‹ค. ์ฃผ์š” ๊ตฌ๋ฌธ์œผ๋กœ๋Š” CREATE, ALTER,DROP๋“ฑ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ ์ •์˜์–ด๋Š” ํ…Œ์ด๋ธ”์„ ์ƒ์„ฑํ•˜๊ฑฐ๋‚˜, ์ˆ˜์ •, ์‚ญ์ œํ•˜๋Š”๋ฐ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.

๐Ÿ“Œ DCL(Data Control Language)์€ ๋ฌด์—‡์ธ๊ฐ€์š”? ์–ด๋–ค ๊ตฌ๋ฌธ์ด ์žˆ๋Š”์ง€๋„ ์„ค๋ช…ํ•ด์ฃผ์„ธ์š”.

โœ… DCL์€ ๋ฐ์ดํ…Œ๋ฒ ์ด์Šค ์‚ฌ์šฉ์ž์—๊ฒŒ ๊ถŒํ•œ์„ ๋ถ€์—ฌํ•˜๊ฑฐ๋‚˜ ํšŒ์ˆ˜ํ•˜๊ธฐ ์œ„ํ•œ ์–ธ์–ด์ž…๋‹ˆ๋‹ค. ์ฃผ๋กœ GRANT, REVOKE ๋“ฑ์„ ์ด์šฉํ•ด ํŠน์ • ์‚ฌ์šฉ์ž์—๊ฒŒ ๊ถŒํ•œ์„ ์ฃผ๊ฑฐ๋‚˜ ํšŒ์ˆ˜ํ•˜๋Š”๋ฐ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.


โ“ Hard Parse & Soft Parse
์œ„์˜ SQL PROCESSING ์‚ฌ์ง„์„ ๋ณด๋ฉด Parsingํ•˜๋Š” ๊ณผ์ •์ด Hard parse์™€ Soft parse๋กœ ๋‚˜๋‰˜๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค.

  • Hard parse
    ๋งŒ์•ฝ์— ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๋‚ด์—์„œ ์ฐธ๊ณ ํ•˜์—ฌ ๋‹ค์‹œ ์‚ฌ์šฉํ•  ๋งŒํ•œ Hash ID๋ฅผ ์ฐพ์ง€ ๋ชปํ•œ๋‹ค๋ฉด(์ฒ˜์Œ ์‹คํ–‰๋˜๋Š” Statement), ์ƒˆ๋กœ์šด ๋ฒ„์ „์˜ ์ฝ”๋“œ๋กœ ๋‹ค์‹œ ์‹คํ–‰์„ ํ•ด์•ผํ•˜๋Š” ๊ฒฝ์šฐ๋ฅผ Hard Parse๋ผ๊ณ  ํ•œ๋‹ค. ์ด๋ ‡๊ฒŒ ํ•˜๋“œ ํŒŒ์‹ฑ์„ ํ•˜๋Š” ๋™์•ˆ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋Š” ์บ์‹œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์™€ ๋ฐ์ดํ„ฐ ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ํ™•์ธํ•œ ์ˆ˜๋งŽ์€ ์บ์‹œ๋ฐ์ดํ„ฐ์— ์ ‘๊ทผํ•ฉ๋‹ˆ๋‹ค. ์ด๋•Œ, ํ•ด๋‹น ์ •์˜๊ฐ€ ๋ณ€๊ฒฝ๋˜์ง€ ์•Š๋„๋ก ํ•„์ˆ˜ ๊ฐœ์ฒด(required object)์— ๋Œ€ํ•œ ๋ž˜์น˜(Latch)๋ผ๋Š” ์ง๋ ฌํ™” ์žฅ์น˜๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๋ž˜์น˜ ๊ฒฝํ•ฉ(Latch Contention)์€ ๋ช…๋ น๋ฌธ ์‹คํ–‰ ์‹œ๊ฐ„์„ ๋Š˜๋ฆฌ๊ณ  ๋™์‹œ์„ฑ์„ ๊ฐ์†Œ์‹œํ‚ต๋‹ˆ๋‹ค.
  • Soft parse
    ๋‹จ์ˆœํ•˜๊ฒŒ ์ƒ๊ฐํ•˜๋ฉด ํ•˜๋“œ ํŒŒ์‹ฑ์˜ ๋ฐ˜๋Œ€๋ผ๊ณ  ์ƒ๊ฐํ•˜๋ฉด ๋  ๊ฒƒ ๊ฐ™๋‹ค. ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์—์„œ Hash ID๋ฅผ ์ฐพ์•„ Plan(Compile)์„ ๊ฐ€์ง„ ๊ฒฝ์šฐ๋ฅผ Soft Parse๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด๋Ÿฌ๋ฉด ์ถ”๊ฐ€์ ์ธ ์ตœ์ ํ™” ์ž‘์—… ์—†์ด Parsing ํ•  ์ˆ˜ ์žˆ๋‹ค.

๐Ÿ’ก What is a Latch?

๋ž˜์น˜๋Š” ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์—์„œ ๋™์‹œ ์•ก์„ธ์Šค๋ฅผ ์ œ์–ดํ•˜๊ณ  ์ƒํ˜ธ๋ฐฐ์ œ๋ฅผ ์‹คํ˜„ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋˜๋Š” ๋™๊ธฐํ™” ๋ฉ”์ปค๋‹ˆ์ฆ˜์ž…๋‹ˆ๋‹ค. ์—ฌ๋Ÿฌ ํŠธ๋žœ์ ์…˜์ด๋‚˜ ์Šค๋ ˆ๋“œ๊ฐ€ ๋™์‹œ์— ๊ณต์œ  ์ž์›์— ์ ‘๊ทผํ•˜๋ฉด ๋ฐ์ดํ„ฐ์˜ ์ผ๊ด€์„ฑ์ด ๊นจ์งˆ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๋ž˜์น˜๋Š” ์ด๋Ÿฌํ•œ ๊ณต์œ  ์ž์›์— ์ ‘๊ทผ์„ ์ œ์–ดํ•˜์—ฌ ๋™์‹œ ์ˆ˜์ •์ด ๋ถˆ๊ฐ€๋Šฅํ•˜๋„๋ก ๋งŒ๋“ญ๋‹ˆ๋‹ค.

๐Ÿ’ก What is a Latch Contention?

๋ž˜์น˜ ๊ฒฝํ•ฉ์€ ์—ฌ๋Ÿฌ ํŠธ๋žœ์ ์…˜์ด๋ผ ์Šค๋ ˆ๋“œ๊ฐ€ ๋™์‹œ์— ๋™์ผํ•œ ์ž์›์— ๋Œ€ํ•ด ์•ก์„ธ์Šค ํ•˜๋ ค๊ณ  ํ•  ๋•Œ ๋ฐœ์ƒํ•˜๊ธฐ ๋•Œ๋ฌธ์—, ํŠธ๋žœ์ ์…˜์˜ ๋ฒ”์œ„๋ฅผ ์ตœ์†Œํ™”ํ•˜๊ฑฐ๋‚˜ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๋””์ž์ธ์„ ์ตœ์ ํ™”ํ•˜์—ฌ ์ž์› ๊ณต์œ ๋ฅผ ํšจ์œจ์ ์œผ๋กœ ๊ด€๋ฆฌํ•˜๋Š” ๊ฒƒ์ด ์ค‘์š”ํ•ฉ๋‹ˆ๋‹ค.


๐Ÿ“Œ ์ฐธ์กฐ ๋ฌด๊ฒฐ์„ฑ์— ๋Œ€ํ•ด์„œ ์„ค๋ช…ํ•ด์ฃผ์„ธ์š”.

โœ… Referential Integrity๋Š” RDBMS์—์„œ ๊ด€๊ณ„๋ฅผ ๋งบ๊ณ  ์žˆ๋Š” ๋‘ ํ…Œ์ด๋ธ” ๊ฐ„์— ์ผ๊ด€๋œ ๊ด€๊ณ„๊ฐ€ ์œ ์ง€๋˜๋Š” ๊ฒƒ์„ ๋ณด์žฅํ•˜๋Š” ๊ฐœ๋…์ž…๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ์ฐธ์กฐ ๋ฌด๊ฒฐ์„ฑ์€ ์™ธ๋ž˜ํ‚ค๋ฅผ ์‚ฌ์šฉํ•จ์œผ๋กœ์จ ์ •์˜๊ฐ€ ๋˜๋ฉฐ ๋ถ€๋ชจํ…Œ์ด๋ธ”์˜ ๊ธฐ๋ณธ ํ‚ค์™€ ์ž์‹ ํ…Œ์ด๋ธ”์˜ ์™ธ๋ž˜ ํ‚ค ๊ฐ„์˜ ๊ด€๊ณ„๋ฅผ ๊ฐ•์ œํ•˜๋Š” ์—ญํ• ์„ ํ•˜๊ฒŒ๋” ๊ตฌํ˜„์ด ๋ฉ๋‹ˆ๋‹ค.

CREATE TABLE Students = (
Students_Id int,
name VARCHAR(30),
Department_Id VARCHAR(30), 
PRIMARY KEY(Students_Id),
FOREIGN KEY(Department_Id) REFERENCES Departments(Department_Id)
); 

CREATE TABLE Departments = (
Department_Id int,
department VARCHAR(30),
PRIMARY KEY(Students_Id)
);
  • ๋ถ€๋ชจ ํ…Œ์ด๋ธ”์˜ ๊ธฐ๋ณธ ํ‚ค ๊ฐ’์ด ๋ณ€๊ฒฝ๋˜๋ฉด: ์˜ˆ๋ฅผ ๋“ค์–ด, "ํ•™๊ณผ" ํ…Œ์ด๋ธ”์˜ ์–ด๋–ค ํ•™๊ณผID ๊ฐ’์ด ๋ณ€๊ฒฝ๋˜๋ฉด, ์ด์™€ ๊ด€๋ จ๋œ "ํ•™์ƒ" ํ…Œ์ด๋ธ”์˜ ๋ชจ๋“  ํ•ด๋‹น ํ•™๊ณผID๋„ ํ•จ๊ป˜ ๋ณ€๊ฒฝ๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

  • ๋ถ€๋ชจ ํ…Œ์ด๋ธ”์˜ ๊ธฐ๋ณธ ํ‚ค ๊ฐ’์ด ์‚ญ์ œ๋˜๋ฉด: "ํ•™๊ณผ" ํ…Œ์ด๋ธ”์—์„œ ์–ด๋–ค ํ•™๊ณผ๊ฐ€ ์‚ญ์ œ๋˜๋ฉด, ํ•ด๋‹น ํ•™๊ณผ์— ์†ํ•œ ๋ชจ๋“  ํ•™์ƒ ๋ฐ์ดํ„ฐ๋„ ํ•จ๊ป˜ ์‚ญ์ œ๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.


๐Ÿ“Œ CASCADE ์„ค์ •์— ๋Œ€ํ•ด์„œ ์„ค๋ช…ํ•ด์ฃผ์„ธ์š”.

โœ… CASCADE ์„ค์ •์€ ๋ถ€๋ชจ ํ…Œ์ด๋ธ”์—์„œ ์ˆ˜ํ–‰ํ•œ ์—…๋ฐ์ดํŠธ๋‚˜ ์‚ญ์ œ ์ž‘์—…์ด ์ž์‹ ํ…Œ์ด๋ธ”์—๋„ ์ ์šฉ๋˜๋„๋ก ํ•˜๋Š” ์˜ต์…˜์ž…๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ CASCADE๋กœ ์„ค์ •์„ ํ•˜๊ณ  DELETE ํ˜น์€ INSERT๋ฅผ ํ•˜๋ฉด, ๊ตฌ๋ถ„ ์—†์ด ๋ฐ์ดํ„ฐ๊ฐ€ ๋ถ€๋ชจ, ์ž์‹ ํ…Œ์ด๋ธ” ๋ชจ๋‘ ๋ฐ”๋€Œ๊ณ  ์ฐธ์กฐ์— ๋Œ€ํ•œ ๋‚ด์šฉ๋„ ์‚ฌ๋ผ์ ธ ๋ฒ„๋ฆฌ๊ธฐ ๋•Œ๋ฌธ์— ์ฃผ์˜ ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.


๐Ÿ“Œ VIEW์— ๋Œ€ํ•ด์„œ ์„ค๋ช…ํ•ด์ฃผ์„ธ์š”.

โœ… VIEW๋Š” ํ•˜๋‚˜ ์ด์ƒ์˜ ํ…Œ์ด๋ธ”์—์„œ ์œ ๋„๋œ ๊ฐ€์ƒ ํ…Œ์ด๋ธ”์ž…๋‹ˆ๋‹ค. ์ €์žฅ ๊ณต๊ฐ„์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  ์ฟผ๋ฆฌ ๊ฒฐ๊ณผ๋ฅผ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. ์ฃผ๋กœ ํŠน์ • ์กฐ๊ฑด์— ๋งž๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ๋…ธ์ถœํ•˜๊ฑฐ๋‚˜ ํŠน์ • ํ˜•ํƒœ๋กœ ์กฐ์ž‘ํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ์ด์œ ์— ์žˆ์–ด์„œ ๋ทฐ์—์„œ ๋ฐ์ดํ„ฐ์˜ ์ˆ˜์ •์ด๋‚˜ ์‚ญ์ œ๋Š” ์•„์ฃผ ์ œํ•œ์ ์œผ๋กœ ์ด๋ค„์ง‘๋‹ˆ๋‹ค.

View๋ฅผ ๋ฌ˜์‚ฌํ•˜๋Š” ์„ค๋ช… ์‚ฌ์ง„
CREATE VIEW ์šฐ์ˆ˜๊ณ ๊ฐ(๊ณ ๊ฐ์•„์ด๋””, ๊ณ ๊ฐ์ด๋ฆ„, ๋‚˜์ด)
AS SELECT ๊ณ ๊ฐ์•„์ด๋””, ๊ณ ๊ฐ์ด๋ฆ„, ๋‚˜์ด

FROM ๊ณ ๊ฐ
WHERE ๋“ฑ๊ธ‰ = 'vip'

WITH CHECK OPTION;

SELECT * FROM ์šฐ์ˆ˜๊ณ ๊ฐ;

์œ„ ๊ฐ™์ด ์ฝ”๋“œ๋ฅผ ๊ตฌ์„ฑํ•˜๋ฉด ์šฐ์ˆ˜๊ณ ๊ฐ์ด๋ผ๋Š” ๋ทฐ๋ฅผ ๋งŒ๋“ค๊ณ  ๊ทธ ๋ทฐ์— ๊ณ ๊ฐ ์•„์ด๋””๋ฅผ AS ๋’ค์˜ SELET์™€ ๊ฐ™์ด ๋งŒ๋“ค๊ณ  ๊ฐœ์ค‘์— ๋“ฑ๊ธ‰์ด vip์ธ ์ธ์Šคํ„ด์Šค๋ฅผ ๊ฐ€์ ธ์˜ค๋ฉด ์•„๋ž˜์™€ ๊ฐ™๋‹ค. ์—ฌ๊ธฐ์— WITH CHECK. OPTIONS๋ฅผ ํ†ตํ•ด ๋ทฐ ์ƒ์„ฑ ํ›„ vip ๋“ฑ๊ธ‰์ด ์•„๋‹Œ ์ธ์Šคํ„ด์Šค์— ๋Œ€ํ•ด ์‚ฝ์ž… ๋ฐ ์—ฐ์‚ฐ ๋“ฑ์„ ์ œํ•œํ•œ๋‹ค.

์—…๋กœ๋“œ์ค‘..


๐Ÿ“Œ SELECT ์ ˆ์˜ ์ฒ˜๋ฆฌ์ˆœ์„œ์— ๋Œ€ํ•ด์„œ ์„ค๋ช…ํ•ด์ฃผ์„ธ์š”.

โœ… SELECT ์ ˆ์˜ ์ฒ˜๋ฆฌ ์ˆœ์„œ๋Š” FROM, WHERE, GROUP BY, HAVING, SELECT,ORDER BY ์ž…๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์—์„œ ํ•„์š”ํ•œ ํ…Œ์ด๋ธ”์„ ์ฐพ์•„ ๊ฒฐํ•ฉํ•˜๊ณ , ์กฐ๊ฑด์„ ์ ์šฉํ•˜๋ฉฐ, ๊ทธ๋ฃนํ™”ํ•˜๊ณ , ํ•„์š”ํ•œ ์—ด์„ ์„ ํƒํ•˜์—ฌ ๊ฒฐ๊ณผ๋ฅผ ์ •๋ ฌํ•ฉ๋‹ˆ๋‹ค. ์ด ๊ฐ™์€ ์‹คํ–‰ ์ˆœ์„œ๋ฅผ ์ž˜ ์•Œ๊ณ  ์žˆ์–ด์•ผ ์ฟผ๋ฆฌ๋ฅผ ์‹คํ–‰ํ•จ์— ์žˆ์–ด์„œ ๊ฒฐ๊ณผ๋ฅผ ๋ฏธ๋ฆฌ ์˜ˆ์ธกํ•  ์ˆ˜ ์žˆ๋‹ค!

์—…๋กœ๋“œ์ค‘..

SELECT ์—ฐ์‚ฐ ์ฒ˜๋ฆฌ ์ˆœ์„œ

๐Ÿ“Œ SELECT ~ FOR UPDATE ๊ตฌ๋ฌธ์— ๋Œ€ํ•ด์„œ ์„ค๋ช…ํ•ด์ฃผ์„ธ์š”.

โœ… ์„ ํƒ๋œ ํ–‰๋“ค์— ๋Œ€ํ•ด ๋ฐฐํƒ€์ ์ธ LOCK์„ ์„ค์ •ํ•˜๋Š” ๊ธฐ๋Šฅ์ž…๋‹ˆ๋‹ค. SELECT FOR UPDATE๋ฌธ์„ ํ†ตํ•ด ์ปค์„œ ๊ฒฐ๊ณผ ์ง‘ํ•ฉ์˜ ๋ ˆ์ฝ”๋“œ๋ฅผ ์ž ๊ธ€ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๋ฌธ์„ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด ๋ ˆ์ฝ”๋“œ๋ฅผ ๋ณ€๊ฒฝํ•  ํ•„์š”๋Š” ์—†์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์ผ๋ฐ˜์ ์ธ LOCK์˜ ํ˜•ํƒœ๋Š” SELECT SHARED LOCK ์˜ ํ˜•ํƒœ๋ฅผ ๊ฐ€์ง‘๋‹ˆ๋‹ค. ์‰ฌ์šด ๋ง๋กœ "SELECT ~ FOR UPDATE ๊ตฌ๋ฌธ์€ "๋ฐ์ดํ„ฐ ์ˆ˜์ •ํ•˜๋ ค๊ณ  SELECT ํ•˜๋Š” ์ค‘์ด์•ผ~ ๋‹ค๋ฅธ ์‚ฌ๋žŒ๋“ค์€ ๋ฐ์ดํ„ฐ์— ์† ๋Œ€์ง€ ๋งˆ!" ๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค."๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค :)

์—…๋กœ๋“œ์ค‘..

SELECT ~ UPDATE ์„ค๋ช…์„ ์œ„ํ•œ ์˜ˆ์‹œ ํ…Œ์ด๋ธ”

๋น ๋ฅธ ์ดํ•ด๋ฅผ ์œ„ํ•ด์„œ ํƒ€ ๋ธ”๋กœ๊ทธ์—์„œ ๋“ค์—ˆ๋˜ ์˜ˆ์‹œ๋ฅผ ๋“ค์–ด ์—ฌ๊ธฐ์„œ ๋™์‹œ์— ๋‘ ์‚ฌ๋žŒ์ด ๊ฐ™์€ ์˜ํ™”๋ฅผ ์˜ˆ๋งคํ•˜๋Š” ์ƒํ™ฉ์„ ๊ฐ€์ •ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค!

์—…๋กœ๋“œ์ค‘..

๋‘ ์‚ฌ๋žŒ์ด ๋™์‹œ์— ๋ฐ์ดํ„ฐ์— ์ ‘๊ทผํ•˜์—ฌ ์›ํ•˜๋Š” ๋ฐ์ดํ„ฐ๋ฅผ SELECTํ•˜๊ณ  ์˜ˆ๋งค๋ฅผ ์™„๋ฃŒ๋ฅผ ํ•˜๋ฉด์„œ ์ขŒ์„์ด ์ค„์–ด๋“ค๊ฒŒ ๋˜๋Š”๋ฐ ์ด๋•Œ ๊ฐ™์ด ์˜ˆ๋งคํ•˜๋˜ ๋‹ค๋ฅธ ์‚ฌ๋žŒ๋„ ๋˜‘๊ฐ™์ด SELECT๋ฅผ ํ•˜์—ฌ ์˜ํ™” ์˜ˆ๋งค๋ฅผ ํ•˜๊ฒŒ ๋˜์–ด ๊ฒฐ๊ณผ์ ์œผ๋กœ๋Š” 10 ์ž๋ฆฌ๊ฐ€ ๋‚จ์•„์•ผํ•˜๋‚˜, 11์ž๋ฆฌ๊ฐ€ ๋‚จ์•˜๋‹ค๊ณ  ๋‚˜์˜ค๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

์—…๋กœ๋“œ์ค‘..

โ—๏ธ์ด๋Ÿฐ ์ƒํ™ฉ์— ๋Œ€๋น„ํ•˜๊ธฐ ์œ„ํ•ด์„œ! SELECT ~ FOR UPDATE๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ ์ž…๋‹ˆ๋‹ค. SELECT ~ FOR UPDATE ๊ฐ™์€ ๊ฒฝ์šฐ์—๋Š” ๋™์‹œ์— ๊ฐ™์€ ๋ฐ์ดํ„ฐ์— ์ ‘์†ํ•˜์—ฌ ์ˆ˜์ •ํ•˜๋ ค๊ณ  ํ•˜๋Š” ๊ฒฝ์šฐ ๋–  ๋นจ๋ฆฌ ์ฒ˜๋ฆฌ๋œ ์ฟผ๋ฆฌ๋ฅผ ๋จผ์ € ์šฐ์„ ์œผ๋กœ ์ฒ˜๋ฆฌํ•˜๋ฉฐ ๋‹ค๋ฅธ ์ฟผ๋ฆฌ์˜ ์ˆ˜ํ–‰์„ ์ œํ•œํ•˜๋Š” ๊ฒƒ ์ž…๋‹ˆ๋‹ค!

๊ทธ๋ž˜์„œ ๋ฐฉ๊ธˆ ์˜ˆ์‹œ์˜ ์ƒํ™ฉ์œผ๋กœ ๋‹ค์‹œ ๋Œ์•„๊ฐ€ ๋ณด๋ฉด ๋™์‹œ์— ๋‘์‚ฌ๋žŒ์ด ์˜ํ™”๋ฅผ ์˜ˆ๋งค๋ฅผ ํ•˜๋Š” ๊ฒฝ์šฐ๋Š” ํ•œ ์‚ฌ๋žŒ์ด ์˜ํ™”ํ‘œ๋ฅผ ๊ตฌ๋งคํ•œ ๊ฒƒ์— ๋Œ€ํ•œ ์ฒ˜๋ฆฌ๊ฐ€ ๋๋‚˜๋ฉด ๊ทธ๋•Œ ๋‹ค๋ฅธ ์‚ฌ๋žŒ์ด ์œ„์™€ ๊ฐ™์€ ๊ณผ์ •์„ ํ•œ๋ฒˆ ๋” ํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.


๐Ÿ“Œ GROUP BY์ ˆ & ORDER BY์ ˆ์— ๋Œ€ํ•ด์„œ ์„ค๋ช…ํ•ด์ฃผ์„ธ์š”.

GROUP BY & ORDER BY๋Š” ๋‹จ์–ด๋ฅผ ๋ณด๋ฉด ์•Œ ์ˆ˜ ์žˆ๋“ฏ์ด ๋ฐ์ดํ„ฐ, ์ฟผ๋ฆฌ์— ๋Œ€ํ•œ ๊ฒฐ๊ณผ๋ฅผ ํŠน์ •ํ•œ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌํ•˜๊ฑฐ๋‚˜ ๋ฌถ๋Š”๋ฐ ์‚ฌ์šฉํ•˜๋Š” MySQL ์ฟผ๋ฆฌ์–ธ์–ด ์ž…๋‹ˆ๋‹ค.

  • ์œ„์˜ SELECT FOR UPDATE๋ฅผ ์„ค๋ช…ํ•˜๋Š” ๊ณผ์ •์— ์žˆ์–ด ์‚ฌ์šฉํ•œ Table์„ ํ†ตํ•ด ์„ค๋ช… ๋“œ๋ฆฌ๊ฒ ์Šต๋‹ˆ๋‹ค.
    - GROUP BY
    SELECT movie_num, seat, name AS movie_name 
    FROM CGV GROUP BY seat, movie_name; 
    ์œ„ ์ฝ”๋“œ๋Š” GCV๋ผ๋Š” ํ…Œ์ด๋ธ”์—์„œ ๋ชจ๋“  ์ปฌ๋Ÿผ์„ ๊ฐ€์ ธ์˜ค๋Š”๋ฐ ๊ฐœ ์ค‘ name์ด๋ผ๋Š” attribute๋ฅผ movie_name์œผ๋กœ ํ˜ธ์ถœํ–ˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๊ทธ ์ค‘์— seat์™€ movie_name์„ ๊ทธ๋ฃนํ™”ํ•˜์—ฌ ๊ฐ ์˜ํ™”๋‹น ์ž”์—ฌ์ขŒ์„์ด ์–ผ๋งˆ๋‚˜ ์žˆ๋Š”์ง€ ์‰ฝ๊ฒŒ ๋ณผ ์ˆ˜ ์žˆ๊ฒŒ ๋งŒ๋“ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
    • ORDER BY
      SELECT movie_num, seat, name AS movie_name 
      FROM CGV ORDER BY seat DSC;
      ๋ผ๊ณ  ์ฟผ๋ฆฌ๋ฅผ ์ž‘์„ฑํ•˜๊ฒŒ ๋  ๊ฒฝ์šฐ ์ขŒ์„์ด ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌ์ด ๋˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ฆ‰ ๊ฐ€์žฅ ๋†’์€ ๊ฒƒ์ด ๋จผ์ €์˜ค๊ณ  ๋‚ด๋ ค ๊ฐˆ ์ˆ˜๋ก ์ž”์—ฌ์ขŒ์„์ด ์ ์€ ๊ฒƒ์œผ๋กœ ๋ฐฐ์น˜๋ฉ๋‹ˆ๋‹ค.

๐Ÿ“Œ INNER JOIN๊ณผ OUTER JOIN์˜ ์ฐจ์ด์ ์— ๋Œ€ํ•ด์„œ ์„ค๋ช…ํ•ด์ฃผ์„ธ์š”.

  • INNER JOIN์€ ์ผ์น˜ํ•˜๋Š” ํ–‰๋งŒ ๋ฐ˜ํ™˜์„ ํ•ฉ๋‹ˆ๋‹ค. ์ฆ‰, ์กฐ์ธ ์กฐ๊ฑด์„ ๋งŒ์กฑํ–ˆ์„ ๊ฒฝ์šฐ์˜ ํ–‰๋งŒ ๊ฒฐํ•ฉ๋˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.
SELECT * FROM table1 INNER JOIN table2 
ON table1.column = table2.column;

์œ„์˜ ์ฝ”๋“œ๋ธ”๋Ÿญ์„ ๋ณด๋ฉด INNER JOIN table2 ON table1.column - table2.column; ์ด๋Ÿฐ ๋ถ€๋ถ„์ด ์žˆ๋Š”๋ฐ ON ๋’ค์— ๋‚˜์˜ค๋Š” ์กฐ๊ฑด์ด TRU๊ฐ€ ๋˜์•ผ์ง€๋งŒ JOIN์ด ๋˜๋Š” ์ฟผ๋ฆฌ์ž…๋‹ˆ๋‹ค.

  • ๊ทธ์— ๋ฐ˜์— OUTER JOIN์€ ์กฐ๊ฑด์ด ๋ถ€ํ•ฉํ•˜๊ธฐ ์•Š๋Š” ํ–‰๋„ ํฌํ•จํ•˜์—ฌ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ๋‹จ์ˆœํ•˜๊ฒŒ ์—ด์„ ์‚ฝ์ž…ํ•˜๋Š” ๊ฒƒ์ด๋ผ๊ณ  ์ƒ๊ฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ OUTER JOIN์€ LEFT OUTER JOIN, RIGHT OUTER JOIN์ด ์žˆ์Šต๋‹ˆ๋‹ค.

๐Ÿ“Œ LEFT OUTER JOIN, RIGHT OUTER JOIN์— ๋Œ€ํ•ด์„œ ์„ค๋ช… ํ•ด์ฃผ์„ธ์š”.

  • Left Onnner Join : ์™ผ์ชฝ์˜ ํ…Œ์ด๋ธ”(table1)์˜ ๋ชจ๋“  ํ–‰์„ ์„ ํƒํ•˜๊ณ (๊ธฐ์ค€์œผ๋กœ ์žก๊ณ ), ์˜ค๋ฅธ์ชฝ ํ…Œ์ด๋ธ”๊ณผ ์ผ์น˜ํ•˜๋Š” ํ–‰์ด ์žˆ์œผ๋ฉด(ON ๋’ค์˜ ์กฐ๊ฑด) ๊ฒฐํ•ฉํ•ฉ๋‹ˆ๋‹ค.
SELECT * FROM table1 LEFT OUTER JOIN table2 
ON table1.column = table2.column;
  • Right Outer Join : ์œ„์˜ LEFT OUTER JOIN๊ณผ๋Š” ๋ฐ˜๋Œ€๋กœ ์˜ค๋ฅธ์ชฝ ํ…Œ์ด๋ธ”(table2)์˜ ๋ชจ๋“  ํ–‰์„ ์„ ํƒํ•˜๊ณ (๊ธฐ์ค€์œผ๋กœ ์ƒ๊ฐํ•˜๊ณ ), ์™ผ์ชฝ ํ…Œ์ด๋ธ”(table1)๊ณผ ์ผ์น˜ํ•˜๋Š” ํ–‰์ด ์žˆ์œผ๋ฉด(ON ๋’ค์— ์กฐ๊ฑด) ๊ฒฐํ•ฉํ•ฉ๋‹ˆ๋‹ค.
SELECT * FROM table1 RIGHT OUTER JOIN table2 
ON table1.column = table2.column;

๐Ÿ“Œ CROSS JOIN์— ๋Œ€ํ•ด์„œ๋„ ์„ค๋ช…ํ•ด์ฃผ์„ธ์š”.

SELECT * FROM table1 CROSS JOIN table2;

CROSS JOIN์€ ์œ„์—์„œ ์–ธ๊ธ‰ํ•œ OUTER JOIN๊ณผ ๊ฐ€์žฅ ์œ ์‚ฌํ•œ ๊ฐœ๋…์„ ๊ฐ€์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ํŠน์ˆ˜ํ•œ ์กฐ๊ฑด์—†์ด table2๋ฅผ table1๊ณผ ๊ฒฐํ•ฉํ•˜๊ฒŒ ํ•˜๋Š” ์ฟผ๋ฆฌ ์ž…๋‹ˆ๋‹ค.


๐Ÿ“Œ ์„œ๋ธŒ์ฟผ๋ฆฌ์— ๋Œ€ํ•ด์„œ ์„ค๋ช…ํ•ด์ฃผ์„ธ์š”.

์„œ๋ธŒ์ฟผ๋ฆฌ๋ผ๋Š” ๊ฒƒ์€ ํ•˜๋‚˜์˜ SQL Statement์— ํฌํ•จ๋˜์–ด ์žˆ๋Š” ๋˜๋‹ค๋ฅธ Statement๋ฅผ ๋งํ•ฉ๋‹ˆ๋‹ค.

ํฌ๊ฒŒ ์„œ๋ธŒ์ฟผ๋ฆฌ๋Š” 4๊ฐ€์ง€๋กœ ๋‚˜๋ˆŒ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค

  • ๋‹จ์ผํ–‰ ์„œ๋ธŒ ์ฟผ๋ฆฌ : ์„œ๋ธŒ์ฟผ๋ฆฌ๊ฐ€ ๋‹จ์ผ ํ–‰ ๋น„๊ต์—ฐ์‚ฐ์ž(=,>,< ...)์™€ ํ•จ๊ป˜ ์‚ฌ์šฉํ•  ๋•Œ๋Š” ์„œ๋ธŒ์ฟผ๋ฆฌ์˜ ๊ฒฐ๊ณผ ๊ฑด์ˆ˜๊ฐ€ ๋ฐ˜๋“œ์‹œ 1๊ฑด ์ดํ•˜์—ฌ์•ผ ํ•ฉ๋‹ˆ๋‹ค.
SELECT * FROM CGV WHERE seat = 
(SELECT seat FROM MEGABOX WHERE seat = '8')
ORDER BY *;

์ด๋ ‡๊ฒŒ ์กฐ๊ฑด์„ ๊ฑธ์—ˆ์„ ๋•Œ MEGEBOX๋ผ๋Š” ํ…Œ์ด๋ธ”์˜ seat๊ฐ€ 8๊ณผ ๊ฐ™์€ ๊ฐ’์ด 2๊ฐœ ์ด์ƒ ์žˆ์„ ๊ฒฝ์šฐ์—๋Š” ๋ฌด์กฐ๊ฑด ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๋งŒ์•ฝ ๊ทธ๋ฃน ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๊ฒŒ ๋  ๊ฒฝ์šฐ์—๋Š” ๊ทธ๋ฃน ์ž์ฒด๊ฐ€ ํ•œ ๊ฑด์œผ๋กœ ์ฒ˜๋ฆฌ๊ฐ€ ๋˜๊ธฐ ๋•Œ๋ฌธ์— ๋‹จ์ผํ–‰ ์„œ๋ธŒ์ฟผ๋ฆฌ๋กœ์จ ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

SELECT * FROM CGV WHERE seat = 
(SELECT seat FROM MEGABOX WHERE seat = '8')
GROUP BY *;
  • ๋‹ค์ค‘ ์นผ๋Ÿผ ์„œ๋ธŒ์ฟผ๋ฆฌ : ์„œ๋ธŒ์ฟผ๋ฆฌ์˜ ๊ฒฐ๊ณผ๋กœ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ์ปฌ๋Ÿผ์ด ๋ฐ˜ํ™˜๋˜์–ด ๋ฉ”์ธ์ฟผ๋ฆฌ ์กฐ๊ฑด๊ณผ ๋™์‹œ์— ๋น„๊ต๋˜๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•œ๋‹ค.
SELECT * FROM CGV WHERE seat = 
(SELECT seat,movie_num FROM MEGABOX WHERE seat,movie_num = '8')
ORDER BY *;
  • ์—ฐ๊ด€ ์„œ๋ธŒ์ฟผ๋ฆฌ : ์„œ๋ธŒ์ฟผ๋ฆฌ ๋‚ด์— ๋ฉ”์ธ์ฟผ๋ฆฌ ์ปฌ๋Ÿผ์ด ์‚ฌ์šฉ๋œ ์„œ๋ธŒ์ฟผ๋ฆฌ ์ž…๋‹ˆ๋‹ค.
SELECT * FROM CGV CGV WHERE IN seat = 
(SELECT CGV.seat,CGV.movie_num FROM MEGABOX MEGABOX WHERE MEGABOX.seat,MEGABOX.movie_num = '8')
ORDER BY *;
  • ๊ทธ ๋ฐ–์˜ ์ฟผ๋ฆฌ๋“ค
    1. SELECT์ ˆ์— ์‚ฌ์šฉํ•˜๋Š” ์„œ๋ธŒ์ฟผ๋ฆฌ : ์Šค์นผ๋ผ ์„œ๋ธŒ์ฟผ๋ฆฌ๋ผ๊ณ ๋„ ํ•˜๋ฉฐ, ์Šค์นผ๋ผ ์„œ๋ธŒ์ฟผ๋ฆฌ๋Š” ํ•œ ํ–‰, ํ•œ ์ปฌ๋Ÿผ๋งŒ์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ์„œ๋ธŒ์ฟผ๋ฆฌ ์ž…๋‹ˆ๋‹ค!
    1. FROM ์ ˆ์— ์‚ฌ์šฉํ•˜๋Š” ์„œ๋ธŒ์ฟผ๋ฆฌ : ์ธ๋ผ์ธ ๋ทฐ๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ๊ธฐ๋ณธ์ ์œผ๋กœ FROM ์ ˆ์—๋Š” ํ…Œ์ด๋ธ” ๋ช…์ด ์˜ค๋„๋ก ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ FROM์ ˆ์— ์„œ๋ธŒ์ฟผ๋ฆฌ๊ฐ€ ์‚ฌ์šฉ๋˜๋ฉด ๋™์ ์œผ๋กœ ์ƒ์„ฑ๋œ ํ…Œ์ด๋ธ”์ธ ๊ฒƒ ์ฒ˜๋Ÿผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
      SELECT T1.C1, T2.C1, T2.C2 FROM T1 T1,
      (SELECT C1, C2 FROM T2) T2 WHERE T1.C1 = T2.C1;
    2. HAVING ์ ˆ์—์„œ ์‚ฌ์šฉํ•˜๋Š” ์„œ๋ธŒ์ฟผ๋ฆฌ : ๊ทธ๋ฃนํ•จ์ˆ˜์™€ ํ•จ๊ป˜ ์‚ฌ์šฉ๋  ๋•Œ ๊ทธ๋ฃน์นญ๋œ ๊ฒฐ๊ณผ์— ๋Œ€ํ•ด ๋ถ€๊ฐ€์ ์ธ ์กฐ๊ฑด์„ ์ฃผ๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
    3. UPDATE ๋ฌธ์˜ SET์ ˆ์—์„œ์˜ ์„œ๋ธŒ์ฟผ๋ฆฌ, INSERT๋ฌธ์˜ VALUES ์ ˆ์—์„œ ์„œ๋ธŒ์ฟผ๋ฆฌ๋„ HAVING ์ ˆ์—์„œ ์‚ฌ์šฉํ• ๋•Œ ๋ชฉ์ ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

๐Ÿ“Œ DROP, TRUNCATE, DELETE์— ๊ฐ๊ฐ์— ๋Œ€ํ•ด ์„ค๋ช…ํ•ด์ฃผ์„ธ์š”. ์–ด๋–ค์ฐจ์ด๊ฐ€ ์žˆ๋‚˜์š”?

  • DROP : ํ…Œ์ด๋ธ” ์ „์ฒด๋ฅผ ์‚ญ์ œํ•˜๊ณ  ํ•ด๋‹น ํ…Œ์ด๋ธ”์˜ ๊ตฌ์กฐ ์ž์ฒด๋ฅผ ํŒŒ๊ดดํ•ฉ๋‹ˆ๋‹ค.
  • TRUNCATE : ํ…Œ์ด๋ธ”์˜ ๋ชจ๋“  ํ–‰์„ ์‚ญ์ œํ•˜์ง€๋งŒ ํ…Œ์ด๋ธ” ๊ตฌ์กฐ๋Š” ์œ ์ง€ํ•ฉ๋‹ˆ๋‹ค.
  • DELETE : ํ…Œ์ด๋ธ”์˜ ์ผ๋ถ€ ๋˜๋Š” ๋ชจ๋“  ํ–‰์„ ์‚ญ์ œํ•ฉ๋‹ˆ๋‹ค. ํ…Œ์ด๋ธ” ๊ตฌ์กฐ๋Š” ์œ ์ง€ ๋ฉ๋‹ˆ๋‹ค.

TRUNCATE์™€ DELETE๋Š” ๊ตฌ์กฐ๊ฐ€ ์œ ์ง€๊ฐ€ ๋˜๋Š” ์ ์ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์— Rollback์ด ๊ฐ€๋Šฅํ•˜์ง€๋งŒ, DROP ๊ฐ™์€ ๊ฒฝ์šฐ์—๋Š” Table structure๊นŒ์ง€ ํŒŒ๊ดดํ•˜๊ธฐ ๋•Œ๋ฌธ์— Rollback์ด ๋ถˆ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

-- DROP
DROP TABLE employees;

-- TRUNCATE
TRUNCATE TABLE employees;

-- DELETE
DELETE FROM employees WHERE department_id = 1;

๐Ÿ“Œ DISTINCT์— ๋Œ€ํ•ด์„œ ์„ค๋ช…ํ•ด์ฃผ์„ธ์š”. ์‚ฌ์šฉํ•ด๋ณธ ๊ฒฝํ—˜๋„ ์„ค๋ช…ํ•ด์ฃผ์„ธ์š”

DISTINCT๋Š” ์ค‘๋ณต๋œ ๊ฒฐ๊ณผ๋ฅผ ์ œ๊ฑฐํ•˜์—ฌ ๊ณ ์œ ํ•œ ๊ฒฐ๊ณผ๋งŒ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ‚ค์›Œ๋“œ ์ž…๋‹ˆ๋‹ค. ์ฃผ๋กœ ์ค‘๋ณต๋œ ๊ฐ’์„ ๊ฐ€์ง„ ์—ด์ด ์žˆ์„ ๋•Œ ์‚ฌ์šฉ๋˜๊ณ , ํŠนํžˆ ๋ฐ์ดํ„ฐ ๋ถ„์„์ด๋‚˜ ๋ณด๊ณ ์„œ ์ž‘์„ฑ ์‹œ์— ์ค‘๋ณต๋œ ๊ฒฐ๊ณผ๋ฅผ ๋ฐฉ์ง€ํ•˜๊ธฐ ์œ„ํ•ด ์ž์ฃผ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.


๐Ÿ“Œ SQL Injection ๊ณต๊ฒฉ์ด ๋ฌด์—‡์ธ์ง€ ์–ด๋–ป๊ฒŒ ๊ณต๊ฒฉ์„ ์˜ˆ๋ฐฉํ•  ์ˆ˜ ์žˆ๋Š”์ง€ ์„ค๋ช…ํ•ด์ฃผ์„ธ์š”.

SQL Injection์€ ์•…์˜์ ์ธ ์‚ฌ์šฉ์ž ์ž…๋ ฅ๋ž€์— SQL ์ฝ”๋“œ๋ฅผ ์‚ฝ์ž…ํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ฅผ ๊ณต๊ฒฉํ•˜๋Š” ๊ธฐ๋ฒ•์ž…๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ๊ณต๊ฒฉ์— ํ•ต์‹ฌ์€ ํด๋ผ์ด์–ธํŠธ ์ธก์—์„œ SQL ์ฟผ๋ฆฌ์— ์‹ ๋ขฐํ•  ์ˆ˜ ์—†๋Š” ๋ฐ์ดํ„ฐ๊ฐ€ ์ž…๋ ฅ๋˜์—ˆ์„ ๋•Œ, ๋ฐ์ดํ„ฐ๊ฐ€ ์ฟผ๋ฆฌ ๋กœ์ง์˜ ์ผ๋ถ€๋กœ ํ•ด์„๋˜์–ด DB์—์„œ ์‹คํ–‰๋  ๋•Œ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค.

  • ๊ณต๊ฒฉ์ „
SELECT * FROM USERTABLE WHERE username='your_user_input' AND password='your_password_input'; 
  • ๊ณต๊ฒฉํ›„
SELECT * FROM USERTABLE WHERE username='random_input' 
OR 1=1 -- 'AND password='random_password';

์—ฌ๊ธฐ์„œ "--"๋Š” ์ฃผ์„์„ ์˜๋ฏธํ•˜๋ฉฐ, ์„œ๋ธŒ์ฟผ๋ฆฌ๋ฅผ ๋ชจ๋‘ ์˜๋ฏธ์—†๋Š” ๋ฌธ์ž์—ด๋กœ ๋งŒ๋“ค์–ด ๋ฒ„๋ฆฌ๊ฒŒ ๋œ๋‹ค. ์ด๋Ÿฌํ•œ ๊ณต๊ฒฉ์„ ์˜ˆ๋ฐฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์‚ฌ์šฉ์ž ์ž…๋ ฅ์˜ ์œ ํšจ์„ฑ ๊ฒ€์ฆ์„ ํ•˜๊ณ  ์ด์Šค์ผ€์ดํ”„ ์ฒ˜๋ฆฌ๋ฅผ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค.

// ์˜ˆ๋ฐฉ ๋ฐฉ๋ฒ•: Prepared Statements ์‚ฌ์šฉ
$stmt = $conn->prepare("SELECT * FROM users WHERE username = ? AND password = ?");
$stmt->bind_param("ss", $username, $password);
$stmt->execute();

๐Ÿ“Œ ์•Œ๊ณ  ์žˆ๋Š” SQL ์•ˆํ‹ฐํŒจํ„ด์ด ์žˆ๋‹ค๋ฉด ์„ค๋ช…ํ•ด์ฃผ์„ธ์š”.

SQL Antipattern์ด๋ผ๋Š” ๊ฒƒ์€ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ ํ”ํžˆ ๋ฐœ์ƒํ•˜๋Š” ๋ถ€์ ์ ˆํ•œ ์ฟผ๋ฆฌ๋‚˜ ์„ค๊ณ„ ๋“ฑ์˜ ํŒจํ„ด์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ์•ˆํ‹ฐํŒจํ„ด์€ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์˜ ์„ฑ๋Šฅ์ €ํ•˜, ์œ ์ง€๋ณด์ˆ˜์˜ ์–ด๋ ค์›€๋“ฑ์„ ์ดˆ๋ž˜ ํ•  ์ˆ˜ ์žˆ์–ด ์œ ์˜ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

๋Œ€ํ‘œ์  ์•ˆํ‹ฐํŒจํ„ด

  1. SELECT * ์‚ฌ์šฉ : ์‹ค์ œ๋กœ ํ•„์š”ํ•œ ๋ฐ์ดํ„ฐ๋ณด๋‹ค ๋งŽ์€ ๋ฐ์ดํ„ฐ๋ฅผ ์ „์†กํ•˜๊ฒŒ ๋จ์œผ๋กœ ์„ฑ๋Šฅ์— ์˜ํ–ฅ์„ ์ค„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  2. ์ˆซ์ž๋กœ๋œ ์ธ๋ฑ์Šค ์‚ฌ์šฉ : ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ๋”ฐ๋ผ ๋ฌธ์ž์—ด์ด๋‚˜ ๋‹ค๋ฅธ ํ˜•์‹์œผ๋กœ ์ •์˜๊ฐ€ ๋˜์–ด ์žˆ์„ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๋ฌด์ž‘์ • ์ธ๋ฑ์Šค๋ฅผ ์ˆซ์ž๋กœ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์„ ํ”ผํ•˜๊ณ  ์ •ํ™•ํ•œ ๋ฐ์ดํ„ฐ ํ˜•์‹์„ ์‚ฌ์šฉํ•˜๊ฑฐ๋‚˜ ํ˜•๋ณ€ํ™˜์„ ํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค.

3. ORM์˜ N+1 ๋ฌธ์ œ? ์กฐ๊ธˆ ๋” ๊ณต๋ถ€๊ฐ€ ํ•„์š”ํ•จ

๐Ÿ“Œ ํŽ˜์ด์ง€๋„ค์ด์…˜์„ ๊ตฌํ˜„ํ•œ๋‹ค๊ณ  ํ–ˆ์„๋•Œ ์ฟผ๋ฆฌ๋ฅผ ์–ด๋–ป๊ฒŒ ์ž‘์„ฑํ•ด์•ผํ• ๊นŒ์š”?

"ํŽ˜์ง€๋„ค์ด์…˜"์ด๋ž€ ๋Œ€๋Ÿ‰์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์‚ฌ์šฉ์ž์—๊ฒŒ ๋ณด์—ฌ์ค„ ๋•Œ, ํ•œ ๋ฒˆ์— ๋ชจ๋“  ๋ฐ์ดํ„ฐ๋ฅผ ๋กœ๋“œํ•˜๋Š” ๋Œ€์‹  ์ผ์ •๋Ÿ‰์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๋‚˜๋ˆ„์–ด ๋ณด์—ฌ์ฃผ๋Š” ๊ธฐ์ˆ ์„ ๋งํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ํ†ตํ•ด ์›น์˜ ๋กœ๋”ฉ ์†๋„๋ฅผ ๋‹จ์ถ• ์‹œํ‚ค๋Š” ๋“ฑ์˜ ์ผ์„ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

  • LIMIT, OFFSET ์‚ฌ์šฉ
    ์œ„์˜ LIMIT๊ณผ OFFSET์„ ์‚ฌ์šฉํ•˜์—ฌ ํŠน์ • ๋ฒ”์œ„์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์™€ ๋น ๋ฅด๊ฒŒ ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ์–ด ์˜ค๋Š” ์ฟผ๋ฆฌ๋ฅผ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.
    "The LIMIT clause can be used to constrain the number of rows returned by the SELECT statement. LIMIT takes one or two numeric arguments, which must both be nonnegative integer constants, with these exceptions:"

reference :
1. What is Query?
2. Velog ์บก์…˜ ๋„ฃ๊ธฐ!
3. SQL processing logic
4. Entirely information about SQL with pictures
5. Definition of Latch contention
6. SQL Query
7. Comprehension about SELECT FOR UPDATE
8.Details about sub-query, More examples
9. SQL Injection

profile
๋ฐฑ์—”๋“œ ํ™”์ดํŒ… :)

0๊ฐœ์˜ ๋Œ“๊ธ€