[MySQL] ALTER / VIEW / IF NULL
28 Nov 2021 -
1 minute read
ALTER - ํ ์ด๋ธ ๊ด๋ฆฌ
-
ํ ์ด๋ธ ์ด๋ฆ ๋ณ๊ฒฝ
ALTER TABLE ๊ธฐ์กดํ ์ด๋ธ๋ช RENAME ๋ฐ๊ฟํ ์ด๋ธ๋ช ;
-
ํ ์ด๋ธ ์ปฌ๋ผ๋ช ๋ณ๊ฒฝ
ALTER TABLE ํ ์ด๋ธ๋ช CHANGE COLUMN ๊ธฐ์กด์ปฌ๋ผ๋ช ๋ฐ๊ฟ์ปฌ๋ผ๋ช ๋ฐ์ดํฐํ์ ;
-
ํ ์ด๋ธ ์ปฌ๋ผ ์ถ๊ฐ
ALTER TABLE ํ ์ด๋ธ๋ช ADD COLUMN ์ถ๊ฐํ ์ปฌ๋ผ๋ช ํ ์ดํฐํ์ [constraint];
-
ํ ์ด๋ธ ์ปฌ๋ผ ์ญ์
ALTER TABLE ํ ์ด๋ธ๋ช DROP COLUMN ์ญ์ ํ ์ปฌ๋ผ๋ช ;
- ์ฐธ์กฐ ๊ด๊ณ๊ฐ ์ค์ ๋ ์ปฌ๋ผ ์ญ์ : ON DELETE CASCADE
-
ํ ์ด๋ธ ์ปฌ๋ผ ์๋ฃํ ๋ณ๊ฒฝ
ALTER TABLE ํ ์ด๋ธ๋ช CHANGE COLUMN ์ปฌ๋ผ๋ช ์ปฌ๋ผ๋ช ๋ณ๊ฒฝํ ๋ฐ์ดํฐํ์ ; ALTER TABLE ํ ์ด๋ธ๋ช MODIFY ์ปฌ๋ผ๋ช ๋ณ๊ฒฝํ ๋ฐ์ดํฐํ์ ;
-
ํ ์ด๋ธ ์ปฌ๋ผ ์ฌ์ด์ฆ ๋ณ๊ฒฝ
ALTER TABLE table_name MODIFY column_name ๋ฐ์ดํฐํ์ (์ฌ์ด์ฆ);
-
์ ์ฝ์กฐ๊ฑด ์ถ๊ฐ
-
PK, UNIQUE
ALTER TABLE ํ ์ด๋ธ๋ช ADD CONSTRAINT ์ถ๊ฐํ ์ ์ฝ์กฐ๊ฑด(์ปฌ๋ผ๋ช );
-
FK
ALTER TABLE FK์ถ๊ฐํ ํ ์ด๋ธ๋ช ADD FOREIGN KEY(์ปฌ๋ผ๋ช ) REFERENCE ์ฐธ์กฐํ ํ ์ด๋ธ๋ช (์ปฌ๋ผ๋ช );
-
-
์ ์ฝ์กฐ๊ฑด ์ญ์
-
PK, UNIQUE
ALTER TABLE ํ ์ด๋ธ๋ช DROP CONSTRAINT ์ญ์ ํ ์ ์ฝ์กฐ๊ฑด์ด์๋์ปฌ๋ผ๋ช ;
-
FK
ALTER TABLE ํ ์ด๋ธ๋ช DROP FOREIGN KEY ์ญ์ ํ ์ ์ฝ์กฐ๊ฑด์ด์๋์ปฌ๋ผ๋ช ;
-
VIEW
-
SELECT๋ฌธ์ ๋ณ๋์ ๋ณ์นญ์ผ๋ก ๋ณด๊ดํด๋๊ณ ๋ณ์นญ์ ํ ์ด๋ธ์ฒ๋ผ ์ฌ์ฉํ๋ ํ๋ก๊ทธ๋จ ๋ชจ๋, ๊ฐ์์ ํ ์ด๋ธ
-
UPDATE, DELETE, INSERT ๋ถ๊ฐ๋ฅ, SELECT๋ง ๊ฐ๋ฅ
-
๋ทฐ ์์ฑ
CREATE VIEW view_name AS( SELECT์ฟผ๋ฆฌ );
-
๋ทฐ ์คํ
SELECT * FROM view_name;
-
๋ทฐ ์ญ์
DROP VIEW view_name;
-
ex)
create table member( num int unique auto_increment, name char(10), age int, addr char(20) ); insert into member (name, age, addr) values ('A', 25, '์ ์ฌ๋'), ('B', 34, '์ฐํฌ๋'), ('C', 32, '์์ด๋'), ('D', 28, 'ํ๊ธฐ๋'), ('E', 34, 'ํฉ์ ๋'); create view maxage_view as ( select name, age, addr from member where age in ( select max(age) from member ) order by name ); select * from maxage_view;
IF NULL
-
ํด๋น ์ปฌ๋ผ ๊ฐ์ด NULL์ ๋ฐํํ ๋ ๋ค๋ฅธ ๊ฐ์ผ๋ก ์ถ๋ ฅํ ์ ์๋๋ก ํ๋ ํจ์
-
SELECT IFNULL(column_name, NULL์ผ๊ฒฝ์ฐ๋์ฒด๊ฐ) FROM table_name; SELECT *, IFNULL(column_name, NULL์ผ๊ฒฝ์ฐ๋์ฒด๊ฐ) FROM table_name;
-
ex)
create table score( subject char(10), score int ); insert into score (subject, score) values ('๊ตญ์ด', 89), ('์์ด', 93), ('์ํ', null), ('ํ๊ตญ์ฌ', 97); select subject as ๊ณผ๋ชฉ, ifnull(score, '๋ฏธ์์') as ์ ์ from score;