To use ALTER TABLE, you need
ALTER, INSERT, and
CREATE privileges for the table.
IGNORE is a MySQL extension to standard
SQL. It controls how ALTER TABLE works if
there are duplicates on unique keys in the new table or if
warnings occur when strict mode is enabled. If
IGNORE is not specified, the copy is
aborted and rolled back if duplicate-key errors occur. If
IGNORE is specified, only the first row
is used of rows with duplicates on a unique key, The other
conflicting rows are deleted. Incorrect values are truncated
to the closest matching acceptable value.
-
You can issue multiple ADD,
ALTER, DROP, and
CHANGE clauses in a single ALTER
TABLE statement, separated by commas. This is a
MySQL extension to standard SQL, which allows only one of
each clause per ALTER TABLE statement.
For example, to drop multiple columns in a single statement,
do this:
ALTER TABLE t2 DROP COLUMN c, DROP COLUMN d;
CHANGE
col_name, DROP
col_name, and
DROP INDEX are MySQL extensions to
standard SQL.
MODIFY is an Oracle extension to
ALTER TABLE.
The word COLUMN is optional and can be
omitted.
If you use ALTER TABLE
tbl_name RENAME TO
new_tbl_name without
any other options, MySQL simply renames any files that
correspond to the table tbl_name.
There is no need to create a temporary table. (You can also
use the RENAME TABLE statement to rename
tables. See Section 13.1.10, “RENAME TABLE Syntax”.)
column_definition clauses use the
same syntax for ADD and
CHANGE as for CREATE
TABLE. Note that this syntax includes the column
name, not just its data type. See
Section 13.1.5, “CREATE TABLE Syntax”.
-
You can rename a column using a CHANGE
old_col_name
column_definition
clause. To do so, specify the old and new column names and
the type that the column currently has. For example, to
rename an INTEGER column from
a to b, you can do
this:
ALTER TABLE t1 CHANGE a b INTEGER;
If you want to change a column's type but not the name,
CHANGE syntax still requires an old and
new column name, even if they are the same. For example:
ALTER TABLE t1 CHANGE b b BIGINT NOT NULL;
You can also use MODIFY to change a
column's type without renaming it:
ALTER TABLE t1 MODIFY b BIGINT NOT NULL;
If you use CHANGE or
MODIFY to shorten a column for which an
index exists on the column, and the resulting column length
is less than the index length, MySQL shortens the index
automatically.
When you change a data type using CHANGE
or MODIFY, MySQL tries to convert
existing column values to the new type as well as possible.
To add a column at a specific position within a table row,
use FIRST or AFTER
col_name. The default
is to add the column last. You can also use
FIRST and AFTER in
CHANGE or MODIFY
operations.
ALTER ... SET DEFAULT or ALTER
... DROP DEFAULT specify a new default value for a
column or remove the old default value, respectively. If the
old default is removed and the column can be
NULL, the new default is
NULL. If the column cannot be
NULL, MySQL assigns a default value, as
described in Section 11.1.4, “Data Type Default Values”.
DROP INDEX removes an index. This is a
MySQL extension to standard SQL. See
Section 13.1.7, “DROP INDEX Syntax”.
If columns are dropped from a table, the columns are also
removed from any index of which they are a part. If all
columns that make up an index are dropped, the index is
dropped as well.
If a table contains only one column, the column cannot be
dropped. If what you intend is to remove the table, use
DROP TABLE instead.
-
DROP PRIMARY KEY drops the primary index.
Note: In older versions of MySQL, if no
primary index existed, DROP PRIMARY KEY
would drop the first UNIQUE index in the
table. This is not the case in MySQL 5.1, where
trying to use DROP PRIMARY KEY on a table
with no primary key give rises to an error.
If you add a UNIQUE INDEX or
PRIMARY KEY to a table, it is stored
before any non-unique index so that MySQL can detect
duplicate keys as early as possible.
ORDER BY enables you to create the new
table with the rows in a specific order. Note that the table
does not remain in this order after inserts and deletes.
This option is useful primarily when you know that you are
mostly to query the rows in a certain order most of the
time. By using this option after major changes to the table,
you might be able to get higher performance. In some cases,
it might make sorting easier for MySQL if the table is in
order by the column that you want to order it by later.
-
If you use ALTER TABLE on a
MyISAM table, all non-unique indexes are
created in a separate batch (as for REPAIR
TABLE). This should make ALTER
TABLE much faster when you have many indexes.
This feature can be activated explicitly. ALTER
TABLE ... DISABLE KEYS tells MySQL to stop
updating non-unique indexes for a MyISAM
table. ALTER TABLE ... ENABLE KEYS then
should be used to re-create missing indexes. MySQL does this
with a special algorithm that is much faster than inserting
keys one by one, so disabling keys before performing bulk
insert operations should give a considerable speedup. Using
ALTER TABLE ... DISABLE KEYS requires the
INDEX privilege in addition to the
privileges mentioned earlier.
-
The FOREIGN KEY and
REFERENCES clauses are supported by the
InnoDB storage engine, which implements
ADD [CONSTRAINT
[symbol]] FOREIGN KEY (...)
REFERENCES ... (...). See
Section 14.2.6.4, “FOREIGN KEY Constraints”. For other
storage engines, the clauses are parsed but ignored. The
CHECK clause is parsed but ignored by all
storage engines. See Section 13.1.5, “CREATE TABLE Syntax”. The
reason for accepting but ignoring syntax clauses is for
compatibility, to make it easier to port code from other SQL
servers, and to run applications that create tables with
references. See Section 1.9.5, “MySQL Differences from Standard SQL”.
You cannot add a foreign key and drop a foreign key in
separate clauses of a single ALTER TABLE
statement. You must use separate statements.
-
InnoDB supports the use of ALTER
TABLE to drop foreign keys:
ALTER TABLE tbl_name DROP FOREIGN KEY fk_symbol;
You cannot add a foreign key and drop a foreign key in
separate clauses of a single ALTER TABLE
statement. You must use separate statements.
For more information, see
Section 14.2.6.4, “FOREIGN KEY Constraints”.
-
If you want to change the table default character set and
all character columns (CHAR,
VARCHAR, TEXT) to a
new character set, use a statement like this:
ALTER TABLE tbl_name CONVERT TO CHARACTER SET charset_name;
Warning: The preceding
operation converts column values between the character sets.
This is not what you want if you have a
column in one character set (like latin1)
but the stored values actually use some other, incompatible
character set (like utf8). In this case,
you have to do the following for each such column:
ALTER TABLE t1 CHANGE c1 c1 BLOB;
ALTER TABLE t1 CHANGE c1 c1 TEXT CHARACTER SET utf8;
The reason this works is that there is no conversion when
you convert to or from BLOB columns.
If you specify CONVERT TO CHARACTER SET
binary, the CHAR,
VARCHAR, and TEXT
columns are converted to their corresponding binary string
types (BINARY,
VARBINARY, BLOB). This
means that the columns no longer will have a character set
and a subsequent CONVERT TO operation
will not apply to them.
To change only the default character
set for a table, use this statement:
ALTER TABLE tbl_name DEFAULT CHARACTER SET charset_name;
The word DEFAULT is optional. The default
character set is the character set that is used if you do
not specify the character set for a new column which you add
to a table (for example, with ALTER TABLE ... ADD
column).
-
For an InnoDB table that is created with
its own tablespace in an .ibd file,
that file can be discarded and imported. To discard the
.ibd file, use this statement:
ALTER TABLE tbl_name DISCARD TABLESPACE;
This deletes the current .ibd file, so
be sure that you have a backup first. Attempting to access
the table while the tablespace file is discarded results in
an error.
To import the backup .ibd file back
into the table, copy it into the database directory, and
then issue this statement:
ALTER TABLE tbl_name IMPORT TABLESPACE;
See Section 14.2.3.1, “Using Per-Table Tablespaces”.
-
A number of partitioning-related extensions to
ALTER TABLE were added in MySQL 5.1.5.
These can be used with partitioned tables for
repartitioning, for adding, dropping, merging, and splitting
partitions, and for performing partitioning maintenance.
The partition_definition clause
for ALTER TABLE ADD PARTITION supports
the same options as the clause of the same name does for the
CREATE TABLE statement clause of the same
name. (See Section 13.1.5, “CREATE TABLE Syntax”, for the syntax
and description.) Suppose that you have the partitioned
table created as shown here:
CREATE TABLE t1 (
id INT,
year_col INT
)
PARTITION BY RANGE (year_col) (
PARTITION p0 VALUES LESS THAN (1991),
PARTITION p1 VALUES LESS THAN (1995),
PARTITION p2 VALUES LESS THAN (1999)
);
You can add a new partition p3 to this
table for storing values less then 2002
as follows:
ALTER TABLE t1 ADD PARTITION (PARTITION p3 VALUES LESS THAN (2002));
Note: You cannot use
ALTER TABLE to add partitions to a table
that is not not already partitioned.
DROP PARTITION can be used to drop one or
more RANGE or LIST
partitions. This statement cannot be used with
HASH or KEY
partitions; instead, use COALESCE
PARTITION (see below). Any data that was stored in
the dropped partitions named in the
partition_names list is
discarded. For example, given the table
t1 defined previously, you can drop the
partitions named p0 and
p1 as shown here:
ALTER TABLE t1 DROP PARTITION p0, p1;
Note that DROP PARTITION does not work
with tables that use the NDB Cluster
storage engine. See
Section 17.3.1, “Management of RANGE and LIST Partitions”, and
Section 16.9, “Known Limitations of MySQL Cluster”.
ADD PARTITION and DROP
PARTITION do not currently support IF
[NOT] EXISTS. It is also not possible to rename a
partition or a partitioned table. Instead, if you wish to
rename a partition, you must drop and re-create the
partition; if you wish to rename a partitioned table, you
must instead drop all partitions, rename the table, and then
add back the partitions that were dropped.
COALESCE PARTITION can be used with a
table that is partitioned by HASH or
KEY to reduce the number of partitions by
number. Suppose that you have
created table t2 using the following
definition:
CREATE TABLE t2 (
name VARCHAR (30),
started DATE
)
PARTITION BY HASH( YEAR(started) )
PARTITIONS 6;
You can reduce the number of partitions used by
t2 from 6 to 4 using the following
statement:
ALTER TABLE t2 COALESCE PARTITION 2;
The data contained in the last
number partitions will be merged
into the remaining partitions. In this case, partitions 4
and 5 will be merged into the first 4 partitions (the
partitions numbered 0, 1, 2, and 3.
To change some but not all the partitions used by a
partitioned table, you can use REORGANIZE
PARTITION. This statement can be used in several
ways:
To merge a set of partitions into a single partition.
This can be done by naming several partitions in the
partition_names list and
supplying a single definition for
partition_definition.
To split an existing partition into several partitions.
You can accomplish this by naming a single partition for
partition_names and providing
multiple
partition_definitions.
To change the ranges for a subset of partitions defined
using VALUES LESS THAN or the value
lists for a subset of partitions defined using
VALUES IN.
Note: For partitions that
have not been explicitly named, MySQL automatically provides
the default names p0,
p1, p2, and so on.
For more detailed information about and examples of
ALTER TABLE ... REORGANIZE PARTITION
statements, see Section 17.3, “Partition Management”.
Several additional clauses provide partition maintenance and
repair functionality analogous to that implemented for
non-partitioned tables by statements such as CHECK
TABLE and REPAIR TABLE (which
are not supported for partitioned
tables). These include ANALYZE PARTITION,
CHECK PARTITION, OPTIMIZE
PARTITION, REBUILD PARTITION,
and REPAIR PARTITION. Each of these
options takes a partition_names
clause consisting of one or more names of partitions,
separated by commas. The partitions must already exist in
the table to be altered. For more information, and for
examples of these, see
Section 17.3.3, “Maintenance of Partitions”.