티스토리 뷰

개발/DB

[mysql] 컬럼 추가/삭제

Jaeyeon Baek 2013. 3. 21. 16:30


컬럼 추가

alter table 테이블_이름 add 컬럼_이름 타입 ;

mysql> desc new_table;
+-------+--------------+------+-----+---------+-------+
| Field | Type         | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| title | varchar(50)  | YES  |     | NULL    |       | 
| descr | varchar(128) | YES  |     | NULL    |       | 
+-------+--------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

mysql> 
mysql> 
mysql> alter table new_table add num int(4) not null default 0;
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc new_table;
+-------+--------------+------+-----+---------+-------+
| Field | Type         | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| title | varchar(50)  | YES  |     | NULL    |       | 
| descr | varchar(128) | YES  |     | NULL    |       | 
| num   | int(4)       | NO   |     | 0       |       | 
+-------+--------------+------+-----+---------+-------+
3 rows in set (0.00 sec)


컬럼 삭제

mysql> desc new_table;
+-------+--------------+------+-----+---------+-------+
| Field | Type         | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| title | varchar(50)  | YES  |     | NULL    |       | 
| descr | varchar(128) | YES  |     | NULL    |       | 
| num   | int(4)       | NO   |     | 0       |       | 
+-------+--------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

mysql> 
mysql> alter table new_table drop num;
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc new_table;
+-------+--------------+------+-----+---------+-------+
| Field | Type         | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| title | varchar(50)  | YES  |     | NULL    |       | 
| descr | varchar(128) | YES  |     | NULL    |       | 
+-------+--------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

mysql>


댓글
최근에 올라온 글
최근에 달린 댓글
글 보관함
Total
Today
Yesterday