CREATE DATABASE zerobase DEFAULT CHARACTER SET utf8mb4;
⇊
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sakila |
| sys |
| testdb |
| world |
| zerobase |
+--------------------+
8 rows in set (0.00 sec)
ㄴDEFAULT CHARACTER SET:기본자료형 , utf8mb4:다국어,이모지 지원
CREATE TABLE tablename(
columnname datatype,
columnname datatype,
...
)
mysql> use testdb
Database changed
mysql> CREATE TABLE mytable
-> (
-> id int,
-> name varchar(16)
-> );
Query OK, 0 rows affected (0.02 sec)
SHOW TABLES;
⇊
+------------------+
| Tables_in_testdb |
+------------------+
| mytable |
+------------------+
DESC tablename;
mysql> DESC mytable;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int | YES | | NULL | |
| name | varchar(16) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
ALTER TABLE tablenameRENAME new_tablenameALTER TABLE mytable RENAME person;
⇊
mysql> show tables;
+------------------+
| Tables_in_testdb |
+------------------+
| person |
+------------------+
table column 추가 문법
ALTER TABLE table name
ADD COLUMN columnname datatype;
ALTER TABLE person ADD COLUMN agee double;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int | YES | | NULL | |
| name | varchar(16) | YES | | NULL | |
| agee | double | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+table column 변경 문법-datatype
ALTER TABLE tablename
MODIFY COLUMN columnname datatype;
mysql> ALTER TABLE person MODIFY COLUMN agee 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 | |
| agee | int | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
table column 변경 문법-name
ALTER TABLE tablename
CHANGE COLUMN old_columnname new_dolumnname new_datatype;
mysql> ALTER TABLE person CHANGE COLUMN agee age int;
Query OK, 0 rows affected (0.01 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 | |
+-------+-------------+------+-----+---------+-------+
Table column 삭제 문법
ALTER TABLE tablename
DROP COLUMN columnname;
mysql> ALTER TABLE person DROP COLUMN age;
Query OK, 0 rows affected (0.01 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 | |
+-------+-------------+------+-----+---------+-------+
table 삭제 문법
DROP TABLE tablename
person 테이블 삭제
mysql> DROP TABLE person;
Query OK, 0 rows affected (0.01 sec)
mysql> show tables;
Empty set (0.00 sec)