MySQL Select displaying Data type in a separate column?


You can use INFORMATION_SCHEMA.COLUMNS for this. Let us first create a table −

mysql> create table DemoTable
   (
   Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   Name varchar(20)
   );
Query OK, 0 rows affected (0.73 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable(Name) values('Chris');
Query OK, 1 row affected (0.25 sec)

mysql> insert into DemoTable(Name) values('Robert');
Query OK, 1 row affected (0.26 sec)

mysql> insert into DemoTable(Name) values('Sam');
Query OK, 1 row affected (0.12 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

This will produce the following output −

+----+--------+
| Id | Name   |
+----+--------+
| 1  | Chris  |
| 2  | Robert |
| 3  | Sam    |
+----+--------+
3 rows in set (0.00 sec)

Following is the query to display data type and data value −

mysql> SELECT DemoTable.Name, col1.DATA_TYPE FROM DemoTable,INFORMATION_SCHEMA.COLUMNS col1 WHERE col1.TABLE_NAME='DemoTable' AND COLUMN_NAME='Name';

This will produce the following output −

+--------+-----------+
| Name   | DATA_TYPE |
+--------+-----------+
| Chris  | varchar   |
| Robert | varchar   |
| Sam    | varchar   |
+--------+-----------+
3 rows in set (0.04 sec)

Updated on: 30-Jul-2019

53 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements