09/10: MySQL > Altering Tables
Category: Study
Posted by: hajime osako
Add, delete and modify a column:
ALTER TABLE my_table ADD new_date_column DATE NOT NULL [FIRST | AFTER existing_column];
ALTER TABLE my_table DROP dropping_column;
ALTER TABLE my_table MODIFY modifying_column DATETIME NOT NULL;
ALTER TABLE my_table CHANGE original_column new_name CHAR(40) NOT NULL;
ALTER TABLE my_table CHANGE original_column new_name CHAR(40) NOT NULL AUTO_INCREMENT, ADD PRIMARY KEY(new_name);
* You should specify the all column attributes when you use MODIFY. If you want to disallow NULL in the column, the column definition provided for MODIFY must include the NOT NULL attribute.
Renaming a table:
ALTER TABLE my_table RENAME TO my_renamed_table;
RENAME TABLE my_table1 TO tmp, my_table2 TO my_table1, tmp TO my_table2;
* For TEMPORARY tables, RENAME TABLE does not work.
ALTER TABLE my_table ADD new_date_column DATE NOT NULL [FIRST | AFTER existing_column];
ALTER TABLE my_table DROP dropping_column;
ALTER TABLE my_table MODIFY modifying_column DATETIME NOT NULL;
ALTER TABLE my_table CHANGE original_column new_name CHAR(40) NOT NULL;
ALTER TABLE my_table CHANGE original_column new_name CHAR(40) NOT NULL AUTO_INCREMENT, ADD PRIMARY KEY(new_name);
* You should specify the all column attributes when you use MODIFY. If you want to disallow NULL in the column, the column definition provided for MODIFY must include the NOT NULL attribute.
Renaming a table:
ALTER TABLE my_table RENAME TO my_renamed_table;
RENAME TABLE my_table1 TO tmp, my_table2 TO my_table1, tmp TO my_table2;
* For TEMPORARY tables, RENAME TABLE does not work.