SQL - [DDL | CREATE, ALTER, DROP]

์†”๋น„ยท2024๋…„ 1์›” 15์ผ
0

์‹ค์Šตํ™˜๊ฒฝ๊ตฌ์ถ•


CREATE DATABASE zerobase DEFAULT CHARACTER SET utf8mb4;
# DEFAULT CHARACTER SET utf8mb4;
# UTF8MB4๋ผ๋Š” ์ธ์ฝ”๋”ฉ์“ฐ๊ฒ ๋‹ค๋Š”๊ฒƒ

Query OK, 1 row affected (0.02 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| zerobase           |
+--------------------+
5 rows in set (0.00 sec)

use zerobase;
Database changed



Table



DDL (๋ฐ์ดํ„ฐ ์ •์˜์–ด)


๋ฐ์ดํ„ฐ ๊ตฌ์กฐ๋ฅผ ์ •์˜ํ•˜๋Š”๋ฐ ์‚ฌ์šฉ๋˜๋Š” ๋ช…๋ น์–ด
๊ตฌ์กฐ๋ฅผ ์ƒ์„ฑ(CREATE)ํ•˜๊ฑฐ๋‚˜ ์‚ญ์ œ(DROP), ์ด๋ฆ„์„ ๋ณ€๊ฒฝ(ALTER)ํ•œ๋‹ค.


๐Ÿงท ํ…Œ์ด๋ธ” ์ƒ์„ฑ

CREATE TABLE table_name (
    column1 ๋ฐ์ดํ„ฐ์œ ํ˜•,
    column2 ๋ฐ์ดํ„ฐ์œ ํ˜•);

๐Ÿงท ํ…Œ์ด๋ธ” ํ™•์ธ

  SHOW TABLES;

๐Ÿงท ํ…Œ์ด๋ธ” ์ •๋ณดํ™•์ธ

  DESC table_name;
# mytable ์ƒ์„ฑ
 create table mytable
    -> ( id int, name varchar(16) );
    #ID(์ •์ˆ˜ํ˜•)์™€ NAME(๊ฐ€๋ณ€๋ฌธ์žํ˜•) ์นผ๋Ÿผ์„ ๊ฐ€์ง„ TABLE์„ ์ƒ์„ฑ
Query OK, 0 rows affected (0.06 sec)

# ํ…Œ์ด๋ธ”ํ™•์ธ
mysql> show tables;
+--------------------+
| Tables_in_zerobase |
+--------------------+
| mytable            |
+--------------------+
1 row in set (0.02 sec)

# mytable ์ •๋ณดํ™•์ธ
mysql> desc mytable;
ERROR 1146 (42S02): Table 'zerobase.mytables' doesn't exist
mysql>  desc mytable;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | int         | YES  |     | NULL    |       |
| name  | varchar(16) | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

๐Ÿงท ํ…Œ์ด๋ธ” ์‚ญ์ œ

  DROP table_name;

๐Ÿงท ํ…Œ์ด๋ธ”๋ช… ๋ณ€๊ฒฝ

  ALTER TABLE table_name
  RENAME new_table_name

๐Ÿงท ํ…Œ์ด๋ธ” ์ปฌ๋Ÿผ ์ถ”๊ฐ€

  ALTER TABLE table_name
  ADD COLUMN column type ;

๐Ÿงท ํ…Œ์ด๋ธ” ์ปฌ๋Ÿผ ์ •๋ณด ์ˆ˜์ •

  ALTER TABLE table_name
  MODIFY COLUMN column new_type ;

๐Ÿงท ํ…Œ์ด๋ธ” ์ปฌ๋Ÿผ ์ด๋ฆ„ ์ˆ˜์ •

  ALTER TABLE table_name
  CHANGE COLUMN old_column new_column new_type ;

๐Ÿงท ํ…Œ์ด๋ธ” ์ปฌ๋Ÿผ ์‚ญ์ œ

  ALTER TABLE table_name
  DROP COLUMN column ;
  • ํ…Œ์ด๋ธ”๋ช… mytable -> person ์œผ๋กœ ๋ณ€๊ฒฝ
ALTER TABLE mytable RENAME person;
Query OK, 0 rows affected (0.04 sec)

mysql> show tables;
+--------------------+
| Tables_in_zerobase |
+--------------------+
| person             |
+--------------------+
1 row in set (0.00 sec)

mysql> desc person;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | int         | YES  |     | NULL    |       |
| name  | varchar(16) | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
  • ํ…Œ์ด๋ธ” agee ์ปฌ๋Ÿผ ์ถ”๊ฐ€
ALTER TABLE person ADD COLUMN agee double;
Query OK, 0 rows affected (0.03 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc person;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | int         | YES  |     | NULL    |       |
| name  | varchar(16) | YES  |     | NULL    |       |
| agee  | double      | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
  • ํ…Œ์ด๋ธ” ์ •๋ณด (ํƒ€์ž… double -> int) ์ˆ˜์ •
ALTER TABLE person MODIFY COLUMN agee int;
Query OK, 0 rows affected (0.08 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc person;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | int         | YES  |     | NULL    |       |
| name  | varchar(16) | YES  |     | NULL    |       |
| agee  | int         | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
  • ํ…Œ์ด๋ธ” ์ปฌ๋Ÿผ๋ช… agee -> age๋กœ ๋ณ€๊ฒฝ
ALTER TABLE person CHANGE COLUMN agee age int;
Query OK, 0 rows affected (0.03 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc person
    -> ;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | int         | YES  |     | NULL    |       |
| name  | varchar(16) | YES  |     | NULL    |       |
| age   | int         | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
  • ํ…Œ์ด๋ธ” age ์ปฌ๋Ÿผ ์‚ญ์ œ
ALTER TABLE person DROP COLUMN age;
Query OK, 0 rows affected (0.03 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc person
    -> ;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | int         | YES  |     | NULL    |       |
| name  | varchar(16) | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
  • person ํ…Œ์ด๋ธ” ์‚ญ์ œ
 DROP TABLE person;
Query OK, 0 rows affected (0.03 sec)

mysql> show tables;
Empty set (0.00 sec)

Daily Study Note

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