SQL null

greenTeaยท2023๋…„ 6์›” 14์ผ
0

NULL ๊ฐ’ ์‚ฌ์šฉ๋ฒ•

  1. ๐ŸงNULL ๊ฐ’ ๋น„๊ต: NULL ๊ฐ’์€ ๋‹ค๋ฅธ ๊ฐ’ํ•˜๊ณ ๋Š” ๋‹ค๋ฅธ ํŠน์ง•์„ ์ง€๋‹ˆ๊ณ  ์žˆ๊ธฐ์— ๋‹ค๋ฅธ ๊ฐ’๊ณผ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ๋‹ค๋ฆ…๋‹ˆ๋‹ค. NULL๋น„๊ต๋Š” ์•„๋ž˜์™€ ๊ฐ™์€ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
  • = ์—ฐ์‚ฐ์ž ๋Œ€์‹  IS NULL์„ ์‚ฌ์šฉํ•˜์—ฌ NULL ๊ฐ’์„ ํ™•์ธ
  • <> ์—ฐ์‚ฐ์ž ๋Œ€์‹  IS NOT NULL์„ ์‚ฌ์šฉํ•˜์—ฌ NULL ๊ฐ’์ด ์•„๋‹Œ์ง€ ํ™•์ธ

IS NULL

SELECT * FROM table_name WHERE column_name IS NULL;
SELECT * FROM table_name WHERE column_name IS NOT NULL;
  1. ๐ŸฅณNULL ๊ฐ’ ์ฒ˜๋ฆฌ: NULL ๊ฐ’์„ ๋‹ค๋ฃฐ ๋•Œ ์ฃผ์˜ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
  • NULL ๊ฐ’์„ ๋ฌด์‹œํ•˜๊ณ ์ž ํ•  ๋•Œ๋Š” COALESCE ํ•จ์ˆ˜๋‚˜ ISNULL ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
  • NULL ๊ฐ’์„ ๋‹ค๋ฅธ ๊ฐ’์œผ๋กœ ๋Œ€์ฒดํ•˜๊ณ ์ž ํ•  ๋•Œ๋Š” IFNULL ํ•จ์ˆ˜๋‚˜ CASE ๋ฌธ์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

COALESCE , IFNULL, CASE

SELECT COALESCE(column_name, 'N/A') FROM table_name;
SELECT IFNULL(column_name, 'N/A') FROM table_name;
SELECT CASE WHEN column_name IS NULL THEN 'N/A' ELSE column_name END FROM table_name;

์ฃผ์˜์‚ฌํ•ญ

  1. NULL ๊ฐ’๊ณผ์˜ ๋น„๊ต ๊ฒฐ๊ณผ๋Š” ํ•ญ์ƒ UNKNOWN์ž…๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ WHERE ์ ˆ์—์„œ NULL ๊ฐ’๊ณผ ๋น„๊ตํ•˜๋Š” ๊ฒฝ์šฐ ์˜ˆ์ƒ์น˜ ๋ชปํ•œ ๊ฒฐ๊ณผ๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

  2. NULL = NULL๊ณผ ๊ฐ™์€ ๋น„๊ต๋Š” ํ•ญ์ƒ UNKNOWN๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋ฏ€๋กœ NULL ๊ฐ’์„ ํ™•์ธํ•˜๋ ค๋ฉด IS NULL์„ ์‚ฌ์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

  3. NULL ๊ฐ’๊ณผ์˜ ์‚ฐ์ˆ  ์—ฐ์‚ฐ ๋˜๋Š” ๋ฌธ์ž์—ด ์—ฐ๊ฒฐ์€ ๊ฒฐ๊ณผ๊ฐ€ NULL์ด ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

profile
greenTea์ž…๋‹ˆ๋‹ค.

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