SQL

Simba_bยท2022๋…„ 11์›” 1์ผ
0
post-thumbnail

๐Ÿ› ๏ธ SQL

SQL : Structure Query Language
RDBMS์— ์ •๋ณด๋ฅผ ๊ด€๋ฆฌ ๋ฐ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•œ ์–ธ์–ด

  • ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์—์„œ ์ •๋ณด๋ฅผ ์ €์žฅ, ์—…๋ฐ์ดํŠธ, ์ œ๊ฑฐ, ๊ฒ€์ƒ‰ ๋ฐ ๊ฒ€์ƒ‰
  • ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์„ฑ๋Šฅ์„ ์œ ์ง€ ๊ด€๋ฆฌํ•˜๊ณ  ์ตœ์ ํ™”

โœ”๏ธSQL ๋ช…๋ น์–ด

DDL(Data Definition Language, ๋ฐ์ดํ„ฐ ์ •์˜์–ด)
๊ฐ DB๊ตฌ์กฐ๋ฅผ ์ •์˜ํ•˜๊ธฐ ์œ„ํ•œ ๋ช…๋ น์–ด ์ง‘ํ•ฉ

  • CREATE : ํ…Œ์ด๋ธ” ์ƒ์„ฑ
  • ALTER : ํ…Œ์ด๋ธ” ์ˆ˜์ •
  • DROP : ํ…Œ์ด๋ธ” ๊ตฌ์กฐ์™€ ๋ฐ์ดํ„ฐ ๋ชจ๋‘ ์‚ญ์ œ(ROLLBACK๋ถˆ๊ฐ€)
  • TRUNCATE : ๊ตฌ์กฐ๋ฅผ ๋‘๊ณ  ๋ฐ์ดํ„ฐ๋งŒ ์‚ญ์ œ

DML(Data Manipulation Language, ๋ฐ์ดํ„ฐ ์กฐ์ž‘์–ด)
DB์•ˆ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์กฐ์ž‘ํ•˜๊ธฐ ์œ„ํ•œ ๋ช…๋ น์–ด ์ง‘ํ•ฉ

  • SELECT : ํ…Œ์ด๋ธ” ์กฐํšŒ
  • INSERT : ๋ฐ์ดํ„ฐ ์‚ฝ์ž…
  • DELETE : ๋ฐ์ดํ„ฐ ์‚ญ์ œ(ROLLBACK๊ฐ€๋Šฅ)
  • UPDATE : ๋ฐ์ดํ„ฐ ์ˆ˜์ •

DCL(Data Control Language, ๋ฐ์ดํ„ฐ ์ œ์–ด์–ด)
DB๋ฅผ ์ œ์–ดํ•˜๊ณ  ํ†ต์ œํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•˜๋Š” ๋ช…๋ น์–ด ์ง‘ํ•ฉ

  • GRANT : ๊ถŒํ•œ ๋ถ€์—ฌ
  • REVOKE : ๊ถŒํ•œ ํšŒ์ˆ˜

TCL(Transaction Control Language, ๋ฐ์ดํ„ฐ ์ œ์–ด์–ด)
ํŠธ๋žœ์žญ์…˜ ์ œ์–ด ๋ช…๋ น์œผ๋กœ DCL์—์„œ ๋ถ„๋ฆฌํ•ด ํ‘œํ˜„ํ•˜๊ธฐ๋„ ํ•จ

  • COMMIT : ํŠธ๋žœ์žญ์…˜ ์ฒ˜๋ฆฌ๊ฐ€ ์ •์ƒ์ ์œผ๋กœ ์ข…๋ฃŒ๋˜์–ด ์ˆ˜ํ–‰ํ•œ ๋ณ€๊ฒฝ ๋‚ด์šฉ DB์— ๋ฐ˜์˜
  • ROLLBACK : ํ•˜๋‚˜์˜ ํŠธ๋žœ์žญ์…˜ ์ฒ˜๋ฆฌ๊ฐ€ ๋น„์ •์ƒ์  ์ข…๋ฃŒ๋˜์–ด DB์˜ ์ผ๊ด€์„ฑ์ด ๊นจ์กŒ์„ ๋•Œ, ํŠธ๋žœ์žญ์…˜์ด ํ–‰ํ•œ ๋ชจ๋“  ๋ณ€๊ฒฝ์ž‘์—… ์ทจ์†Œํ•ด ์ด์ „ ์ƒํƒœ๋กœ ๋˜๋Œ๋ฆฌ๋Š” ์—ฐ์‚ฐ

