I want to get the column data type of a mysql table.
我想获取mysql表的列数据类型。
Thought I could use MYSQLFIELD
structure but it was enumerated field types.
虽然我可以使用MYSQLFIELD结构,但它是枚举字段类型。
Then I tried with mysql_real_query()
然后我尝试使用mysql_real_query()
The error which i am getting is query was empty
我得到的错误是查询为空
How do I get the column data type?
如何获得列数据类型?
10 个解决方案
#1
67
You can use the information_schema columns table:
可以使用information_schema列表:
SELECT DATA_TYPE FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_name = 'tbl_name' AND COLUMN_NAME = 'col_name';
#2
49
The query below returns a list of information about each field, including the MySQL field type. Here is an example:
下面的查询返回关于每个字段的信息列表,包括MySQL字段类型。这是一个例子:
SHOW FIELDS FROM tablename
/* returns "Field", "Type", "Null", "Key", "Default", "Extras" */
See this manual page.
见本手册页面。
#3
22
Most answers are duplicates, it might be useful to group them. Basically two simple options have been proposed.
大多数答案都是重复的,对它们进行分组可能是有用的。基本上提出了两个简单的选择。
First option
The first option has 4 different aliases, some of which are quite short :
第一个选项有4个不同的别名,其中一些很短:
EXPLAIN db_name.table_name;
DESCRIBE db_name.table_name;
SHOW FIELDS FROM db_name.table_name;
SHOW COLUMNS FROM db_name.table_name;
(NB : as an alternative to db_name.table_name
, one can use a second FROM
: db_name FROM table_name
).
(NB:作为db_name的替代品。表_name,可以使用第二个FROM: db_name FROM table_name)。
This gives something like :
这就给出了如下内容:
+------------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------------+--------------+------+-----+---------+-------+
| product_id | int(11) | NO | PRI | NULL | |
| name | varchar(255) | NO | MUL | NULL | |
| description | text | NO | | NULL | |
| meta_title | varchar(255) | NO | | NULL | |
+------------------+--------------+------+-----+---------+-------+
Second option
The second option is a bit longer :
第二个选择稍微长一点:
SELECT
COLUMN_NAME, DATA_TYPE
FROM
INFORMATION_SCHEMA.COLUMNS
WHERE
TABLE_SCHEMA = 'db_name'
AND
TABLE_NAME = 'table_name';
It is also less talkative :
它也不那么健谈:
+------------------+-----------+
| column_name | DATA_TYPE |
+------------------+-----------+
| product_id | int |
| name | varchar |
| description | text |
| meta_title | varchar |
+------------------+-----------+
It has the advantage of allowing selection per column, though, using AND COLUMN_NAME = 'column_name'
(or like
).
不过,它允许对每个列进行选择,使用和COLUMN_NAME = ' COLUMN_NAME '(或类似)。
#4
10
To get data types of all columns:
获取所有列的数据类型:
describe table_name
or just a single column:
或者仅仅是一栏:
describe table_name column_name
#5
5
Refer this link
请参考这个链接
mysql> SHOW COLUMNS FROM mytable FROM mydb;
mysql> SHOW COLUMNS FROM mydb.mytable;
Hope this may help you
希望这能对你有所帮助
#6
#7
2
First select the Database using use testDB;
then execute
首先使用testDB选择数据库;然后执行
desc `testDB`.`images`;
-- or
SHOW FIELDS FROM images;
Output:
输出:
#8
2
Query to find out all the datatype of columns being used in any database
查询以查找任何数据库中使用的列的所有数据类型
SELECT distinct DATA_TYPE FROM INFORMATION_SCHEMA.columns
WHERE table_schema = '<db_name>' AND column_name like '%';
#9
1
SHOW COLUMNS FROM mytable
从mytable显示列
Self contained complete examples are often useful.
自我包含的完整示例通常是有用的。
<?php
// The server where your database is hosted localhost
// The name of your database mydatabase
// The user name of the database user databaseuser
// The password of the database user thesecretpassword
// Most web pages are in utf-8 so should be the database array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8")
try
{
$pdo = new PDO("mysql:host=localhost;dbname=mydatabase", "databaseuser", "thesecretpassword", array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
}
catch(PDOException $e)
{
die('Could not connect: ' . $e->getMessage());
}
$sql = "SHOW COLUMNS FROM mytable";
$query = $pdo->prepare($sql);
$query->execute();
$err = $query->errorInfo();
$bug = $err[2];
if ($bug != "") { echo "<p>$bug</p>"; }
while ($row = $query->fetch(PDO::FETCH_ASSOC))
{
echo "<pre>" . print_r($row, true) . "</pre>";
}
/* OUTPUT SAMPLE
Array
(
[Field] => page_id
[Type] => char(40)
[Null] => NO
[Key] =>
[Default] =>
[Extra] =>
)
Array
(
[Field] => last_name
[Type] => char(50)
More ...
*/
?>
#10
1
ResultSet rs = Sstatement.executeQuery("SELECT * FROM Table Name");
ResultSetMetaData rsMetaData = rs.getMetaData();
int numberOfColumns = rsMetaData.getColumnCount();
System.out.println("resultSet MetaData column Count=" + numberOfColumns);
for (int i = 1; i <= numberOfColumns; i++) {
System.out.println("column number " + i);
System.out.println(rsMetaData.getColumnTypeName(i));
}
#1
67
You can use the information_schema columns table:
可以使用information_schema列表:
SELECT DATA_TYPE FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_name = 'tbl_name' AND COLUMN_NAME = 'col_name';
#2
49
The query below returns a list of information about each field, including the MySQL field type. Here is an example:
下面的查询返回关于每个字段的信息列表,包括MySQL字段类型。这是一个例子:
SHOW FIELDS FROM tablename
/* returns "Field", "Type", "Null", "Key", "Default", "Extras" */
See this manual page.
见本手册页面。
#3
22
Most answers are duplicates, it might be useful to group them. Basically two simple options have been proposed.
大多数答案都是重复的,对它们进行分组可能是有用的。基本上提出了两个简单的选择。
First option
The first option has 4 different aliases, some of which are quite short :
第一个选项有4个不同的别名,其中一些很短:
EXPLAIN db_name.table_name;
DESCRIBE db_name.table_name;
SHOW FIELDS FROM db_name.table_name;
SHOW COLUMNS FROM db_name.table_name;
(NB : as an alternative to db_name.table_name
, one can use a second FROM
: db_name FROM table_name
).
(NB:作为db_name的替代品。表_name,可以使用第二个FROM: db_name FROM table_name)。
This gives something like :
这就给出了如下内容:
+------------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------------+--------------+------+-----+---------+-------+
| product_id | int(11) | NO | PRI | NULL | |
| name | varchar(255) | NO | MUL | NULL | |
| description | text | NO | | NULL | |
| meta_title | varchar(255) | NO | | NULL | |
+------------------+--------------+------+-----+---------+-------+
Second option
The second option is a bit longer :
第二个选择稍微长一点:
SELECT
COLUMN_NAME, DATA_TYPE
FROM
INFORMATION_SCHEMA.COLUMNS
WHERE
TABLE_SCHEMA = 'db_name'
AND
TABLE_NAME = 'table_name';
It is also less talkative :
它也不那么健谈:
+------------------+-----------+
| column_name | DATA_TYPE |
+------------------+-----------+
| product_id | int |
| name | varchar |
| description | text |
| meta_title | varchar |
+------------------+-----------+
It has the advantage of allowing selection per column, though, using AND COLUMN_NAME = 'column_name'
(or like
).
不过,它允许对每个列进行选择,使用和COLUMN_NAME = ' COLUMN_NAME '(或类似)。
#4
10
To get data types of all columns:
获取所有列的数据类型:
describe table_name
or just a single column:
或者仅仅是一栏:
describe table_name column_name
#5
5
Refer this link
请参考这个链接
mysql> SHOW COLUMNS FROM mytable FROM mydb;
mysql> SHOW COLUMNS FROM mydb.mytable;
Hope this may help you
希望这能对你有所帮助
#6
3
Please use the below mysql query.
请使用下面的mysql查询。
SELECT COLUMN_NAME, DATA_TYPE, CHARACTER_MAXIMUM_LENGTH
FROM information_schema.columns
WHERE table_schema = '<DATABASE NAME>'
AND table_name = '<TABLE NAME>'
AND COLUMN_NAME = '<COLOMN NAME>'
#7
2
First select the Database using use testDB;
then execute
首先使用testDB选择数据库;然后执行
desc `testDB`.`images`;
-- or
SHOW FIELDS FROM images;
Output:
输出:
#8
2
Query to find out all the datatype of columns being used in any database
查询以查找任何数据库中使用的列的所有数据类型
SELECT distinct DATA_TYPE FROM INFORMATION_SCHEMA.columns
WHERE table_schema = '<db_name>' AND column_name like '%';
#9
1
SHOW COLUMNS FROM mytable
从mytable显示列
Self contained complete examples are often useful.
自我包含的完整示例通常是有用的。
<?php
// The server where your database is hosted localhost
// The name of your database mydatabase
// The user name of the database user databaseuser
// The password of the database user thesecretpassword
// Most web pages are in utf-8 so should be the database array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8")
try
{
$pdo = new PDO("mysql:host=localhost;dbname=mydatabase", "databaseuser", "thesecretpassword", array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
}
catch(PDOException $e)
{
die('Could not connect: ' . $e->getMessage());
}
$sql = "SHOW COLUMNS FROM mytable";
$query = $pdo->prepare($sql);
$query->execute();
$err = $query->errorInfo();
$bug = $err[2];
if ($bug != "") { echo "<p>$bug</p>"; }
while ($row = $query->fetch(PDO::FETCH_ASSOC))
{
echo "<pre>" . print_r($row, true) . "</pre>";
}
/* OUTPUT SAMPLE
Array
(
[Field] => page_id
[Type] => char(40)
[Null] => NO
[Key] =>
[Default] =>
[Extra] =>
)
Array
(
[Field] => last_name
[Type] => char(50)
More ...
*/
?>
#10
1
ResultSet rs = Sstatement.executeQuery("SELECT * FROM Table Name");
ResultSetMetaData rsMetaData = rs.getMetaData();
int numberOfColumns = rsMetaData.getColumnCount();
System.out.println("resultSet MetaData column Count=" + numberOfColumns);
for (int i = 1; i <= numberOfColumns; i++) {
System.out.println("column number " + i);
System.out.println(rsMetaData.getColumnTypeName(i));
}