๐ŸŒˆ [Section2] 11. SQL

ํ˜„์ฃผยท2022๋…„ 10์›” 6์ผ
0

bootcamp

๋ชฉ๋ก ๋ณด๊ธฐ
30/71

๐Ÿ“• ์˜ค๋Š˜ ๋ฐฐ์šด ๋‚ด์šฉ!

  • SQL / NoSQL
  • SQL ๊ธฐ๋ณธ ๋ฌธ๋ฒ•

โœ๏ธ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค (Database)

  • ์ปดํ“จํ„ฐ ์‹œ์Šคํ…œ์— ์ „์ž์ ์œผ๋กœ ์ €์žฅ๋˜๋Š” ์กฐ์งํ™”๋œ ๋ฐ์ดํ„ฐ ๋ชจ์Œ

  • ๋ฐ์ดํ„ฐ๋ฅผ ์กฐ์งํ™” ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋Œ€๋Ÿ‰์˜ ๋ฐ์ดํ„ฐ ํšจ์œจ์ ์œผ๋กœ ๊ด€๋ฆฌ ๊ฐ€๋Šฅ

โœ๏ธ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๊ด€๋ฆฌ ์‹œ์Šคํ…œ (DBMS)

  • Database Management System

  • ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ฅด ์‹ค์งˆ์ ์œผ๋กœ ๊ตฌํ˜„ํ•˜๊ธฐ ์œ„ํ•˜์—ฌ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ

Ex. MySQL, Oracle, SQLite, MariaDB, PostgresSQL ๋“ฑ

โœ”๏ธ RDBMS
๊ด€๊ณ„ํ˜• ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๊ด€๋ฆฌ ์‹œ์Šคํ…œ


โœ๏ธ SQL (Structured Query Language)

  • ๊ตฌ์กฐํ™”๋œ ์ฟผ๋ฆฌ ์–ธ์–ด

  • ๊ด€๊ณ„ํ˜• ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์ „์šฉ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด

โœ”๏ธ ์ฟผ๋ฆฌ (query)
๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์—์„œ ์›ํ•˜๋Š” ๋ฐ์ดํ„ฐ๋งŒ ์ถ”๋ฆฌ๊ณ  ์‹ถ์„ ๋•Œ ํ•˜๋Š” ๋ช…๋ น(์งˆ์˜๋ฌธ)
โžœ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ์ฟผ๋ฆฌ๋ฅผ ๋ณด๋‚ด ์›ํ•˜๋Š” ๋ฐ์ดํ„ฐ๋งŒ ํ•„ํ„ฐ๋งํ•˜์—ฌ ๊ฐ€์ ธ์˜ค๊ฑฐ๋‚˜ ์‚ฝ์ž…ํ•  ์ˆ˜ ์žˆ์Œ
โ €
Ex. ํšŒ์›๋“ค ์ค‘ ์„ฑ๋ณ„์ด ๋‚จ์ž์ธ ํšŒ์›๋งŒ ๊ฐ€์ ธ์™€

โœ๏ธ NoSQL (Non Structured Query Language)

  • ๊ตฌ์กฐํ™”๋˜์ง€ ์•Š์€ ์ฟผ๋ฆฌ ์–ธ์–ด

  • ๋น„๊ด€๊ณ„ํ˜• ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์ „์šฉ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด


โœ๏ธ SQL ๊ธฐ๋ฐ˜ <๊ด€๊ณ„ํ˜• ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค>

  • ๊ณ ์ •๋œ ํ–‰(row)๊ณผ ์—ด(column)๋กœ ๊ตฌ์„ฑ๋œ ํ…Œ์ด๋ธ”์— ๋ฐ์ดํ„ฐ ์ €์žฅ

  • ๊ฐ ์—ด์„ ํ•˜๋‚˜์˜ ์†์„ฑ์— ๋Œ€ํ•œ ์ •๋ณด ์ €์žฅ / ๊ฐ ํ–‰์€ ๊ฐ ์—ด์˜ ๋ฐ์ดํ„ฐ ํ˜•์‹์— ๋งž๋Š” ๋ฐ์ดํ„ฐ ์ €์žฅ

  • ํ…Œ์ด๋ธ”์˜ ๊ตฌ์กฐ์™€ ๋ฐ์ดํ„ฐ ํƒ€์ž… ๋“ฑ์„ ์‚ฌ์ „์— ์ •์˜ํ•˜๊ณ , ํ…Œ์ด๋ธ”์— ์ •์˜๋œ ๋‚ด์šฉ์— ์•Œ๋งž์€ ํ˜•ํƒœ์˜ ๋ฐ์ดํ„ฐ๋งŒ ์‚ฝ์ž…ํ•  ์ˆ˜ ์žˆ์Œ

  • SQL์„ ๊ธฐ๋ฐ˜์œผ๋กœ ํ•˜์—ฌ ์ •๋ณด ์ฟผ๋ฆฌ

  • ๋ฐ์ดํ„ฐ๋ฅผ ์ž…๋ ฅํ•  ๋•Œ ์Šคํ‚ค๋งˆ์— ๋งž๊ฒŒ ์ž…๋ ฅํ•ด์•ผํ•ด์„œ ์Šคํ‚ค๋งˆ๊ฐ€ ๋šœ๋ ทํ•˜๊ฒŒ ๋ณด์ž„
    (ํ…Œ์ด๋ธ” ๊ฐ„์˜ ๊ด€๊ณ„๋ฅผ ์ง๊ด€์ ์œผ๋กœ ํŒŒ์•… ๊ฐ€๋Šฅ)

Ex. MySQL, Oracle, SQLite, PostgresSQL, MariaDB ๋“ฑ

โœ” ์žฅ์ 

  • ๋ฐ์ดํ„ฐ ์„ฑ๋Šฅ์ด ์ผ๋ฐ˜์ ์œผ๋กœ ์ข‹์•„ ์ •๋ ฌ, ํƒ์ƒ‰, ๋ถ„๋ฅ˜๊ฐ€ ๋น ๋ฆ„

  • ์‹ ๋ขฐ์„ฑ์ด ๋†’์•„ ๋ฐ์ดํ„ฐ์˜ ๋ฌด๊ฒฐ์„ฑ์„ ๋ณด์žฅ

  • ์ •๊ทœํ™”์— ๋”ฐ๋ฅธ ๊ฐฑ์‹  ๋น„์šฉ์„ ์ตœ์†Œํ™”
    ( ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์„ค๊ณ„์‹œ ์ค‘๋ณต์„ ์ตœ์†Œํ™”ํ•ด์„œ ๊ตฌ์กฐํ™”ํ•˜๋Š” ํ”„๋กœ์„ธ์Šค )

โœ” ๋‹จ์ 

  • ๊ธฐ์กด์— ์ž‘์„ฑ๋œ ์Šคํ‚ค๋งˆ์˜ ์ˆ˜์ •์ด ์–ด๋ ค์›€

  • ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์˜ ๋ถ€ํ•˜๋ฅผ ๋ถ„์„ํ•˜๊ธฐ ์–ด๋ ค์›€

  • ๋น…๋ฐ์ดํ„ฐ๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š”๋ฐ ๋งค์šฐ ๋น„ํšจ์œจ์ 

โœ”๏ธ ์‚ฌ์šฉ

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

  • ์†Œํ”„ํŠธ์›จ์–ด์— ์‚ฌ์šฉ๋˜๋Š” ๋ฐ์ดํ„ฐ๊ฐ€ ๊ตฌ์กฐ์ ์ด๊ณ  ์ผ๊ด€๋œ ๊ฒฝ์šฐ
    โžœ ๋‹ค์–‘ํ•œ ๋ฐ์ดํ„ฐ ์œ ํ˜•๊ณผ ๋†’์€ ํŠธ๋ž˜ํ”ฝ์„ ์ง€์›ํ•˜๋„๋ก ์„ค๊ณ„๋œ NoSQL ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ฅผ ์‚ฌ์šฉํ•ด์•ผ๋งŒ ํ•˜๋Š” ์ด์œ ๊ฐ€ ์—†๊ธฐ ๋•Œ๋ฌธ

โœ๏ธ NoSQL ๊ธฐ๋ฐ˜ <๋น„๊ด€๊ณ„ํ˜• ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค>

  • ๋ฐ์ดํ„ฐ๊ฐ€ ๊ณ ์ •๋˜์–ด์žˆ์ง€ ์•Š์€ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค

  • ๊ด€๊ณ„ํ˜• ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ณด๋‹ค ๋” ์œตํ†ต์„ฑ์žˆ๋Š” ๋ฐ์ดํ„ฐ ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•˜์—ฌ, ๋ฐ์ดํ„ฐ ์ €์žฅ/๊ฒ€์ƒ‰์— ํŠนํ™”๋œ ๋งค์ปค๋‹ˆ์ฆ˜ ์ œ๊ณต

  • NoSQL์„ ๊ธฐ๋ฐ˜์œผ๋กœ ํ•˜์—ฌ ์ •๋ณด ์ฟผ๋ฆฌ

  • ์œ ์—ฐํ•œ ์Šคํ‚ค๋งˆ๋ฅผ ์ œ๊ณตํ•˜์—ฌ, ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ์–ด์˜ฌ ๋•Œ ์Šคํ‚ค๋งˆ์— ๋”ฐ๋ผ ๋ฐ์ดํ„ฐ ์ฝ์–ด์˜ด
    (schema on read)

  • ๋ฐ์ดํ„ฐ๋ฅผ ์ž…๋ ฅํ•˜๋Š” ๋ฐฉ์‹์— ๋”ฐ๋ผ, ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ์–ด์˜ฌ ๋•Œ ์˜ํ–ฅ์„ ๋ฏธ์นจ

  • Key-Value ํƒ€์ž…, ๋ฌธ์„œํ˜•(Document) ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค, Wide-Column ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค, ๊ทธ๋ž˜ํ”„(Graph) ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋กœ ๊ตฌ์„ฑ

    • Key-Value ํƒ€์ž…
      โžœ ์†์„ฑ์„ key-value์˜ ์Œ์œผ๋กœ ๋‚˜ํƒ€๋‚ด๋Š” ๋ฐ์ดํ„ฐ ๋ฐฐ์—ด์˜ ํ˜•ํƒœ๋กœ ์ €์žฅ
      ( key - ์†์„ฑ ์ด๋ฆ„ / value - ๊ทธ ์†์„ฑ์˜ ๋ฐ์ดํ„ฐ ๊ฐ’ )
      Ex. Redis, Dynamo ๋“ฑ
      โ €
    • ๋ฌธ์„œํ˜•(Document) ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค
      โžœ ํ…Œ์ด๋ธ”์ด ์•„๋‹Œ ๋ฌธ์„œ์ฒ˜๋Ÿผ ์ €์žฅํ•˜๋Š” ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค
      ( JSON๊ณผ ์œ ์‚ฌํ•œ ํ˜•์‹์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฌธ์„œํ™”ํ•˜์—ฌ ์ €์žฅ )
      โžœ ๊ฐ ๋ฌธ์„œ๋Š” ํ•˜๋‚˜์˜ ์†์„ฑ์— ๋Œ€ํ•œ ๋ฐ์ดํ„ฐ ๊ฐ€์ง€๊ณ  ์žˆ๊ณ , ์ปฌ๋ ‰์…˜์ด๋ผ๊ณ  ํ•˜๋Š” ๊ทธ๋ฃน์œผ๋กœ ๋ฌถ์–ด์„œ ๊ด€๋ฆฌ
      Ex. MongoDB
      โ €
    • Wide-Column ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค
      โžœ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์˜ ์—ด(column)์— ๋Œ€ํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ์ง‘์ค‘์ ์œผ๋กœ ๊ด€๋ฆฌํ•˜๋Š” ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค
      โžœ ๊ฐ ์—ด์—๋Š” key-value ํ˜•์‹์œผ๋กœ ๋ฐ์ดํ„ฐ๊ฐ€ ์ €์žฅ๋˜๊ณ , ์ปฌ๋Ÿผ ํŒจ๋ฐ€๋ฆฌ(column families)๋ผ๋Š” ์—ด์˜ ์ง‘ํ•ฉ์ฒด ๋‹จ์œ„๋กœ ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ ๊ฐ€๋Šฅ
      โžœ ํ•˜๋‚˜์˜ ํ–‰์— ๋งŽ์€ ์—ด์„ ํฌํ•จํ•  ์ˆ˜ ์žˆ์–ด์„œ ์œ ์—ฐ์„ฑ์ด ๋†’์Œ
      โžœ ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ์— ํ•„์š”ํ•œ ์—ด์˜ ์œ ์—ฐํ•œ ์„ ํƒ์ด ๊ฐ€๋Šฅํ•˜๊ธฐ์— ๊ทœ๋ชจ๊ฐ€ ํฐ ๋ฐ์ดํ„ฐ ๋ถ„์„์— ์ฃผ๋กœ ์‚ฌ์šฉ
      Ex. Cassandra, HBase
      โ €
    • ๊ทธ๋ž˜ํ”„(Graph) ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค
      โžœ ์ž๋ฃŒ๊ตฌ์กฐ์˜ ๊ทธ๋ž˜ํ”„์™€ ๋น„์Šทํ•œ ํ˜•์‹์œผ๋กœ ๋ฐ์ดํ„ฐ ๊ฐ„์˜ ๊ด€๊ณ„๋ฅผ ๊ตฌ์„ฑํ•˜๋Š” ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค
      โžœ ๋…ธ๋“œ(nodes)์— ์†์„ฑ๋ณ„(entities)๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅ
      ( ๊ฐ ๋…ธ๋“œ๊ฐ„ ๊ด€๊ณ„๋Š” ์„ (edge)์œผ๋กœ ํ‘œํ˜„ )
      Ex. Neo4J, InfiniteGraph

โœ” ์žฅ์ 

  • ๋Œ€์šฉ๋Ÿ‰ ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ์— ํšจ์œจ์ 

  • ๋›ฐ์–ด๋‚œ ํ™•์žฅ์„ฑ์œผ๋กœ ๊ฒ€์ƒ‰์— ์œ ๋ฆฌ.

  • ์ตœ์ ํ™”๋œ ํ‚ค ๊ฐ’ ์ €์žฅ ๊ธฐ๋ฒ•์„ ์‚ฌ์šฉํ•˜์—ฌ ์‘๋‹ต์†๋„๋‚˜ ์ฒ˜๋ฆฌํšจ์œจ ๋“ฑ์—์„œ ์„ฑ๋Šฅ์ด ๋›ฐ์–ด๋‚จ

โœ” ๋‹จ์ 

  • ์ฟผ๋ฆฌ ์ฒ˜๋ฆฌ์‹œ ๋ฐ์ดํ„ฐ๋ฅผ ํŒŒ์‹ฑ ํ›„ ์—ฐ์‚ฐ์„ ํ•ด์•ผํ•ด์„œ ํฐ ํฌ๊ธฐ์˜ ๋ฌธ์„œ ๋‹ค๋ฃฐ ๋•Œ๋Š” ์„ฑ๋Šฅ์ด ์ €ํ•˜๋จ

โœ”๏ธ ์‚ฌ์šฉ

  • ๋ฐ์ดํ„ฐ์˜ ๊ตฌ์กฐ๊ฐ€ ๊ฑฐ์˜ ๋˜๋Š” ์ „ํ˜€ ์—†๋Š” ๋Œ€์šฉ๋Ÿ‰์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜๋Š” ๊ฒฝ์šฐ
    โžœ ๋Œ€๋ถ€๋ถ„ ์ €์žฅํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐ์ดํ„ฐ ์œ ํ˜•์— ์ œํ•œ์ด ์—†์–ด ์–ธ์ œ๋“ ์ง€ ์ƒˆ ์œ ํ˜• ์ถ”๊ฐ€ ๊ฐ€๋Šฅ
    โžœ ์ •ํ˜•ํ™” ๋˜์ง€ ์•Š์€ ๋งŽ์€ ์–‘์˜ ๋ฐ์ดํ„ฐ๊ฐ€ ํ•„์š”ํ•œ ๊ฒฝ์šฐ ํšจ์œจ์ 

  • ํด๋ผ์šฐ๋“œ ์ปดํ“จํŒ… ๋ฐ ์ €์žฅ๊ณต๊ฐ„์„ ์ตœ๋Œ€ํ•œ ํ™œ์šฉํ•˜๋Š” ๊ฒฝ์šฐ
    โžœ ์ €๋ ดํ•œ ๋น„์šฉ์˜ ์†”๋ฃจ์…˜ ์ œ๊ณต๋ฐ›์„ ์ˆ˜ ์žˆ๊ณ , ํ™•์žฅ์„ฑ์ด ์ค‘์š”ํ•œ ๊ฒฝ์šฐ์—๋„ ๋ฒˆ๊ฑฐ๋กœ์›€ ์—†์ด ํ™•์žฅ์ด ๊ฐ€๋Šฅ

  • ๋น ๋ฅด๊ฒŒ ์„œ๋น„์Šค๋ฅผ ๊ตฌ์ถ•ํ•˜๋Š” ๊ณผ์ •์—์„œ ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ๋ฅผ ์ž์ฃผ ์—…๋ฐ์ดํŠธ ํ•˜๋Š” ๊ฒฝ์šฐ
    โžœ ์Šคํ‚ค๋งˆ๋ฅผ ๋ฏธ๋ฆฌ ์ค€๋น„ํ•  ํ•„์š”๊ฐ€ ์—†์–ด ๋น ๋ฅธ ๊ฐœ๋ฐœ์— ์œ ๋ฆฌ
    Ex. ์‹œ์žฅ์— ๋น ๋ฅด๊ฒŒ ํ”„๋กœํ† ํƒ€์ž…์„ ์ถœ์‹œํ•ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ


