MySQL 中有一个内置的 schema,名叫 information_schema
,里面存储了各类元信息。
在做需求时有一个场景,是获取一个表的列的类型,可以用 information_schema.COLUMNS
。它本质上是个 view。
对于这样一个表:
CREATE TABLE `target_db`.`multi_type_column_tab`
(
`id` int NOT NULL AUTO_INCREMENT,
`double_value` double DEFAULT NULL,
`decimal_value` decimal(10, 3) DEFAULT NULL,
`date_value` date DEFAULT NULL,
`time_value` time DEFAULT NULL,
`datetime_value` datetime DEFAULT NULL,
`timestamp_value` timestamp NULL DEFAULT NULL,
`year_value` year DEFAULT NULL,
`varchar_value` varchar(100) DEFAULT NULL,
`varbinary_value` varbinary(100) DEFAULT NULL,
`signed_int_value` int DEFAULT NULL,
`unsigned_int_value` int unsigned DEFAULT NULL,
`signed_bigint_value` bigint DEFAULT NULL,
`unsigned_bigint_value` bigint unsigned DEFAULT NULL,
`bit_value` bit(20) DEFAULT NULL,
`bool_value` tinyint(1) DEFAULT NULL,
PRIMARY KEY (`id`)
) DEFAULT CHARSET = utf8mb4;
其对应的 infromation_schema.COLUMNS
条目如下 :
TABLE_CATALOG | TABLE_SCHEMA | TABLE_NAME | COLUMN_NAME | ORDINAL_POSITION | COLUMN_DEFAULT | IS_NULLABLE | DATA_TYPE | CHARACTER_MAXIMUM_LENGTH | CHARACTER_OCTET_LENGTH | NUMERIC_PRECISION | NUMERIC_SCALE | DATETIME_PRECISION | CHARACTER_SET_NAME | COLLATION_NAME | COLUMN_TYPE | COLUMN_KEY | EXTRA | PRIVILEGES | COLUMN_COMMENT | GENERATION_EXPRESSION | SRS_ID |
def | target_db | multi_type_column_tab | id | 1 | NULL | NO | int | NULL | NULL | 10 | 0 | NULL | NULL | NULL | int | PRI | auto_increment | select,insert,update,references | NULL | ||
def | target_db | multi_type_column_tab | double_value | 2 | NULL | YES | double | NULL | NULL | 22 | NULL | NULL | NULL | NULL | double | select,insert,update,references | NULL | ||||
def | target_db | multi_type_column_tab | decimal_value | 3 | NULL | YES | decimal | NULL | NULL | 10 | 3 | NULL | NULL | NULL | decimal(10,3) | select,insert,update,references | NULL | ||||
def | target_db | multi_type_column_tab | date_value | 4 | NULL | YES | date | NULL | NULL | NULL | NULL | NULL | NULL | NULL | date | select,insert,update,references | NULL | ||||
def | target_db | multi_type_column_tab | time_value | 5 | NULL | YES | time | NULL | NULL | NULL | NULL | 0 | NULL | NULL | time | select,insert,update,references | NULL | ||||
def | target_db | multi_type_column_tab | datetime_value | 6 | NULL | YES | datetime | NULL | NULL | NULL | NULL | 0 | NULL | NULL | datetime | select,insert,update,references | NULL | ||||
def | target_db | multi_type_column_tab | timestamp_value | 7 | NULL | YES | timestamp | NULL | NULL | NULL | NULL | 0 | NULL | NULL | timestamp | select,insert,update,references | NULL | ||||
def | target_db | multi_type_column_tab | year_value | 8 | NULL | YES | year | NULL | NULL | NULL | NULL | NULL | NULL | NULL | year | select,insert,update,references | NULL | ||||
def | target_db | multi_type_column_tab | varchar_value | 9 | NULL | YES | varchar | 100 | 400 | NULL | NULL | NULL | utf8mb4 | utf8mb4_0900_ai_ci | varchar(100) | select,insert,update,references | NULL | ||||
def | target_db | multi_type_column_tab | varbinary_value | 10 | NULL | YES | varbinary | 100 | 100 | NULL | NULL | NULL | NULL | NULL | varbinary(100) | select,insert,update,references | NULL | ||||
def | target_db | multi_type_column_tab | signed_int_value | 11 | NULL | YES | int | NULL | NULL | 10 | 0 | NULL | NULL | NULL | int | select,insert,update,references | NULL | ||||
def | target_db | multi_type_column_tab | unsigned_int_value | 12 | NULL | YES | int | NULL | NULL | 10 | 0 | NULL | NULL | NULL | int unsigned | select,insert,update,references | NULL | ||||
def | target_db | multi_type_column_tab | signed_bigint_value | 13 | NULL | YES | bigint | NULL | NULL | 19 | 0 | NULL | NULL | NULL | bigint | select,insert,update,references | NULL | ||||
def | target_db | multi_type_column_tab | unsigned_bigint_value | 14 | NULL | YES | bigint | NULL | NULL | 20 | 0 | NULL | NULL | NULL | bigint unsigned | select,insert,update,references | NULL | ||||
def | target_db | multi_type_column_tab | bit_value | 15 | NULL | YES | bit | NULL | NULL | 20 | NULL | NULL | NULL | NULL | bit(20) | select,insert,update,references | NULL | ||||
def | target_db | multi_type_column_tab | bool_value | 16 | NULL | YES | tinyint | NULL | NULL | 3 | 0 | NULL | NULL | NULL | tinyint(1) | select,insert,update,references | NULL |