MySQL Metadata Queries – Column Structure and Definition With the COLUMNS Table

The INFORMATION_SCHEMA database is full of information and metadata about your database(s). Columns are a necessity and their definition ensures sound storage and data integrity. Learn all about them with the COLUMNS table.

The Newsletter for PHP and MySQL Developers

Receive a copy of my ebook, “10 MySQL Tips For Everyone”, absolutely free when you subscribe to the OpenLampTech newsletter.

Image by Gerd Altmann from Pixabay 

💡 Clarification: The INFORMATION_SCHEMAtables‘ are actually VIEWS.

INFORMATION_SCHEMA COLUMNS Table

While there are several columns that make up the COLUMNS table, in this query I’ll SELECT just a few to give you an idea of what is available:

SELECT COLUMN_NAME, ORDINAL_POSITION, DATA_TYPE, COLUMN_DEFAULT, IS_NULLABLE
FROM COLUMNS
WHERE TABLE_SCHEMA = 'walking'
AND TABLE_NAME = 'walking_stats';

Like always, the key to retrieving those specific rows you are interested in is based on the WHERE clause conditional(s).

Related: Limit Rows with the WHERE clause - MySQL Beginner Series

In the COLUMNS table, it comes down to filtering the rows based on the TABLE_SCHEMA and TABLE_NAME columns.


Recommended Beginner Book: I highly recommend the book, Learning SQL: Generate, Manipulate, and Retrieve Data, for SQL beginners. This was my first real SQL book and helped solidify so many concepts for me. You won’t be disappointed.


INFORMATION_SCHEMA COLUMNS Table For Scripting

You may find good use in creating some ‘base scripts‘ from the data in the COLUMNS table. There are absolutely other means to create Data Definition Language (DDL) scripts for your tables but you can make some of them yourself.

SELECT
CONCAT('ALTER TABLE some_table ADD COLUMN ' ,COLUMN_NAME, ' ',
CASE WHEN UPPER(DATA_TYPE) = 'DECIMAL' THEN CONCAT(UPPER(DATA_TYPE),'(', NUMERIC_PRECISION,',', NUMERIC_SCALE, ')')
ELSE UPPER(DATA_TYPE) END,
';') AS cols_definition
FROM COLUMNS
WHERE TABLE_SCHEMA = 'walking'
AND TABLE_NAME = 'walking_stats';

Using SQL to create SQL is quite powerful and handy. Although the above example produces the beginnings of a ‘rough script‘, you get the idea of some of the SQL scripting you can do with the information in the COLUMNS table.

Thank you for reading this post. Please share it with someone else who would enjoy it as well.




Josh Otwell has a passion to grow as a PHP Developer, SQL expert, and technical blogger/writer.

Disclaimer: The majority of examples in this post, are performed in a personal development/learning workstation environment and should not be considered production quality or ready. Your particular goals and needs may vary. Like always, just because you can do something doesn’t mean you should. My opinions are my own.

More ways I can help

Disclosure: Some of this blog post’s services and product links are affiliate links. At no additional cost to you, should you make a purchase by clicking through one of them, I will receive a commission.

The Newsletter for PHP and MySQL Developers

Receive a copy of my ebook, “10 MySQL Tips For Everyone”, absolutely free when you subscribe to the OpenLampTech newsletter.


📰 Get your brand, product, or service the attention it needs with affordable classified ad placement in the OpenLampTech newsletter. Thank you for your support!


Hey thanks for commenting! Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.