技术文章和资源

技术文章(时间排序)

热门类别

Python PHP MySQL JDBC Linux

如何获取 MySQL 表列的数据类型?

mysqlmysqli database

您可以借助"information_schema.columns"获取 MySQL 表列的数据类型。

语法如下 −

SELECT DATA_TYPE from INFORMATION_SCHEMA.COLUMNS where
table_schema = 'yourDatabaseName' and table_name = 'yourTableName'

为了理解上述语法,我们首先创建一个表 −

mysql> create table DataTypeDemo
−> (
   −> Id int,
   −> Address varchar(200),
   −> Money decimal(10,4)
−> );
Query OK, 0 rows affected (0.60 sec)

应用上述语法获取 MySQL 列的数据类型。查询如下 −

mysql> select data_type from information_schema.columns where table_schema = 'business' and able_name = 'DataTypeDemo';

The following is the output −

+-----------+
| DATA_TYPE |
+-----------+
| int       |
| varchar   |
| decimal   |
+-----------+
3 rows in set (0.00 sec)

如果需要,在输出的数据类型之前也包含列名。查询如下 −

mysql> select column_name,data_type from information_schema.columns where table_schema = 'business' and table_name = 'DataTypeDemo';

以下输出显示与数据类型对应的列名 −

+-------------+-----------+
| COLUMN_NAME | DATA_TYPE |
+-------------+-----------+
| Id          | int       |
| Address     | varchar   |
| Money       | decimal   |
+-------------+-----------+
3 rows in set (0.00 sec)


相关文章