database, query

mysql table / column(type) 변경

디츠 2014. 10. 8. 10:04

◆ 테이블 이름 변경

   mysql> alter table 현재_테이블_이름 rename 새로운_테이블_이름;

mysql> show tables;

+----------------+

| Tables_in_oops |

+----------------+

| old_table        | 

+----------------+

1 row in set (0.00 sec)

 

mysql> 

mysql> alter table old_table rename new_table;

Query OK, 0 rows affected (0.00 sec)

 

mysql> 

mysql> show tables;

+----------------+

| Tables_in_oops |

+----------------+

| new_table       | 

+----------------+

1 row in set (0.00 sec)

 

mysql>

◆ 테이블 컬럼 이름 변경

   mysql> alter table 테이블_이름 change 현재_컬럼_이름 새로운_컬럼_이름 타입;

mysql> desc new_table;

+-------+--------------+------+-----+---------+-------+

| Field   | Type             | Null   | Key | Default    | Extra |

+-------+--------------+------+-----+---------+-------+

| name | varchar(50)    | YES  |       | NULL      |         | 

| descr | varchar(128)   | YES  |       | NULL      |         | 

+-------+--------------+------+-----+---------+-------+

2 rows in set (0.00 sec)


mysql> alter table new_table change name title varchar(16);

Query OK, 0 rows affected (0.00 sec)

Records: 0  Duplicates: 0  Warnings: 0

 

mysql> desc new_table;

+-------+--------------+------+-----+---------+-------+

| Field   | Type             | Null   | Key  | Default  | Extra   |

+-------+--------------+------+-----+---------+-------+

| title    | varchar(16)   | YES   |       | NULL     |          | 

| descr  | varchar(128) | YES   |       | NULL     |          | 

+-------+--------------+------+-----+---------+-------+

2 rows in set (0.00 sec)

 

mysql>

◆ 테이블 컬럼 타입 변경

   mysql> alter table 테이블_이름 modify 컬럼명 변경_타입;

mysql> desc new_table;

+-------+--------------+------+-----+---------+-------+

| Field   | Type             | Null   | Key | Default   | Extra   |

+-------+--------------+------+-----+---------+-------+

| title    | varchar(16)  | YES  |       | NULL     |          | 

| descr | varchar(128)   | YES  |       | NULL     |          | 

+-------+--------------+------+-----+---------+-------+

2 rows in set (0.00 sec)

 

mysql> 

mysql> alter table new_table modify title varchar(50);

Query OK, 0 rows affected (0.00 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> 

출처 : http://iamoops.tistory.com/277