โœ๏ธ SQL ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์™€ NoSQL ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์˜ ์ฐจ์ด์ 

1. ๋ฐ์ดํ„ฐ ์ €์žฅ(Storage) ๋ฐฉ์‹

โœ”๏ธ SQL ๊ธฐ๋ฐ˜ ๊ด€๊ณ„ํ˜• ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค

  • SQL์„ ์ด์šฉํ•ด์„œ ๋ฐ์ดํ„ฐ๋ฅผ ํ…Œ์ด๋ธ”์— ์ €์žฅ

  • ๋ฏธ๋ฆฌ ์ž‘์„ฑ๋œ ์Šคํ‚ค๋งˆ๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ์ •ํ•ด์ง„ ํ˜•์‹์— ๋งž๊ฒŒ ๋ฐ์ดํ„ฐ ์ €์žฅ

โœ”๏ธ NoSQL ๊ธฐ๋ฐ˜ ๋น„๊ด€๊ณ„ํ˜• ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค

  • key-value, document, wide-column, graph ๋“ฑ์˜ ๋ฐฉ์‹์œผ๋กœ ๋ฐ์ดํ„ฐ ์ €์žฅ

2. ์Šคํ‚ค๋งˆ(Schema)

โœ”๏ธ SQL ๊ธฐ๋ฐ˜ ๊ด€๊ณ„ํ˜• ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค

  • ๊ณ ์ •๋œ ํ˜•์‹์˜ ์Šคํ‚ค๋งˆ๊ฐ€ ํ•„์š”
    ( ์ฒ˜๋ฆฌํ•˜๋ ค๋Š” ๋ฐ์ดํ„ฐ ์†์„ฑ๋ณ„๋กœ ์—ด(column)์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ๋ฏธ๋ฆฌ ์ •ํ•ด๋‘์–ด์•ผ ํ•จ )

  • ์Šคํ‚ค๋งˆ๋Š” ๋‚˜์ค‘์— ๋ณ€๊ฒฝ ๊ฐ€๋Šฅ
    But, ์ด ๊ฒฝ์šฐ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์ „์ฒด๋ฅผ ์ˆ˜์ •ํ•˜๊ฑฐ๋‚˜ ์˜คํ”„๋ผ์ธ(down-time)์œผ๋กœ ์ „ํ™˜ํ•ด์•ผํ•จ

โœ”๏ธ NoSQL ๊ธฐ๋ฐ˜ ๋น„๊ด€๊ณ„ํ˜• ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค

  • ๊ด€๊ณ„ํ˜• ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ณด๋‹ค ๋™์ ์œผ๋กœ ์Šคํ‚ค๋งˆ์˜ ํ˜•ํƒœ ๊ด€๋ฆฌ ๊ฐ€๋Šฅ

  • ํ–‰์„ ์ถ”๊ฐ€ํ•  ๋•Œ ์ฆ‰์‹œ ์ƒˆ๋กœ์šด ์—ด์„ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ๊ณ , ๊ฐœ๋ณ„ ์†์„ฑ์— ๋Œ€ํ•ด์„œ ๋ชจ๋“  ์—ด์— ๋Œ€ํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ˜๋“œ์‹œ ์ž…๋ ฅํ•˜์ง€ ์•Š์•„๋„ ๋จ

3. ์ฟผ๋ฆฌ(Querying)

โœ”๏ธ SQL ๊ธฐ๋ฐ˜ ๊ด€๊ณ„ํ˜• ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค

  • ํ…Œ์ด๋ธ”์˜ ํ˜•์‹๊ณผ ํ…Œ์ด๋ธ”๊ฐ„์˜ ๊ด€๊ณ„์— ๋งž์ถฐ ๋ฐ์ดํ„ฐ ์š”์ฒญ
    โžœ ์ •๋ณด๋ฅผ ์š”์ฒญ์‹œ, ๊ตฌ์กฐํ™”๋œ ์ฟผ๋ฆฌ ์–ธ์–ด(SQL) ์‚ฌ์šฉ

โœ”๏ธ NoSQL ๊ธฐ๋ฐ˜ ๋น„๊ด€๊ณ„ํ˜• ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค

  • ๋ฐ์ดํ„ฐ ๊ทธ๋ฃน ์ž์ฒด๋ฅผ ์กฐํšŒํ•˜๋Š” ๊ฒƒ์— ์ดˆ์ 
    โžœ ๊ทธ๋ž˜์„œ ๊ตฌ์กฐํ™” ๋˜์ง€ ์•Š์€ ์ฟผ๋ฆฌ ์–ธ์–ด๋กœ๋„ ๋ฐ์ดํ„ฐ ์š”์ฒญ ๊ฐ€๋Šฅ

  • UnQL(UnStructured Query Language)์ด๋ผ๊ณ  ๋งํ•˜๊ธฐ๋„ ํ•จ

4. ํ™•์žฅ์„ฑ(Scalability)

โœ”๏ธ SQL ๊ธฐ๋ฐ˜ ๊ด€๊ณ„ํ˜• ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค

  • ์ˆ˜์ง์ ์œผ๋กœ ํ™•์žฅ
    ( ๊ธฐ๋ณธ๊ฒƒ์„ ์ƒˆ ํ•˜๋“œ์›จ์–ด๋กœ ๊ต์ฒดํ•˜์—ฌ ์„ฑ๋Šฅ์„ ํ™•์žฅํ•˜๋Š” ๊ฒƒ )

    ์™„์ „ํžˆ high-end์ธ ํ•˜๋“œ์›จ์–ด๋ฅผ ๊ตฌ๋งคํ•˜์ง€ ์•Š์„ ๊ฒฝ์šฐ์—๋Š” ๋น„์šฉ ์ €๋ ด
    But, ํ™•์žฅ์ด ๋ฐ˜๋ณต๋  ์ˆ˜๋ก ๊ธฐํ•˜๊ธ‰์ˆ˜์ ์ธ ๋น„์šฉ์ด ๋“ค์–ด๊ฐ )

  • ์‹ค์ œ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๊ฐ€ ๋‹ค์šด๋˜์—ˆ์„ ๋•Œ, ๋‹จ์ผ ํ•˜๋“œ์›จ์–ด๋ฅผ ํ™œ์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋ชจ๋“  ์„œ๋ฒ„๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ๋ชปํ•จ
    ( ์ด๋ฅผ ๋ฐฉ์ง€ํ•˜๋ ค๊ณ , ๋™์‹œ์— ๋ฐฑ์—…ํ•˜์—ฌ ๋‹ค์šด๋˜์—ˆ์„ ๊ฒฝ์šฐ, ๋ฐ”๋กœ ๊ทธ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋กœ ๋„˜์–ด๊ฐ€๋„๋ก ์‚ฌ์šฉ )

  • ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๊ฐ€ ๊ตฌ์ถ•๋œ ํ•˜๋“œ์›จ์–ด์˜ ์„ฑ๋Šฅ์„ ๋งŽ์ด ์ด์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋น„์šฉ์ด ๋งŽ์ด ๋“ฆ

  • ์—ฌ๋Ÿฌ ์„œ๋ฒ„์— ๊ฑธ์ณ์„œ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์˜ ๊ด€๊ณ„ ์ •์˜๊ฐ€ ๊ฐ€๋Šฅํ•˜์ง€๋งŒ, ๋งค์šฐ ๋ณต์žกํ•˜๊ณ  ์‹œ๊ฐ„ ๋งŽ์ด ์†Œ๋ชจ

โœ”๏ธ NoSQL ๊ธฐ๋ฐ˜ ๋น„๊ด€๊ณ„ํ˜• ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค

  • ์ˆ˜ํ‰์ ์œผ๋กœ ํ™•์žฅ
    ( ๋ณ‘๋ ฌ์ ์œผ๋กœ, ๋ณด๋‹ค ๊ฐ’์‹ผ ์„œ๋ฒ„ ์ฆ์„ค, ๋˜๋Š” ํด๋ผ์šฐ๋“œ ์„œ๋น„์Šค ์ด์šฉํ•˜์—ฌ ์ด์˜ ์–‘์„ ๋Š˜๋ ค ํ™•์žฅ )
    โžœ ์ƒ๋Œ€์ ์œผ๋กœ ๋น„์šฉ์ด ์ €๋ ด

  • ์ด๋ฅผ ์œ„ํ•œ ์„œ๋ฒ„๋ฅผ ์ถ”๊ฐ€์ ์œผ๋กœ ๊ตฌ์ถ•ํ•˜๋ฉด, ๋งŽ์€ ํŠธ๋ž˜ํ”ฝ์„ ๋ณด๋‹ค ํŽธ๋ฆฌํ•˜๊ฒŒ ์ฒ˜๋ฆฌ ๊ฐ€๋Šฅ


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

  • ์—ฌ๋Ÿฌ๊ฐœ์˜ ์ž‘์—…์„ ํ•˜๋‚˜๋กœ ๋ฌถ์€ ์‹คํ–‰ ์œ ๋‹›
    ( ํ•œ ํŠธ๋žœ์žญ์…˜ ์•ˆ์˜ ์—ฌ๋Ÿฌ ์ž‘์—… ์ค‘ ํ•˜๋‚˜๋ผ๋„ ์‹คํŒจํ•˜๋ฉด ์‹คํŒจํ•œ ๊ฒƒ์œผ๋กœ ํŒ๋‹จ, ๋ชจ๋‘ ์™„๋ฃŒํ•ด์•ผ ์ •์ƒ์ ์œผ๋กœ ์ข…๋ฃŒ )

Ex. ๋‚ด ๊ณ„์ขŒ์—์„œ B ๊ณ„์ขŒ๋กœ 10๋งŒ์›์„ ์ด์ฒดํ•œ๋‹ค๊ณ  ํ•  ๋•Œ, ๋‚ด ๊ณ„์ขŒ์—์„œ๋Š” 10๋งŒ์›์ด ๋น ์ ธ๋‚˜๊ฐ€๊ณ  B ๊ณ„์ขŒ์—์„œ๋Š” 10๋งŒ์›์ด ๋“ค์–ด์™€์•ผํ•จ.
์ด ๋•Œ, ๋‘ ์•ก์…˜ ๋ชจ๋‘ ์„ฑ๊ณตํ•ด์•ผ ๊ณ„์ขŒ์ด์ฒด ์„ฑ๊ณตํ•œ ๊ฒƒ

โœ” ํŠธ๋žœ์žญ์…˜์˜ ํŠน์„ฑ โžœ ACID

ํ•˜๋‚˜์˜ ํŠธ๋žœ์žญ์…˜(transaction)์˜ ์•ˆ์ „์„ฑ์„ ๋ณด์žฅํ•˜๊ธฐ ์œ„ํ•ด ํ•„์š”ํ•œ ์„ฑ์งˆ

1. Atomicity (์›์ž์„ฑ)

  • ์„ฑ๊ณตํ•  ๊ฑฐ๋ฉด ๋‹ค ์„ฑ๊ณต, ์‹คํŒจํ•  ์š”์†Œ๊ฐ€ ํ•˜๋‚˜๋ผ๋„ ์žˆ๋‹ค๋ฉด ๋ฌด์กฐ๊ฑด ์‹คํŒจ
    ( ํ•˜๋‚˜๋ผ๋„ ์‹คํŒจํ•œ๋‹ค๋ฉด ํ•˜๋‚˜์˜ ๋‹จ์œ„๋กœ ๋ฌถ์—ฌ์žˆ๋Š” ๋ชจ๋“  ์ž‘์—…์ด ์‹คํŒจํ•˜๊ฒŒ ๋งŒ๋“ค์–ด ๊ธฐ์กด ๋ฐ์ดํ„ฐ ๋ณดํ˜ธํ•จ )

    Ex. ์œ„์˜ ๊ณ„์ขŒ์ด์ฒด ์˜ˆ์‹œ์—์„œ,
    ๋‚ด ๊ณ„์ขŒ์—์„œ๋งŒ ๋ˆ์ด ๋น ์ ธ๋‚˜๊ฐ€๊ณ  B ๊ณ„์ขŒ์—๋Š” ๋ˆ์ด ๋“ค์–ด์˜ค์ง€ ์•Š๋Š”๋‹ค๋ฉด ๋‚ด ๋ˆ์€ ์ฆ๋ฐœ๋จ
    โžœ ํ•˜๋‚˜๋ผ๋„ ์‹คํŒจํ•˜๋ฉด ์‹คํŒจํ•˜๊ธฐ ์ „์˜ ์ƒํ™ฉ์œผ๋กœ ๋Œ์•„๊ฐ ( rollback )

2. Consistency (์ผ๊ด€์„ฑ)

  • ํ•˜๋‚˜์˜ ํŠธ๋žœ์žญ์…˜์—์„œ๋Š” ์ƒํƒœ๊ฐ€ ์ผ๊ด€๋˜์–ด์•ผํ•จ
    ( ํŠธ๋žœ์žญ์…˜์ด ์ผ์–ด๋‚œ ์ดํ›„์˜ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์˜ ์ œ์•ฝ์ด๋‚˜ ๊ทœ์น™์„ ๋งŒ์กฑํ•ด์•ผํ•œ๋‹ค๋Š” ์˜๋ฏธ )

    Ex. ์€ํ–‰์— ์ง€๊ธˆ๊นŒ์ง€ ๋‚ด ์ด๋ฆ„์œผ๋กœ ์˜ˆ๊ธˆ์„ ํ–ˆ๋Š”๋ฐ, ๊ฐ‘์ž๊ธฐ ๊ทธ ์ค‘ ํ•˜๋‚˜์˜ ์ด๋ฆ„์„ ๋นผ๊ฑฐ๋‚˜ ์ด๋ฆ„์„ ์—†์ด ์˜ˆ๊ธˆ์„ ํ•˜๋Š” ๊ฒƒ์€ ์•ˆ๋จ
    โžœ ์ผ๊ด€๋˜๊ฒŒ ์ด๋ฆ„์„ ๋„ฃ๊ณ  ์˜ˆ๊ธˆํ•ด์•ผํ•จ

3. Isolation (๊ฒฉ๋ฆฌ์„ฑ, ๊ณ ๋ฆฝ์„ฑ)

  • ๊ฐ๊ฐ์˜ ํŠธ๋žœ์žญ์…˜์€ ๋…๋ฆฝ์ , ์„œ๋กœ์˜ ์—ฐ์‚ฐ์„ ํ™•์ธ๋ฐ›๊ฑฐ๋‚˜ ์˜ํ–ฅ X
    โžœ ๋™์‹œ์— ์‹คํ–‰๋˜๋Š” ๊ฒƒ์ด ์—ฐ์†์œผ๋กœ ์‹คํ–‰๋œ ๊ฒƒ๊ณผ ๋™์ผํ•œ ๊ฒฐ๊ณผ๋ฅผ ๋‚˜ํƒ€๋ƒ„
    ( ์ด๋ ‡๊ฒŒ ๋™์‹œ์— ์‹คํ–‰๋˜๊ฑฐ๋‚˜ ์—ฐ์†์œผ๋กœ ์‹คํ–‰๋  ๋•Œ์˜ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์ƒํƒœ๊ฐ€ ๋™์ผํ•ด์•ผํ•จ)

    Ex. ๋‚ด ๊ณ„์ขŒ์—์„œ B ๊ณ„์ขŒ์— 10๋งŒ์›์„ ๋ณด๋‚ด๋Š” ๊ฒƒ๊ณผ C ๊ณ„์ขŒ์— 10๋งŒ์›์„ ๋ณด๋‚ด๋Š” ๊ฒƒ์€ ์„œ๋กœ ๋‹ค๋ฅธ ๊ฒƒ
    โžœ ์ด ๋‘ ์•ก์…˜์ด ์„œ๋กœ ์˜ํ–ฅ์„ ๋ฏธ์น˜๋ฉด ์•ˆ๋จ
    โžœ ๋ˆ์„ ๋™์‹œ์— ๋ณด๋‚ด๋Š” ๊ฒƒ๊ณผ B์— ๋ณด๋‚ด๊ณ  C์— ๋ณด๋‚ด๊ณ  ์ด๋ ‡๊ฒŒ ์—ฐ์†์œผ๋กœ ๋ณด๋‚ด๋Š” ๊ฒƒ๊ณผ ๋™์ผํ•œ ๊ฒฐ๊ณผ ๋‚˜์™€์•ผํ•จ

