ALTER TABLE

Stai visualizzando una vecchia versione di questo article. Visualizza la versione più recente.

Sintassi

ALTER [ONLINE] [IGNORE] TABLE nome_tabella
    specifica_alter [, specifica_alter] ...

specifica_alter:
    opzione_tabella ...
  | ADD [COLUMN] nome_colonna definizione_colonna
        [FIRST | AFTER nome_colonna ]
  | ADD [COLUMN] (nome_colonna definizione_colonna,...)
  | ADD {INDEX|KEY} [nome_indice]
        [tipo_indice] (nome_colonna_indice,...) [nome_indice] ...
  | ADD [CONSTRAINT [symbol]] PRIMARY KEY
        [tipo_indice] (nome_colonna_indice,...) [nome_indice] ...
  | ADD [CONSTRAINT [symbol]]
        UNIQUE [INDEX|KEY] [nome_indice]
        [tipo_indice] (nome_colonna_indice,...) [nome_indice] ...
  | ADD FULLTEXT [INDEX|KEY] [nome_indice]
        (nome_colonna_indice,...) [opzione_indice] ...
  | ADD SPATIAL [INDEX|KEY] [nome_indice]
        (nome_colonna_indice,...) [opzione_indice] ...
  | ADD [CONSTRAINT [simbolo]]
        FOREIGN KEY [tipo_indice] (nome_colonna_indice,...)
        definizione_riferimento
  | ALTER [COLUMN] nome_colonna  {SET DEFAULT letterale | DROP DEFAULT}
  | CHANGE [COLUMN] vecchio_nome_colonna nuovo_nome_colonna definizione_colonna
        [FIRST|AFTER nome_colonna ]
  | MODIFY [COLUMN] nome_colonna  column_definition
        [FIRST | AFTER nome_colonna ]
  | DROP [COLUMN] nome_colonna 
  | DROP PRIMARY KEY
  | DROP {INDEX|KEY} nome_indice
  | DROP FOREIGN KEY simbolo_fk
  | DISABLE KEYS
  | ENABLE KEYS
  | RENAME [TO] nuovo_nome_tabella
  | ORDER BY nome_colonna [, nome_colonna] ...
  | CONVERT TO CHARACTER SET nome_charset [COLLATE nome_collation]
  | [DEFAULT] CHARACTER SET [=] nome_charset [COLLATE [=] nome_collation]
  | DISCARD TABLESPACE
  | IMPORT TABLESPACE
  | opzioni_partizione
  | ADD PARTITION (definizione_partizione)
  | DROP PARTITION nomi_partizioni
  | COALESCE PARTITION numero
  | REORGANIZE PARTITION [partition_names INTO (definizioni_partizioni)]
  | ANALYZE PARTITION nomi_partizioni
  | CHECK PARTITION nomi_partizioni
  | OPTIMIZE PARTITION nomi_partizioni
  | REBUILD PARTITION nomi_partizioni
  | REPAIR PARTITION nomi_partizioni
  | REMOVE PARTITIONING

definizione_colonna:
    tipo_dato [NOT NULL | NULL] [DEFAULT valore_default]
      [AUTO_INCREMENT] [UNIQUE [KEY] | [PRIMARY] KEY]
      [COMMENT 'stringa']
      [COLUMN_FORMAT {FIXED|DYNAMIC|DEFAULT}]
      [STORAGE {DISK|MEMORY|DEFAULT}]
  | tipo_dato [GENERATED ALWAYS]  AS   ( <espressione> )  {VIRTUAL | PERSISTENT}
      [UNIQUE] [UNIQUE KEY] [COMMENT 'string']

nome_colonna_indice:
    nome_colonna [(length)] [ASC | DESC]

tipo_indice:
    USING {BTREE | HASH | RTREE}

opzione_indice:
    KEY_BLOCK_SIZE [=] valore
  | index_type
  | WITH PARSER nome_parser
  | COMMENT 'stringa'

Spiegazione

ALTER TABLE serve a modificare la struttura di una tabella esistente. Ad esempio è possibile aggiungere o eliminare colonne, creare o distruggere indici, modificare il tipo delle colonne esistenti, rinominare le colonne o rinominare la tabella stessa. E' possibile modificare il commento della tabella o il suo Storage Engine.

When does ALTER TABLE copy all data?

MySQL/MariaDB's has always had a very rich ALTER TABLE; You can do all changes you need with one command. One downside with this has been that for most cases ALTER TABLE does a full copy of the table, which can take a long time if the table is big.

Over time more and more operations will be made online (done at once or at least very fast). Here is a list of the things that can be done 'at once' without having to copy the table:

  • Changing a column name
  • Changing display length of a integer like INT(2) -> INT(3)
  • Changing a table comment
  • Adding a new enum option last to a list
  • Renaming a table

In MariaDB 5.3, you can use the ALTER ONLINE TABLE to ensure that your ALTER TABLE is instant; If it can't be done instantly you will get an error:

create table t1 (a int, e enum ('red','green'));
alter online table t1 modify e enum('red','green','blue');
-> Query OK, 0 rows affected (0.11 sec)
-> Records: 0  Duplicates: 0  Warnings: 0

alter online table t1 add c int;
-> ERROR 1656 (HY000): Can't execute the given 'ALTER' command as online

Progress Reports

In MariaDB 5.3 you can get progress reports for ALTER TABLE in clients which support the new progress reporting protocol. From the mysql client:

MariaDB> alter table test engine=Aria;
Stage: 1 of 2 'copy to tmp table'    46% of stage

The progress report is also shown in SHOW PROCESSLIST and information_schema.processlist.

See Also:

Commenti

Sto caricando i commenti......
Content reproduced on this site is the property of its respective owners, and this content is not reviewed in advance by MariaDB. The views, information and opinions expressed by this content do not necessarily represent those of MariaDB or any other party.