mysql ALTER 語句的使用

2021-07-15 14:42:10 字數 4190 閱讀 2328

基本語法:

alter [online | offline] [ignore] tabletbl_name

[alter_specification[,alter_specification] ...]

[partition_options]

alter_specification:

table_options

| add [column]col_namecolumn_definition

[first | aftercol_name]

| add [column] (col_namecolumn_definition,...)

| add [index_name]

[index_type] (index_col_name,...) [index_option] ...

| add [constraint [symbol]] primary key

[index_type] (index_col_name,...) [index_option] ...

| add [constraint [symbol]]

unique [index|key] [index_name]

[index_type] (index_col_name,...) [index_option] ...

| add fulltext [index|key] [index_name]

(index_col_name,...) [index_option] ...

| add spatial [index|key] [index_name]

(index_col_name,...) [index_option] ...

| add [constraint [symbol]]

foreign key [index_name] (index_col_name,...)

reference_definition

| alter [column]col_name

| change [column]old_col_namenew_col_namecolumn_definition

[first|aftercol_name]

| modify [column]col_namecolumn_definition

[first | aftercol_name]

| drop [column]col_name

| drop primary key

| dropindex_name

| drop foreign keyfk_symbol

| disable keys

| enable keys

| rename [to|as]new_tbl_name

| order bycol_name[,col_name] ...

| convert to character setcharset_name[collatecollation_name]

| [default] character set [=]charset_name[collate [=]collation_name]

| discard tablespace

| import tablespace

| force

| add partition (partition_definition)

| drop partitionpartition_names

| truncate partition

| coalesce partitionnumber

| reorganize partition [partition_namesinto (partition_definitions)]

| analyze partition

| check partition

| optimize partition

| rebuild partition

| repair partition

| partition bypartitioning_expression

| remove partitioning

index_col_name:

col_name[(length)] [asc | desc]

index_type:

using

index_option:

key_block_size [=]value

|index_type

| with parserparser_name

| comment 'string'

table_options:

table_option[[,]table_option] ... (seecreate tableoptions)

partition_options:

(seecreate tableoptions)

其中table_options:包括engine,row_format,auto_increment,character set,comment

auto_increment 可以表示當前自動增長的值,comment表示表的注釋。

× 在大多數執行atler table語句的時候,資料庫都會建立乙個臨時的表,它是舊表的乙個輔助,資料庫會等待所有的其他操作完成以後才講對錶的更變加入到臨時表中。

× 在alter table 語句執行的時候,表仍然是可讀的,但是所有對錶的更新與寫操作都會被阻塞,知道新錶被準備好。

× 有些atler table的操作不會阻塞乙個表,對錶中列進行重新命名不會阻塞表。

× 對錶的元資料的修改不會導致表的複製:比如對錶的重新命名,列預設值的修改,還有就是增加乙個列舉成員等等。alter tablewithadd partition,drop partition,coalesce partition,rebuild partition, orreorganize partitiondoes not create any temporary tables (except when used withndbtables);

however, these operations can and do create temporary partition files.

Mysql alter常見使用語句

新增主鍵 alter table tabelname add new field id int 5 unsigned default 0 not null auto increment add primary key new field id 增加乙個新列 alter table rpt fmp e...

MySQL Alter語句 運用

修改表名 mysql alter table student rename person 這裡的student是原名,person是修改過後的名字 用rename來重新命名,也可以使用rename to 修改欄位的資料型別 mysql alter table person modify name v...

Mysql alter語句的用法

1 刪除列 alter table 表名字 drop 列名稱 2 增加列 alter table 表名字 add 列名稱 int not null comment 注釋說明 3 修改列的型別資訊 alter table 表名字 change 列名稱 新列名稱 這裡可以用和原來列同名即可 bigint...