4. Durability (์ง€์†์„ฑ)

  • ํ•˜๋‚˜์˜ ์„ฑ๊ณต๋œ ํŠธ๋žœ์žญ์…˜ ๋กœ๊ทธ๊ฐ€ ๊ธฐ๋ก๋˜๊ณ  ์˜๊ตฌ์ ์œผ๋กœ ๋‚จ์Œ

    Ex1. ๋‚ด ๊ณ„์ขŒ์—์„œ B ๊ณ„์ขŒ๋กœ 10๋งŒ์› ๋ณด๋‚ด๋ ค๊ณ  ํ•  ๋•Œ,
    ์ด์ฒด๋ฅผ ํ•˜๊ณ  ์€ํ–‰์—์„œ ์„ฑ๊ณตํ–ˆ๋‹ค๋Š” ๋ฌธ์ž(๋กœ๊ทธ)๊ฐ€ ์™”๊ณ  ์ดํ›„ ์€ํ–‰ ์„œ๋ฒ„์— ์—๋Ÿฌ๊ฐ€ ๋‚ฌ๋‹ค๊ณ  ํ•จ
    โžœ ์„ฑ๊ณต์ ์œผ๋กœ ๋กœ๊ทธ๊ฐ€ ์˜จ ํ›„ ์—๋Ÿฌ๊ฐ€ ๋‚ฌ์œผ๋‹ˆ ์ด์ฒด ์„ฑ๊ณต๊ณผ ํ•จ๊ป˜ ๋กœ๊ทธ๊ฐ€ ๊ธฐ๋ก๋จ


    Ex2. ๋‚ด ๊ณ„์ขŒ์—์„œ B ๊ณ„์ขŒ๋กœ 10๋งŒ์› ๋ณด๋‚ด๋ ค๊ณ  ํ•  ๋•Œ,
    ์ด์ฒด๋ฅผ ํ–ˆ๋Š”๋ฐ ์€ํ–‰ ์„œ๋ฒ„์— ์—๋Ÿฌ๊ฐ€ ๋‚ฌ๋‹ค๊ณ  ํ•˜๊ณ  ์„ฑ๊ณตํ–ˆ๋‹ค๋Š” ๋ฌธ์ž(๋กœ๊ทธ)๋„ ์˜ค์ง€ ์•Š์Œ
    โžœ ์—๋Ÿฌ๊ฐ€ ๋‚˜์„œ ์ด์ฒด์— ์‹คํŒจํ–ˆ์œผ๋‹ˆ ์ด์ฒด ์ „์œผ๋กœ rollback๋œ ๊ฒƒ


โœ๏ธ SQL ์ฟผ๋ฆฌ๋ฌธ ๋ฌธ๋ฒ•

๋”œ๋ฆฌํŠธ - ํ…Œ์ด๋ธ”์˜ ์š”์†Œ ์‚ญ์ œ (์ด ๋ฐ‘ ์„ธ๊ฐœ ์ค‘์— ์–˜๋งŒ ๋กœ๊ทธ๊ฐ€ ๋‚จ์•„์„œ ๋ณต๊ตฌ ๊ฐ€๋Šฅ)
๋“œ๋กญ - ํ…Œ์ด๋ธ” ์ž์ฒด๋ฅผ ์‚ญ์ œ
TRUNCATE - ํ…Œ์ด๋ธ”์˜ ํ‹€์€ ๋†”๋‘๊ณ  ๋น„์šฐ๊ธฐ (์ƒ์„ฑ๋œ ์ดˆ๊ธฐ ์ƒํƒœ๋กœ ๋งŒ๋“ค๊ธฐ)
(TRUNCATE๋Š” ์ž์ฃผ ์•ˆ์จ์„œ ์™ธ์šฐ์ง€ ์•Š์•„๋„ ๋จ)

[์ฐธ๊ณ ] https://www.w3schools.com/sql/default.asp

CREATE DATABASE

๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์ƒ์„ฑ

CREATE DATABASE ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค_์ด๋ฆ„;

DROP DATABASE

๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์‚ญ์ œ
( ๋กœ๊ทธ๊ฐ€ ๋‚จ์ง€ ์•Š์•„ ๋ณต๊ตฌ ๋ถˆ๊ฐ€๋Šฅ )

DROP DATABASE ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค_์ด๋ฆ„;

CREATE TABLE

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

CREATE TABLE ํ…Œ์ด๋ธ”_์ด๋ฆ„ ( ์—ด1 ์ด๋ฆ„, ์—ด2 ์ด๋ฆ„, ... );

DROP TABLE

ํ…Œ์ด๋ธ” ์‚ญ์ œ
( ๋กœ๊ทธ๊ฐ€ ๋‚จ์ง€ ์•Š์•„ ๋ณต๊ตฌ ๋ถˆ๊ฐ€๋Šฅ )

DROP TABLE ํ…Œ์ด๋ธ”_์ด๋ฆ„;

ALTER TABLE

ํ…Œ์ด๋ธ”์—์„œ ์—ด์˜ ์š”์†Œ๋“ค(๋ฐ์ดํ„ฐ๋“ค)์„ add, delete, modify
( ๋ฐ์ดํ„ฐ ํƒ€์ž…์€ int, varchar(255), date ๋“ฑ )

ALTER TABLE ํ…Œ์ด๋ธ”_์ด๋ฆ„
ADD ์—ด_์ด๋ฆ„ ๋ฐ์ดํ„ฐ_ํƒ€์ž…;
ALTER TABLE ํ…Œ์ด๋ธ”_์ด๋ฆ„
DROP COLUMN ์—ด_์ด๋ฆ„;
ALTER TABLE table_name
MODIFY COLUMN ์—ด_์ด๋ฆ„ ๋ฐ์ดํ„ฐ_ํƒ€์ž…;

SELECT

๋ฐ์ดํ„ฐ ์…‹์— ํฌํ•จ๋  ํŠน์„ฑ์„ ํŠน์ •ํ•˜์—ฌ ์กฐํšŒ

FROM

๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์—์„œ ์–ด๋–ค ํ…Œ์ด๋ธ”์˜ ๊ฒฐ๊ณผ๋ฅผ ์กฐํšŒํ•  ์ง€

SELECT Country FROM Customers;

โžœ Customers ํ…Œ์ด๋ธ”์˜ Country ์—ด๋งŒ ์…€๋ ‰ํ•ด์ค˜
( ์ค‘๋ณต์ด ์žˆ์–ด๋„ ๊ทธ๋Œ€๋กœ ๋ณด์—ฌ์คŒ )

DISTINCT

์ค‘๋ณต ์ œ๊ฑฐ ์—ญํ• 

SELECT DISTINCT Country FROM Customers;

โžœ Customers ํ…Œ์ด๋ธ”์˜ Country ์—ด์—์„œ ์ค‘๋ณต์„ ์—†์•ค ๋‹ค ๋‹ค๋ฅธ ์—ด๋“ค์„ ์…€๋ ‰ํ•ด์ค˜
( ์ค‘๋ณต์ด ์žˆ์œผ๋ฉด ์ค‘๋ณต์„ ์—†์• ๊ณ  ๋ณด์—ฌ์คŒ )


WHERE

ํ•„ํ„ฐ ์—ญํ•  ( ์„ ํƒ์ ์œผ๋กœ ์‚ฌ์šฉ ๊ฐ€๋Šฅ )

SELECT * FROM Customers
WHERE Country='Mexico';

