๐ŸŒˆ [Section2] 12. SQL ์‹ค์Šต

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

bootcamp

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

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

  • SQL ์‹ค์Šต

๐Ÿ˜œ SQL ์‹ค์Šต

SQL ํ€ด์ฆˆ๋„ ํ’€๊ณ  โฌ‡๏ธ

SQL ์ฟผ๋ฆฌ๋ฌธ์˜ ๋นˆ์นธ์„ ์ฑ„์›Œ๋„ฃ๋Š” ์—ฐ์Šต๋„ ํ•ด๋ณด๊ณ  โฌ‡๏ธ

MY SQL์„ ์„ค์น˜ํ•˜์—ฌ ๊ทธ ์•ˆ์—์„œ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค, ํ…Œ์ด๋ธ” ๋“ฑ์„ ๋งŒ๋“ค์–ด ์ง์ ‘ ๋ˆˆ์œผ๋กœ ํ™•์ธ๋„ ํ•ด๋ณด์•˜๋‹ค! โฌ‡๏ธ

  1. CREATE DATABASE Practice CHARACTER SET UTF8; ๋กœ Practice๋ผ๋Š” ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ฅผ ์ƒ์„ฑํ•˜๊ณ 

  2. SHOW DATABASE; ๋กœ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๊ฐ€ ์ƒ์„ฑ์ด ๋˜์—ˆ๋Š”์ง€ ํ™•์ธํ•˜์˜€๋‹ค.

ํ™•์ธ ํ›„, ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๊ฐ€ ์—ฌ๋Ÿฌ๊ฐœ ์žˆ์–ด์„œ ๋‚ด๊ฐ€ ์ƒ์„ฑํ•œ Practice ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์—์„œ ์—ฌ๋Ÿฌ ์ฟผ๋ฆฌ๋ฌธ์„ ์‹คํ–‰ํ•˜๋ ค๋ฉด ๊ทธ๊ฒƒ์„ ์‚ฌ์šฉํ•ด์•ผํ•˜๊ธฐ ๋•Œ๋ฌธ์—

  1. use Practice; ๋กœ ๊ทธ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋กœ ์ ‘๊ทผํ•˜์˜€๋‹ค.

  2. ๊ทธ๋ฆฌ๊ณ  SHOW TABLES; ์„ ํ•˜๋ฉด ์•„์ง ํ…Œ์ด๋ธ”์„ ์ƒ์„ฑํ•˜์ง€ ์•Š์•˜๊ธฐ ๋•Œ๋ฌธ์— Empty set์ด๋ผ๋Š” ๋ง์ด ๋‚˜์˜จ๋‹ค.

