MYSQL ALTER TABLE
Changing storage engine; rebuild table; change file_per_table
For example, if t1 is currently not an InnoDB table, this statement changes its storage engine to InnoDB:
ALTER TABLE t1 ENGINE = InnoDB;
If the table is already InnoDB, this will rebuild the table and its indexes and have an effect similar to OPTIMIZE TABLE. You may gain some disk space improvement.
If the value of innodb_file_per_table is currently different than the value in effect when t1 was built, this will convert to (or from) file_per_table.
ALTER COLUMN OF TABLE
CREATE DATABASE stackoverflow;
USE stackoverflow;
Create table stack(
id_user int NOT NULL,
username varchar(30) NOT NULL,
password varchar(30) NOT NULL
);
ALTER TABLE stack ADD COLUMN submit date NOT NULL; -- add new column
ALTER TABLE stack DROP COLUMN submit; -- drop column
ALTER TABLE stack MODIFY submit DATETIME NOT NULL; -- modify type column
ALTER TABLE stack CHANGE submit submit_date DATETIME NOT NULL; -- change type and name of column
ALTER TABLE stack ADD COLUMN mod_id INT NOT NULL AFTER id_user; -- add new column after existing
column
Change auto-increment value
Changing an auto-increment value is useful when you don't want a gap in an AUTO_INCREMENT column after a massive deletion.
For example, you got a lot of unwanted (advertisement) rows posted in your table, you deleted them, and you want to fix the gap in auto-increment values. Assume the MAX value of AUTO_INCREMENT column is 100 now. You can use the following to fix the auto-increment value.
ALTER TABLE your_table_name AUTO_INCREMENT = 101;