โœ”๏ธ SQL ๊ธฐ๋ณธ ์ง‘๊ณ„ํ•จ์ˆ˜

์ง‘๊ณ„ํ•จ์ˆ˜๊ธฐ๋Šฅ
COUNTํŠน์ • ์—ด ๊ฐ’์˜ ๊ฐฏ์ˆ˜, ํ–‰์˜ ๊ฐฏ์ˆ˜
MAXํŠน์ • ์—ด ๊ฐ’ ์ค‘ ์ตœ๋Œ“๊ฐ’
MINํŠน์ • ์—ด ๊ฐ’ ์ค‘ ์ตœ์†Ÿ๊ฐ’
SUMํŠน์ • ์—ด ๊ฐ’์˜ ํ•ฉ๊ณ„
AVGํŠน์ • ์—ด ๊ฐ’์˜ ํ‰๊ท 

โœ”๏ธ Join

๋‘ ๊ฐœ์˜ ํ…Œ์ด๋ธ”์„ ๋ฌถ์–ด์„œ ํ•˜๋‚˜์˜ ๊ฒฐ๊ณผ๋ฅผ ์ถ”์ถœํ•  ๊ฒฝ์šฐ ํ™œ์šฉ

์ถœ์ฒ˜) https://www.codeproject.com/Articles/33052/Visual-Representation-of-SQL-Joins

INNER JOIN

๋‘ ํ…Œ์ด๋ธ”์„ ์กฐ์ธํ•  ๋•Œ, ๋‘ ํ…Œ์ด๋ธ”์— ๋ชจ๋‘ ์ง€์ •ํ•œ ์—ด์˜ ๋ฐ์ดํ„ฐ๊ฐ€ ์กด์žฌํ• ๋•Œ ์ถœ๋ ฅ

์ถœ์ฒ˜) https://www.codeproject.com/Articles/33052/Visual-Representation-of-SQL-Joins

OUTER JOIN

๋‘ ํ…Œ์ด๋ธ”์„ ์กฐ์ธํ•  ๋•Œ, 1๊ฐœ์˜ ํ…Œ์ด๋ธ”์—๋งŒ ๋ฐ์ดํ„ฐ๊ฐ€ ์žˆ์œผ๋ฉด ์ถœ๋ ฅ

  • LEFT OUTER JOIN :์™ผ์ชฝ ํ…Œ์ด๋ธ”์˜ ๋ชจ๋“  ๊ฐ’์ด ์ถœ๋ ฅ
  • RIHGT OUTER JOIN : ์˜ค๋ฅธ์ชฝ ํ…Œ์ด๋ธ”์˜ ๋ชจ๋“  ๊ฐ’์ด ์ถœ๋ ฅ
  • FULL OUTER JOIN : ์™ผ์ชฝ๊ณผ ์˜ค๋ฅธ์ชฝ ํ…Œ์ด๋ธ”์˜ ๋ชจ๋“  ๊ฐ’์ด ์ถœ๋ ฅ
์ถœ์ฒ˜) https://hongong.hanbit.co.kr/sql-%EA%B8%B0%EB%B3%B8-%EB%AC%B8%EB%B2%95-joininner-outer-cross-self-join/

CROSS JOIN

ํ•œ์ชฝ ํ…Œ์ด๋ธ”์˜ ๋ชจ๋“  ํ–‰๊ณผ ๋‹ค๋ฅธ ์ชฝ ํ…Œ์ด๋ธ”์˜ ๋ชจ๋“  ํ–‰์„ ์กฐ์ธ

์ถœ์ฒ˜) https://hongong.hanbit.co.kr/sql-%EA%B8%B0%EB%B3%B8-%EB%AC%B8%EB%B2%95-joininner-outer-cross-self-join/

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