SQL

SQL

Tags
Backend
SQL
Published
Author
SQL(Structured Query Language)SQL ๋ฌธ๋ฒ• ์ˆœ์„œSQL ์‹ค์ œ ์‹คํ–‰ ์ˆœ์„œํ…Œ์ด๋ธ” ๊ตฌ์กฐ ๋ณด๊ธฐDB ๋งŒ๋“ค๊ธฐTABLEํ…Œ์ด๋ธ” ์กฐํšŒํ…Œ์ด๋ธ” ์ƒ์„ฑํ…Œ์ด๋ธ” ์‚ญ์ œํ…Œ์ด๋ธ” ๋ณ€๊ฒฝDBEntity(์—”ํ„ฐํ‹ฐ)Attribute(์†์„ฑ)Relationship(๊ด€๊ณ„)Identifier(์‹๋ณ„์ž)๋ฐ์ดํ„ฐ ์‚ฝ์ž… (INSERT)๋™์‹œ์— ์—ฌ๋Ÿฌ ํ–‰ INSERT ๋ฐฉ๋ฒ•select๋œ ๊ฒฐ๊ณผ INSERT ๋ฐฉ๋ฒ•ํŠน๋ณ„ํ•œ ๊ฐ’ INSERT ๋ฐฉ๋ฒ•๊ณ ์œ ํ‚ค ๊ฐ’ ๋งŒ๋“ค๊ธฐ(Sequence ์ƒ์„ฑ๊ธฐ ์ด์šฉ)Sequence ํ™•์ธ๊ณ ์œ ํ‚ค ์ƒ์„ฑ ๋ฐ ์ž…๋ ฅcolumn ์„ ํƒ ์กฐํšŒ (SELECT)์ „์ฒด ํ…Œ์ด๋ธ” *LIMIT OFFSET๋ ˆ์ฝ”๋“œ ์‚ญ์ œ (DELETE)์ˆ˜์ • (UPDATE)TRUNCATEDROP๊ธฐํƒ€ ํ•จ์ˆ˜wildecards(%, _)NULL ์ฒดํฌ์—ฐ์‚ฐ์ž ์šฐ์„ ์ˆœ์œ„ASGROUP BYJOINInner JOINSELF INNER JOINOUTER JOINLEFT OUTER JOINRIGHT OUTER JOINFULL OUTER JOINJOIN ์ •๋ฆฌ๋ณธAPPLY ์—ฐ์‚ฐ์žInline view (์ธ๋ผ์ธ ๋ทฐ)์„œ๋ธŒ์ฟผ๋ฆฌMulti Row Sub Query (๋‹ค์ค‘ ํ–‰ ์„œ๋ธŒ์ฟผ๋ฆฌ)MSSQL๋ฐ์ดํ„ฐ ๊ฒ€์ƒ‰BETWEEN A AND BROLLUPPIVOTCASE WHENํ”„๋กœ์‹œ์ € ์˜ค๋ฅ˜ ์ฒ˜๋ฆฌํ•จ์ˆ˜Stored Procedure(์ €์žฅ ํ”„๋กœ์‹œ์ €)์ผ๋ฐ˜ ์ฟผ๋ฆฌ๋ฌธ VS ์ €์žฅ ํ”„๋กœ์‹œ์ €๋ช…๋ น์–ดMERGE ๊ตฌ๋ฌธWHEN ๊ตฌ๋ฌธEXITS, NOT EXITSSELECT ์ปฌ๋Ÿผ ๊ฐ’ ๋ณ€์ˆ˜๋กœ ์ง€์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•dbodbo์™€ ์Šคํ‚ค๋งˆ ์ฐจ์ดTriggerMSSQL ํŠธ๋ฆฌ๊ฑฐqueryUNIONCursor๋ฌธCursorํŠน์ง•๋‚ด ์„œ๋ฒ„์˜ ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ๋Ÿ‰STRING_AGG()CHARINDEX()IIFTOP์‹œ์ž‘์ผ์ž ์ข…๋ฃŒ์ผ์ž ์‚ฌ์ด ๋ชจ๋“  ์ผ์ž ์กฐํšŒPartition byPaging QueryOFFSET ROWS FETCH์กฐํšŒ ์ˆœ๋ฒˆ ๋งค๊ธฐ๊ธฐ๋ถ„์„ํ•จ์ˆ˜over()over(ORDER BY ์ปฌ๋Ÿผ)over(partition by ์ปฌ๋Ÿผ)over(partition by ์ปฌ๋Ÿผ1 ORDER BY ์ปฌ๋Ÿผ2) Select ๋ฌธ ๋‘๊ฐœ ํ•ฉ์น˜๊ธฐcount( ) ํ•จ์ˆ˜DATE(๋‚ ์งœ๊ด€๋ จ)date_addํŠน์ •๋‚ ์งœ 1์ผ๋ถ€ํ„ฐ ํŠน์ •๋‚ ์งœ๊นŒ์ง€๋…„, ๋‹ฌ, ์ผ (์กฐํšŒ)ํŠน์ • ๋‹ฌ๊นŒ์ง€ ์กฐํšŒdense_rank()ROW NUMBER()WITH (NOLOCK)์ž„์‹œํ…Œ์ด๋ธ” ์ปฌ๋Ÿผ๋ช… ๊ฐ€์ ธ์˜ค๊ธฐํ…Œ์ด๋ธ” ์ปฌ๋Ÿผ๋ช… ๊ฐ€์ ธ์˜ค๊ธฐSQL ๋กœ๊ทธ ํŒŒ์ผ1. ๋กœ๊ทธ ํŒŒ์ผ ํ™•์ธ2. ๋กœ๊ทธ ํŒŒ์ผ ์šฉ๋Ÿ‰ ์ค„์ด๊ธฐSQL ์ฟผ๋ฆฌ ์ฒ˜๋ฆฌ ์†๋„ ํ™•์ธIN, NOT IN ์—ฐ์‚ฐ์žINNOT IN, ์ฃผ์˜์‚ฌํ•ญ์‚ฌ์šฉ์ž ์ •์˜ ํ•จ์ˆ˜(User-defined Fuction, UDF)์ด์ ์Šค์นผ๋ผ ํ•จ์ˆ˜ (Scalar User-defined Fuction, scalar UDF)ํ…Œ์ด๋ธ” ๋ฐ˜ํ™˜ ํ•จ์ˆ˜ (Table-valued Fuction, TVF)์‹œ์Šคํ…œ ํ•จ์ˆ˜ (System Fuction)Fuction ์ƒ์„ฑFunction ์‹คํ–‰Function ์ˆ˜์ •Function ์‚ญ์ œMSSQL , 0์œผ๋กœ ๋‚˜๋ˆ„๊ธฐ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•˜์˜€์Šต๋‹ˆ๋‹ค ๊ด€๋ จ ์ฒ˜๋ฆฌgroup by(์ง‘๊ณ„ํ•จ์ˆ˜) vs partition by(์œˆ๋„์šฐํ•จ์ˆ˜)group bypartition by๋น„์œจ ๊ด€๋ จ ํ•จ์ˆ˜OUTER JOIN VS INNER JOIN ์„ฑ๋Šฅ์ฟผ๋ฆฌ๋ฌธ ๋นจ๊ฐ„์ค„ ํ‘œ์‹œ ํ•ด๊ฒฐSQL ๋“ค์—ฌ์”Œ๊ธฐ ์˜ค๋ฅ˜ํŠธ๋žœ์žญ์…˜(Transaction)์ปค๋ฐ‹ ๋˜๋Š” ๋กค๋ฐฑํ•˜๊ธฐ ์ „ ์ƒํƒœ์ปค๋ฐ‹ ํ•œ ํ›„ ์ƒํƒœ์ธ๋ฑ์Šค(INDEX)ํ…Œ์ด๋ธ” ์•ก์„ธ์Šค ๋ฐฉ๋ฒ•์ธ๋ฑ์Šค ์ƒ์„ฑ์ธ๋ฑ์Šค ์‚ญ์ œํด๋Ÿฌ์Šคํ„ฐํ˜• ์ธ๋ฑ์Šค์‹คํ–‰ ๊ณ„ํš ๋…ผ๋ฆฌ ๋ฐ ๋ฌผ๋ฆฌ ์—ฐ์‚ฐ์ž ์ฐธ์กฐCompute ScalarStream Aggregate์žฌ๊ท€์  ๊ณตํ†ต ํ…Œ์ด๋ธ” ํ‘œํ˜„์‹ (CTE)์•ต์ปค ๋ฉค๋ฒ„(Anchor Member) ์žฌ๊ท€ ๋ฉค๋ฒ„(Recursive Member)์‹คํ–‰ ๊ณผ์ •with ์ ˆ์˜ ๋™์ž‘ ๋ฐฉ์‹Inline ViewMaterializeBulk InsertREPLICATEOUTPUT ์ ˆEmergency ๋ฐฑ์—…1. ๋น„์ƒ๋กœ๊ทธ ๋ฐฑ์—…(Tail-Log Backup)2. DB ๋ฐฑ์—…๋ณธ ๋ถˆ๋Ÿฌ์™€์„œ ๋ถ™์ด๊ธฐ3. ๋ณต์› ์‹œํ‚ฌ ์‹œ์  ์ฐพ๊ธฐ4. Restore ์ง„ํ–‰ ๋ฐ Recovery State๋ฅผ โ€˜RECOVERYโ€™๋กœ ๋ฐ”๊พธ๊ธฐ5. ๋ณต์›ํ•œ Dummy DB๋ฅผ ํ†ตํ•ด ์›๋ž˜ DB์˜ ๋ฐ”๋€ ๋ถ€๋ถ„ ์›๋ณตํ•˜๊ธฐ๋ณ€๊ฒฝ ๋‚ด์šฉ ์ถ”์ 1. ์„ธํŒ…2. ๋ณ€๊ฒฝ ๋‚ด์šฉ ๊ฐ€์ ธ์˜ค๊ธฐ

SQL(Structured Query Language)

๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์ •์˜, ์กฐ์ž‘, ์ œ์–ดํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ๋˜๋Š” ๋ช…๋ น์–ด
  • DDL(๋ฐ์ดํ„ฐ ์ •์˜์–ด) : ํ…Œ์ด๋ธ”๊ณผ ๊ฐ™์€ ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ๋ฅผ ์ •์˜ํ•˜๋Š”๋ฐ ์‚ฌ์šฉ๋˜๋Š” ๋ช…๋ น์–ด๋“ค๋กœ ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ์™€ ๊ด€๋ จ๋œ ๋ช…๋ น์–ด
  • DML(๋ฐ์ดํ„ฐ ์กฐ์ž‘์–ด) : ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ์ €์žฅ๋œ ๋ฐ์ดํ„ฐ๋ฅผ ์กฐํšŒํ•˜๊ฑฐ๋‚˜ ์‚ฝ์ž…, ์ˆ˜์ •, ์‚ญ์ œํ•˜๋Š” ๋“ฑ์˜ ์—ญํ• ์„ ํ•˜๋Š” ์–ธ์–ด
  • DCL(๋ฐ์ดํ„ฐ ์ œ์–ด์–ด) : ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ์ ‘๊ทผํ•˜๊ณ  ๊ฐ์ฒด๋“ค์„ ์‚ฌ์šฉํ•˜๋„๋ก ๊ถŒํ•œ์„ ์ฃผ๊ณ  ํšŒ์ˆ˜ํ•˜๋Š” ๋ช…๋ น์–ด
  • TCL(ํŠธ๋žœ์žญ์…˜ ์ œ์–ด์–ด) : ๋…ผ๋ฆฌ์ ์ธ ์ž‘์—…์˜ ๋‹จ์œ„๋ฅผ ๋ฌถ์–ด์„œ DML์— ์˜ํ•ด ์กฐ์ž‘๋œ ๊ฒฐ๊ณผ๋ฅผ ์ž‘์—…๋‹จ์œ„ ๋ณ„๋กœ ์ œ์–ดํ•˜๋Š” ๋ช…๋ น์–ด

SQL ๋ฌธ๋ฒ• ์ˆœ์„œ

SELECT โ†’ FROM โ†’ WHERE โ†’ GROUP BY โ†’ HAVING โ†’ ORDER BY

SQL ์‹ค์ œ ์‹คํ–‰ ์ˆœ์„œ

FROM โ†’ ON โ†’ JOIN โ†’ WHERE โ†’ GROUP BY โ†’ HAVING โ†’ SELECT โ†’ DISTINCT โ†’ ORDER BY
  • FROM : ๊ฐ ํ…Œ์ด๋ธ” ํ™•์ธ
  • ON : ์กฐ์ธ ์กฐ๊ฑด ํ™•์ธ
  • JOIN : ํ…Œ์ด๋ธ” ์กฐ์ธ(๋ณ‘ํ•ฉ)
  • WHERE : ๋ฐ์ดํ„ฐ ์ถ”์ถœ ์กฐ๊ฑด ํ™•์ธ
  • GROUP BY : ํŠน์ • ์นผ๋Ÿผ์œผ๋กœ ๋ฐ์ดํ„ฐ ๊ทธ๋ฃนํ™”
  • HAVING : ๊ทธ๋ฃนํ™” ์ดํ›„ ๋ฐ์ดํ„ฐ ์ถ”์ถœ ์กฐ๊ฑด ํ™•์ธ
  • SELECT : ๋ฐ์ดํ„ฐ ์ถ”์ถœ
  • DISTINCT : ์ค‘๋ณต ์ œ๊ฑฐ
  • ORDER BY : ๋ฐ์ดํ„ฐ ์ •๋ ฌ

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

DESC tablename

DB ๋งŒ๋“ค๊ธฐ

use master GO if NOT EXISTS ( select name from sys.databases where name = N'DevelopmentDB' ) create database [DevelopmentDB] GO

TABLE

ํ…Œ์ด๋ธ” ์กฐํšŒ

  • ๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌ
SELECT ํ•„๋“œ๋ช… FROM ํ…Œ์ด๋ธ”๋ช… ORDER BY ํ•„๋“œ๋ช… DESC;
  • ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ
SELECT ํ•„๋“œ๋ช… FROM ํ…Œ์ด๋ธ”๋ช… ORDER BY ํ•„๋“œ๋ช… ASC;

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

CREATE TABLE users( id INTEGER PRIMARY KEY, name TEXT ); CREATE TABLE ํ…Œ์ด๋ธ”๋ช… ( ์ปฌ๋Ÿผ1 ๋ฐ์ดํ„ฐํƒ€์ž… ์ œ์•ฝ์กฐ๊ฑด, ์ปฌ๋Ÿผ2 ๋ฐ์ดํ„ฐํƒ€์ž… ์ œ์•ฝ์กฐ๊ฑด, ์ปฌ๋Ÿผ3 ๋ฐ์ดํ„ฐํƒ€์ž… ์ œ์•ฝ์กฐ๊ฑด, ... ); // NOT NULL : ๊ณต๋ฐฑ์„ ํ—ˆ์šฉํ•˜์ง€ ์•Š๊ฒ ๋‹ค๋Š” ์˜ต์…˜ // AUTO_INCREMENT : ์ž๋ฃŒํ˜•์ด INT(์ •์ˆ˜ํ˜•)์ผ๋•Œ ์ ์šฉ ๊ฐ€๋Šฅ, ๋ฐ์ดํ„ฐ๊ฐ€ ๋งŽ์•„์งˆ ๋•Œ๋งˆ๋‹ค 1์”ฉ ์ž๋™ ์ฆ๊ฐ€ // PRIMARY KEY : ์ค‘๋ณต ๊ฐ’ ํ—ˆ์šฉ ์•ˆํ•˜๋Š” ์ปฌ๋Ÿผ 1๊ฐœ ์„ ํƒ (ํ…Œ์ด๋ธ” ๋‚ด ๊ณ ์œ ๊ฐ’ ํ•„์š”ํ•œ ๊ฒฝ์šฐ)
notion image

ํ…Œ์ด๋ธ” ์‚ญ์ œ

DROP TABLE users;

ํ…Œ์ด๋ธ” ๋ณ€๊ฒฝ

  • table ์ด๋ฆ„ ๋ณ€๊ฒฝ
ALTER TABLE <๊ธฐ์กด table๋ช…> RENMAE TO <๋ณ€๊ฒฝํ•˜๊ณ  ์‹ถ์€ table๋ช…>
  • ์ƒˆ๋กœ์šด column ์ถ”๊ฐ€
ALTER TABLE <table๋ช…> ADD COLUMN <์ถ”๊ฐ€ํ•  colum๋ช…> <column ํƒ€์ž…> <์˜ต์…˜>;
  • column ์ด๋ฆ„ ๋ณ€๊ฒฝ
ALTER TABLE <table๋ช…> RENAME COLUMN <๊ธฐ์กด column๋ช…> TO <๋ณ€๊ฒฝํ•˜๊ณ  ์‹ถ์€ column๋ช…>;
  • ํ…Œ์ด๋ธ” ์†์„ฑ ๋ณ€๊ฒฝ