โžœ Customers ํ…Œ์ด๋ธ”์—์„œ Country๊ฐ€ Mexico์ธ ์‚ฌ๋žŒ๋“ค์„ ํ•œ์ •ํ•˜์—ฌ ๊ทธ ์ „์ฒด๋ฅผ ์…€๋ ‰ํ•ด์ค˜
(๋ฌธ์ž๋Š” ''๋กœ ๋ฌถ๊ณ  ์ˆซ์ž๋ฉด ๊ทธ๋ƒฅ ์ˆซ์ž๋งŒ ์”€)

WHERE ์ ˆ ์—ฐ์‚ฐ์ž

  • = - ๊ฐ™์€ ๋ฐ์ดํ„ฐ๊ฐ’ ํ•œ์ •ํ•  ๋•Œ

  • > / < / >= / <= - ๋ฐ์ดํ„ฐ๊ฐ’์„ ๋น„๊ตํ•˜์—ฌ ํ•œ์ •ํ•  ๋•Œ

  • <> / != - ์„ ํƒ๋œ ๋ฒ”์œ„ ์™ธ์˜ ๋‹ค๋ฅธ ๋ฐ์ดํ„ฐ๊ฐ’ ํ•œ์ •ํ•  ๋•Œ

SELECT * FROM Customers
WHERE Country<>'Germany';

โžœ Customers ํ…Œ์ด๋ธ”์—์„œ Country๊ฐ€ Germany๊ฐ€ ์•„๋‹Œ ๊ณ ๊ฐ ์ „์ฒด

  • BETWEEN AND - ์„ ํƒ๋œ ๋ฒ”์œ„ ์‚ฌ์ด์˜ ๋ฐ์ดํ„ฐ๊ฐ’ ํ•œ์ •ํ•  ๋•Œ
SELECT * FROM Products
WHERE Price BETWEEN 10 AND 20;

โžœ Products ํ…Œ์ด๋ธ”์—์„œ Price๊ฐ€ 10-20 ์‚ฌ์ด์ธ ์ œํ’ˆ ์ „์ฒด ์…€๋ ‰
(NOT BETWEEN ํ•˜๋ฉด ์‚ฌ์ด๊ฐ€ ์•„๋‹Œ ๊ฒƒ)

SELECT * FROM Orders
WHERE OrderDate BETWEEN '1996-07-01' AND '1996-07-31';

โžœ Orders ํ…Œ์ด๋ธ”์—์„œ OrderDate๊ฐ€ 1996-07-01 ~ 1996-07-31์ธ ์ œํ’ˆ ์„ ํƒ

  • AND - AND๋กœ ๊ตฌ๋ถ„๋œ ๋ชจ๋“  ์กฐ๊ฑด์ด true์ธ ๊ฒฝ์šฐ
SELECT * FROM Customers
WHERE Country='Germany' AND City='Berlin';

โžœ Customers ํ…Œ์ด๋ธ”์—์„œ Country๊ฐ€ Germany ์ด๊ณ  City๊ฐ€ Berlin์ธ ๊ณ ๊ฐ ์ „์ฒด

  • OR - OR๋กœ ๊ตฌ๋ถ„๋œ ์กฐ๊ฑด๋“ค ์ค‘ ํ•˜๋‚˜๋ผ๋„ true์ธ ๊ฒฝ์šฐ
SELECT * FROM Customers
WHERE Country='Germany' OR City='Berlin';

โžœ Customers ํ…Œ์ด๋ธ”์—์„œ Country๊ฐ€ Germany ์ด๊ฑฐ๋‚˜ City๊ฐ€ Berlin์ธ ๊ณ ๊ฐ ์ „์ฒด

  • NOT - ์กฐ๊ฑด์ด ๋ชจ๋‘ true๊ฐ€ ์•„๋‹Œ ๊ฒฝ์šฐ
SELECT * FROM Customers
WHERE NOT Country='Germany';

โžœ Customers ํ…Œ์ด๋ธ”์—์„œ Country๊ฐ€ Germany๊ฐ€ ์•„๋‹Œ ๊ณ ๊ฐ ์ „์ฒด

  • IS NULL / IS NOT NULL - ๊ฐ’์ด NULL์ธ ๊ฒƒ๋งŒ ํ•œ์ •ํ•˜์—ฌ ๋ฐ˜ํ™˜ํ•˜๊ณ  NULL์ด ์•„๋‹Œ ๊ฒƒ๋งŒ ํ•œ์ •ํ•˜์—ฌ ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ฒƒ

  • LIKE / IN - WHERE ์•ˆ์—์„œ๋„ ๋˜ ๊ฐ’์˜ ํŠน์ง•์œผ๋กœ ํ•œ์ •ํ•  ๋•Œ


IS NULL / IS NOT NULL

WHERE ์ ˆ ์•ˆ์—์„œ NULL ๊ฐ’ ํ…Œ์ŠคํŠธ

SELECT CustomerName, ContactName, Address
FROM Customers
WHERE Address IS NULL;

โžœ Customers ํ…Œ์ด๋ธ”์—์„œ Address ๊ฐ’์ด null์ธ ์• ๋“ค ์ค‘์— CustomerName, ContactName, Address๋งŒ ์…€๋ ‰ํ•ด์ค˜

SELECT CustomerName, ContactName, Address
FROM Customers
WHERE Address IS NOT NULL;

โžœ Customers ํ…Œ์ด๋ธ”์—์„œ Address ๊ฐ’์ด null์ด ์•„๋‹Œ ์• ๋“ค ์ค‘์— CustomerName, ContactName, Address๋งŒ ์…€๋ ‰ํ•ด์ค˜


LIKE

์™€์ผ๋“œ์นด๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ WHERE ์ ˆ ์•ˆ์—์„œ ์—ด์˜ ํŠน์ • ํŒจํ„ด์„ ์ฐพ๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ

  • LIKE 'a%' - a๋กœ ์‹œ์ž‘ํ•˜๋Š” ๋‹จ์–ด (๋‹จ์–ด๊ฐ€ ๊ทธ๋ƒฅ a์—ฌ๋„ ์ƒ๊ด€ X)

  • LIKE '%a' - a๋กœ ๋๋‚˜๋Š” ๋‹จ์–ด

  • LIKE '%a%' - ์ค‘๊ฐ„์— a๊ฐ€ ์žˆ๋Š” ๋‹จ์–ด

  • LIKE '_a%' - ๋‘๋ฒˆ์งธ ๋ฌธ์ž๊ฐ€ a์ธ ๋‹จ์–ด

  • LIKE 'a_%' - a๋กœ ์‹œ์ž‘ํ•˜์—ฌ ๊ธธ์ด๊ฐ€ ์ตœ์†Œ 2๋Š” ๋˜๋Š” ๋‹จ์–ด

  • LIKE 'a__%' - a๋กœ ์‹œ์ž‘ํ•˜์—ฌ ๊ธธ์ด๊ฐ€ ์ตœ์†Œ 3์€ ๋˜๋Š” ๋‹จ์–ด

  • LIKE 'a%b' - a๋กœ ์‹œ์ž‘ํ•˜์—ฌ b๋กœ ๋๋‚˜๋Š” ๋‹จ์–ด

SELECT * FROM Customers
WHERE CustomerName NOT LIKE 'a%';

โžœ Customers ํ…Œ์ด๋ธ”์—์„œ CustomerName์ด a๋กœ ์‹œ์ž‘ํ•˜์ง€ ์•Š๋Š” ์ „์ฒด ์…€๋ ‰


WHILDCARD

LIKE ์—ฐ์‚ฐ์ž์™€ ํ•จ๊ป˜ ์‚ฌ์šฉ๋จ

  • %, _

  • [], - - ๋‹จ์–ด ์•ˆ ๋ฌธ์ž์˜ ๋ฒ”์œ„ ํ•œ์ •

SELECT * FROM Customers
WHERE City LIKE '[acb]%';

โžœ Customers ํ…Œ์ด๋ธ”์—์„œ City๊ฐ€ a / c / b ๋กœ ์‹œ์ž‘ํ•˜๋Š” ๋ชจ๋“  ๊ณ ๊ฐ ์„ ํƒ

SELECT * FROM Customers
WHERE City LIKE '[a-c]%';