CREATE TABLE user (
    -> id int PRIMARY KEY AUTO_INCREMENT,
    -> name varchar(255),
    -> email varchar(255)
    -> );`

๋กœ user๋ผ๋Š” ํ…Œ์ด๋ธ”์„ ์ƒ์„ฑํ•˜์—ฌ ๊ทธ ์•ˆ์— ๊ฐ ํƒ€์ž…๋“ค์˜ ์—ด์„ ์ƒ์„ฑํ•˜์˜€๋‹ค.

( ์—ฌ๊ธฐ์„œ AUTO_INCREMENT โžœ ๋ฐ์ดํ„ฐ๊ฐ€ ์‚ฝ์ž… ๋  ๋•Œ๋งˆ๋‹ค PK๋ฅผ 1์”ฉ ์ฆ๊ฐ€ํ•ด์ฃผ๋Š” ์—ญํ•  )

  1. SHOW TABLES; ํ•˜๋ฉด ์–ด๋–ค ์ด๋ฆ„์˜ ํ…Œ์ด๋ธ”์ด ์ƒ์„ฑ๋˜์—ˆ๋Š”์ง€ ๋ณผ ์ˆ˜ ์žˆ๊ณ 

  2. desc user; ํ•˜๋ฉด ํ•ด๋‹น user ํ…Œ์ด๋ธ” ์ „์ฒด๋ฅผ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

( ์—ฌ๊ธฐ์„œ desc โžœ describe )

ํ…Œ์ด๋ธ”์˜ ์—ด์„ ์ƒ์„ฑํ–ˆ์œผ๋‹ˆ,

  1. INSERT INTO user(name, email) values ("hyunju", "wnguswn7/naver.com"); ๋กœ ๊ฐ ์—ด์— ๊ฐ’์„ ๋„ฃ์–ด ๋ณด์•˜๋‹ค.
    ( ์ด๋ฉ”์ผ์€ @๊ฐ€ ๋˜์ง€ ์•Š์•„ /๋กœ ๋Œ€์ฒด ํ–ˆ๋‹ค! )

  2. ์ดํ›„, DESC user;๋กœ user ํ…Œ์ด๋ธ” ์ „์ฒด๋ฅผ ์‚ดํŽด๋ณด๋ฉด ๊ฐ ๊ฐ’์ด ์ž˜ ๋“ค์–ด๊ฐ„ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

์ด์ œ ํ…Œ์ด๋ธ”์„ ์™„์„ฑํ–ˆ์œผ๋‹ˆ, ๋ฐฐ์šด ์ฟผ๋ฆฌ๋ฌธ์„ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ’์„ ์กฐํšŒํ•  ์ˆ˜ ์žˆ๋‹ค!! ๐Ÿ‘

  1. ์ผ๋‹จ SELECT * FROM user; ๋กœ user ํ…Œ์ด๋ธ”์˜ ์ „์ฒด๋ฅผ ์กฐํšŒํ•ด ๋ณด์•˜๊ณ ,

  2. SELECT email FROM user; ๋กœ user ํ…Œ์ด๋ธ”์˜ email ํ•„๋“œ๋งŒ ์„ ํƒํ•˜์—ฌ ์กฐํšŒํ•ด ๋ณด์•˜๋‹ค!

  1. ๊ทธ๋ฆฌ๊ณ  ์ด ํ…Œ์ด๋ธ”์—์„œ gender์˜ Null ๊ฐ’์„ Not Null๋กœ ๋ฐ”๊พธ๊ณ ์‹ถ์–ด์„œ

ALTER TABLE user MODIFY COLUMN gender varchar(255) NOT NULL; ์ด๋ ‡๊ฒŒ ์ˆ˜์ •ํ•˜๋ ค๊ณ  ํ–ˆ๋Š”๋ฐ

์ž๊พธ Invalid use of NULL value ๋ผ๋Š” ์—๋Ÿฌ๊ฐ€ ๋–ด๋‹ค.

์ด ๋ฌธ์ œ์˜ ์›์ธ์€ gender column์ด ํ˜„์žฌ null ๊ฐ’์„ ๊ฐ€์ง€๊ณ  ์žˆ์–ด ๋ฐ”๋กœ not null๋กœ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์—†๋˜ ๊ฒƒ !

13-1. ๊ทธ๋ž˜์„œ update user set gender='male' where gender is null; ๋กœ ์ € null์„ ์ž„์˜์˜ ๊ฐ’ male๋กœ ๋ณ€๊ฒฝํ•ด์ฃผ๊ณ 

13-2. ๋‹ค์‹œ ALTER TABLE user MODIFY COLUMN gender varchar(255) NOT NULL; ๋กœ ๊ฐ’์„ ๋ณ€๊ฒฝํ•ด์ฃผ์—ˆ๋‹ค.

  1. ๊ทธ๋ฆฌ๊ณ  desc user; ๋กœ user ํ…Œ์ด๋ธ”์„ ๋ณด๋‹ˆ ๋ณ€๊ฒฝ๋œ ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค!

[์ฐธ๊ณ ] https://stackoverflow.com/questions/22971586/mysql-alter-table-causes-error-invalid-use-of-null-value/22971654


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

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

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