ALTER TABLE [ํ…Œ์ด๋ธ”๋ช…] change [ํ•ด๋‹นcolumn๋ช…] [๋ฐ”๊ฟ€ column๋ช…] [๋ณ€๊ฒฝํ• ์†์„ฑ]
  • ๋ฐ์ดํ„ฐ ์ œ์•ฝ ์กฐ๊ฑด ์ถ”๊ฐ€, ์‚ญ์ œ
-- primary key ALTER TABLE [ํ…Œ์ด๋ธ”๋ช…] ADD CONSTRAINT primary key (์ปฌ๋Ÿผ๋ช…); -- foreign key ALTER TABLE [ํ…Œ์ด๋ธ”๋ช…] ADD CONSTRAINT foreign key (์ปฌ๋Ÿผ๋ช…) references [๋ถ€๋ชจํ…Œ์ด๋ธ”๋ช…] (pk์ปฌ๋Ÿผ๋ช…)

DB

Entity(์—”ํ„ฐํ‹ฐ)

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

Attribute(์†์„ฑ)

  • ์—ด(column)
  • ์ธ์Šคํ„ด์Šค๋กœ ์˜๋งˆ์ƒ ๋”์ด์ƒ ๋ถ„๋ฆฌ๋˜์ง€ ์•Š๋Š” ์ตœ์†Œ์˜ ๋ฐ์ดํ„ฐ ๋‹จ์œ„

Relationship(๊ด€๊ณ„)

  • ํ–‰(row)
  • ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ์ €์žฅ๋œ ๋ฐ์ดํ„ฐ ๋‚ด์šฉ์˜ ์ „์ฒด ์ง‘ํ•ฉ์„ ์˜๋ฏธ

Identifier(์‹๋ณ„์ž)

  • ํ•˜๋‚˜์˜ ์—”ํ„ฐํ‹ฐ ๋‚ด์— ๊ตฌ์„ฑ๋˜์–ด ์žˆ๋Š” ์—ฌ๋Ÿฌ ์†์„ฑ๋“ค ์ค‘์— ์—”ํ„ฐํ‹ฐ๋ฅผ ๋Œ€ํ‘œํ•  ์ˆ˜ ์žˆ๋Š” ์†์„ฑ
  • ํ•˜๋‚˜์˜ ์—”ํ„ฐํ‹ฐ ์•ˆ์—๋Š” ๋ฐ˜๋“œ์‹œ ์œ ์ผํ•œ ์‹๋ณ„์ž๊ฐ€ ์กด์žฌํ•ด์•ผ ํ•จ

๋ฐ์ดํ„ฐ ์‚ฝ์ž… (INSERT)

INSERT INTO users (name, age) VALUES ('ํ™๊ธธ๋™', 23);
table์˜ column์— ์žˆ๋Š” ๋ชจ๋“  VALUE๋ฅผ ๋„ฃ์„๋•Œ์—๋Š” column๋ช…์„ ์ ์–ด์ฃผ์ง€ ์•Š์•„๋„ ๋จ

๋™์‹œ์— ์—ฌ๋Ÿฌ ํ–‰ INSERT ๋ฐฉ๋ฒ•

INSERT INTO users (name, age) VALUES ('๊ฐ€๋‚˜๋‹ค',20), ('hongil',44)

select๋œ ๊ฒฐ๊ณผ INSERT ๋ฐฉ๋ฒ•

INSERT INTO dbo.users (deptno, name, age) SELECT deptno + 1 , 'tansan' , 30 FROM users WHERE deptno IN (60, 70, 80)

ํŠน๋ณ„ํ•œ ๊ฐ’ INSERT ๋ฐฉ๋ฒ•

--NULL ๊ฐ’ ๋„ฃ๊ธฐ insert into s_customer values (216, 'Tester', null, null, 'good' 2, 12, null); -- Empty String ๋„ฃ๊ธฐ insert into s_customer values (216, "", "", "", 'good' 2, 12, ""); -- ํ•จ์ˆ˜ ์‚ฌ์šฉํ•˜์—ฌ insertํ•˜๊ธฐ insert into s_customer values (27,"", 'Student', USER, SYSDATE, null); -- USER : ํ˜„์žฌ USER ์ด๋ฆ„, SYSDATE : ํ˜„์žฌ ์‹œ์Šคํ…œ ์ผ์ž,์‹œ๊ฐ„

๊ณ ์œ ํ‚ค ๊ฐ’ ๋งŒ๋“ค๊ธฐ(Sequence ์ƒ์„ฑ๊ธฐ ์ด์šฉ)

Sequence ํ™•์ธ

select object_name from user_objects where object_type = 'SEQUENCE';

๊ณ ์œ ํ‚ค ์ƒ์„ฑ ๋ฐ ์ž…๋ ฅ

insert into s_dept(id, name, region_id) values (s_dept_id.NEXTVAL, 'Education', 1); -- Sequence_name.NEXTVAL : ํ˜„์žฌ๊ฐ’์˜ ๋‹ค์Œ๊ฐ’์„ ์ฃผ๊ณ  ์ž์‹ ์€ ๋‹ค์Œ๊ฐ’์„ ๊ฐ€์ง„๋‹ค. -- Sequence_name.CURRRVAL : ํ˜„์žฌ๊ฐ’์„ ์ค€๋‹ค.
  • ํ•œ๋ฒˆ ๋ถ€์—ฌ๋œ Sequence ๊ฐ’์€ ๋˜๋Œ๋ฆด ์ˆ˜ ์—†๋‹ค.
  • ์žฌ์ •์˜ํ•˜์—ฌ ์ดˆ๊ธฐ๊ฐ’์„ ์žฌ์„ค์ • ๊ฐ€๋Šฅ

column ์„ ํƒ ์กฐํšŒ (SELECT)

์ „์ฒด ํ…Œ์ด๋ธ” *

LIMIT

์œ„์—์„œ N๊ฐœ์˜ ๋ฐ์ดํ„ฐ
SELECT rowid, name FROM users LIMIT 2;

OFFSET

์•ž์— N๊ฐœ ์ œ๊ฑฐ
SELECT rowid, name FROM users LIMIT 1 OFFSET 2;

๋ ˆ์ฝ”๋“œ ์‚ญ์ œ (DELETE)

DELETE FROM users WHERE rowid=2; SELECT rowid, * FROM users; rowid name age address ----- ---- --- ------- 1 ํ™๊ธธ๋™ 30 ์„œ์šธ 3 ๊ฐ€๋‚˜๋‹ค 23 ๋ถ€์‚ฐ

์ˆ˜์ • (UPDATE)

UPDATE users SET name="ํ™๊ธธ๋™", address="์ œ์ฃผ๋„" WHERE rowid='3' SELECT * FROM users; rowid name age address ----- ---- --- ------- 1 ํ™๊ธธ๋™ 30 ์„œ์šธ 3 ํ™๊ธธ๋™ 23 ์ œ์ฃผ๋„
UPDATE dbo.dept // ์Šคํ‚ค๋งˆ ๋ช… SET dname = 'SALES TEAM' // ์—…๋ฐ์ดํŠธ ์ปฌ๋Ÿผ = ์—…๋ฐ์ดํŠธ๊ฐ’๊ฐ’ , loc = 'TEXAS'

TRUNCATE

TRUNECATE TABLE [์Šคํ‚ค๋งˆ๋ช…].[ํ…Œ์ด๋ธ”๋ช…]
  • ํ…Œ์ด๋ธ” ๋‚ด ๋ฐ์ดํ„ฐ๋ฅผ ์‚ญ์ œํ•˜๋Š” ์ฟผ๋ฆฌ
  • ๋‹ค๋งŒ ํ…Œ์ด๋ธ”์„ ์‚ญ์ œํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹Œ ํ…Œ์ด๋ธ” ๋‚ด ๋ฐ์ดํ„ฐ๋ฅผ ์‚ญ์ œํ•˜์—ฌ ์†Œ์œ„ ํ…Œ์ด๋ธ” ๊ป๋ฐ๊ธฐ๋Š” ๋‚จ์•„ ์žˆ์Œ
  • AUTO COMMIT์ด์—ฌ์„œ ํ•œ๋ฒˆ ์‚ญ์ œํ•˜๋ฉด ๋ณต๊ตฌ X

DROP

DROP TABLE [์Šคํ‚ค๋งˆ๋ช…].[ํ…Œ์ด๋ธ”๋ช…]
  • ํ…Œ์ด๋ธ” ์ „์ฒด๋ฅผ ์‚ญ์ œํ•˜๋Š” ์ฟผ๋ฆฌ
  • AUTO COMMIT์ด์—ฌ์„œ ํ•œ๋ฒˆ ์‚ญ์ œํ•˜๋ฉด ๋ณต๊ตฌ X
notion image

๊ธฐํƒ€ ํ•จ์ˆ˜

  • COUNT : ์„ ํƒํ•œ ํ–‰์˜ ๋ ˆ์ฝ”๋“œ ๊ฐฏ์ˆ˜ ์กฐํšŒ
SELECT COUNT(*) FROM users; // users ํ…Œ์ด๋ธ” ์ „์ฒด ๋ ˆ์ฝ”๋“œ ๊ฐฏ์ˆ˜
  • AVG : ํ‰๊ท 
SELECT AVG(age) FROM users WHERE age>=30; // 30์‚ด ์ด์ƒ์ธ ์‚ฌ๋žŒ๋“ค ๋‚˜์ด์˜ ํ‰๊ท 
  • MAX : ๊ฐ€์žฅ ๋†’์€ ๊ฐ’
SELECT name, MAX(age) FROM users; // ๋‚˜์ด๊ฐ€ ๊ฐ€์žฅ ๋งŽ์€ ์‚ฌ๋žŒ๊ณผ ๊ทธ ๋‚˜์ด๋ฅผ ์กฐํšŒ

wildecards(%, _)

  • % : ์ด ์ž๋ฆฌ์— ๋ฌธ์ž์—ด์ด ์กด์žฌํ•  ์ˆ˜๋„ ์•„๋‹ ์ˆ˜๋„ ์žˆ๋‹ค
  • _ : ๋ฌด์กฐ๊ฑด ๋‹จ์ผ ๋ฌธ์ž๊ฐ€ ๋“ค์–ด๊ฐ€์•ผ ํ•œ๋‹ค
SELECT * FROM users WHERE name LIKE '%๋™';

NULL ์ฒดํฌ

WHERE col_name is not null WHERE col_name is null
SELECT ANIMAL_TYPE, INFNULL(NAME, 'No name') NAME, SEX_UPON_INTKE FROM ANIMAL_INS

์—ฐ์‚ฐ์ž ์šฐ์„ ์ˆœ์œ„

  1. ๋ชจ๋“  ๋น„๊ต ์—ฐ์‚ฐ์ž
  1. NOT
  1. AND
  1. OR

AS

SELECT ๋ฌธ์„ ์‚ฌ์šฉํ•  ๋•Œ ๊ฒฐ๊ณผ๋กœ ๋‚˜์˜ค๋Š” ์นผ๋Ÿผ์— AS๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ณ„์นญ์„ ์ค„ ์ˆ˜ ์žˆ๋‹ค.

GROUP BY

๊ฐ™์€ ๊ฐ’์„ ๊ฐ€์ง„ ํ–‰๋ผ๋ฆฌ ํ•˜๋‚˜์˜ ๊ทธ๋ฃน์œผ๋กœ ๋ญ‰์ณ์ค๋‹ˆ๋‹ค.
ex) ํ–‰์ด ๋‘๊ฐœ๊ฐ€ ๋œ๊ฑธ ํ•˜๋‚˜์˜ ํ–‰์œผ๋กœ ํ•ฉ์น˜๊ธฐ ์œ„ํ•จ
์ง‘๊ณ„ํ•จ์ˆ˜
  • count
  • sum
  • max
  • min
  • avg

JOIN

๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์—์„œ โ€˜๋‘ ๊ฐœ ์ด์ƒ์˜ ํ…Œ์ด๋ธ”โ€™์„ ์—ฐ๊ฒฐํ•˜์—ฌ โ€˜ํ•˜๋‚˜์˜ ๊ฒฐ๊ณผ์˜ ํ…Œ์ด๋ธ”โ€™๋กœ ๋งŒ๋“œ๋Š”๊ฒƒ์„ ์˜๋ฏธ
  • Inner Join
  • Outer Join

Inner JOIN

๋‘ ํ…Œ์ด๋ธ”์—์„œ โ€˜๊ณตํ†ต๋œ ๊ฐ’โ€™์„ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ํ–‰๋“ค๋งŒ์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
SELECT * FROM ํ…Œ์ด๋ธ”1 INNER JOIN ํ…Œ์ด๋ธ”2 ON ํ…Œ์ด๋ธ”1.์—ด = ํ…Œ์ด๋ธ”2.์—ด;
notion image

SELF INNER JOIN

ํ•˜๋‚˜์˜ ํ…Œ์ด๋ธ” ๋‚ด์—์„œ ๋‹ค๋ฅธ ์—ด์„ ์ฐธ์กฐํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•˜๋Š” โ€˜์ž๊ธฐ ์ž์‹ ๊ณผ์˜ ์กฐ์ธโ€™๋ฐฉ๋ฒ•
SELECT ํ…Œ์ด๋ธ”1.์—ด, ํ…Œ์ด๋ธ”2.์—ด FROM ํ…Œ์ด๋ธ”1 t1 JOIN ํ…Œ์ด๋ธ”1 t2 ON ํ…Œ์ด๋ธ”1.์—ด = ํ…Œ์ด๋ธ”2.์—ด;

OUTER JOIN

๋‘ ํ…Œ์ด๋ธ”์—์„œ โ€˜๊ณตํ†ต๋œ ๊ฐ’์„ ๊ฐ€์ง€์ง€ ์•Š๋Š” ํ–‰๋“คโ€™๋„ ๋ฐ˜ํ™˜
  • ์–ด๋Š ํ•œ์ชฝ ํ…Œ์ด๋ธ”์ด ๊ธฐ์ค€์ด ๋˜์–ด ๋‹ค๋ฅธ์ชฝ ํ…Œ์ด๋ธ”์˜ ์—ฐ๊ฒฐ๋˜๋Š” ์กฐ๊ฑด์— ์ƒ๊ด€์—†์ด ๊ธฐ์ค€์ด ๋˜๋Š” ํ…Œ์ด๋ธ”์€ ๋ฌด์กฐ๊ฑด ์ถ”์ถœ๋˜๋Š” ์กฐ์ธ์ด๋‹ค.

LEFT OUTER JOIN

โ€˜์™ผ์ชฝ ํ…Œ์ด๋ธ”์˜ ๋ชจ๋“  ํ–‰โ€™๊ณผ โ€˜์˜ค๋ฅธ์ชฝ ํ…Œ์ด๋ธ”์—์„œ ์™ผ์ชฝ ํ…Œ์ด๋ธ”๊ณผ ๊ณตํ†ต๋œ ๊ฐ’โ€™์„ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ํ–‰๋“ค์„ ๋ฐ˜ํ™˜
SELECT * FROM ํ…Œ์ด๋ธ”1 LEFT JOIN ํ…Œ์ด๋ธ”2 ON ํ…Œ์ด๋ธ”1.์—ด = ํ…Œ์ด๋ธ”2.์—ด;
notion image

RIGHT OUTER JOIN

โ€˜์˜ค๋ฅธ์ชฝ ํ…Œ์ด๋ธ”์˜ ๋ชจ๋“  ํ–‰โ€™๊ณผ โ€˜์™ผ์ชฝ ํ…Œ์ด๋ธ”์—์„œ ์˜ค๋ฅธ์ชฝ ํ…Œ์ด๋ธ”๊ณผ ๊ณตํ†ต๋œ ๊ฐ’โ€™์„ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ํ–‰๋“ค์„ ๋ฐ˜ํ™˜
SELECT * FROM ํ…Œ์ด๋ธ”1 RIGHT JOIN ํ…Œ์ด๋ธ”2 ON ํ…Œ์ด๋ธ”1.์—ด = ํ…Œ์ด๋ธ”2.์—ด;
notion image

FULL OUTER JOIN

๋‘ ํ…Œ์ด๋ธ”์—์„œ โ€˜๋ชจ๋“  ๊ฐ’โ€™์„ ๋ฐ˜ํ™˜
SELECT * FROM ํ…Œ์ด๋ธ”1 FULL OUTER JOIN ํ…Œ์ด๋ธ”2 ON ํ…Œ์ด๋ธ”1.์—ด = ํ…Œ์ด๋ธ”2.์—ด;
notion image

JOIN ์ •๋ฆฌ๋ณธ

