MySQL 테이블 컬럼 추가

(MySQL 컬럼 추가에서 넘어옴)
MySQL 컬럼 추가
MySQL 컬럼 생성

1 방법 1: 맨 뒤에 추가[ | ]

ALTER TABLE `테이블명` ADD `컬럼명` 자료형
mysql> DESC employee;
+---------------+-------------+------+-----+---------+-------+
| Field         | Type        | Null | Key | Default | Extra |
+---------------+-------------+------+-----+---------+-------+
| id            | int(11)     | NO   | PRI | NULL    |       |
| name          | varchar(16) | NO   |     | NULL    |       |
| department_id | int(11)     | NO   |     | NULL    |       |
+---------------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

mysql> ALTER TABLE `employee` ADD `comments` VARCHAR(200) NOT NULL;
Query OK, 4 rows affected (0.41 sec)
Records: 4  Duplicates: 0  Warnings: 0

mysql> DESC employee;
+---------------+--------------+------+-----+---------+-------+
| Field         | Type         | Null | Key | Default | Extra |
+---------------+--------------+------+-----+---------+-------+
| id            | int(11)      | NO   | PRI | NULL    |       |
| name          | varchar(16)  | NO   |     | NULL    |       |
| department_id | int(11)      | NO   |     | NULL    |       |
| comments      | varchar(200) | NO   |     | NULL    |       |
+---------------+--------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

2 방법 2: 맨 앞에 추가[ | ]

ALTER TABLE `테이블명` ADD `새컬럼명` 자료형 FIRST

3 방법 3: 지정 컬럼 다음에 추가 ★[ | ]

ALTER TABLE `테이블명` ADD `새컬럼명` 자료형 AFTER `앞컬럼명`
mysql> DESC employee;
+---------------+-------------+------+-----+---------+-------+
| Field         | Type        | Null | Key | Default | Extra |
+---------------+-------------+------+-----+---------+-------+
| id            | int(11)     | NO   | PRI | NULL    |       |
| name          | varchar(16) | NO   |     | NULL    |       |
| department_id | int(11)     | NO   |     | NULL    |       |
+---------------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

mysql> ALTER TABLE `employee` ADD `comments` VARCHAR(200) NOT NULL AFTER `name`;
Query OK, 4 rows affected (0.10 sec)
Records: 4  Duplicates: 0  Warnings: 0

mysql> DESC employee;
+---------------+--------------+------+-----+---------+-------+
| Field         | Type         | Null | Key | Default | Extra |
+---------------+--------------+------+-----+---------+-------+
| id            | int(11)      | NO   | PRI | NULL    |       |
| name          | varchar(16)  | NO   |     | NULL    |       |
| comments      | varchar(200) | NO   |     | NULL    |       |
| department_id | int(11)      | NO   |     | NULL    |       |
+---------------+--------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
→ 새 컬럼 comments가 name 컬럼의 바로 다음에 생성되었다.

4 같이 보기[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}