Find tables with specific column names in MySQL
This article explains how to find all tables with a specific column name in MySQL, but before that, you must understand the schema (database) in which these tables are stored. Let's start with that.
Schemas in MySQL
A database/schema specifies logical restrictions, including table names, fields, data types, and connections between these entities, which govern how data is arranged in a relational database.
Create the schema/database:
mysql> CREATE SCHEMA schema_name;
or
mysql> CREATE DATABASE schema_name;
These queries above are used to create a schema/database, showing similar output. You must first select the schema you just created in order to use and run queries against that schema.
Using Schema:
mysql> USE schema_name;
However, if you want to drop a specific schema or database, then you have to run the following query.
Delete the schema:
mysql> DROP SCHEMA schema_name;
Create tables in MySQL Schema and list them
All data in a database is stored in tables, which are called database objects.
Data is logically arranged in a table using a spreadsheet-like layout of rows and columns. Each column represents a record field, and each row represents a different record.
Create the table:
mysql> CREATE TABLE table_name(
column_name datatype,
column_name datatype,
...
);
In MySQL, table and column names are not case sensitive. This means that if you create a table named MY_TABLE and my_table, both are identical.
If you do not need a table, you can remove it from the schema using the following query.
mysql> DROP TABLE table_name;
A schema/database can contain many tables. You can imagine a schema like the folders you create on your operating system.
Sometimes, it becomes overwhelming and you may forget about the tables you created in a schema and want to list all the tables in a particular schema.
In this case, the following query is very useful. First, make sure you have selected your schema using USE schema_name; ask.
Show all tables:
mysql> SHOW TABLES;
If you also want to know, what are the table types in your schema, you can further modify the query above:
mysql> SHOW FULL TABLES;
In addition, if you want to get a list of tables in another schema without switching the current schema, MySQL provides this functionality; you can use the FORM or IN clause in your query:
mysql> SHOW TABLES FROM schema_name;
or
mysql> SHOW TABLES IN schema_name;
These are the basic queries that help you to get the basic details from the schema. However, you can also modify this query more which will fetch the exact results you want and you can like it more.
List tables with a specific column name in MySQL
If you want to find tables from any schema that contain some specific columns, you are looking for that.
With the help of MySQL view called INFORMATION_SCHEMA.COLUMNS
, we can get and view all columns or specific columns from all tables.
By default, it will get columns from every TABLE in the schema. However, you can filter these columns using the SELECT, FROM, and WHERE clauses.
mysql> SELECT DISTINCT table_name
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME IN ('column_1', 'column_2', ...)
AND TABLE_SCHEMA = 'schema_name';
Or, a simpler version of the above query:
mysql> SELECT * FROM INFORMATION_SCHEMA.COLUMNS
WHERE column_name = 'column_names';
If you are not sure about the exact names of the columns you want to get, there is an alternative option: regular expressions.
MySQL supports regular expressions from one of many regular expression combinations. So, for example, you can use the WHERE and LIKE clauses to find the columns you want with just one word.
Subsequently, using '%column_word%'
the regular expression, '%column_word%'
any value containing the mentioned word at any position will be found.
mysql> SELECT * FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME LIKE '%column_word%';
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Display tables and database structure in MySQL
Publish Date:2025/04/23 Views:97 Category:MySQL
-
Today, we will learn about queries in MySQL that can display the table and database structure. We will use the mysqldump utility, DESCRIBE the , SHOW TABLES and SHOW CREATE TABLE the statements. We are using MySQL version 8.0.28 while writi
Select first row from MySQL table
Publish Date:2025/04/23 Views:112 Category:MySQL
-
Today, we will explore three scenarios and their solutions where we want to select the first row from a MySQL table. In the first scenario, we will learn to get the first row from a MySQL table where there are multiple instances of a partic
Insert timestamp into MySQL table
Publish Date:2025/04/23 Views:77 Category:MySQL
-
Today, we will learn how to TIMESTAMP insert date and time into a type column of a MySQL table according to the table definition. Create a MySQL table First, we will create the tables that we will use in this tutorial. Sample code: CREATE T
The difference between two tables in MySQL
Publish Date:2025/04/23 Views:102 Category:MySQL
-
In this article, we will learn how to find the difference between two tables in MySQL. The difference between two tables in MySQL We often need to compare two tables to find records in one table that have no matching records in the other ta
MySQL sorts data alphabetically
Publish Date:2025/04/23 Views:129 Category:MySQL
-
In this article, we will learn about various ways to sort data alphabetically in MySQL. Sort MySQL data alphabetically When you use the SELECT command to query data from a table, the rows in the result set are in arbitrary order. To order t
Display the current database in MySQL
Publish Date:2025/04/23 Views:199 Category:MySQL
-
This article focuses on the various queries that can be used to display the current database in MySQL. We will learn by using the Windows command line and MySQL Workbench. Display the current database in MySQL We can use the following query
Check if a database exists in MySQL
Publish Date:2025/04/23 Views:179 Category:MySQL
-
In this article, we will introduce many ways to check if a database exists in MySQL. Check if the database exists in MySQL The system schema is the schema that MySQL uses. It includes tables that contain data needed by a running MySQL serve
Get the sum of multiple columns in MySQL
Publish Date:2025/04/23 Views:125 Category:MySQL
-
In this article, we will learn how to sum multiple columns in MySQL. Sum multiple columns in MySQL You can use aggregate functions SUM() to calculate the total value in a collection. SUM() The function calculation does not consider NULL val
MySQL ForEach Loop
Publish Date:2025/04/23 Views:164 Category:MySQL
-
This article describes how to simulate a foreach loop in MySQL using INSERT, SELECT, WHERE, and JOIN in one statement. MySQL foreach loop To understand foreach loop simulation, let us create three tables with the following names and attribu