Bài viết này liệt kê ALTER TABLE
trong SQL cú pháp, như được thực hiện bởi các hệ thống quản lý cơ sở dữ liệu khác nhau (DBMS). Cú pháp được liệt kê chính xác như mỗi nhà cung cấp đã liệt kê nó trên trang web của họ. Nhấp vào liên kết hiện hành để xem thêm chi tiết về cú pháp cho một nhà cung cấp cụ thể.
Các DBMS được đề cập là MySQL, SQL Server, PostgreSQL và Cơ sở dữ liệu Oracle.
MySQL
Từ Hướng dẫn tham khảo MySQL 5.7:
ALTER [IGNORE] TABLEtbl_name
[alter_specification
[,alter_specification
] ...] [partition_options
]alter_specification
:table_options
| ADD [COLUMN]col_name
column_definition
[FIRST | AFTERcol_name
] | ADD [COLUMN] (col_name
column_definition
,...) | ADD {INDEX|KEY} [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
| ALGORITHM [=] {DEFAULT|INPLACE|COPY} | ALTER [COLUMN]col_name
{SET DEFAULTliteral
| DROP DEFAULT} | CHANGE [COLUMN]old_col_name
new_col_name
column_definition
[FIRST|AFTERcol_name
] | LOCK [=] {DEFAULT|NONE|SHARED|EXCLUSIVE} | MODIFY [COLUMN]col_name
column_definition
[FIRST | AFTERcol_name
] | DROP [COLUMN]col_name
| DROP PRIMARY KEY | DROP {INDEX|KEY}index_name
| DROP FOREIGN KEYfk_symbol
| DISABLE KEYS | ENABLE KEYS | RENAME [TO|AS]new_tbl_name
| RENAME {INDEX|KEY}old_index_name
TOnew_index_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 | {WITHOUT|WITH} VALIDATION | ADD PARTITION (partition_definition
) | DROP PARTITIONpartition_names
| DISCARD PARTITION {partition_names
| ALL} TABLESPACE | IMPORT PARTITION {partition_names
| ALL} TABLESPACE | TRUNCATE PARTITION {partition_names
| ALL} | COALESCE PARTITIONnumber
| REORGANIZE PARTITIONpartition_names
INTO (partition_definitions
) | EXCHANGE PARTITIONpartition_name
WITH TABLEtbl_name
[{WITH|WITHOUT} VALIDATION] | ANALYZE PARTITION {partition_names
| ALL} | CHECK PARTITION {partition_names
| ALL} | OPTIMIZE PARTITION {partition_names
| ALL} | REBUILD PARTITION {partition_names
| ALL} | REPAIR PARTITION {partition_names
| ALL} | REMOVE PARTITIONING | UPGRADE PARTITIONINGindex_col_name
:col_name
[(length
)] [ASC | DESC]index_type
: USING {BTREE | HASH}index_option
: KEY_BLOCK_SIZE [=]value
|index_type
| WITH PARSERparser_name
| COMMENT 'string
'table_options
:table_option
[[,]table_option
] ...table_option
: ENGINE [=]engine_name
| AUTO_INCREMENT [=]value
| AVG_ROW_LENGTH [=]value
| [DEFAULT] CHARACTER SET [=]charset_name
| CHECKSUM [=] {0 | 1} | [DEFAULT] COLLATE [=]collation_name
| COMMENT [=] 'string
' | COMPRESSION [=] {'ZLIB'|'LZ4'|'NONE'} | CONNECTION [=] 'connect_string
' | DATA DIRECTORY [=] 'absolute path to directory
' | DELAY_KEY_WRITE [=] {0 | 1} | ENCRYPTION [=] {'Y' | 'N'} | INDEX DIRECTORY [=] 'absolute path to directory
' | INSERT_METHOD [=] { NO | FIRST | LAST } | KEY_BLOCK_SIZE [=]value
| MAX_ROWS [=]value
| MIN_ROWS [=]value
| PACK_KEYS [=] {0 | 1 | DEFAULT} | PASSWORD [=] 'string
' | ROW_FORMAT [=] {DEFAULT|DYNAMIC|FIXED|COMPRESSED|REDUNDANT|COMPACT} | STATS_AUTO_RECALC [=] {DEFAULT|0|1} | STATS_PERSISTENT [=] {DEFAULT|0|1} | STATS_SAMPLE_PAGES [=]value
| TABLESPACEtablespace_name
| UNION [=] (tbl_name
[,tbl_name
]...)partition_options
: PARTITION BY { [LINEAR] HASH(expr
) | [LINEAR] KEY [ALGORITHM={1|2}] (column_list
) | RANGE{(expr
) | COLUMNS(column_list
)} | LIST{(expr
) | COLUMNS(column_list
)} } [PARTITIONSnum
] [SUBPARTITION BY { [LINEAR] HASH(expr
) | [LINEAR] KEY [ALGORITHM={1|2}] (column_list
) } [SUBPARTITIONSnum
] ] [(partition_definition
[,partition_definition
] ...)]partition_definition
: PARTITIONpartition_name
[VALUES {LESS THAN {(expr
|value_list
) |MAXVALUE
} | IN (value_list
)}] [[STORAGE] ENGINE [=]engine_name
] [COMMENT [=]'comment_text'
] [DATA DIRECTORY [=] ''] [INDEX DIRECTORY [=] '
data_dir
'] [MAX_ROWS [=]
index_dir
max_number_of_rows
] [MIN_ROWS [=]min_number_of_rows
] [TABLESPACE [=] tablespace_name] [(subpartition_definition
[,subpartition_definition
] ...)]subpartition_definition
: SUBPARTITIONlogical_name
[[STORAGE] ENGINE [=]engine_name
] [COMMENT [=]'comment_text'
] [DATA DIRECTORY [=] ''] [INDEX DIRECTORY [=] '
data_dir
'] [MAX_ROWS [=]
index_dir
max_number_of_rows
] [MIN_ROWS [=]min_number_of_rows
] [TABLESPACE [=] tablespace_name]
Máy chủ SQL
Từ Tham chiếu Transact-SQL:
ALTER TABLE [ database_name . [ schema_name ] . | schema_name . ] table_name { ALTER COLUMN column_name { [ type_schema_name. ] type_name [ ( { precision [ , scale ] | max | xml_schema_collection } ) ] [ COLLATE collation_name ] [ NULL | NOT NULL ] [ SPARSE ] | {ADD | DROP } { ROWGUIDCOL | PERSISTED | NOT FOR REPLICATION | SPARSE } } | [ WITH { CHECK | NOCHECK } ] | ADD { <column_definition> | <computed_column_definition> | <table_constraint> | <column_set_definition> } [ ,...n ] | DROP { [ CONSTRAINT ] { constraint_name [ WITH ( <drop_clustered_constraint_option> [ ,...n ] ) ] } [ ,...n ] | COLUMN { column_name } [ ,...n ] } [ ,...n ] | [ WITH { CHECK | NOCHECK } ] { CHECK | NOCHECK } CONSTRAINT { ALL | constraint_name [ ,...n ] } | { ENABLE | DISABLE } TRIGGER { ALL | trigger_name [ ,...n ] } | { ENABLE | DISABLE } CHANGE_TRACKING [ WITH ( TRACK_COLUMNS_UPDATED = { ON | OFF } ) ] | SWITCH [ PARTITION source_partition_number_expression ] TO target_table [ PARTITION target_partition_number_expression ] | SET ( FILESTREAM_ON = { partition_scheme_name | filegroup | "default" | "NULL" } ) | REBUILD [ [PARTITION = ALL] [ WITH ( <rebuild_option> [ ,...n ] ) ] | [ PARTITION = partition_number [ WITH ( <single_partition_rebuild_option> [ ,...n ] ) ] ] ] | <table_option> | <filetable_option> } [ ; ]
Tùy chọn
<column_set_definition> ::= column_set_name XML COLUMN_SET FOR ALL_SPARSE_COLUMNS <drop_clustered_constraint_option> ::= { MAXDOP = max_degree_of_parallelism | ONLINE = {ON | OFF } | MOVE TO { partition_scheme_name ( column_name ) | filegroup | "default" } } <table_option> ::= { SET ( LOCK_ESCALATION = { AUTO | TABLE | DISABLE } ) } <filetable_option> ::= { [ { ENABLE | DISABLE } FILETABLE_NAMESPACE ] [ SET ( FILETABLE_DIRECTORY = directory_name ) ] } <single_partition_rebuild__option> ::= { SORT_IN_TEMPDB = { ON | OFF } | MAXDOP = max_degree_of_parallelism | DATA_COMPRESSION = { NONE | ROW | PAGE} } }
PostgreSQL
Từ Hướng dẫn sử dụng PostgreSQL 9.5:
ALTER TABLE [ IF EXISTS ] [ ONLY ] name [ * ] action [, ... ] ALTER TABLE [ IF EXISTS ] [ ONLY ] name [ * ] RENAME [ COLUMN ] column_name TO new_column_name ALTER TABLE [ IF EXISTS ] [ ONLY ] name [ * ] RENAME CONSTRAINT constraint_name TO new_constraint_name ALTER TABLE [ IF EXISTS ] name RENAME TO new_name ALTER TABLE [ IF EXISTS ] name SET SCHEMA new_schema ALTER TABLE ALL IN TABLESPACE name [ OWNED BY role_name [, ... ] ] SET TABLESPACE new_tablespace [ NOWAIT ] where action is one of: ADD [ COLUMN ] column_name data_type [ COLLATE collation ] [ column_constraint [ ... ] ] DROP [ COLUMN ] [ IF EXISTS ] column_name [ RESTRICT | CASCADE ] ALTER [ COLUMN ] column_name [ SET DATA ] TYPE data_type [ COLLATE collation ] [ USING expression ] ALTER [ COLUMN ] column_name SET DEFAULT expression ALTER [ COLUMN ] column_name DROP DEFAULT ALTER [ COLUMN ] column_name { SET | DROP } NOT NULL ALTER [ COLUMN ] column_name SET STATISTICS integer ALTER [ COLUMN ] column_name SET ( attribute_option = value [, ... ] ) ALTER [ COLUMN ] column_name RESET ( attribute_option [, ... ] ) ALTER [ COLUMN ] column_name SET STORAGE { PLAIN | EXTERNAL | EXTENDED | MAIN } ADD table_constraint [ NOT VALID ] ADD table_constraint_using_index ALTER CONSTRAINT constraint_name [ DEFERRABLE | NOT DEFERRABLE ] [ INITIALLY DEFERRED | INITIALLY IMMEDIATE ] VALIDATE CONSTRAINT constraint_name DROP CONSTRAINT [ IF EXISTS ] constraint_name [ RESTRICT | CASCADE ] DISABLE TRIGGER [ trigger_name | ALL | USER ] ENABLE TRIGGER [ trigger_name | ALL | USER ] ENABLE REPLICA TRIGGER trigger_name ENABLE ALWAYS TRIGGER trigger_name DISABLE RULE rewrite_rule_name ENABLE RULE rewrite_rule_name ENABLE REPLICA RULE rewrite_rule_name ENABLE ALWAYS RULE rewrite_rule_name DISABLE ROW LEVEL SECURITY ENABLE ROW LEVEL SECURITY FORCE ROW LEVEL SECURITY NO FORCE ROW LEVEL SECURITY CLUSTER ON index_name SET WITHOUT CLUSTER SET WITH OIDS SET WITHOUT OIDS SET TABLESPACE new_tablespace SET { LOGGED | UNLOGGED } SET ( storage_parameter = value [, ... ] ) RESET ( storage_parameter [, ... ] ) INHERIT parent_table NO INHERIT parent_table OF type_name NOT OF OWNER TO { new_owner | CURRENT_USER | SESSION_USER } REPLICA IDENTITY { DEFAULT | USING INDEX index_name | FULL | NOTHING } and table_constraint_using_index is: [ CONSTRAINT constraint_name ] { UNIQUE | PRIMARY KEY } USING INDEX index_name [ DEFERRABLE | NOT DEFERRABLE ] [ INITIALLY DEFERRED | INITIALLY IMMEDIATE ]
Cơ sở dữ liệu Oracle
Từ Tài liệu Trực tuyến Cơ sở dữ liệu Oracle 12 c Bản phát hành 1 (12.1):
ALTER TABLE [ schema. ] table [ alter_table_properties | column_clauses | constraint_clauses | alter_table_partitioning | alter_external_table | move_table_clause | modify_opaque_type ] [ enable_disable_clause | { ENABLE | DISABLE } { TABLE LOCK | ALL TRIGGERS } ] ... ;
Phần sau phác thảo chi tiết các tùy chọn khác nhau.
alter_table_properties ::=
{ { { physical_attributes_clause | logging_clause | table_compression | inmemory_alter_table_clause | ilm_clause | supplemental_table_logging | allocate_extent_clause | deallocate_unused_clause | { CACHE | NOCACHE } | RESULT_CACHE ( MODE {DEFAULT | FORCE} ) | upgrade_table_clause | records_per_block_clause | parallel_clause | row_movement_clause | flashback_archive_clause }... | RENAME TO new_table_name } [ alter_iot_clauses ] [ alter_XMLSchema_clause ] | { shrink_clause | READ ONLY | READ WRITE | REKEY encryption_spec | [NO] ROW ARCHIVAL | ADD attribute_clustering_clause | MODIFY CLUSTERING [ clustering_when ] [ zonemap_clause ] | DROP CLUSTERING } }
column_clauses ::=
{ { add_column_clause | modify_column_clauses | drop_column_clause | add_period_clause | drop_period_clause }... | rename_column_clause | { modify_collection_retrieval }... | { modify_LOB_storage_clause }... | { alter_varray_col_properties }... }
constraint_clauses ::=
{ ADD { { out_of_line_constraint }... | out_of_line_REF_constraint } | MODIFY { CONSTRAINT constraint_name | PRIMARY KEY | UNIQUE (column [, column ]...) } constraint_state [ CASCADE ] | RENAME CONSTRAINT old_name TO new_name | { drop_constraint_clause }... }
alter_table_partitioning ::=
{ modify_table_default_attrs | alter_interval_partitioning | set_subpartition_template | modify_table_partition | modify_table_subpartition | move_table_partition | move_table_subpartition | add_table_partition | coalesce_table_partition | drop_table_partition | drop_table_subpartition | rename_partition_subpart | truncate_partition_subpart | split_table_partition | split_table_subpartition | merge_table_partitions | merge_table_subpartitions | exchange_partition_subpart }
alter_external_table ::=
{ add_column_clause | modify_column_clauses | drop_column_clause | parallel_clause | external_data_properties | REJECT LIMIT { integer | UNLIMITED } | PROJECT COLUMN { ALL | REFERENCED } } [ add_column_clause | modify_column_clauses | drop_column_clause | parallel_clause | external_data_properties | REJECT LIMIT { integer | UNLIMITED } | PROJECT COLUMN { ALL | REFERENCED } ]...
move_table_clause ::=
MOVE [ ONLINE ] [ segment_attributes_clause ] [ table_compression ] [ index_org_table_clause ] [ { LOB_storage_clause | varray_col_properties }... ] [ parallel_clause ] [ allow_disallow_clustering ]
mod_opaque_type ::=
MODIFY OPAQUE TYPE anydata_column STORE ( type_name [, type_name ]... ) UNPACKED
enable_disable_clause ::=
{ ENABLE | DISABLE } [ VALIDATE | NOVALIDATE ] { UNIQUE (column [, column ]...) | PRIMARY KEY | CONSTRAINT constraint_name } [ using_index_clause ] [ exceptions_clause ] [ CASCADE ] [ { KEEP | DROP } INDEX ]
Để biết chi tiết về các điều khoản phụ của các thành phần khác nhau, hãy xem tài liệu chính thức của Oracle.
Sử dụng GUI
Việc thay đổi bảng cũng có thể được thực hiện thông qua GUI trong mỗi hệ thống quản lý cơ sở dữ liệu hoặc bằng cách sử dụng bất kỳ công cụ GUI nào có sẵn để sử dụng với sản phẩm đó.
Ví dụ:MySQL Workbench có thể được sử dụng với MySQL.