站內搜尋

Wednesday, February 13, 2013

[MYSQL] 修改資料表結構 "ALTER"


一、新增欄位(ADD),並設定PRIMARY KEY

ALTER TABLE mytable ADD COLUMN id INT NOT NULL AUTO_INCREMENT FIRST,ADD PRIMARY KEY(id)f

如果要指定新增欄位的位置

ALTER TABLE mytable ADD COLUMN phone VARCHAR(10) AFTER id

註:AFTER可換成LAST或BEFORE,還有其他SECODE等

二、修改欄位(CHANGE)

ALTER TABLE mytable CHANGE COLUMN id new_id INT NOT NULL AUTO_INCREMENT,ADD PRIMARY KEY('naw_id')

註:連續CHANGE則為

ALTER TABLE mytable CHANGE COLUMN name new_name VARCHAR(100),CHANGE COLUMN email new_email VARCHAR(30)

三、修改資料型別(MODIFY)

ALTER TABLE mytable MODIFY COLUMN phone VARCHAR(120)

四、刪除資料欄(DROP)

ALTER TABLE mytable DROP COLUMN phone

No comments:

Post a Comment