notion image
INNER JOIN (๋‚ด๋ถ€์กฐ์ธ)์€ ๋‘ ํ…Œ์ด๋ธ”์„ ์กฐ์ธํ•  ๋•Œ ๋‘ ํ…Œ์ด๋ธ”์— ๋ชจ๋‘ ์ง€์ •ํ•œ ์—ด์˜ ๋ฐ์ดํ„ฐ๊ฐ€ ์žˆ์–ด์•ผํ•œ๋‹ค. OUTER JOIN (์™ธ๋ถ€์กฐ์ธ)์€ ๋‘ ํ…Œ์ด๋ธ”์„ ์กฐ์ธํ•  ๋•Œ 1๊ฐœ์˜ ํ…Œ์ด๋ธ”์—๋งŒ ๋ฐ์ดํ„ฐ๊ฐ€ ์žˆ์–ด๋„ ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์˜จ๋‹ค. CROSS JOIN (์ƒํ˜ธ์กฐ์ธ)์€ ํ•œ์ชฝ ํ…Œ์ด๋ธ”์˜ ๋ชจ๋“  ํ–‰๊ณผ ๋‹ค๋ฅธ ์ชฝ ํ…Œ์ด๋ธ”์˜ ๋ชจ๋“  ํ–‰์„ ์กฐ์ธํ•˜๋Š” ๊ธฐ๋Šฅ์ด๋‹ค. SELF JOIN (์ž์ฒด์กฐ์ธ)์€ ์ž์‹ ์ด ์ž์‹ ๊ณผ ์กฐ์ธํ•œ๋‹ค๋Š” ์˜๋ฏธ๋กœ 1๊ฐœ์˜ ํ…Œ์ด๋ธ”์„ ์‚ฌ์šฉํ•œ๋‹ค.

APPLY ์—ฐ์‚ฐ์ž

SELECT .. FROM [์™ธ๋ถ€ ํ…Œ์ด๋ธ”] CROSS | OUTER APPLY (SELECT .. FROM [๋‚ด๋ถ€ ํ…Œ์ด๋ธ”] WHERE [์กฐ์ธ ์กฐ๊ฑด์ž])
  • CROSS APPLY = INNER JOIN
    • ๋‚ด๋ถ€ ํ…Œ์ด๋ธ”์˜ ์ง‘ํ•ฉ์œผ๋กœ๋ถ€ํ„ฐ ์กฐ์ธ ํ‚ค๋กœ ๊ฒฐํ•ฉํ•œ ์™ธ๋ถ€ ํ…Œ์ด๋ธ”์˜ ํ–‰๋งŒ์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
  • OUTER APPLY = LEFT OUTER JOIN
    • ๋‚ด๋ถ€ ํ…Œ์ด๋ธ”์˜ ์ง‘ํ•ฉ์œผ๋กœ๋ถ€ํ„ฐ ์กฐ์ธ ํ‚ค์— ๊ฒฐํ•ฉํ•œ ์™ธ๋ถ€ ํ…Œ์ด๋ธ”์˜ ํ–‰ + ๊ทธ๋ ‡์ง€ ์•Š์€ ํ–‰ ์ „๋ถ€๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ ‡์ง€ ์•Š์€ ํ–‰์€ ์—ด์—์„œ NULL์„ ํ‘œ์‹œํ•ฉ๋‹ˆ๋‹ค.
    • ํ…Œ์ด๋ธ” ๋ฐ˜ํ™˜ ํ•จ์ˆ˜๋ฅผ ํ…Œ์ด๋ธ”์ฒ˜๋Ÿผ ์‚ฌ์šฉํ•ด ์กฐ์ธ ๊ฐ€๋Šฅ
    • ์กฐ์ธ ์กฐ๊ฑด์„ ON์ด ์•„๋‹Œ ๋‚ด๋ถ€ ํ…Œ์ด๋ธ”์˜ WHERE ๋ฌธ์œผ๋กœ ์„ค์ • ๊ฐ€๋Šฅ
    • outer apply ์•ˆ์— select ๋ฌธ์— ์ง‘๊ณ„ํ•จ์ˆ˜๋ฅผ ์“ธ ๋•Œ group by๋ฅผ ๋ฐ˜๋“œ์‹œ ์‚ฌ์šฉํ•˜์ง€ ์•Š์•„๋„ ๋จ

Inline view (์ธ๋ผ์ธ ๋ทฐ)

SELECT ์ ˆ์˜ ๊ฒฐ๊ณผ๋ฅผ FROM ์ ˆ์—์„œ ํ•˜๋‚˜์˜ ํ…Œ์ด๋ธ”์ฒ˜๋Ÿผ ์‚ฌ์šฉํ•˜๊ณ  ์‹ถ์„ ๋•Œ ์‚ฌ์šฉ

์„œ๋ธŒ์ฟผ๋ฆฌ

์ฃผ ์ฟผ๋ฆฌ์—์„œ ์ข…์†๋œ ๋ถ€ ์ฟผ๋ฆฌ๋กœ์จ ์ข…์†์„ฑ์„ ๊ฐ–๋Š” ์ฟผ๋ฆฌ
  • ์กฐ๊ฑด์ ˆ์—์„œ ์ƒ์ˆ˜๊ฐ’์ด๋‚˜ ์›๋ž˜์˜ ๊ฐ’์œผ๋กœ ๋น„๊ตํ•  ์ˆ˜ ์—†๊ณ  ์–ด๋–ค ๊ฐ€๊ณต์„ ํ•œ ๊ฐ’์œผ๋กœ ๋น„๊ตํ•˜๊ณ ์ž ํ•  ๋•Œ์— ๋งŽ์ด ์‚ฌ์šฉ
  • ์ฃผ ์ฟผ๋ฆฌ์˜ ์†์„ฑ์„ ๊ทธ๋Œ€๋กœ ์ด์–ด ๋ฐ›๋Š”๋‹ค

Multi Row Sub Query (๋‹ค์ค‘ ํ–‰ ์„œ๋ธŒ์ฟผ๋ฆฌ)

  • IN : ๊ฐ™์€ ๊ฐ’์„ ์ฐพ์Œ (=)
  • >ANY : ์ตœ์†Œ๊ฐ’์„ ๋ฐ˜ํ™˜
  • <ANY : ์ตœ๋Œ€๊ฐ’์„ ๋ฐ˜ํ™˜
  • <ALL : ์ตœ์†Œ๊ฐ’์„ ๋ฐ˜ํ™˜
  • >ALL : ์ตœ๋Œ€๊ฐ’์„ ๋ฐ˜ํ™˜
> any(a, b) : a,b ์ค‘ ์ตœ์†Œ๋ณด๋‹ค ํฐ > all(a, b) : a,b ์ค‘ ์ตœ๋Œ€๋ณด๋‹ค ํฐ < any(a, b) : a,b ์ค‘ ์ตœ๋Œ€๋ณด๋‹ค ์ž‘์€ < all(a, b) : a,b ์ค‘ ์ตœ์†Œ๋ณด๋‹ค ์ž‘์€
๐Ÿ’ก
where์ ˆ์ด ์•„๋‹Œ! ์„œ๋ธŒ์ฟผ๋ฆฌ์— min, max๋ฅผ ์‚ฌ์šฉํ•ด๋„ ๋ฌธ์ œ๊ฐ€ ์—†์Œ

MSSQL

๋ฐ์ดํ„ฐ ๊ฒ€์ƒ‰

์ƒ์œ„ n๊ฐœ์˜ ๋ฐ์ดํ„ฐ๋งŒ ๊ฐ€์ ธ์˜ค๊ธฐ
SELECT TOP n * FROM ํ…Œ์ด๋ธ”๋ช…
์นผ๋Ÿผ์ด ํŒจํ„ด๊ณผ ๊ฐ™์€ ๋ฐ์ดํ„ฐ ๊ฐ€์ ธ์˜ค๊ธฐ
SELECT * FROM ํ…Œ์ด๋ธ”๋ช… WHERE ์นผ๋Ÿผ LIKE 'ํŒจํ„ด' // 'k%'(k๋กœ ์‹œ์ž‘๋˜๋Š”),'%k%'(์ค‘๊ฐ„์— k๊ฐ€ ์žˆ๋Š”), 'k%'(k๋กœ ๋๋‚˜๋Š”) // 'p_'(p๋กœ ์‹œ์ž‘ํ•˜๋Š” 2์ž๋ฆฌ), '_ _p'(3์ž๋ฆฌ ๋ฐ์ดํ„ฐ์ค‘ p๋กœ ๋๋‚˜๋Š”)
isnull, not null ์ฒ˜๋ฆฌ
WHERE ISNULL(์นผ๋Ÿผ,"") // is null WHERE ISNULL(์นผ๋Ÿผ,"")<> // not null

BETWEEN A AND B

SELECT * FROM employeeWHERE salary WHERE BETWEEN 300 AND 500; // salary๊ฐ€ 300์ด์ƒ 500์ดํ•˜์ธ ๋ฐ์ดํ„ฐ ์กฐํšŒ

ROLLUP

GROUP BY๋ฌธ๊ณผ ํ•จ๊ป˜ ์‚ฌ์šฉ๋˜๋Š” ํ•จ์ˆ˜์ธ๋ฐ, GROUP BY๋กœ GROUPING๋œ ํ–‰๋“ค ๋ฐ์ดํ„ฐ ์ดํ•ฉ์„ ๋‚˜ํƒ€๋‚ด๋Š”๋ฐ ์‚ฌ์šฉ
SELECT job, deptno, SUM(sal) AS total_sal FROM emp WHERE job IN ('MANAGER', 'CLERK', 'SALESMAN') GROUP BY ROLLUP(job, deptno)
notion image
์†Œ๊ณ„ ์ œ๊ฑฐ ๋ฐฉ๋ฒ•
SELECT job, deptno, SUM(sal) AS total_sal FROM emp WHERE job IN ('MANAGER', 'CLERK', 'SALESMAN') GROUP BY ROLLUP(job, deptno) HAVING GROUPING(job) = 1 OR GROUPING
notion image

PIVOT

ํ–‰์œผ๋กœ ๋˜์–ด์žˆ๋Š” ๋ฐ์ดํ„ฐ๋“ค์„ ์—ด๋กœ ๋ณ€ํ™˜ํ•ด์„œ ๋ณด์—ฌ์ฃผ๋Š” ํ•จ์ˆ˜
SELECT [PIVOT์—ด1], [PIVOT์—ด2], ...FROM ํ…Œ์ด๋ธ”๋ช… PIVOT (๋‚˜ํƒ€๋‚ด๊ณ ์žํ•˜๋Š” ๊ฐ’, FOR ํ–‰->์—ด๋กœ ๋ฐ”๊ฟ€ ์—ด IN ([PIVOT์—ด1], [PIVOT์—ด2], ...)) as ๋ณ„์นญ // UNPIVOT์€ ๋ฐ˜๋Œ€

CASE WHEN

CASE WHEN ์กฐ๊ฑด์ ˆ THEN ์ฐธ์ผ๋•Œ ๊ฐ’ ELSE ๊ฑฐ์ง“์ผ๋•Œ ๊ฐ’ END ์ปฌ๋Ÿผ๋ช… --MSSQL ๋‹ค์ค‘ CASE WHEN ์‚ฌ์šฉ ๋ฐฉ๋ฒ•ย  CASE WHEN [์กฐ๊ฑด์ ˆ1] THEN [์ฐธ์ผ ๋•Œ ๊ฐ’] WHEN [์กฐ๊ฑด์ ˆ2] THEN [์ฐธ์ผ ๋•Œ ๊ฐ’] WHEN [์กฐ๊ฑด์ ˆ3] THEN [์ฐธ์ผ ๋•Œ ๊ฐ’] ELSE [์กฐ๊ฑด์— ๋งž๋Š” ๊ฒƒ์ด ์—†์„ ๋•Œ ๊ฐ’] END [์ปฌ๋Ÿผ๋ช…]

ํ”„๋กœ์‹œ์ € ์˜ค๋ฅ˜ ์ฒ˜๋ฆฌ

RAISERROR('๋ฉ”์‹œ์ง€', ์˜ค๋ฅ˜์‹ฌ๊ฐ๋„, ์ƒํƒœ[์˜ต์…˜]) // 1~10 : ์ผ๋ฐ˜ ์‚ฌ์šฉ์ž ์ •๋ณด // 11~16 : ์‚ฌ์šฉ์ž ์ •์˜ ์˜ค๋ฅ˜ // 17~25 : ์‹œ์Šคํ…œ ์˜ค๋ฅ˜

ํ•จ์ˆ˜

1. ์ˆ˜์น˜ํ•จ์ˆ˜ ROUND(์ˆ˜์น˜๊ฐ’, ๋ฐ˜์˜ฌ๋ฆผ์œ„์น˜) // ๋ฐ˜์˜ฌ๋ฆผ ๋ฐ ์ž๋ฅด๊ธฐ ABS(์ˆ˜์น˜๋ฐ์ดํ„ฐ) // ์ ˆ๋Œ“๊ฐ’ SIGN(์ˆ˜์น˜๋ฐ์ดํ„ฐ) // ๋ถ€ํ˜ธ SQRT(์ˆ˜์น˜๊ฐ’) // ์ œ๊ณฑ๊ทผ POWER(์ˆ˜์น˜๊ฐ’, n) // n์Šน 2. ๋ฌธ์ž์—ด ํ•จ์ˆ˜ ์ •๋ฆฌ Ascii() // ๋ฌธ์ž์—ด์˜ ์ œ์ผ ์™ผ์ชฝ ๋ฌธ์ž์˜ ์•„์Šคํ‚ค ์ฝ”๋“œ ๊ฐ’์„ ๋ฐ˜ํ™˜ Char() // ์ •์ˆ˜ ์•„์Šคํ‚ค ์ฝ”๋“œ๋ฅผ ๋ฌธ์ž๋กœ ๋ฐ˜ํ™˜ Charindex() // ๋ฌธ์ž์—ด์—์„œ ์ง€์ •ํ•œ ์‹์˜ ์œ„์น˜๋ฅผ ๋ฐ˜ํ™˜ Difference() // ๋‘ ๋ฌธ์ž์‹์— SOUNDEX ๊ฐ’ ๊ฐ„์˜ ์ฐจ์ด๋ฅผ ์ •์ˆ˜๋กœ ๋ฐ˜ํ™˜ Left(), Right() // ๋ฌธ์ž์—ด ์™ผ์ชฝ์ด๋‚˜ ์˜ค๋ฅธ์ชฝ์—์„œ๋ถ€ํ„ฐ ์ง€์ •ํ•œ ์ˆ˜๋งŒํผ์˜ ๋ฌธ์ž๋ฅผ ๋ฐ˜ํ™˜ Len() // ๋ฌธ์ž์—ด์˜ ๊ธธ์ด ๋ฐ˜ํ™˜ Lower() // ๋Œ€๋ฌธ์ž๋ฅผ ์†Œ๋ฌธ์ž๋กœ ๋ฐ˜ํ™˜ Ltrim(), Rtrim() // ๋ฌธ์ž์—ด์˜ ์™ผ์ชฝ์ด๋‚˜ ์˜ค๋ฅธ์ชฝ ๊ณต๋ฐฑ ์ œ๊ฑฐ Nchar() // ์ง€์ •ํ•œ ์ •์ˆ˜ ์ฝ”๋“œ์˜ ์œ ๋‹ˆ์ฝ”๋“œ ๋ฌธ์ž ๋ฐ˜ํ™˜ Replace() // ๋ฌธ์ž์—ด์—์„œ ๋ฐ”๊พธ๊ณ  ์‹ถ์€ ๋ฌธ์ž ๋‹ค๋ฅธ ๋ฌธ์ž๋กœ ๋ฐ˜ํ™˜ Replicate() // ๋ฌธ์ž์‹์„ ์ง€์ €ํ•œ ํšŸ์ˆ˜๋งŒํผ ๋ฐ˜๋ณต Reverse() // ๋ฌธ์ž์—ด์„ ์—ญ์ˆœ์œผ๋กœ ์ถœ๋ ฅ Space() // ์ง€์ •ํ•œ ์ˆ˜๋งŒํผ์˜ ๊ณต๋ฐฑ ๋ฌธ์ž ๋ฐ˜ํ™˜ Substring() // ๋ฌธ์ž,์ด์ง€๋Šํ…์ŠคํŠธ ๋˜๋Š” ์ด๋ฏธ์ง€ ์‹์˜ ์ผ๋ถ€๋ฅผ ๋ฐ˜ํ™˜ Unicode() // ์‹์— ์žˆ๋Š” ์ฒซ๋ฒˆ์งธ ๋ฌธ์ž์˜ ์œ ๋‹ˆ์ฝ”๋“œ ์ •์ˆ˜ ๊ฐ’์œผ ๋ฐ˜ํ™˜ Upper() // ์†Œ๋ฌธ์ž๋ฅผ ๋Œ€๋ฌธ์ž๋กœ ๋ฐ˜ํ™˜ Isnumeric() // ํ•ด๋‹น ๋ฌธ์ž์—ด์ด ์ˆซ์žํ˜•์ด๋ฉด 1 ์•„๋‹ˆ๋ฉด 0์„ ๋ฐ˜ํ™˜ Isdate() // ํ•ด๋‹น ๋ฌธ์ž์—ด์ด Datatime์ด๋ฉด 1 ์•„๋‹ˆ๋ฉด 0์„ ๋ฐ˜ํ™˜

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

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

