改变 MySQL 的 SHOW COLUMNS "Extra" 值

Alter MySQL's SHOW COLUMS "Extra" value

有没有办法更改 SHOW COLUMNS/DESCRIBE 句子中显示的 Extra 列的值?

有关此专栏的文档说明如下:

Extra

Any additional information that is available about a given column. The value is nonempty in these cases:

  • auto_increment for columns that have the AUTO_INCREMENT attribute.

  • on update CURRENT_TIMESTAMP for TIMESTAMP or DATETIME columns that have the ON UPDATE CURRENT_TIMESTAMP attribute.

  • VIRTUAL GENERATED or VIRTUAL STORED for generated columns.

  • DEFAULT_GENERATED for columns that have an expression default value.

我有下一个 table 列的信息,但我想删除 start_date 列的 Extra 值。

有办法吗?

+--------------------+--------------------+------+-----+-------------------+-------------------+
|       Field        |        Type        | Null | Key |      Default      |       Extra       |
+--------------------+--------------------+------+-----+-------------------+-------------------+
| id_machine_product | "int(10) unsigned" | NO   | PRI | NULL              | auto_increment    |
| ...                | ...                | ...  | ... | ...               | ...               |
| start_date         | timestamp          | NO   |     | CURRENT_TIMESTAMP | DEFAULT_GENERATED |
+--------------------+--------------------+------+-----+-------------------+-------------------+

编辑:

我在 PHP 中实施了一种指纹验证方法,它区分了 DESCRIBE table 的值,我的生产中的数据库版本没有那个额外的值,即使这些列有一个表达式默认值,所以目前,我希望更改该值,这样我在开发环境中实现的指纹验证方法就不会出错。

生产数据库在 Mysql < 8.0 因此,根据 Bill Karwin 的回答,我的 MySQL 开发环境版本 8.0

遇到了问题

您需要一个 alter table 语句。像

ALTER TABLE `document` MODIFY COLUMN `start_date ` INT AUTO_INCREMENT;

您可以设置一个默认值,例如

DEFAULT 1 NOT NULL 

从你的问题为什么你想要删除额外信息并不清楚。只是注意到该列的默认值是一个表达式。

要将 Extra 字段设为空白,您必须将列的默认值设为常数值或 NULL。

mysql> create table foo ( id int unsigned primary key, start_date timestamp not null default current_timestamp);

mysql> show columns from foo;
+------------+------------------+------+-----+-------------------+-------------------+
| Field      | Type             | Null | Key | Default           | Extra             |
+------------+------------------+------+-----+-------------------+-------------------+
| id         | int(10) unsigned | NO   | PRI | NULL              |                   |
| start_date | timestamp        | NO   |     | CURRENT_TIMESTAMP | DEFAULT_GENERATED |
+------------+------------------+------+-----+-------------------+-------------------+

mysql> alter table foo modify start_date timestamp default null;

mysql> show columns from foo;
+------------+------------------+------+-----+---------+-------+
| Field      | Type             | Null | Key | Default | Extra |
+------------+------------------+------+-----+---------+-------+
| id         | int(10) unsigned | NO   | PRI | NULL    |       |
| start_date | timestamp        | YES  |     | NULL    |       |
+------------+------------------+------+-----+---------+-------+

请注意,额外信息 "DEFAULT_GENERATED" 仅存在于 MySQL 8.0 中。我怀疑这与支持 DEFAULT 子句中的表达式的新功能有关。任何其他表达式也会产生此额外信息。

mysql > alter table foo modify start_date timestamp default (now() + interval 1 hour);

mysql> show columns from foo;
+------------+------------------+------+-----+---------------------------+-------------------+
| Field      | Type             | Null | Key | Default                   | Extra             |
+------------+------------------+------+-----+---------------------------+-------------------+
| id         | int(10) unsigned | NO   | PRI | NULL                      |                   |
| start_date | timestamp        | YES  |     | (now() + interval 1 hour) | DEFAULT_GENERATED |
+------------+------------------+------+-----+---------------------------+-------------------+

话题发起者评论

I have implemented a fingerprint validation method in PHP that diffs the DESCRIBE tables values, I have database versions in production that doesn't have that Extra value even though those columns have an expression default value, so currently, I wish to alter that value so I don't get errors from my implemented fingerprint validation method in my development environment.

更标准的 SQL 方法也适用于 MySQL 8

查询

SELECT 
    information_schema.COLUMNS.COLUMN_NAME AS 'Field'
    , information_schema.COLUMNS.COLUMN_TYPE AS 'Type'
    , information_schema.COLUMNS.IS_NULLABLE AS 'Null'
    , information_schema.COLUMNS.COLUMN_KEY AS 'Key'
    , information_schema.COLUMNS.COLUMN_DEFAULT AS 'Default'
    , information_schema.COLUMNS.EXTRA AS 'Extra'
FROM 
    information_schema.TABLES
INNER JOIN
    information_schema.COLUMNS ON information_schema.TABLES.TABLE_NAME =  information_schema.COLUMNS.TABLE_NAME
WHERE
    information_schema.TABLES.TABLE_NAME = '<table>'

此查询应匹配 DESCRIBE 的输出 然后你可以在 information_schema.COLUMNS.EXTRA 输出上使用 REPLACE() 来删除或编辑你想要的方式。
例如删除额外的功能,如 DEFAULT_GENERATEDVIRTUAL GENERATED (generated columns)