โžœ Customers ํ…Œ์ด๋ธ”์—์„œ City๊ฐ€ a ~ c๋กœ ์‹œ์ž‘ํ•˜๋Š” ๋ชจ๋“  ๊ณ ๊ฐ ์„ ํƒ

  • ^ ๋บ„ ๊ธ€์ž - ์„ ํƒ๋œ ๋ฌธ์ž ์™ธ์˜ ๋‹ค๋ฅธ ๋ฌธ์ž๊ฐ€ ์˜ค๋Š” ๋‹จ์–ด ํ•œ์ •
SELECT * FROM Customers
WHERE City LIKE '[^bsp]%';
SELECT * FROM Customers
WHERE City LIKE '[!bsp]%';
SELECT * FROM Customers
WHERE City NOT LIKE '[bsp]%';

โžœ Customers ํ…Œ์ด๋ธ”์—์„œ City๊ฐ€ b, s, p ์ค‘ ํ•˜๋‚˜๋กœ๋„ ์‹œ์ž‘ํ•˜์ง€ ์•Š๋Š” ๋ชจ๋“  ๊ณ ๊ฐ ์„ ํƒ


IN

WHERE์ ˆ๊ณผ ํ•จ๊ป˜ ์‚ฌ์šฉ๋˜์–ด ์—ฌ๋Ÿฌ OR ์กฐ๊ฑด๋“ค์„ ๋‘์–ด ๋ฒ”์œ„ ํ•œ์ •

SELECT * FROM Customers
WHERE Country IN ('Germany', 'France', 'UK');

โžœ Customers ํ…Œ์ด๋ธ”์—์„œ Country๊ฐ€ Germany, France, UK ์ค‘ ํ•˜๋‚˜์ธ ๊ณ ๊ฐ ์ „์ฒด ์…€๋ ‰

SELECT * FROM Customers
WHERE Country NOT IN ('Germany', 'France', 'UK');

โžœ Customers ํ…Œ์ด๋ธ”์—์„œ Country๊ฐ€ Germany, France, UK ์ค‘ ํ•˜๋‚˜๋ผ๋„ ์•„๋‹Œ ๊ณ ๊ฐ ์ „์ฒด ์…€๋ ‰

SELECT * FROM Customers
WHERE Country IN (SELECT Country FROM Suppliers);

โžœ Customers ํ…Œ์ด๋ธ”์—์„œ Country๊ฐ€ Customers ํ…Œ์ด๋ธ”dml Suppliers(๊ณต๊ธ‰์—…์ฒด)์™€ ๊ฐ™์€ ๊ณ ๊ฐ๋งŒ ์ „์ฒด ์…€๋ ‰


ORDER BY

๋ฐ์ดํ„ฐ๋ฅผ ๊ธฐ๋ณธ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•˜์—ฌ ์ถœ๋ ฅ

SELECT * FROM Customers
ORDER BY Country;

โžœ Customers ํ…Œ์ด๋ธ”์—์„œ Country ๊ธฐ์ค€์œผ๋กœ ์˜ค๋ฆ„์ฐจ์ˆœ

SELECT * FROM Customers
ORDER BY Country DESC;

โžœ Customers ํ…Œ์ด๋ธ”์—์„œ Country ๊ธฐ์ค€์œผ๋กœ ๋‚ด๋ฆผ์ฐจ์ˆœ

SELECT * FROM Customers
ORDER BY Country, CustomerName;

โžœ Customers ํ…Œ์ด๋ธ”์—์„œ Country, CustomerName ๊ธฐ์ค€์œผ๋กœ ์˜ค๋ฆ„์ฐจ์ˆœ
( ๊ตญ๊ฐ€๋ณ„๋กœ ์˜ค๋ฆ„์ฐจ์ˆœํ•œ ๊ฒƒ๋“ค ์•ˆ์— ๊ณ ๊ฐ ์ด๋ฆ„๋„ ์˜ค๋ฆ„์ฐจ์ˆœ ๋˜์–ด์žˆ์Œ )

SELECT * FROM Customers
ORDER BY Country ASC, CustomerName DESC;

โžœ Customers ํ…Œ์ด๋ธ”์—์„œ Country ๊ธฐ์ค€์œผ๋กœ ์˜ค๋ฆ„์ฐจ์ˆœ, CustomerName ๊ธฐ์ค€์œผ๋กœ ๋‚ด๋ฆผ์ฐจ์ˆœ


INSERT INTO

๋ฐ์ดํ„ฐ ๋„ฃ๊ธฐ

INSERT INTO Customers (CustomerName, ContactName, Address, City, Country)
VALUES ('Cardinal', 'Tom B. Erichsen', 'Stavanger', '4006', 'Norway');

โžœ Customers ํ…Œ์ด๋ธ”์—์„œ ์„ ํƒ๋œ ๊ฐ ์—ด์— values๋ฅผ ๊ฐ๊ฐ ์ถ”๊ฐ€ํ•ด์„œ ํ•˜๋‚˜์˜ ํ–‰ ์ถ”๊ฐ€


UPDATE

๋ฐ์ดํ„ฐ ์ˆ˜์ •
( UPDATEํ•  ๋•Œ WHERE์ ˆ์ด ์—†์œผ๋ฉด ์ „์ฒด์˜ ๋ฐ์ดํ„ฐ๊ฐ€ ๋ฐ”๋€Œ๊ธฐ ๋•Œ๋ฌธ์— ๊ผญ WHERE์ ˆ๋กœ ๋ณ€๊ฒฝ๋  ๋ฐ์ดํ„ฐ๋ฅผ ํ•œ์ •ํ•ด์ค˜์•ผํ•จ )

UPDATE Customers
SET ContactName = 'Alfred Schmidt', City= 'Frankfurt'
WHERE CustomerID = 1;

โžœ Customers ํ…Œ์ด๋ธ”์—์„œ ID๊ฐ€ 1์ธ ์• ๋“ค์˜ ContactName๊ณผ City๋ฅผ ๊ฐ๊ฐ Alfred Schmidt, Frankfurt๋กœ ๋ณ€๊ฒฝ


DELETE

ํ…Œ์ด๋ธ” ์•ˆ์˜ ์กด์žฌํ•˜๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์‚ญ์ œํ•  ๋•Œ ์‚ฌ์šฉ
( DELETEํ•  ๋•Œ WHERE์ ˆ์ด ์—†์œผ๋ฉด ์ „์ฒด์˜ ๋ฐ์ดํ„ฐ๊ฐ€ ์‚ญ์ œ๋˜๊ธฐ ๋•Œ๋ฌธ์— ๊ผญ WHERE์ ˆ๋กœ ์‚ญ์ œ๋  ๋ฐ์ดํ„ฐ๋ฅผ ํ•œ์ •ํ•ด์ค˜์•ผํ•จ )
( ๋กœ๊ทธ๊ฐ€ ๋‚จ์•„์„œ ๋ฐ์ดํ„ฐ ๋ณต๊ตฌ ๊ฐ€๋Šฅ )

DELETE FROM Customers
WHERE CustomerName='Alfreds Futterkiste';

โžœ Customers ํ…Œ์ด๋ธ”์—์„œ CustomerName์ด Alfreds Futterkiste์ธ ์• ๋ฅผ ์‚ญ์ œํ•ด์ค˜

DELETE FROM Customers;

โžœ Customers ํ…Œ์ด๋ธ”์—์„œ ๋ชจ๋“  ํ–‰ ์‚ญ์ œ (ํ…Œ์ด๋ธ”์€ ๋‚จ์•„์žˆ์Œ)


LIMIT

์ถœ๋ ฅํ•  ๋ฐ์ดํ„ฐ์˜ ๊ฐœ์ˆ˜ ํ•œ์ • ( ์ฟผ๋ฆฌ๋ฌธ์˜ ๊ฐ€์žฅ ๋งˆ์ง€๋ง‰์— ์ถ”๊ฐ€ )

SELECT * FROM Customers
WHERE Country='Germany'
LIMIT 3;

โžœ Customers ํ…Œ์ด๋ธ”์—์„œ Country๊ฐ€ Germany์ธ ์• ๋“ค ์ค‘ ์œ„์— 3๊ฐœ๋งŒ ์ „์ฒด ์„ ํƒ


MIN / MAX

์ตœ์†Œ๊ฐ’ / ์ตœ๋Œ€๊ฐ’

SELECT MIN(Price) AS SmallestPrice
FROM Products;