์ผ๋ฐ˜ ์ฟผ๋ฆฌ๋ฌธ VS ์ €์žฅ ํ”„๋กœ์‹œ์ €

์ผ๋ฐ˜ ์ฟผ๋ฆฌ๋ฌธ ์ž‘๋™ ๋ฐฉ์‹
๊ตฌ๋ฌธ๋ถ„์„ โ†’ ๊ฐœ์ฒด ์ด๋ฆ„ ํ™•์ธ โ†’ ์‚ฌ์šฉ๊ถŒํ•œ ํ™•์ธ โ†’ ์ตœ์ ํ™” โ†’ ์ปดํŒŒ์ผ ๋ฐ ์‹คํ–‰๊ณ„ํš ๋“ฑ๋ก(๋ฉ”๋ชจ๋ฆฌ(์บ์‹œ)) โ†’ ์‹คํ–‰
  • ๊ตฌ๋ฌธ๋ถ„์„ : ๊ตฌ๋ฌธ ์ž์ฒด์— ์˜ค๋ฅ˜๊ฐ€ ์—†๋Š”์ง€ ๋ถ„์„
  • ๊ฐœ์ฒด ์ด๋ฆ„ ํ™•์ธ : ํ…Œ์ด๋ธ”์ด ํ˜„์žฌ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ์žˆ๋Š”์ง€ ํ™•์ธ
  • ์‚ฌ์šฉ๊ถŒํ•œ ํ™•์ธ : ํ˜„์žฌ ํ…Œ์ด๋ธ”์— ์ ‘๊ทผ์ค‘์ธ ์‚ฌ์šฉ์ž๊ฐ€ ๊ถŒํ•œ์ด ์žˆ๋Š”์ง€ ํ™•์ธ
  • ์ตœ์ ํ™” : ํ•ด๋‹น ์ฟผ๋ฆฌ๋ฌธ์ด ๊ฐ€์žฅ์ข‹์€ ์„ฑ๋Šฅ์„ ๋‚ผ ์ˆ˜ ์žˆ๋Š” ๊ฒฝ๋กœ๋ฅผ ๊ฒฐ์ •
  • ์ปดํŒŒ์ผ ๋ฐ ์‹คํ–‰ ๊ณ„ํš ๋“ฑ๋ก : ํ•ด๋‹น ์‹คํ–‰๊ณ„ํš ๊ฒฐ๊ณผ๋ฅผ ๋ฉ”๋ชจ๋ฆฌ(์บ์‹œ)์— ๋“ฑ๋ก
์ €์žฅ ํ”„๋กœ์‹œ์ € ์ž‘๋™ ๋ฐฉ์‹
  1. ์ €์žฅํ”„๋กœ์‹œ์ € ์ •์˜ ๋‹จ๊ณ„
    1. Create Procedure โ†’ ๊ตฌ๋ฌธ ๋ถ„์„โ†’ ์ง€์—ฐ๋œ ์ด๋ฆ„ ํ™•์ธ โ†’ ์ƒ์„ฑ๊ถŒํ•œ ํ™•์ธ โ†’ ์‹œ์Šคํ…œ ํ…Œ์ด๋ธ”์— ๋“ฑ๋ก
      • ๊ตฌ๋ฌธ๋ถ„์„ : ๊ตฌ๋ฌธ์˜ ์˜ค๋ฅ˜ ํŒŒ์•…
      • ์ง€์—ฐ๋œ ์ด๋ฆ„ ํ™•์ธ : ์ €์žฅ ํ”„๋กœ์‹œ์ €๋ฅผ ์ •ํ•˜๋Š” ์‹œ์ ์—์„œ ํ•ด๋‹น ๊ฐœ์ฒด(ํ…Œ์ด๋ธ”)๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š์•„๋„ ์ƒ๊ด€์—†๋‹ค. ํ”„๋กœ์‹œ์ € ์‹คํ–‰ ๋‹น์‹œ์— ํ…Œ์ด๋ธ” ์กด์žฌ ์—ฌ๋ถ€ ํ™•์ธํ•จ(๊ฐœ์ฒด์ด๋ฆ„ ํ™•์ธ)
      • ์ƒ์„ฑ๊ถŒํ•œ ํ™•์ธ : ํ˜„์žฌ ์‚ฌ์šฉ์ž๊ฐ€ ์ €์žฅ ํ”„๋กœ์‹œ์ €๋ฅผ ์ƒ์„ฑํ•  ๊ถŒํ•œ์ด ์žˆ๋Š”์ง€ ํ™•์ธ
      • ์‹œ์Šคํ…œ ํ…Œ์ด๋ธ” ๋“ฑ๋ก : ์ €์žฅ ํ”„๋กœ์‹œ์ €์˜ ์ด๋ฆ„ ๋ฐ ์ฝ”๋“œ๊ฐ€ ์‹œ์Šคํ…œ ํ…Œ์ด๋ธ”์— ๋“ฑ๋ก
  1. ์ฒ˜์Œ ์ €์žฅ ํ”„๋กœ์‹œ์ € ์‹คํ–‰
    1. ๊ตฌ๋ฌธ๋ถ„์„ ๋‹จ๊ณ„ ๋น ์ง„ ์ผ๋ฐ˜ ์ฟผ๋ฆฌ๋ฌธ ์ž‘๋™ ๋ฐฉ์‹๊ณผ ๋™์ผ
      • ์ •์˜ ๋‹จ๊ณ„์—์„œ ์ง€์—ฐ๋œ ์ด๋ฆ„ํ™•์ธ์—์„œ ๋ฏธ๋ฃจ์–ด๋‘์—ˆ๋˜ ํ•ด๋‹น ๊ฐœ์ฒด ์กด์žฌ ์œ ๋ฌด๋ฅผ ๊ฐœ์ฒด ์ด๋ฆ„ ํ™•์ธ์„ ํ†ตํ•ด ์ˆ˜ํ–‰ํ•œ๋‹ค.
  1. ์ดํ›„ ์ €์žฅ ํ”„๋กœ์‹œ์ € ์‹คํ–‰
    1. ์ €์žฅ ํ”„๋กœ์‹œ์ € ๋‘๋ฒˆ์งธ ์‹คํ–‰ โ†’ ๋ฉ”๋ชจ๋ฆฌ(์บ์‹œ)ํ™•์ธ โ†’ ์‹คํ–‰
      • ๋ฉ”๋ชจ๋ฆฌ(์บ์‹œ)์— ์žˆ๋Š” ๊ฒƒ์„ ๊ทธ๋Œ€๋กœ ๊ฐ€์ ธ์™€ ์žฌ์‚ฌ์šฉํ•˜๊ฒŒ ๋˜์–ด ์ˆ˜ํ–‰์‹œ๊ฐ„์„ ๋‹จ์ถ•

๋ช…๋ น์–ด

CREATE PROC ํ”„๋กœ์‹œ์ €๋ช… AS SQL ๋ฌธ -- ์ €์žฅ ํ”„๋กœ์‹œ์ € CREATE PROC ํ”„๋กœ์‹œ์ €๋ช… ๋ณ€์ˆ˜์„ ์–ธ AS SQL ๋ฌธ -- ์ธ์ˆ˜๋ฅผ ๊ฐ€์ง€๋Š” ์ €์žฅ ํ”„๋กœ์‹œ์ € CREATE PROC ํ”„๋กœ์‹œ์ €๋ช… WITH ENCRYPTION AS SQL ๋ฌธ -- ์ €์žฅ ํ”„๋กœ์‹œ์ € ๋ณด์•ˆ ์„ค์ • CREATE PROC ํ”„๋กœ์‹œ์ €๋ช… ์ธ์ˆ˜1 ๋ฐ์ดํ„ฐํ˜•, ...์ธ์ˆ˜2 ๋ฐ์ดํ„ฐํ˜• OUTPUT -- ์ถœ๋ ฅํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ์ง€์ • AS SQL๋ฌธ RETURN ๋ฆฌํ„ด๊ฐ’ -- RETURN ๊ฐ’์„ ๊ฐ€์ง€๋Š” ์ €์žฅ ํ”„๋กœ์‹œ์ € DROP PROCEDURE ํ”„๋กœ์‹œ์ €๋ช…1, ํ”„๋กœ์‹œ์ €๋ช…2 -- ์ €์žฅ ํ”„๋กœ์‹œ์ € ์‚ญ์ œ EXEC ํ”„๋กœ์‹œ์ €๋ช… ํŒŒ๋ผ๋ฏธํ„ฐ -- ํ”„๋กœ์‹œ์ € ํ˜ธ์ถœ
BEGINโ€ฆEND -- ๋ฌธ์žฅ์˜ ๋ธ”๋ก (SQL๋ฌธ๊ณผ ๋ณ€์ˆ˜์„ ์–ธ์€ BEGIN~END ์‚ฌ์ด์— ์ž‘์„ฑ) BEGIN END๋Š” ์ƒ๋žต๊ฐ€๋Šฅ DECLARE @๋ณ€์ˆ˜๋ช… ๋ฐ์ดํ„ฐํ˜• -- ๋ณ€์ˆ˜ ์„ ์–ธ SET @๋ณ€์ˆ˜๋ช…=๊ฐ’ -- ๋ณ€์ˆ˜์— ๊ฐ’ ์ง€์ • SET NOCOUNT ON -- ๊ฒฐ๊ณผ ์ ์šฉ๋œ ํ–‰์˜ ๊ฐœ์ˆ˜๋ฅผ ํ‘œ์‹œํ•˜์ง€ ์•Š๋„๋กํ•˜๋Š” ์˜ต์…˜ PRINT @๋ณ€์ˆ˜๋ช… -- ํ•œ๊ฐœ์˜ ๋ณ€์ˆ˜ ์ถœ๋ ฅ SELECT @๋ณ€์ˆ˜1, @๋ณ€์ˆ˜2 -- ์—ฌ๋Ÿฌ๊ฐœ์˜ ๋ณ€์ˆ˜ ์ถœ๋ ฅ IF -- ์กฐ๊ฑด ์ˆ˜ํ–‰ ์ˆ˜ํ–‰1 ELSE ์ˆ˜ํ–‰2 WHILE ์กฐ๊ฑด1 -- ๋ฐ˜๋ณต ์ˆ˜ํ–‰ BEGIN IF ์กฐ๊ฑด2 BREAK -- WHILE ๋ฃจํ”„๋ฅผ ๋น ์ ธ๋‚˜๊ฐ„๋‹ค CONTINUE -- ์ˆ˜ํ–‰์„ ์ฒ˜๋ฆฌํ•˜์ง€ ์•Š๊ณ  ์กฐ๊ฑด1๋กœ ๋˜๋Œ์•„๊ฐ„๋‹ค ์ˆ˜ํ–‰ END EXEC ์ €์žฅํ”„๋กœ์‹œ์ € -- SQL๋ฌธ์„ ์‹คํ–‰ EXEC @(๋ณ€์ˆ˜๋กœ ์ง€์ •๋œ SQL๋ฌธ) GO -- BATCH๋ฅผ ๊ตฌ๋ถ„ ์ง€์ •
ํ…Œ์ด๋ธ”์˜ ๋ฐ์ดํ„ฐ ์œ ๋ฌด ํ™•์ธ
IF EXISTS (SELECT๋ฌธ) -- ์กฐ๊ฑด๋ฌธ BEGIN -- ๊ฐ’์ด ํ•˜๋‚˜์ด์ƒ ์žˆ์„ ์‹œ ์‹คํ–‰ SELECT1 END ELSE BEGIN -- ๊ฐ’์ด ์—†์„ ๋•Œ ์‹คํ–‰ SELECT2 END -- ๋ฆฌํ„ดํ˜•์‹ bool
ํ…Œ์ด๋ธ”์—์„œ ์ตœ์ƒ์œ„ ํ•œ์ค„์— ๋ฐ์ดํ„ฐ๋“ค ์œ ๋ฌด
IF EXISTS (SELECT TOP 1 1 from ...) BEGIN ... END -- ()์•ˆ์— ์ฟผ๋ฆฌ๋ฌธ์ด ์žˆ๋Š๋ƒ ์—†๋Š๋ƒ ์žˆ์œผ๋ฉด (BEGIN) -- ์ƒ์œ„ 1๊ฐœ ํ–‰๋งŒ ๋ฐ˜ํ™˜ํ•˜๊ณ  ํ•ด๋‹น ํ–‰์— ๋Œ€ํ•ด ์ •์ˆ˜ 1 ์ฆ‰, true ์กฐ๊ฑด์ด๋ฉด BEGIN ์‹คํ–‰

MERGE ๊ตฌ๋ฌธ

UPDATE, DELETE, INSERT๋ฅผ ํ•œ ๋ฒˆ์— ์ž‘์—…์ด ๊ฐ€๋Šฅ
(merge๋ฌธ์„ ์•ˆ ์“ฐ๋ฉด select๋กœ if๋ฌธ์„ ๊ฑธ์–ด์•ผ upadate๋‚˜ insert๊ฐ€ ๊ฐ€๋Šฅ)
MERGE INTO (๋ณ€๊ฒฝํ•  ํ…Œ์ด๋ธ”) USING (๋น„๊ตํ•  ํ…Œ์ด๋ธ”) ON (์กฐ๊ฑด๋ฌธ) WHEN MATCHED THEN (์กฐ๊ฑด์„ ๋งŒ์กฑํ•  ๊ฒฝ์šฐ) UPDATE SET WHEN NOT MATCHED THEN (์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜์ง€ ์•Š์„ ๊ฒฝ์šฐ) DELETE
  • Target ํ…Œ์ด๋ธ”๊ณผ Source ํ…Œ์ด๋ธ” ๋ชจ๋‘ ์กด์žฌํ•˜๋Š” ๊ฒฝ์šฐ์—” MATCHED ์กฐ๊ฑด์— ํ•ด๋‹นํ•˜๊ณ , ํ•ด๋‹น ํ–‰์€ Target ํ…Œ์ด๋ธ”์˜ ํ–‰์„ Updateํ•˜๊ฑฐ๋‚˜ Deleteํ•  ์ˆ˜ ์žˆ๋‹ค.
  • Target ํ…Œ์ด๋ธ”์—๋Š” ์กด์žฌํ•˜์ง€๋งŒ Source ํ…Œ์ด๋ธ”์—๋Š” ์กด์žฌํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ๋Š” NOT MATCHED BY SOURCE ์กฐ๊ฑด์— ํ•ด๋‹นํ•˜๋ฉฐ, ํ•ด๋‹น ํ–‰์€ MATCHED์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ Target ํ…Œ์ด๋ธ”์˜ ํ–‰์„ Updateํ•˜๊ฑฐ๋‚˜ Deleteํ•  ์ˆ˜ ์žˆ๋‹ค.
  • Source ํ…Œ์ด๋ธ”์—๋Š” ์กด์žฌํ•˜์ง€๋งŒ Target ํ…Œ์ด๋ธ”์—๋Š” ์กด์žฌํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ๋Š” NOT MATHCED BY TARGET ์กฐ๊ฑด์— ํ•ด๋‹นํ•˜๊ณ , ํ•ด๋‹น ํ–‰์€ Target ํ…Œ์ด๋ธ”๋กœ INSERT๋งŒ ๊ฐ€๋Šฅํ•˜๋‹ค.

WHEN ๊ตฌ๋ฌธ

MATCHED
  • MERGE๋ฌธ์— 1๊ฐœ๋งŒ ํฌํ•จ
  • MATCHED์ ˆ์„ 2๊ฐœ ํฌํ•จ์‹œํ‚ฌ ๊ฒฝ์šฐ, ์ถ”๊ฐ€ ์กฐ๊ฑด(AND condition)์ ˆ์„ ์ง€์ •ํ•ด์•ผ ํ•˜๊ณ  ๊ฐ๊ฐ UPDATE/DELETE ๋™์ž‘์„ ์ง€์ •ํ•ด์•ผ ํ•œ๋‹ค.
NOT MATCHED BY TARGET
  • MERGE๋ฌธ์—๋Š” 1๊ฐœ๋งŒ ํฌํ•จ
  • BY TARGET์€ ์ƒ๋žตํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ON์ ˆ์— ์ผ์น˜ํ•˜์ง€ ์•Š์ง€๋งŒ ์ถ”๊ฐ€ ๊ฒ€์ƒ‰ ์กฐ๊ฑด์„ ์ถฉ์กฑํ•˜๋Š” ๋ชจ๋“  ํ–‰์— ๋Œ€ํ•ด INSERT ์ˆ˜ํ–‰ํ•œ๋‹ค.
