I want to get the column data type of a mysql table.
Thought I could use MYSQLFIELD
structure but it was enumerated field types.
Then I tried wi
Refer this link
mysql> SHOW COLUMNS FROM mytable FROM mydb;
mysql> SHOW COLUMNS FROM mydb.mytable;
Hope this may help you
Most answers are duplicates, it might be useful to group them. Basically two simple options have been proposed.
The first option has 4 different aliases, some of which are quite short :
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
).
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 | |
+------------------+--------------+------+-----+---------+-------+
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
).
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 '%';
SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA='SCHEMA_NAME' AND COLUMN_KEY='PRI'; WHERE COLUMN_KEY='PRI';
To get data types of all columns:
describe table_name
or just a single column:
describe table_name column_name