โžœ Products ํ…Œ์ด๋ธ”์—์„œ SmallestPrice๋ผ๋Š” ์ด๋ฆ„์˜ ํ–‰์œผ๋กœ Price์—ด์˜ ๋ฐ์ดํ„ฐ ์ค‘ ์ตœ์†Œ๊ฐ’ ์…€๋ ‰

SELECT MAX(Price) AS LargestPrice
FROM Products;

โžœ Products ํ…Œ์ด๋ธ”์—์„œ LargestPrice๋ผ๋Š” ์ด๋ฆ„์˜ ํ–‰์œผ๋กœ Price์—ด์˜ ๋ฐ์ดํ„ฐ ์ค‘ ์ตœ๋Œ€๊ฐ’ ์…€๋ ‰


COUNT / AVG / SUM

  • COUNT() - ( )์•ˆ ์„ ํƒ๋œ ์ˆซ์ž ์—ด์˜ ๋ฐ์ดํ„ฐ ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜
SELECT COUNT(ProductID)
FROM Products;

โžœ Products ํ…Œ์ด๋ธ”์—์„œ productID์˜ ์ˆ˜
(NULL ๊ฐ’์€ ๊ณ„์‚ฐX)

  • AVG() - ( )์•ˆ ์„ ํƒ๋œ ์ˆซ์ž ์—ด์˜ ํ‰๊ท  ๊ฐ’ ๋ฐ˜ํ™˜
SELECT AVG(Price)
FROM Products;

โžœ Products ํ…Œ์ด๋ธ”์—์„œ Price์—ด ๋ฐ์ดํ„ฐ(๊ฐ€๊ฒฉ)์˜ ํ‰๊ท 
(NULL ๊ฐ’์€ ๋ฌด์‹œ)

  • SUM() - ( )์•ˆ ์„ ํƒ๋œ ์ˆซ์ž ์—ด์˜ ์ด ํ•ฉ๊ณ„ ๋ฐ˜ํ™˜
SELECT SUM(Quantity)
FROM OrderDetails;

โžœ OrderDetails ํ…Œ์ด๋ธ”์—์„œ Quantity ์—ด์˜ ํ•ฉ๊ณ„


AS

๋ณ„์นญ ๋งŒ๋“ค๊ธฐ

SELECT CustomerID AS ID, CustomerName AS Customer
FROM Customers;

โžœ Customers ํ…Œ์ด๋ธ”์—์„œ ๊ณ ๊ฐID ์—ด์˜ ๋ณ„์นญ์€ ID๋กœ, CustomerName ์—ด์˜ ๋ณ„์นญ์€ Customer๋กœ ํ•˜์—ฌ ์…€๋ ‰

SELECT CustomerName AS Customer, ContactName AS [Contact Person]
FROM Customers;

โžœ Customers ํ…Œ์ด๋ธ”์—์„œ CustomerName ์—ด์˜ ๋ณ„์นญ์€ Customer๋กœ, ContactName ์—ด์˜ ๋ณ„์นญ์€ Contact Person์œผ๋กœ ํ•˜์—ฌ ์…€๋ ‰
( ๋ณ„์นญ ์ด๋ฆ„์— ๊ณต๋ฐฑ์ด ํฌํ•จ๋œ ๊ฒฝ์šฐ ํฐ๋”ฐ์˜ดํ‘œ ๋˜๋Š” ๋Œ€๊ด„ํ˜ธ๊ฐ€ ํ•„์š”)


JOIN

  • INNER JOIN - ๋‘ ํ…Œ์ด๋ธ”์—์„œ ์ผ์น˜ํ•˜๋Š” ๊ฐ’์„ ๊ฐ€์ง„ ๋ ˆ์ฝ”๋“œ ๋ฐ˜ํ™˜ (๊ต์ง‘ํ•ฉ)
    ( Join ์•ž์— ์•„๋ฌด๊ฒƒ๋„ ์•ˆ์“ฐ๊ณ  Join๋งŒ ์“ฐ๋ฉด ์ž๋™์œผ๋กœ INNER JOIN์œผ๋กœ ์‚ฌ์šฉ๋จ )
SELECT Orders.OrderID, Customers.CustomerName, Orders.OrderDate
FROM Orders
INNER JOIN Customers ON Orders.CustomerID=Customers.CustomerID;

โžœ Orders ํ…Œ์ด๋ธ”์—์„œ Orders ํ…Œ์ด๋ธ”์˜ CustomerID ํ•„๋“œ์™€ Customers ํ…Œ์ด๋ธ”์˜ CustomerID ํ•„๋“œ๊ฐ€ ์ผ์น˜ํ•˜๋Š” ๊ณ ๊ฐ๋“ค๋งŒ ์ถ”๋ ค์„œ Orders.OrderID, Customers.CustomerName, Orders.OrderDate ์…€๋ ‰

  • LEFT (OUTER) JOIN - ์™ผ์ชฝ ํ…Œ์ด๋ธ”์˜ ๋ชจ๋“  ๋ ˆ์ฝ”๋“œ ๋ฐ˜ํ™˜ํ•˜๊ณ  (๊ณตํ†ต๋˜์ง€ ์•Š์€ ํ–‰๋„), ์˜ค๋ฅธ์ชฝ ํ…Œ์ด๋ธ”์—์„œ๋Š” ์™ผ์ชฝ ํ…Œ์ด๋ธ”๊ณผ ๊ต์ง‘ํ•ฉ๋œ ๋ ˆ์ฝ”๋“œ๋งŒ ๋ฐ˜ํ™˜
    ( JOIN ์—ฐ์‚ฐ์ž ๊ธฐ์ค€ ์™ผ์ชฝ์ด LEFE / ์˜ค๋ฅธ์ชฝ์ด RIGHT )

  • RIGHT (OUTER) JOIN - ์˜ค๋ฅธ์ชฝ ํ…Œ์ด๋ธ”์˜ ๋ชจ๋“  ๋ ˆ์ฝ”๋“œ ๋ฐ˜ํ™˜ํ•˜๊ณ  (๊ณตํ†ต๋˜์ง€ ์•Š์€ ํ–‰๋„), ์™ผ์ชฝ ํ…Œ์ด๋ธ”์—์„œ๋Š” ์˜ค๋ฅธ์ชฝ ํ…Œ์ด๋ธ”๊ณผ ๊ต์ง‘ํ•ฉ๋œ ๋ ˆ์ฝ”๋“œ๋งŒ ๋ฐ˜ํ™˜
    ( ์‚ฌ์‹ค LEFT (OUTER) JOIN ์‚ฌ์šฉํ•  ๋•Œ, ๊ฐ ํ…Œ์ด๋ธ”์˜ ์œ„์น˜๋งŒ ๋ฐ”๊พธ๋ฉด RIGHT (OUTER) JOIN์™€ ๊ฐ™๊ธฐ ๋•Œ๋ฌธ์— ์ž˜ ์•ˆ์”€ )

  • FULL (OUTER) JOIN - ๋‘ ํ…Œ์ด๋ธ”์— ์ผ์น˜ํ•˜๋Š” ํ•ญ๋ชฉ์ด ์žˆ๋Š” ๊ฒฝ์šฐ, ๋‘ ํ…Œ์ด๋ธ”์˜ ๋ชจ๋“  ๋ ˆ์ฝ”๋“œ ๋ฐ˜ํ™˜


GROUP BY

๊ทธ๋ฃน์œผ๋กœ ๋ฌถ์–ด์„œ ๊ฐ ๊ทธ๋ฃน์˜ ๊ฒฐ๊ณผ ๋ฐ˜ํ™˜

SELECT COUNT(CustomerID), Country
FROM Customers
GROUP BY Country;

โžœ Customers ํ…Œ์ด๋ธ”์—์„œ Country ๋ณ„๋กœ CustomerId์˜ ์ˆ˜์™€ ๊ตญ๊ฐ€ ์…€๋ ‰


๐ŸŒˆ ๋Š๋‚€์ 

๋ฌธ๋ฒ• ์š”์†Œ๋“ค์ด ๋งŽ์•˜์ง€๋งŒ ๊ทธ๋ž˜๋„ ์ด๋ฒˆ ํ•™์Šต์€ ์ˆ˜์›”ํ–ˆ๋‹ค!!
ํ•˜์ง€๋งŒ ์—ญ์‹œ ์ •๋ฆฌ๊ฐ€ ์˜ค๋ž˜ ๊ฑธ๋ฆฐ๋‹ค ใ… 

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