NOT MATCHED BY SOURCE
  • MERGE๋ฌธ์— ์ตœ๋Œ€ 2๊ฐœ
  • WHEN MATCHED๊ฐ€ ์ ์šฉ๋˜์ง€ ์•Š์€ ๊ฒฝ์šฐ์—๋งŒ ์ ์šฉ
  • 2๊ฐœ ํฌํ•จ์‹œํ‚ฌ ๊ฒฝ์šฐ, ์ถ”๊ฐ€ ์กฐ๊ฑด์ ˆ์„ ์ง€์ •ํ•ด์•ผ ํ•˜๊ณ  ๊ฐ๊ฐ UPDATE/DELETE ๋™์ž‘์„ ์ง€์ •
  • ์ถ”๊ฐ€ ๊ฒ€์ƒ‰ ์กฐ๊ฑด์€ ๋Œ€์ƒ ํ…Œ์ด๋ธ”์˜ ์—ด๋งŒ ์ฐธ์กฐํ•  ์ˆ˜ ์žˆ๋‹ค.

EXITS, NOT EXITS

  • EXITS์—ฐ์‚ฐ์ž๋Š” ์„œ๋ธŒ์ฟผ๋ฆฌ์— ๋ฐ์ดํ„ฐ๊ฐ€ ์กด์žฌํ•˜๋Š”์ง€ ์ฒดํฌํ•˜๊ณ  ์กด์žฌํ•  ๊ฒฝ์šฐ TRUE๋ฅผ ๋ฐ˜ํ™˜
  • NOT EXITS ์—ฐ์‚ฐ์ž๋Š” ์„œ๋ธŒ์ฟผ๋ฆฌ์— ๋ฐ์ดํ„ฐ๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š์„ ๊ฒฝ์šฐ TRUE๋ฅผ ๋ฐ˜ํ™˜

SELECT ์ปฌ๋Ÿผ ๊ฐ’ ๋ณ€์ˆ˜๋กœ ์ง€์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•

declare @name varchar(100); declare @age int; select @name = NAME, @age = AGE from Student where seq = 10;

dbo

dbo(database owner)์˜ ์•ฝ์ž๋กœ ๋ชจ๋“  ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์˜ ๊ธฐ๋ณธ์ ์œผ๋กœ ํฌํ•จ๋˜๋Š” ์Šคํ‚ค๋งˆ ์ด๋ฆ„์ž…๋‹ˆ๋‹ค.
์Šคํ‚ค๋งˆ๋Š” ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์—์„œ ๊ฐ์ฒด(ํ…Œ์ด๋ธ”, ๋ทฐ ๋“ฑ)๋ฅผ ๊ตฌ์„ฑํ•˜๊ณ  ๊ด€๋ฆฌํ•˜๋Š” ๋…ผ๋ฆฌ์ ์ธ ์ปจํ…Œ์ด๋„ˆ์ž…๋‹ˆ๋‹ค.

dbo์™€ ์Šคํ‚ค๋งˆ ์ฐจ์ด

  • TEST๋ผ๋Š” ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์ด ์†Œ์œ ์ž(dbo)๊ฐ€ test์ผ์ง€๋ผ๋„, table์ด๋ผ๋Š” ํ…Œ์ด๋ธ”์€ test.table๋กœ ์ฐธ์กฐํ•  ์ˆ˜ ์—†๋‹ค.
  • ํ…Œ์ด๋ธ”๋ช… ์•ž์— ๋ถ™๋Š” ๋ช…์นญ์€ ์Šคํ‚ค๋งˆ ์ด๋ฆ„

Trigger

ํŠน์ • ํ…Œ์ด๋ธ”์— INSERT, DELETE, UPDATE ๋“ฑ๊ณผ ๊ฐ™์€ ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ•  ๋•Œ ์ž๋™์œผ๋กœ ์‹คํ–‰
  1. ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ํŠธ๋ฆฌ๊ฑฐ
    1. ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์—์„œ ๋ฐ์ดํ„ฐ INSERT, DELETE, UPDATE ๋“ฑ์˜ ์ž‘์—…์ด ๋ฐœ์ƒํ•  ๋•Œ ํŠน์ • ์ž‘์—…์„ ์ž๋™์œผ๋กœ ์‹คํ–‰ํ•œ๋‹ค.
  1. ํƒ€์ด๋จธ ๋˜๋Š” ์Šค์ผ€์ค„๋ง ํŠธ๋ฆฌ๊ฑฐ
    1. ์ •๊ธฐ์ ์œผ๋กœ ๋ฐ˜๋ณต๋˜๋Š” ์ž‘์—…์„ ์˜ˆ์•ฝํ•˜์—ฌ ์ง€์ •๋œ ์‹œ๊ฐ„ ํŠธ๋ฆฌ๊ฑฐ๋ฅผ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค.
  1. ์ด๋ฉ”์ผ ๋˜๋Š” ๋ฉ”์‹œ์ง€ ๊ธฐ๋ฐ˜ ํŠธ๋ฆฌ๊ฑฐ
    1. ์ด๋ฉ”์ผ ๋„์ฐฉ, ๋ฉ”์‹œ์ง€ ์ˆ˜์‹  ๋“ฑ๊ณผ ๊ฐ™์€ ์™ธ๋ถ€ ์ด๋ฒคํŠธ๋ฅผ ๊ฐ์ง€ํ•˜๊ณ  ํ•ด๋‹น ์ด๋ฒคํŠธ์— ๋Œ€ํ•ด ์ž๋™์œผ๋กœ ์ฒ˜๋ฆฌํ•˜๋Š” ๊ธฐ๋Šฅ ๊ตฌํ˜„์ด ๊ฐ€๋Šฅ
  1. ํŒŒ์ผ ์‹œ์Šคํ…œ ํŠธ๋ฆฌ๊ฑฐ
    1. ํŒŒ์ผ ๋˜๋Š” ๋””๋ ‰ํ† ๋ฆฌ์˜ ์ƒ์„ฑ, ์ˆ˜์ • ๋˜๋Š” ์‚ญ์ œ์™€ ๊ฐ™์€ ํŒŒ์ผ ์‹œ์Šคํ…œ ๋ณ€๊ฒฝ ์‚ฌํ•ญ์„ ๊ฐ์ง€ํ•˜๊ณ  ์—ฐ๊ด€๋œ ์ž‘์—…์„ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค.
  1. ์™ธ๋ถ€ API ํ˜ธ์ถœ ๊ธฐ๋ฐ˜ ํŠธ๋ฆฌ๊ฑฐ
    1. ์™ธ๋ถ€ ์„œ๋น„์Šค๋‚˜ API ํ˜ธ์ถœ ๊ฒฐ๊ณผ์— ๋”ฐ๋ผ ํŠน์ • ์ž‘์—…์ด ์ž๋™์œผ๋กœ ์‹คํ–‰๋˜๋„๋ก ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.

MSSQL ํŠธ๋ฆฌ๊ฑฐ

  • ์ž๋™์œผ๋กœ ์‹คํ–‰๋˜๋Š” ํ”„๋กœ์‹œ์ €์˜ ์ผ์ข…, ์ˆ˜๋™์œผ๋กœ๋Š” ์‹คํ–‰ ๋ถˆ๊ฐ€
  • ํ•œ ํ…Œ์ด๋ธ”๊ณผ ๊ด€๋ จ๋˜์–ด์„œ๋งŒ ์กด์žฌ ํ•  ์ˆ˜ ์žˆ์Œ
-- ํŠธ๋ฆฌ๊ฑฐ ์ƒ์„ฑ CREATE TRIGGER [TriggerName] ON [TableName] [AFTER, BEFORE] INSERT, UPDATE, DELETE // ์›ํ•˜๋Š” ์ด๋ฒคํŠธ ์ง€์ • AS BEGIN // ์‹คํ–‰๋  ์ฝ”๋“œ ๋˜๋Š” ์ž‘์—…์„ ์—ฌ๊ธฐ์— ์ž‘์„ฑ END;

query

set nocount on; select CodeID, CodeName from Information.CommonCodeDetail where CodeTypeID = 'AREA' and // ๋…ผ๋ฆฌ์—ฐ์‚ฐ์ž and, or CodeName like '%' + isnull(@CodeName, '') + '%' // isnull(null๊ฐ’์ด ์žˆ๋Š” ์นผ๋Ÿผ๋ช…,'') set nocount off;

UNION

2๊ฐœ ์ด์ƒ์˜ SELECT ์ฟผ๋ฆฌ ๊ฒฐ๊ณผ๋ฅผ ํ•˜๋‚˜๋กœ ์—ฐํ•ฉํ•ด์ฃผ๋Š” ์ง‘ํ•ฉ ์—ฐ์‚ฐ์ž
  • ์—ด์˜ ๊ฐœ์ˆ˜์™€ ์ˆœ์„œ๊ฐ€ ๋ชจ๋“  ์ฟผ๋ฆฌ์—์„œ ๋™์ผํ•ด์•ผํ•จ
  • ๋ฐ์ดํ„ฐ ํ˜•์‹์ด ํ˜ธํ™˜๋˜์–ด์•ผํ•ฉ๋‹ˆ๋‹ค
๐Ÿ’ก
union all์€ ์ค‘๋ณต๊ฐ’๋„ ์ „๋ถ€ ๋ณด์—ฌ์คŒ โ†’ ์†๋„๋Š” ๋” ๋น ๋ฆ„ union์€ ๋‘ ์ฟผ๋ฆฌ์˜ ๊ฒฐ๊ณผ์˜ ์ค‘๋ณต๊ฐ’์„ ์ œ๊ฑฐํ•ด์„œ ๋ณด์—ฌ์คŒ
-- UNION select [์ปฌ๋Ÿผ1], [์ปฌ๋Ÿผ2], [์ปฌ๋Ÿผ3], ... FROM ํ…Œ์ด๋ธ”๋ช… UNION select [์ปฌ๋Ÿผ1], [์ปฌ๋Ÿผ2], [์ปฌ๋Ÿผ3], ... FROM ํ…Œ์ด๋ธ”๋ช… -- UNION ALL select [์ปฌ๋Ÿผ1], [์ปฌ๋Ÿผ2], [์ปฌ๋Ÿผ3], ... FROM ํ…Œ์ด๋ธ”๋ช… UNION ALL select [์ปฌ๋Ÿผ1], [์ปฌ๋Ÿผ2], [์ปฌ๋Ÿผ3], ... FROM ํ…Œ์ด๋ธ”๋ช…
notion image
  • ๋‘ ํ…Œ์ด๋ธ”์˜ ๋ฐ์ดํ„ฐ๊ฐ€ ์ƒˆ๋กœ์šด 'ํ–‰'์œผ๋กœ ์ถ”๊ฐ€๋˜์–ด์„œ ์ˆœ์ฐจ์ ์œผ๋กœ ๋ฐ์ดํ„ฐ๊ฐ€ ์ถ”๊ฐ€

Cursor๋ฌธ

Cursor

  • ๋ฐ์ดํ„ฐ ์ž‘์—…์„ ํ•  ๋•Œ ํ…Œ์ด๋ธ”์—์„œ ์ง‘ํ•ฉ ๋‹จ์œ„๊ฐ€ ์•„๋‹Œ ํ–‰ ๋‹จ์œ„๋กœ ์ฒ˜๋ฆฌ๋ฅผ ํ•ด์•ผํ•  ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ์‹
  • DML (SELECT, UPDATE, DELETE, INSERT)

ํŠน์ง•

  • ์ง‘ํ•ฉ์ด ์•„๋‹Œ ํ–‰ ๋‹จ์œ„๋กœ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๊ธฐ๋•Œ๋ฌธ์— ์†๋„๊ฐ€ ๋Š๋ฆฌ๋‹ค
  • ํ…Œ์ด๋ธ”์—์„œ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ํ–‰์„ ์กฐํšŒํ•œ ํ›„ ์ฟผ๋ฆฌ์˜ ๊ฒฐ๊ณผ๋ฅผ ํ•œ ํ–‰์”ฉ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฐฉ์‹
DECLARE : ์ปค์„œ๋ฅผ ์ •์˜ํ•˜๋Š” ๋“ฑ ์ปค์„œ์— ๊ด€๋ จ๋œ ์„ ์–ธ์„ ํ•˜๋Š” ๋ช…๋ น
OPEN : ์ปค์„œ๊ฐ€ ์งˆ์˜ ๊ฒฐ๊ณผ์˜ ์ฒซ๋ฒˆ์งธ ํŠœํ”Œ(ํ–‰)์„ ํฌ์ธํŠธ ํ•˜๋„๋ก ์„ค์ •ํ•˜๋Š” ๋ช…๋ น
FETCH : ์งˆ์˜ ๊ฒฐ๊ณผ์˜ ํŠœํ”Œ๋“ค ์ค‘ ํ˜„์žฌ์˜ ๋‹ค์Œ ํŠœํ”Œ(ํ–‰)๋กœ ์ปค์„œ๋ฅผ ์ด๋™์‹œํ‚ค๋Š” ๋ช…๋ น
CLOSE : ์งˆ์˜ ์ˆ˜ํ–‰ ๊ฒฐ๊ณผ์— ๋Œ€ํ•œ ์ฒ˜๋ฆฌ ์ข…๋ฃŒ ์‹œ ์ปค์„œ๋ฅผ ๋‹ซ๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•˜๋Š” ๋ช…๋ น
DELLOCATE : ์ปค์„œ์™€ ์ปค์„œ ์ด๋ฆ„ ๋˜๋Š” ์ปค์„œ ๋ณ€์ˆ˜ ๊ฐ„์˜ ๊ด€๊ณ„๋ฅผ ์ œ๊ฑฐ

๋‚ด ์„œ๋ฒ„์˜ ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ๋Ÿ‰

SELECT physical_memory_kb, virtual_memory_kb, committed_kb, commmitted_target_kb, FROM sys.dm_os_sys_info;
  • physical_memory_kb : ์„œ๋ฒ„์— ์„ค์น˜๋œ ์ด ์‹ค์ œ ๋ฉ”๋ชจ๋ฆฌ
  • virtual_memory_kb : SQL Server์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์ด ๊ฐ€์ƒ ๋ฉ”๋ชจ๋ฆฌ ์–‘
  • Committed_kb : ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ํŽ˜์ด์ง€์—์„œ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด ๋ฒ„ํผ ์บ์‹œ์—์„œ ํ˜„์žฌ ํ• ๋‹นํ•œ ๋ฉ”๋ชจ๋ฆฌ ์–‘
  • Committed_target_kb : ๋ฒ„ํผ ์บ์‹œ๊ฐ€ ์‚ฌ์šฉํ•˜๊ณ ์žํ•˜๋Š” ๋ฉ”๋ชจ๋ฆฌ ์–‘

STRING_AGG()

์—ฌ๋Ÿฌ ํ–‰์˜ ์ปฌ๋Ÿผ ๊ฐ’์„ ํ•˜๋‚˜์˜ ์ปฌ๋Ÿผ์œผ๋กœ ํ•ฉ์น  ๋•Œ
notion image
SELECT job, STRING_AGG(ename, ',') WITHIN GROUP(ORDER BY ename) enames FROM emp WHERE job IN ('MANAGER', 'SALESMAN') GROUP BY job

CHARINDEX()

๋ฌธ์ž์—ด์—์„œ ํŠน์ • ๋ฌธ์ž๋ฅผ ์ฐพ๊ณ  ์œ„์น˜๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜
  • ๋ฌธ์ž์—ด์˜ ์•ž๋ถ€ํ„ฐ ๋˜๋Š” ํŠน์ • ์œ„์น˜๋ถ€ํ„ฐ ๋ฌธ์ž๋ฅผ ๊ฒ€์ƒ‰
  • CHARINDEX(โ€์ฐพ์„ ๋ฌธ์žโ€, โ€œ๋ฌธ์ž์—ดโ€, โ€œ์‹œ์ž‘์œ„์น˜โ€)
๊ธฐ๋ณธ ์‚ฌ์šฉ๋ฒ•
SELECT CHARINDEX('sql', 'Microsoft SQL Server')
์‹œ์ž‘์œ„์น˜๋ฅผ ์ง€์ •ํ•˜์—ฌ ์ฐพ๊ธฐ
SELECT CHARINDEX('sql', 'Microsoft SQL Server', 11)
๋Œ€์†Œ๋ฌธ์ž๋ฅผ ๊ตฌ๋ถ„ํ•˜์—ฌ ์ฐพ๊ธฐ
SELECT CHARINDEX('SQL', 'Microsoft SQL Server' COLLATE Latin1_General_CS_AS) // ๋Œ€์†Œ๋ฌธ์ž ๊ตฌ๋ถ„ : COLLATE Latin1_General_CS_AS // ๋Œ€์†Œ๋ฌธ์ž ๊ตฌ๋ฌธ์•ˆํ•จ : COLLATE Latin1_General_CI_AS (๊ธฐ๋ณธ๊ฐ’)
๋‘๋ฒˆ์งธ ๋ฌธ์ž ์ฐพ๊ธฐ
DECLARE @str VARCHAR(100) = 'SQL Server 2017, SQL Server 2019' SELECT CHARINDEX('sql', @str, CHARINDEX('sql', @str) + 1)

