MODIFY COLUMN 会重置语句中未指定的属性吗?
Will MODIFY COLUMN reset attributes that are not specified in the statement?
MODIFY COLUMN
语句是否会丢弃 MariaDB 中未指定的属性?
例如;考虑下面的tableand语句,执行语句后会保留默认值吗?
t
+-------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| c | int(11) | NO | PRI | 5 | |
+-------+---------+------+-----+---------+-------+
ALTER TABLE t
MODIFY COLUMN c INT(11) NULL
我认为文档有点不清楚;
Allows you to modify the type of a column. The column will be at the same place as the original column and all indexes on the column will be kept. Note that when modifying column, you should specify all attributes for the new column.
这是否意味着应该指定所有属性,否则将丢失?
默认属性将丢失。
MariaDB [test]> DESC test_table;
+----------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+---------+------+-----+---------+-------+
| column_a | int(11) | NO | PRI | 5 | |
+----------+---------+------+-----+---------+-------+
1 row in set (0.002 sec)
MariaDB [test]> ALTER TABLE test_table
-> MODIFY COLUMN column_a INT(11) NULL;
Query OK, 0 rows affected (0.007 sec)
Records: 0 Duplicates: 0 Warnings: 0
MariaDB [test]> DESC test_table;
+----------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+---------+------+-----+---------+-------+
| column_a | int(11) | NO | PRI | NULL | |
+----------+---------+------+-----+---------+-------+
1 row in set (0.000 sec)
根据文档,您需要指定所有属性,并且在您的示例中没有明确指定默认值。比较:
MariaDB [test]> ALTER TABLE test_table
-> MODIFY COLUMN column_a INT(11) NULL DEFAULT 5;
Query OK, 0 rows affected (0.007 sec)
Records: 0 Duplicates: 0 Warnings: 0
MariaDB [test]> DESC test_table;
+----------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+---------+------+-----+---------+-------+
| column_a | int(11) | NO | PRI | 5 | |
+----------+---------+------+-----+---------+-------+
1 row in set (0.001 sec)
希望对您有所帮助。
MODIFY COLUMN
语句是否会丢弃 MariaDB 中未指定的属性?
例如;考虑下面的tableand语句,执行语句后会保留默认值吗?
t
+-------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| c | int(11) | NO | PRI | 5 | |
+-------+---------+------+-----+---------+-------+
ALTER TABLE t
MODIFY COLUMN c INT(11) NULL
我认为文档有点不清楚;
Allows you to modify the type of a column. The column will be at the same place as the original column and all indexes on the column will be kept. Note that when modifying column, you should specify all attributes for the new column.
这是否意味着应该指定所有属性,否则将丢失?
默认属性将丢失。
MariaDB [test]> DESC test_table;
+----------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+---------+------+-----+---------+-------+
| column_a | int(11) | NO | PRI | 5 | |
+----------+---------+------+-----+---------+-------+
1 row in set (0.002 sec)
MariaDB [test]> ALTER TABLE test_table
-> MODIFY COLUMN column_a INT(11) NULL;
Query OK, 0 rows affected (0.007 sec)
Records: 0 Duplicates: 0 Warnings: 0
MariaDB [test]> DESC test_table;
+----------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+---------+------+-----+---------+-------+
| column_a | int(11) | NO | PRI | NULL | |
+----------+---------+------+-----+---------+-------+
1 row in set (0.000 sec)
根据文档,您需要指定所有属性,并且在您的示例中没有明确指定默认值。比较:
MariaDB [test]> ALTER TABLE test_table
-> MODIFY COLUMN column_a INT(11) NULL DEFAULT 5;
Query OK, 0 rows affected (0.007 sec)
Records: 0 Duplicates: 0 Warnings: 0
MariaDB [test]> DESC test_table;
+----------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+---------+------+-----+---------+-------+
| column_a | int(11) | NO | PRI | 5 | |
+----------+---------+------+-----+---------+-------+
1 row in set (0.001 sec)
希望对您有所帮助。