- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How do I use `SHOW COLUMNS` as a valid data source for a table?
For this, you can use INFORMATION_SCHEMA.COLUMNS as shown in the following syntax −
SELECT *FROM (SELECT *FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME= 'yourTableName') anyAliasName;
Let us first create a table:
mysql> create table DemoTable ( StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY, StudentFirstName varchar(20), StudentLastName varchar(20), StudentAge int ); Query OK, 0 rows affected (1.51 sec)
Here is the query to use `SHOW COLUMNS` as a valid data source −
mysql> SELECT *FROM (SELECT *FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME= 'DemoTable')tbl;
This will produce the following output −
+---------------+--------------+-------------+------------------+------------------+----------------+-------------+-----------+--------------------------+------------------------+-------------------+---------------+--------------------+--------------------+-----------------+-------------+------------+----------------+---------------------------------+----------------+-----------------------+--------+ | 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 | sample | DemoTable | StudentId | 1 | NULL | NO | int | NULL | NULL | 10 | 0 | NULL | NULL | NULL | int(11) | PRI | auto_increment | select,insert,update,references | | | NULL | | def | sample | DemoTable | StudentFirstName | 2 | NULL | YES | varchar | 20 | 60 | NULL | NULL | NULL | utf8 | utf8_general_ci | varchar(20) | | | select,insert,update,references | | | NULL | | def | sample | DemoTable | StudentLastName | 3 | NULL | YES | varchar | 20 | 60 | NULL | NULL | NULL | utf8 | utf8_general_ci | varchar(20) | | | select,insert,update,references | | | NULL | | def | sample | DemoTable | StudentAge | 4 | NULL | YES | int | NULL | NULL | 10 | 0 | NULL | NULL | NULL | int(11) | | | select,insert,update,references | | | NULL | +---------------+--------------+-------------+------------------+------------------+----------------+-------------+-----------+--------------------------+------------------------+-------------------+---------------+--------------------+--------------------+-----------------+-------------+------------+----------------+---------------------------------+----------------+-----------------------+--------+ 4 rows in set (0.00 sec)
- Related Articles
- How do I list all the columns in a MySQL table?
- How do I show unique constraints of a table in MySQL?
- How do I show the schema of a table in a MySQL database?
- How to create a table TAB2 having same attributes & columns as for table TAB1
- How do I use the ternary operator ( ? : ) in PHP as a shorthand for “if / else”?
- How can I use MySQL subquery as a table in FROM clause?
- How do we include attributes for table columns in HTML?
- How do I use tabHost for Android?
- How do I view the auto_increment value for a table in MySQL?
- How can I return the values of columns from MySQL table as a set of values?
- How do I get the current AUTO_INCREMENT value for a table in MySQL?
- How do I show a MySQL warning that just happened?
- How do I alter a MySQL table column defaults?
- How do I plot only a table in Matplotlib?
- How can we use MySQL ALTER TABLE command for adding comments on columns?

Advertisements