IIF

์ฐธ๊ณผ ๊ฑฐ์ง“ ๋‘ ๊ฒฝ์šฐ์˜ ๊ฐ’๋งŒ ๋ฆฌํ„ด์ด ๊ฐ€๋Šฅ
DECLARE @score INT = 80 SELECT IIF(@score >= 70, 'ํ•ฉ๊ฒฉ', '๋ถˆํ•ฉ๊ฒฉ') AS score // score // ํ•ฉ๊ฒฉ

TOP

์กฐํšŒ ๊ฐœ์ˆ˜๋ฅผ ์ œํ•œํ•˜๋Š” ๊ฒฝ์šฐ
SELECT TOP(5) ename, job, sal FROM emp ORDER BY sal // sal๋กœ ์ •๋ ฌ ํ›„ 5๊ฐœ๋งŒ ์กฐํšŒ

์‹œ์ž‘์ผ์ž ์ข…๋ฃŒ์ผ์ž ์‚ฌ์ด ๋ชจ๋“  ์ผ์ž ์กฐํšŒ

// ์žฌ๊ท€ ์ฟผ๋ฆฌ(WITH CTE) WITH DateRange(Date) AS ( SELECT CONVERT(DATE, '2021-12-01') --์‹œ์ž‘์ผ์ž UNION ALL SELECT DATEADD(d, 1, Dates) FROM DateRange WHERE Dates < CONVERT(DATE, '2021-12-7) --์ข…๋ฃŒ์ผ์ž ) SELECT * FROM DateRange OPTION (MAXRECURSION 0) // ์žฌ๊ท€ ์ฟผ๋ฆฌ์˜ ๋ฐ˜๋ณต ํšŸ์ˆ˜๋ฅผ ์„ค์ •ํ•˜๋Š” ๋ถ€๋ถ„ // 0์€ ๋ฌดํ•œ๋ฐ˜๋ณต, ์ƒ๋žตํ•˜๋ฉด ์ตœ๋Œ€ 100ํšŒ๊นŒ์ง€๋งŒ ๋ฐ˜๋ณต // 2021-12-01 ~ 2021-12-07
// master..spt_value SELECT DATEADD(d, number, CONVERT(DATE, '2021-12-01')) Dates FROM master..spt_values WHERE type = 'p' AND number <= DATEDIFF(d, CONVERT(DATE, '2021-12-01'), CONVERT(DATE, '2021-12-07')) // 2021-12-01 ~ 2021-12-07

Partition by

๋ฐ์ดํ„ฐ์˜ ์ง‘ํ•ฉ(ํ…Œ์ด๋ธ”)์—์„œ ์–ด๋–ค ํ•ญ๋ชฉ์„ ๊ธฐ์ค€์œผ๋กœ ํ…Œ์ด๋ธ”์„ ๋ถ„๋ฆฌํ•˜์—ฌ ์ˆœ์œ„๋‚˜ ์ˆœ์„œ๋ฅผ ๋‚˜ํƒ€๋‚ผ ๋•Œ ์‚ฌ์šฉ
SELECT ์ˆœ์œ„ํ•จ์ˆ˜() OVER(PARTITION BY ์ปฌ๋Ÿผ๋ช… ORDER BY ์ปฌ๋Ÿผ๋ช…) FROM ํ…Œ์ด๋ธ”๋ช…
  • ์ˆœ์œ„ํ•จ์ˆ˜ : ROW_NUMBER, RANK, DENSE_RANK
example
SELECT empno , ename , job , sal , SUM(sal) OVER(PARTITION BY job) FROM emp WHERE job IN ('MANAGER', 'SALESMAN') ORDER BY job
notion image

Paging Query

DECLARE @PAGE_NO INT = 1 -- ํŽ˜์ด์ง€๋ฒˆํ˜ธ DECLARE @PAGE_SIZE INT = 5 -- ํ•œ ํŽ˜์ด์ง€์— ๋ณด์—ฌ์ค„ row ์ˆ˜ SELECT rownum, name, age, totalCnt from ( SELECT ROW_NUMBER() OVER(ORDER BY age) AS rownum, name, age, COUNT(*) OVER() AS totalCnt from [ํ…Œ์ด๋ธ”] ) A WHERE rownum BETWEEN ((@PAGE_NO-1)*@PAGE_SIZE)+1 AND (@PAGE_NO * @PAGE_SIZE) SELECT t1.* FROM ํ…Œ์ด๋ธ”๋ช… AS t1 WITH(NOLOCK) INNER JOIN ( SELECT id FROM ํ…Œ์ด๋ธ”๋ช… WITH(NOLOCK) ORDER BY reg_date DESC OFFSET (@PAGE_NO) * @PAGE_SIZE ROWS FETCH NEXT (@PAGE_SIZE) ROWS ONLY ) AS t2 ON t1.id = t2.id ORDER BY reg_date DESC;
notion image
notion image

OFFSET ROWS FETCH

ORDER BY ์ปฌ๋Ÿผ OFFEST ์ˆซ์ž {ROW | ROWS} FETCH {FIRST | NEXT} ์ˆซ์ž {ROW | ROWS} ONLY
  • OFFSET ์ˆซ์ž {ROW | ROWS} : ํ•ด๋‹น ์ˆซ์ž ์ดํ›„์˜ ํ–‰๋ถ€ํ„ฐ ์ถœ๋ ฅ
  • FETCH {FIRST | NEXT} ์ˆซ์ž {ROW|ROWS} ONLY : ์ˆซ์ž์˜ ๊ฐœ์ˆ˜๋งŒํผ ์ถœ๋ ฅ

์กฐํšŒ ์ˆœ๋ฒˆ ๋งค๊ธฐ๊ธฐ

SELECT ROW_NUMBER() OVER(ORDER BY (SELECT 1)) AS rownum
  • ROW_NUMBER ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ๋Š” ORDER BY ํ•„์ˆ˜

๋ถ„์„ํ•จ์ˆ˜

:ํ–‰(row)์— ๋Œ€ํ•ด ํŠน์ • ๊ทธ๋ฃน๋ณ„๋กœ ์ง‘๊ณ„๊ฐ’์„ ์‚ฐ์ถœํ•  ๋•Œ ์ฃผ๋กœ ์‚ฌ์šฉ

over()

SELECT employee_id, salary, avg(salary) over() ์ „์ฒดํ‰๊ท  FROM employees;
over() ๋ถ„์„ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์ฟผ๋ฆฌ ์ „์ฒด๋ฅผ ์ง‘๊ณ„

over(ORDER BY ์ปฌ๋Ÿผ)

ํ•ด๋‹น ์ปฌ๋Ÿผ์„ ๊ธฐ์ค€์œผ๋กœ ์ฟผ๋ฆฌ์˜ ๊ฒฐ๊ณผ๊ฐ€ ์ •๋ ฌ
  • ์ฟผ๋ฆฌ ๊ฒฐ๊ณผ ์ „์ฒด๋ฅผ ์ง‘๊ณ„

over(partition by ์ปฌ๋Ÿผ)

ํ•ด๋‹น ์ปฌ๋Ÿผ์„ ๊ธฐ์ค€์œผ๋กœ ๊ธฐ์กด์˜ ๋ฐ์ดํ„ฐ์™€ ์ง‘๊ณ„๋œ ๊ฐ’์„ ํ•จ๊ป˜ ๋‚˜๋ž€ํžˆ ๋ณผ ์ˆ˜ ์žˆ๋‹ค

over(partition by ์ปฌ๋Ÿผ1 ORDER BY ์ปฌ๋Ÿผ2)

์ปฌ๋Ÿผ1์„ ๊ธฐ์ค€์œผ๋กœ ๊ธฐ์กด์˜ ๋ฐ์ดํ„ฐ์™€ ์ง‘๊ณ„๋œ ๊ฐ’์„ ํ•จ๊ป˜ ๋‚˜๋ž€ํžˆ ๋ณด๊ณ  ์ปฌ๋Ÿผ2๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ฟผ๋ฆฌ ๊ฒฐ๊ณผ๊ฐ€ ์ •๋ ฌ

Select ๋ฌธ ๋‘๊ฐœ ํ•ฉ์น˜๊ธฐ

select (select * from Table1) as A, (select * from Table2) as B -- | A B -----|------- -- 1 | 3 4 -- 2 | 2 8

count( ) ํ•จ์ˆ˜

( ) ๋‚ด์— ์žˆ๋Š” ํ•ญ๋ชฉ์˜ ์ˆ˜๋ฅผ ์ถœ๋ ฅ
  • count(name) : name์ด๋ผ๋Š” ์ปฌ๋Ÿผ์˜ ๊ฐœ์ˆ˜, null ๊ฐ’ ํฌํ•จ X
  • count(*) : ์ „์ฒด ํ–‰์˜ ๊ฐœ์ˆ˜, null ๊ฐ’ ํฌํ•จ
  • count(1) : ์ „์ฒด ํ–‰์˜ ๊ฐœ์ˆ˜, null ๊ฐ’ ํฌํ•จ

DATE(๋‚ ์งœ๊ด€๋ จ)

https://chachahoya.tistory.com/74 (๋‚ ์งœ ๋ณ€ํ™˜ํ‘œ)

date_add

๋ฐ์ดํ„ฐ ์กฐํšŒ์‹œ ํ•œ๋‹ฌ์ด๋‚ด, ์ผ์ฃผ์ผ์ด๋‚ด ๋“ฑ ๋ฐ์ดํ„ฐ ๊ธฐ๊ฐ„ ์กฐํšŒ
SELECT * FROM cordinghero.test where date > date_add(now(),interval -1 month);
  • date > date_add ๋ผ๋ฉด ๋’ค์ชฝ์— -1 month์ด๋ฏ€๋กœ ํ•œ ๋‹ฌ์ด๋‚ด
  • date < date_add ๋ผ๋ฉด ํ•œ ๋‹ฌ์ด ์ง€๋‚œ ๋ฐ์ดํ„ฐ

ํŠน์ •๋‚ ์งœ 1์ผ๋ถ€ํ„ฐ ํŠน์ •๋‚ ์งœ๊นŒ์ง€

B.BaseDate between DATEADD(DAY, 1 - DAY(@BaseDate), @BaseDate) AND @BaseDate

๋…„, ๋‹ฌ, ์ผ (์กฐํšŒ)

DATENAME(yy,@BaseDate) + '๋…„ ' + DATENAME(MM, @BaseDate) + '์›” ' + DATENAME(dd, @BaseDate) + '์ผ ' + DATENAME(WEEKDAY, @BaseDate)

ํŠน์ • ๋‹ฌ๊นŒ์ง€ ์กฐํšŒ

select * from where DATEADD(MONTH,0,[๋‚ ์งœ]) <= @EndDate

dense_rank()

ํ•จ์ˆ˜๋Š” ๋™์ผํ•œ ๊ฐ’์ด๋ฉด ์ค‘๋ณต ์ˆœ์œ„๋ฅผ ๋ถ€์—ฌํ•˜๊ณ  ๋‹ค์Œ ์ˆœ์œ„๋Š” ์ค‘๋ณต ์ˆœ์œ„์™€ ์ƒ๊ด€์—†์ด ์ˆœ์ฐจ์ ์œผ๋กœ ๋ฐ˜ํ™˜
ex) 1 โ†’ 1 โ†’ 2 โ†’ 3 โ†’ 4 โ†’ 5 โ†’ 5 โ†’ 6

ROW NUMBER()

ROW_NUMBER() OVER(ORDER BY ๊ธฐ์ค€)
๊ฐ ๋กœ์šฐ๋งˆ๋‹ค ์ˆœ์„œ๋ฅผ ๋ฐ˜์˜

WITH (NOLOCK)

  • ์ปค๋ฐ‹๋˜์ง€ ์•Š์€ ์ฝ๊ธฐ๋ฅผ ํ—ˆ์šฉํ•˜๊ฒ ๋‹ค.
  • ์ฝ๊ธฐ ์ž‘์—…์‹œ ์กฐํšŒํ•˜๋Š” ํ…Œ์ด๋ธ”์ด ์ž ๊ฒจ์žˆ์–ด๋„ ๊ธฐ๋‹ค๋ฆฌ์ง€ ์•Š๊ฒ ๋‹ค.
  • ๋ฐ๋“œ๋ฝ์ด ๋ฐœ์ƒํ•˜๊ฑฐ๋‚˜ ๊ต์ฐฉ์ƒํƒœ๊ฐ€ ๋ฐœ์ƒํ•˜์ง€ ์•Š๋Š”๋‹ค.
select * from test_table WITH (LOCK)

์ž„์‹œํ…Œ์ด๋ธ” ์ปฌ๋Ÿผ๋ช… ๊ฐ€์ ธ์˜ค๊ธฐ

select name from tempdb.sys.columns where object_id = object_id('tempdb..#js_WeightingTable') -- #js_WeightingTable = ์ž„์‹œํ…Œ์ด๋ธ”๋ช… -- name = ์ปฌ๋Ÿผ๋ช…

ํ…Œ์ด๋ธ” ์ปฌ๋Ÿผ๋ช… ๊ฐ€์ ธ์˜ค๊ธฐ

select * from syscolumns where id = object_id('ํ…Œ์ด๋ธ”๋ช…')

SQL ๋กœ๊ทธ ํŒŒ์ผ

1. ๋กœ๊ทธ ํŒŒ์ผ ํ™•์ธ

DBCC SQLPERF(LOGSPACE) -- 1 EXEC SP_HELPFILE -- 2

2. ๋กœ๊ทธ ํŒŒ์ผ ์šฉ๋Ÿ‰ ์ค„์ด๊ธฐ

BACKUP LOG [Database] WITH TRUNCATE_ONLY -- ์ถ•์†Œ์ž‘์—… BACKUP LOG [Database] WITH NO_LOG --๋กœ๊ทธ์‚ญSQ์ œ

SQL ์ฟผ๋ฆฌ ์ฒ˜๋ฆฌ ์†๋„ ํ™•์ธ

SET STATISTICS TIME ON [์‹คํ–‰ํ•  ์ฟผ๋ฆฌ๋ฌธ] SET STATISTICS TIME OFF

IN, NOT IN ์—ฐ์‚ฐ์ž

IN

select * from emp where ename IN ('JONES', 'SCOTT', 'Bob')
IN ์—ฐ์‚ฐ์ž์— ์ž…๋ ฅ๋œ ๊ฐ’ ์ค‘์—์„œ ํ•˜๋‚˜๋ผ๋„ ์ผ์น˜ํ•˜๋Š” ๊ฒƒ์ด ์žˆ์œผ๋ฉด ๋ฆฌ์ŠคํŠธ์— ์กฐํšŒ

NOT IN, ์ฃผ์˜์‚ฌํ•ญ

SELECT * FROM emp WHERE ename NOT IN ('JONES', 'SCOTT', 'MILLER') SELECT * FROM emp WHERE ISNULL(ename.'test') NOT IN ('JONES', 'SCOTT', 'MILLER')
  • NOT IN์„ ์‚ฌ์šฉํ•  ๋•Œ๋Š” ํ•ด๋‹น ์นผ๋Ÿผ์— NULL์ด ์กด์žฌํ•  ๊ฒฝ์šฐ ํ•ด๋‹น ๊ฐ’์ด ์กฐํšŒ๋˜์–ด์•ผ ํ•œ๋‹ค๋ฉด, ๊ผญ ISNULLํ•จ์ˆ˜๋กœ ์น˜ํ™˜ํ•ด์•ผ ํ•ด๋‹น ๊ฐ’์ด ๋ˆ„๋ฝ๋˜์ง€ ์•Š๊ณ  ์กฐํšŒ

์‚ฌ์šฉ์ž ์ •์˜ ํ•จ์ˆ˜(User-defined Fuction, UDF)

  • ํ•ด๋‹น ์ž‘์—…์˜ ๊ฒฐ๊ณผ๋ฅผ ๊ฐ’์œผ๋กœ ๋ฐ˜ํ™˜
    • ๋ฐ˜๋“œ์‹œ ํ•˜๋‚˜์˜ ๋ฐ˜ํ™˜ ๊ฐ’์ด ์žˆ์–ด์•ผ ํ•จ
  • ๋ฐ˜ํ™˜ ๊ฐ’์€ ๋‹จ์ผ ์Šค์นผ๋ผ ๊ฐ’์ด๋‚˜ ๊ฒฐ๊ณผ ์ง‘ํ•ฉ
    • ๋‹จ์ผ ์Šค์นผ๋ผ ๊ฐ’ : ๋‹จ์ผ ๋ฐ์ดํ„ฐ ๊ฐ’์„ ์˜๋ฏธ
    • ๊ฒฐ๊ณผ ์ง‘ํ•ฉ : ์—ด ๋˜๋Š” ํ–‰์˜ ์ผ๋ถ€

์ด์ 

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

์Šค์นผ๋ผ ํ•จ์ˆ˜ (Scalar User-defined Fuction, scalar UDF)

  • RETURNS์ ˆ์— ์ •์˜๋œ ์œ ํ˜•์˜ ๋‹จ์ผ ๋ฐ์ดํ„ฐ ๊ฐ’์„ ๋ฐ˜ํ™˜
  • ๋‹จ์ผ ์Šค์นผ๋ผ ํ•จ์ˆ˜ : ๋‹จ์ผ ์Šค์นผ๋ผ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜
    • ๋‹จ์ˆœํ•œ ์ˆ˜ํ•™์‹์„ ์‚ฌ์šฉ, ๋‚ด์žฅ ํ•จ์ˆ˜์— ์‚ฌ์šฉํ•˜์—ฌ ์ •์˜
  • ๋‹ค์ค‘ ์Šค์นผ๋ผ ํ•จ์ˆ˜ : ๋‹จ์ผ ์Šค์นผ๋ผ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜๊ณ  ์—ฌ๋Ÿฌ๊ฐœ์˜ SQL ๋ฌธ์œผ๋กœ ๊ตฌ์„ฑ๋œ ํ•จ์ˆ˜
    • ๋ฐ˜ํ™˜ ํ˜•์‹ : text, ntext, image, cursor, timestamp๋ฅผ ์ œ์™ธํ•œ ๋ชจ๋“  ๋ฐ์ดํ„ฐ ํ˜•์‹
    • ์ข€ ๋” ๋ณต์žกํ•œ ์—ฐ์‚ฐ์„ ํ–‰

ํ…Œ์ด๋ธ” ๋ฐ˜ํ™˜ ํ•จ์ˆ˜ (Table-valued Fuction, TVF)

  • ํ…Œ์ด๋ธ” ๋ฐ์ดํ„ฐ ํ˜•์‹์„ ๋ฐ˜ํ™˜
  • ํ…Œ์ด๋ธ”์€ ๋‹จ์ผ SELECT ๋ฌธ์˜ ๊ฒฐ๊ณผ ์ง‘ํ•ฉ
  • SELECT๋ฌธ, JOIN์ ˆ ๋˜๋Š” ํ•˜์œ„ ์ฟผ๋ฆฌ์—์„œ ํ…Œ์ด๋ธ”๊ณผ ๋™์ผํ•˜๊ฒŒ ์‚ฌ์šฉ ๊ฐ€๋Šฅ

์‹œ์Šคํ…œ ํ•จ์ˆ˜ (System Fuction)

  • ์‚ฌ์šฉ ํ•  ์ˆ˜ ์žˆ๋Š” ๋งŽ์€ ์‹œ์Šคํ…œ ๊ธฐ๋Šฅ์„ ์ œ๊ณต
  • ์ˆ˜์ • ๋ถˆ๊ฐ€

Fuction ์ƒ์„ฑ

CREATE FUNCTION FN_TEST(@P_MSG VARCHAR(10)) RETURNS VARCHAR AS BEGIN DECLARE @V_RETURN VARCHAR(100); DECLARE @V_STR VARCHAR(10); SET @V_STR = 'abc'; SET @V_RETURN = concat(@V_STR, @P_MSG); RETURN @V_RETURN; END

Function ์‹คํ–‰

SELECT DBO.FN_TEST(โ€™ZZZโ€™) RET_MSG

Function ์ˆ˜์ •

ALTER FUNCTION โ€ฆ

Function ์‚ญ์ œ

DROP FUNCTION FN_TEST

MSSQL , 0์œผ๋กœ ๋‚˜๋ˆ„๊ธฐ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•˜์˜€์Šต๋‹ˆ๋‹ค ๊ด€๋ จ ์ฒ˜๋ฆฌ

-- ๋ฐฉ๋ฒ• 1 select case when isnull(data1,0) = 0 then 0 else data2 end as 'test' from A -- ๋ฐฉ๋ฒ• 2 ์˜ค๋ฅ˜๊ฐ’์ด null๊ฐ’์œผ๋กœ ๋ฐ˜ํ™˜ SET ANSI_WARNINGS OFF SET ARITHIGNORE ON SET ARITHABORT OFF

group by(์ง‘๊ณ„ํ•จ์ˆ˜) vs partition by(์œˆ๋„์šฐํ•จ์ˆ˜)

group by

group by๋Š” ํ•„๋“œ๋กœ ํ…Œ์ด๋ธ”์„ ์ž๋ฅด๊ณ , ์ž˜๋ผ์ง„ ์กฐ๊ฐ ๊ฐœ์ˆ˜ ๋งŒํผ์˜ ๋ ˆ์ฝ”๋“œ ์ˆ˜์— ๋Œ€ํ•œ ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅ
select address, count(*) from Address group by address; --Result address | count ---------------- ์„œ์šธ์‹œ | 3 ์ธ์ฒœ์‹œ | 2 ๋ถ€์‚ฐ์‹œ | 1

partition by

partition by๋Š” ์ž˜๋ ค์ง„ ๋ ˆ์ฝ”๋“œ์˜ ์ข…๋ฅ˜๋ณ„๋กœ ์ž‘์—…์ด ์ฒ˜๋ฆฌ๋˜๋‚˜, ์ถœ๋ ฅ ๊ฒฐ๊ณผ๋Š” ์ง‘์•ฝ๋˜์ง€ ์•Š์œผ๋ฏ€๋กœ ๊ธฐ์กด ๋ ˆ์ฝ”๋“œ ๊ฐœ์ˆ˜๋งŒํผ ์ถœ๋ ฅ
select address, count(*) over(partition by address) from Address; --Result address | count ---------------- ์„œ์šธ์‹œ | 3 ์„œ์šธ์‹œ | 3 ์„œ์šธ์‹œ | 3 ์ธ์ฒœ์‹œ | 2 ์ธ์ฒœ์‹œ | 2 ๋ถ€์‚ฐ์‹œ | 1

๋น„์œจ ๊ด€๋ จ ํ•จ์ˆ˜

  • CUME_DIST
    • ํŒŒํ‹ฐ์…˜ ๋‚ด ์ „์ฒด ๊ฑด์ˆ˜ ์ค‘์—์„œ ํ˜„์žฌ ํ–‰๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ๊ฑด์ˆ˜์— ๋Œ€ํ•œ ๋ˆ„์  ๋ฐฑ๋ถ„์œจ
  • PERCENT_RANK
    • ํŒŒํ‹ฐ์…˜์—์„œ ๊ฐ€์žฅ ์ฒ˜์Œ ๊ฐ’์€ 0 ๊ฐ€์žฅ ๋‚˜์ค‘์˜ ๊ฐ’์€ 1์œผ๋กœ ํ–‰์˜ ์ˆœ์„œ๋ณ„ ๋ฐฑ๋ถ„์œจ ์กฐํšŒ
  • NTILE
    • ํŒŒํ‹ฐ์…˜๋ณ„๋กœ ์ „์ฒด ํ–‰์„ N๋“ฑ๋ถ„ํ•œ ๊ฒฐ๊ณผ๋ฅผ ์กฐํšŒ
  • RATIO_TO_REPORT
    • ํŒŒํ‹ฐ์…˜ ๋‚ด ์ „์ฒด SUM(์ปฌ๋Ÿผ)์— ๋Œ€ํ•œ ํ–‰๋ณ„ ์ปฌ๋Ÿผ๊ฐ’์˜ ๋ฐฑ๋ถ„์œจ์„ ์†Œ์ˆ˜์ ๊นŒ์ง€ ์กฐํšŒ
# PERCENT_RANK ์˜ˆ์‹œ : ๋ถ€์„œ ๋‚ด ๊ธ‰์—ฌ ํผ์„ผํŠธ(๋“ฑ์ˆ˜) ์กฐํšŒ selct deptino, ename, sal, -- ๋ถ€์„œ๋ณ„๋กœ ํŒŒํ‹ฐ์…˜, ๊ธ‰์—ฌ ๋‚ด๋ฆผ์ฐจ์ˆœ PERCENT_RANK() OVER (partition by depno order by sal desc) as per_sal from emp; #NTILE ์˜ˆ์‹œ : ๊ธ‰์—ฌ ๋†’์€ ์ˆœ์„œ๋กœ 4๋“ฑ๋ถ„ select deptno, ename, sal -- 4๋“ฑ๋ถ„ >> 1,2,3,4 ํ• ๋‹น NTILE(4) OVER (order byt sal desc) as n_title from emp;

OUTER JOIN VS INNER JOIN ์„ฑ๋Šฅ

  • ๋น ๋ฅธ ์‹œ๊ฐ„๋‚ด์— ํ•ด์•ผํ•  ๊ฒฝ์šฐ left outer join ๋‚จ๋ฐœ
  • ๋Œ€์‹  ์„ฑ๋Šฅ์ด ์•ˆ ์ข‹์•„์ง
  • inner join ์‹œ๊ฐ„ ๋ณต์žก๋„ O(n)
  • outer join ์‹œ๊ฐ„ ๋ณต์žก๋„ O(n^2)

์ฟผ๋ฆฌ๋ฌธ ๋นจ๊ฐ„์ค„ ํ‘œ์‹œ ํ•ด๊ฒฐ

ํŽธ์ง‘ โ†’ IntelliSense โ†’ ๋กœ์ปฌ ์บ์‹œ ์ƒˆ๋กœ๊ณ ์นจ

SQL ๋“ค์—ฌ์”Œ๊ธฐ ์˜ค๋ฅ˜

์ฟผ๋ฆฌ โ†’ ์ฟผ๋ฆฌ์˜ต์…˜ โ†’ alt + = โ†’ ํ™•์ธ

ํŠธ๋žœ์žญ์…˜(Transaction)

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

์ปค๋ฐ‹ ๋˜๋Š” ๋กค๋ฐฑํ•˜๊ธฐ ์ „ ์ƒํƒœ

  • ๋ชจ๋“  ๋ฐ์ดํ„ฐ์˜ ๋ณ€ํ™”๋Š” ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๋ฒ„ํผ์— ์ €์žฅ๋œ๋‹ค.
  • ํ˜„์žฌ์˜ ์‚ฌ์šฉ์ž๋Š” ์ž์‹ ์ด ๋ณ€ํ™”์‹œํ‚จ ๋ฐ์ดํ„ฐ๋ฅผ SELECT๋ฌธ์„ ํ†ตํ•ด์„œ ๋ณผ ์ˆ˜ ์žˆ์ง€๋งŒ ์ž์‹  ์ด์™ธ์˜ ์‚ฌ์šฉ์ž๋Š” ๊ทธ ๋ณ€ํ™”๋œ ๋‚ด์šฉ์„ ๋ณผ ์ˆ˜ ์—†๋‹ค.
  • ์ž์‹ ์ด ๋ณ€ํ™”์‹œํ‚จ Row์— ๋Œ€ํ•ด์„œ๋Š” ๋ก(Lock)์ด ๊ฑธ๋ฆฐ ์ƒํƒœ์ด๊ธฐ ๋•Œ๋ฌธ์— ๋‹ค๋ฅธ ์‚ฌ์šฉ์ž๊ฐ€ ํ•ด๋‹น Row๋ฅผ ๋ณ€ํ™”์‹œํ‚ค์ง€ ๋ชปํ•œ๋‹ค. ( Row Level Locking Mechanism )
    • ํ…Œ์ด๋ธ”์˜ row๋งˆ๋‹ค ๊ฑธ๋ฆฌ๋Š” lock

์ปค๋ฐ‹ ํ•œ ํ›„ ์ƒํƒœ

  • ํŠธ๋žœ์žญ์…˜ ๋‚ด๋ถ€์—์„œ ๋ณ€ํ™”๋œ ๋‚ด์šฉ์€ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ์˜๊ตฌ์ ์œผ๋กœ ๋ฐ˜์˜
  • ๋‹ค๋ฅธ ๋ชจ๋“  ์‚ฌ์šฉ์ž๊ฐ€ ๋ณ€ํ™”๋œ ๋‚ด์šฉ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.
  • ํ•ด๋‹น Row์— ๋Œ€ํ•œ ๋ก(Lock)์ด ์ œ๊ฑฐ๋œ๋‹ค.

์ธ๋ฑ์Šค(INDEX)

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

ํ…Œ์ด๋ธ” ์•ก์„ธ์Šค ๋ฐฉ๋ฒ•

  1. BY ROWID : ํ…Œ์ด๋ธ”์˜ ํŠน์ • Row์— ๋Œ€ํ•œ ์œ„์น˜ ์ •๋ณด๋ฅผ ๊ฐ€์ง€๊ณ  ์ง์ ‘ ํ…Œ์ด๋ธ”์„ ์•ก์„ธ์Šค
  1. Full-table-Scan : ํ…Œ์ด๋ธ”์— ์กด์žฌํ•˜๋Š” ๋ชจ๋“  Row์— ๋Œ€ํ•ด์„œ ์ˆœ์ฐจ์ ์œผ๋กœ ์•ก์„ธ์Šค
  1. By INDEX : ๋จผ์ € INDEX๋ฅผ ์ฝ๊ณ ์„œ INDEX์˜ ROWID๋ฅผ ๊ฐ€์ง€๊ณ  ํ…Œ์ด๋ธ”์„ Random ์•ก์„ธ์Šค

์ธ๋ฑ์Šค ์ƒ์„ฑ

-- ๊ธฐ๋ณธ ์ƒ์„ฑ create index [index_name] on [table_name] (column_name); -- Unique index ์ƒ์„ฑ create unique index [index_name] on [table_name] (column_name); -- primary key, unique ์ œ์•ฝ์กฐ๊ฑด์„ ์ฃผ๋ฉด unique index๋Š” ์ž๋™์ ์œผ๋กœ ๋งŒ๋“ค์–ด์ง

์ธ๋ฑ์Šค ์‚ญ์ œ

DROP INDEX [์ธ๋ฑ์Šค๋ช…] ON [ํ…Œ์ด๋ธ”๋ช…]

ํด๋Ÿฌ์Šคํ„ฐํ˜• ์ธ๋ฑ์Šค

create clustered index [index_name] on [table_name] (column_name);

์‹คํ–‰ ๊ณ„ํš ๋…ผ๋ฆฌ ๋ฐ ๋ฌผ๋ฆฌ ์—ฐ์‚ฐ์ž ์ฐธ์กฐ

Compute Scalar

์‹์„ ํ‰๊ฐ€ํ•˜์—ฌ ๊ณ„์‚ฐ๋œ ์Šค์นผ๋ผ ๊ฐ’์„ ์ƒ์„ฑ
  • ๊ทธ๋Ÿฌ๋ฉด ์‚ฌ์šฉ์ž์—๊ฒŒ ๋ฐ˜ํ™˜๋˜๊ฑฐ๋‚˜. ์ฟผ๋ฆฌ์˜ ๋‹ค๋ฅธ ์œ„์น˜์—์„œ ์ฐธ์กฐ๋˜๊ฑฐ๋‚˜, ๋‘˜ ๋‹ค ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

Stream Aggregate

ํ•˜๋‚˜ ์ด์ƒ์˜ ์—ด์„ ๊ธฐ์ค€์œผ๋กœ ํ–‰์„ ๊ทธ๋ฃนํ™”ํ•œ ๋‹ค์Œ ์ฟผ๋ฆฌ์—์„œ ๋ฐ˜ํ™˜๋œ ํ•˜๋‚˜ ์ด์ƒ์˜ ์ง‘๊ณ„ ์‹์„ ๊ณ„์‚ฐ
  • ๊ทธ๋ฃน ๋‚ด์˜ ์—ด์— ๋”ฐ๋ผ ์ž…๋ ฅ์„ ์ •๋ ฌํ•ด์•ผ ํ•จ
  • ์ด์ „ Sort ์—ฐ์‚ฐ์ž, ์ •๋ ฌ๋œ ์ธ๋ฑ์Šค ์ฐพ๊ธฐ ๋˜๋Š” ์Šค์บ”์œผ๋กœ ์ธํ•ด ๋ฐ์ดํ„ฐ๊ฐ€ ์•„์ง ์ •๋ ฌ๋˜์ง€ ์•Š์€ ๊ฒฝ์šฐ ์ตœ์ ํ™” ํ”„๋กœ๊ทธ๋žจ์€ ์ด ์—ฐ์‚ฐ์ž ์•ž์— Sort ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉ
  • ๋ฌผ๋ฆฌ ์—ฐ์‚ฐ์ž

์žฌ๊ท€์  ๊ณตํ†ต ํ…Œ์ด๋ธ” ํ‘œํ˜„์‹ (CTE)

์•ต์ปค ๋ฉค๋ฒ„(Anchor Member)

์žฌ๊ท€ ํ”„๋กœ์„ธ์Šค๋ฅผ ์‹œ์ž‘ํ•˜๋Š” ์ดˆ๊ธฐ ์ฟผ๋ฆฌ

์žฌ๊ท€ ๋ฉค๋ฒ„(Recursive Member)

์•ต์ปค ๋ฉค๋ฒ„์˜ ๊ฒฐ๊ณผ๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ๋ฐ˜๋ณต์ ์œผ๋กœ ํ˜ธ์ถœ๋˜๋Š” ์ฟผ๋ฆฌ

์‹คํ–‰ ๊ณผ์ •

  1. ์•ต์ปค ๋ฉค๋ฒ„ ์‹คํ–‰
      • ์•ต์ปค ๋ฉค๋ฒ„ ์ฟผ๋ฆฌ๊ฐ€ ๋จผ์ € ์‹คํ–‰๋˜์–ด ์ดˆ๊ธฐ ๊ฒฐํ•ฉ ์ง‘ํ•ฉ์„ ์ƒ์„ฑ
  1. ์žฌ๊ท€ ๋ฉค๋ฒ„ ์‹คํ–‰
      • ์žฌ๊ท€ ๋ฉค๋ฒ„ ์ฟผ๋ฆฌ๊ฐ€ ์•ต์ปค ๋ฉค๋ฒ„์˜ ๊ฒฐ๊ณผ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ƒˆ๋กœ์šด ๊ฒฐ๊ณผ ์ง‘ํ•ฉ์„ ์ƒ์„ฑ
  1. ๋ฐ˜๋ณต ์‹คํ–‰
      • ์žฌ๊ท€ ๋ฉค๋ฒ„ ์ฟผ๋ฆฌ๊ฐ€ ๋ฐ˜๋ณต์ ์œผ๋กœ ์‹คํ–‰๋˜์–ด ๊ฐ ๋ฐ˜๋ณต์—์„œ ์ด์ „ ๊ฒฐ๊ณผ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ƒˆ ๊ฒฐ๊ณผ๋ฅผ ์ƒ์„ฑ
  1. ์ข…๋ฃŒ ์กฐ๊ฑด ํ™•์ธ
      • ์žฌ๊ท€ ๋ฉค๋ฒ„์˜ ์ข…๋ฃŒ ์กฐ๊ฑด์ด ๋งŒ์กฑ๋˜๋ฉด ๋ฐ˜๋ณต์ด ์ค‘๋‹จ

with ์ ˆ์˜ ๋™์ž‘ ๋ฐฉ์‹

Inline View

with ์ ˆ์— ํฌํ•จ๋œ ์ฟผ๋ฆฌ์— ๋Œ€ํ•ด ๋ฌผ๋ฆฌ์ ์œผ๋กœ ์ž„์‹œ ํ…Œ์ด๋ธ”์„ ์ƒ์„ฑํ•˜์ง€ ์•Š๊ณ  ์ฟผ๋ฆฌ ๊ทธ ์ž์ฒด๋กœ ์ €์žฅํ•ด๋‘๋Š” ๋ฐฉ์‹
  • with ์ ˆ๋กœ ์ •์˜๋œ ํ…Œ์ด๋ธ”์ด ์ฐธ์กฐ๋œ ํšŸ์ˆ˜๋งŒํผ ๋ฐ˜๋ณต ์ˆ˜ํ–‰
  • with์ ˆ์ด ๋ณธ์ ˆ์—์„œ 1๋ฒˆ ์‚ฌ์šฉ๋  ๋•Œ ํ™œ์šฉํ•˜๋Š” ๋ฐฉ์‹
  • ์„œ๋ธŒ์ฟผ๋ฆฌ์™€ ๋‹ค๋ฅผ ๊ฒƒ ์—†๋Š” ์„ฑ๋Šฅ

Materialize

๋‚ด๋ถ€์ ์œผ๋กœ ๋ฉ”๋ชจ๋ฆฌ์— ์ž„์‹œ ํ…Œ์ด๋ธ”์„ ์ƒ์„ฑํ•จ์œผ๋กœ์จ ์ฟผ๋ฆฌ์˜ ๊ฒฐ๊ณผ๊ฐ’์„ ์ €์žฅํ•˜๊ณ , ๋ฐ˜๋ณตํ•ด์„œ ์žฌ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ์‹
  • with ์ ˆ๋กœ ์ •์˜๋œ ํ…Œ์ด๋ธ”์ด 2๋ฒˆ ์ด์ƒ ์‚ฌ์šฉ๋  ๋•Œ ํ™œ์šฉ
  • with์ ˆ์„ ํ†ตํ•ด ์ €์žฅ๋œ ๊ฒฐ๊ณผ๊ฐ’์„ ๋ถˆ๋Ÿฌ์™€ ์‹คํ–‰ ํšŸ์ˆ˜๋ฅผ ์ค„์—ฌ์คŒ์œผ๋กœ์จ ์„ฑ๋Šฅ์˜ ๊ฐœ์„ 

Bulk Insert

CSV ํŒŒ์ผ์„ ์ด์šฉํ•ด์„œ ๋Œ€์šฉ๋Ÿ‰ ๋ฐ์ดํ„ฐ๋ฅผ ํ…Œ์ด๋ธ”์— INSERT ํ•  ๋•Œ ํŽธ๋ฆฌ
BULK INSERT A ํ…Œ์ด๋ธ” FROM 'ํŒŒ์ผ๊ฒฝ๋กœ' WITH (FIRSTROW = 2, FIELDTERMINATOR = ',', ROWTERMINATOR = '\n', BATCHSIZE = 100)
  • FIRSTROW : ์‹œ์ž‘ํ•  ํ–‰
  • FIELDTERMINATOR : ๊ตฌ๋ถ„์ž
  • ROWTERMINATOR : ํ–‰ ๊ตฌ๋ถ„์ž
  • BATCHSIZE : ์ผ๊ด„ ์ฒ˜๋ฆฌ ํ–‰ ์ˆ˜

REPLICATE

์กฐ์ธ์˜ ์™ผ์ชฝ์— ์žˆ๋Š” ํ…Œ์ด๋ธ”์—์„œ ์กฐ์ธ ์—ด์˜ ๊ฐ’์„ ๋ชจ๋“  ๋…ธ๋“œ๋กœ ๋ณต์ œ
์˜ค๋ฅธ์ชฝ ํ…Œ์ด๋ธ”์€ ๋ณต์ œ๋œ ๋ฒ„์ „์˜ ํ•ด๋‹น ์—ด๊ณผ ์กฐ์ธ๋ฉ๋‹ˆ๋‹ค.

OUTPUT ์ ˆ

INSERT, UPDATE, DELETE ๋˜๋Š” MERGE ๋ฌธ์˜ ์˜ํ–ฅ์„ ๋ฐ›๋Š” ๊ฐ ํ–‰์˜ ์ •๋ณด ๋˜๋Š” ๊ฐ ํ–‰์„ ๊ธฐ๋ฐ˜์œผ๋กœ ํ•˜๋Š” ์‹์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
  • output ๊ตฌ๋ฌธ์—๋Š” ์„œ๋ธŒ์ฟผ๋ฆฌ X
notion image
insert into Customer (BusinessCode, CustomerName) output inserted.* values ('0006','๋Œ€์šฐ') DELETE Sales.ShoppingCartItem OUTPUT DELETED.*;
declare @ProductID nvarchar(4) = '5006' declare @ProductName nvarchar(50) = '๋ฐฐํ„ฐ๋ฆฌ3' declare @Standard int = 50 merge Product as T -- Target using ( select @ProductID as ProductID, @ProductName as ProductName, @Standard as Standard ) as S on -- Source T.ProductID = S.ProductID when matched and S.Standard != 0 then update set ProductName = S.ProductName, Standard = S.Standard when matched and S.Standard = 0 then delete when not matched then insert (ProductID, ProductName, Standard) values (S.ProductID, S.ProductName, S.Standard) output inserted.*, deleted.* ;
declare @ProductID nvarchar(4) = '5006' declare @ProductName nvarchar(50) = '๋ฐฐํ„ฐ๋ฆฌ3' declare @Standard int = 50 if(@ProductID != '' and @ProductName != '' and @Standard != 0) begin insert into Product (ProductID, ProductName, Standard) values(@ProductID, @ProductName, @Standard) select * from Product where ProductID != @ProductID and ProductName != @ProductName and Standard != @Standard and exists (select 1 from Product where ProductID = @ProductID) end

Emergency ๋ฐฑ์—…

1. ๋น„์ƒ๋กœ๊ทธ ๋ฐฑ์—…(Tail-Log Backup)

ํ•ด๋‹น DB์˜ ๋งˆ์ง€๋ง‰ ํŠธ๋žœ์žญ์…˜๋กœ๊ทธ ๋ฐฑ์—… ์ดํ›„ ์‹œ์ ์„ ๊ธฐ์ค€์œผ๋กœ ์ง€๊ธˆ๊นŒ์ง€์˜ ๋ชจ๋“  ํŠธ๋žœ์žญ์…˜ ๋กœ๊ทธ๋ฅผ ๋ฐฑ์—…ํ•˜๋Š” ๊ฒƒ
-- ๋น„์ƒ๋กœ๊ทธ ๋ฐฑ์—… -- ์ ˆ๋Œ€ ๊ธฐ์กด์˜ ํŠธ๋žœ์žญ์…˜ ๋กœ๊ทธ ๋ฐฑ์—…๊ณผ ๊ฐ™์€ ํŒŒ์ผ๋ช…์œผ๋กœ ๋ฐฑ์—…์‹œํ‚ค์ง€ ๋ง ๊ฒƒ BACKUP LOG [DB์ด๋ฆ„] TO DISK = '๋น„์ƒ๋กœ๊ทธ ๋ฐฑ์—…์„ ์ €์žฅ์‹œํ‚ฌ ๋””๋ ‰ํ† ๋ฆฌ' WITH NO_TRUNCATE, FORMAT -- FORMAT ์˜ต์…˜์€ ๋งŒ์•ฝ ์ด์ „์˜ ๋น„์ƒ๋กœ๊ทธ ๋ฐฑ์—…์ด ๊ฐ™์€ ํŒŒ์ผ๋ช…์œผ๋กœ ์กด์žฌํ•œ๋‹ค๋ฉด ํ•ด๋‹น ๋ฐฑ์—… ์„ธํŠธ ์ œ๊ฑฐ

2. DB ๋ฐฑ์—…๋ณธ ๋ถˆ๋Ÿฌ์™€์„œ ๋ถ™์ด๊ธฐ

[Object Explorer] - [Databases] - [Restore Database] ํด๋ฆญ
notion image
๋ถ™์ผ DB ๋ฐฑ์—…๋ณธ Add
notion image
๋ณต์›์‹œํ‚ฌ DB์˜ ์ด๋ฆ„ ๋ณ„๋„ ์ง€์ •
notion image
Restore ์˜ต์…˜ ์ง€์ •
notion image

3. ๋ณต์› ์‹œํ‚ฌ ์‹œ์  ์ฐพ๊ธฐ

select top 100 QS.creatino_time, -- SQL SUBSTRING(ST.text, (QS.statement_start_offset/2)+1, ((CASE QS.statemnet_end_offeset when -1 then DATELENGTH(st.text) else QS.statement_end_offset end - QS.statement_start_offset)/2) + 1 ) as statement_text, ST.text, -- ์‹คํ–‰๊ณ„ํš QS.total_worker_time, QS.last_worker_time, QS.max_worker_time, QS.min_worker_time from sys.dm_exec_query_stats QS -- sys.dm_exec_query_stats : ์‹คํ–‰๋œ ์ฟผ๋ฆฌ์— ๋Œ€ํ•œ ํ†ต๊ณ„ ํ…Œ์ด๋ธ” cross apply sys.dm_exec_sql_text(QS.sql_handle) ST -- ๋ณต์›ํ•ด์•ผ ๋  ์‹œ์  where creation_time between '2022-06-29 11:00:00' and '์‹œ๊ฐ„' order by QS.creation_time desc

4. Restore ์ง„ํ–‰ ๋ฐ Recovery State๋ฅผ โ€˜RECOVERYโ€™๋กœ ๋ฐ”๊พธ๊ธฐ

-- ๋น„์ƒ๋กœ๊ทธ ๋ฐฑ์—…๋ณธ์—์„œ ํ•ด๋‹น ์‹œ์ ์„ ์ฐพ์•„ ๋ณต์› RESTORE LOG AdventureWorks2019_dummy from DISK = 'E:\SQL Server BackUp\EmergencyTrnBak.bak' WITH MOVE 'AdventureWorks2017' TO 'C:\Program Files\Microsoft SQL Server\MSSQL15.MSSQLSERVER\MSSQL\DATA\AdventureWorks2019_Dummy.mdf', MOVE 'AdventureWorks2017_log' TO 'C:\Program Files\Microsoft SQL Server\MSSQL15.MSSQLSERVER\MSSQL\DATA\AdventureWorks2019_Dummy_Log.ldf', NORECOVERY, STOPAT = '2022-06-29 11:19:35.946', STATS=10 -- ์‹ค์ˆ˜ํ•œ 'UPDATE ํŠธ๋žœ์žญ์…˜'์˜ ์‹œ์ž‘ ์‹œ๊ฐ„ -0.001์ดˆ ์ง€์ ์œผ๋กœ ๋กค๋ฐฑ -- ๋ณต์›์ด ๋๋‚œ ํ›„ DB์˜ 'Restoring...' ์ƒํƒœ๋ฅผ ํ•ด์ œ. RESTORE DATABASE AdventureWorks2019_Dummy WITH RECOVERY

5. ๋ณต์›ํ•œ Dummy DB๋ฅผ ํ†ตํ•ด ์›๋ž˜ DB์˜ ๋ฐ”๋€ ๋ถ€๋ถ„ ์›๋ณตํ•˜๊ธฐ

'E:\SQL Server BackUp\EmergencyTrnBak.bak' WITH MOVE 'AdventureWorks2017' TO 'C:\Program Files\Microsoft SQL Server\MSSQL15.MSSQLSERVER\MSSQL\DATA\AdventureWorks2019_Dummy.mdf', MOVE 'AdventureWorks2017_log' TO 'C:\Program Files\Microsoft SQL Server\MSSQL15.MSSQLSERVER\MSSQL\DATA\AdventureWorks2019_Dummy_Log.ldf', NORECOVERY, STOPAT = '2022-06-29 11:19:35.946', STATS=10 -- ์‹ค์ˆ˜ํ•œ 'UPDATE ํŠธ๋žœ์žญ์…˜'์˜ ์‹œ์ž‘ ์‹œ๊ฐ„ -0.001์ดˆ ์ง€์ ์œผ๋กœ ๋กค๋ฐฑ -- ๋ณต์›์ด ๋๋‚œ ํ›„ DB์˜ 'Restoring...' ์ƒํƒœ๋ฅผ ํ•ด์ œ. RESTORE DATABASE AdventureWorks2019_Dummy WITH RECOVERY

๋ณ€๊ฒฝ ๋‚ด์šฉ ์ถ”์ 

1. ์„ธํŒ…

-- ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ๋ณ€๊ฒฝ ๋‚ด์šฉ ์ถ”์  ์„ค์ • alter database BusinessDB set change_tracking = on (change_retention = 2 days, auto_cleanup = on) -- ์ถ”์  ์ •๋ณด๊ฐ€ ์œ ์ง€๋˜๋Š” ๊ธฐ๊ฐ„์„ ์ง€์ • --์ด ๊ธฐ๊ฐ„๋ณด๋‹ค ์˜ค๋ž˜๋œ ๋ณ€๊ฒฝ ๋‚ด์šฉ ์ถ”์  ์ •๋ณด๋Š” ์ •๊ธฐ์ ์œผ๋กœ ์ œ๊ฑฐ -- ํ…Œ์ด๋ธ”์— ๋Œ€ํ•œ ๋ณ€๊ฒฝ ๋‚ด์šฉ ์ถ”์„ ์„ค์ • alter table BusinessDB.dbo.Product enable change_tracking with (TRACK_COLUMNS_UPDATED = on)

2. ๋ณ€๊ฒฝ ๋‚ด์šฉ ๊ฐ€์ ธ์˜ค๊ธฐ

declare @last_synchronization_version bigint; SELECT CT.ProductID, ProductName, Standard , CT.SYS_CHANGE_OPERATION, CT.SYS_CHANGE_COLUMNS, CT.SYS_CHANGE_CONTEXT FROM BusinessDB.dbo.Product AS P RIGHT OUTER JOIN CHANGETABLE(CHANGES BusinessDB.dbo.Product, @last_synchronization_version) AS CT on P.ProductID = CT.ProductID;
notion image
ย