DEV Community

Cover image for MariaDb Quick-tip #6 - Find table or column
Allan Simonsen
Allan Simonsen

Posted on

2 1

MariaDb Quick-tip #6 - Find table or column

MariaDB tips and tricks

This is part of a series of quick tips and tricks I have accumulated over the year, that I think can be useful for others.
If you have similar short tips and tricks please leave a comment.

Find table by name

If you have ever worked on a big database with a huge number of tables and you are not yet familiar with the whole data model, you know how hard it is to locate that specific table you are looking for. Or maybe you just need to get a list of tables that have similar names to put into that Power Point presentation you are working on.
Then the following query may be useful to you.

SELECT TABLE_SCHEMA, TABLE_NAME, TABLE_ROWS  
  FROM information_schema.tables 
 WHERE table_name LIKE '%dep%'
Enter fullscreen mode Exit fullscreen mode

DBeaver screenshot

Find table that have a specific column

If you have been a programmer for some time and worked on different projects for different customers then you have probably come across databases with few or entirely without foreign key constraints. There can be a lot of reasons for not having foreign keys constraint and I won't go into them here, but one consequence of not having them is that finding tables that is related to other tables becomes hard so you need another way of finding related tables. And I do hope that the person who implemented the tables named the columns relating the tables by the same column name or a similar name. And if this is the case you can query all your database tables to find all tables containing columns with a specific name. This query below does that.

 SELECT CONCAT(t.TABLE_SCHEMA, '.', t.TABLE_NAME), c.COLUMN_NAME 
   FROM information_schema.TABLES t
   JOIN information_schema.COLUMNS c ON t.TABLE_NAME = c.TABLE_NAME 
  WHERE c.COLUMN_NAME LIKE '%name%'
    AND t.TABLE_SCHEMA <> 'information_schema'
Enter fullscreen mode Exit fullscreen mode

DBeaver screenshot

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay