The Wayback Machine - https://web.archive.org/web/20240604212543/https://www.geeksforgeeks.org/how-to-find-tables-that-contain-a-specific-column-in-sql-using-python/
Open In App

How to find tables that contain a specific column in SQL using Python?

Last Updated : 26 Nov, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

MySQL server is an open-source relational database management system that is a major support for web-based applications. Databases and related tables are the main component of many websites and applications as the data is stored and exchanged over the web. In order to access MySQL databases from a web server, we use various modules in Python such as PyMySQL, mysql.connector, etc. 

In this article, we are going to get the tables having a specific column irrespective of the database. First, we are going to connect to a server. The SQL query that is going to be used is:

 select tab.table_schema as database_name,tab.table_name 
 from information_schema.tables as tab 
 inner join information_schema.columns as col
 on col.table_schema = tab.table_schema 
 and col.table_name = tab.table_name 
 and column_name = 'Specified_Column_Name' 
 where tab.table_type = 'BASE TABLE' 
 order by tab.table_schema, tab.table_name;


Below are some programs which depict how to get the MySQL tables having a particular column:

Example 1

Below is the table description of all the tables, the sample database having the following tables which are going to be accessed by a Python script:

Below is the program to get the details of MySQL tables having a particular column:   

Python3




# Module Imports
import mysql.connector as mariadb
import sys
  
  
# Connect to MariaDB Platform
try:
    conn = mariadb.connect(
        user="root",
        password="",
        database="gfg"
    )
  
except mariadb.Error as e:
    print(f"Error connecting to MariaDB Platform: {e}")
    sys.exit(1)
  
  
# Get cursor object
cur = conn.cursor()
  
  
# Get the table and database details having a particular column
cur.execute("select tab.table_schema as database_name,tab.table_name \
             from information_schema.tables as tab \
             inner join information_schema.columns as col \
             on col.table_schema = tab.table_schema \
             and col.table_name = tab.table_name \
             and column_name = 'Worker_ID' \
             where tab.table_type = 'BASE TABLE' \
             order by tab.table_schema, tab.table_name;")
  
  
# Display the tables
for(database_name, tab) in cur:
    print(f"Table Name: {tab}Database Name: {database_name}")


Output:

Example 2

Here is another example to get table details irrespective of the database having a particular column, below is the description of all the tables:

Below is the python script to get table details having particular column Names:

Python3




# Module Imports
import mysql.connector as mariadb
import sys
  
  
# Connect to MariaDB Platform
try:
    conn = mariadb.connect(
        user="root",
        password="",
        database="gfg"
    )
  
except mariadb.Error as e:
    print(f"Error connecting to MariaDB Platform: {e}")
    sys.exit(1)
  
  
# Get cursor object
cur = conn.cursor()
  
  
# Get the table and database details having a particular column
cur.execute("select tab.table_schema as database_name,tab.table_name \
             from information_schema.tables as tab \
             inner join information_schema.columns as col \
             on col.table_schema = tab.table_schema \
             and col.table_name = tab.table_name \
             and column_name = 'Names' \
             where tab.table_type = 'BASE TABLE' \
             order by tab.table_schema, tab.table_name;")
  
  
# Display the tables
for(database_name, tab) in cur:
    print(f"\n\nTable Name: {tab}\nDatabase Name: {database_name}")


Output:



Similar Reads

Select rows that contain specific text using Pandas
While preprocessing data using pandas dataframe there may be a need to find the rows that contain specific text. In this article we will discuss methods to find the rows that contain specific text in the columns or rows of a dataframe in pandas. Dataset in use: jobAge_RangeSalaryCredit-RatingSavingsBuys_HoneOwnMiddle-agedHighFair10000YesGovtYoungLo
3 min read
Python | Plotting column charts in excel sheet with data tables using XlsxWriter module
Prerequisite: Create and Write on an excel sheetXlsxWriter is a Python library using which one can perform multiple operations on excel files like creating, writing, arithmetic operations and plotting graphs. Let’s see how to plot charts with Data tables using realtime data.Charts are composed of at least one series of one or more data points. Seri
5 min read
How to Drop Rows that Contain a Specific Value in Pandas?
In this article, we will discuss how to drop rows that contain a specific value in Pandas. Dropping rows means removing values from the dataframe we can drop the specific value by using conditional or relational operators. Method 1: Drop the specific value by using Operators We can use the column_name function along with the operator to drop the sp
3 min read
How to Drop Rows that Contain a Specific String in Pandas?
In Pandas, we can drop rows from a DataFrame that contain a specific string in a particular column. In this article, we are going to see how to drop rows that contain a specific string in Pandas. Eliminating Rows Containing a Specific StringBasically, this function will search for the string in the given column and return the rows respective to tha
4 min read
How to import excel file and find a specific column using Pandas?
To read specific columns from an Excel file in Pandas, you have the flexibility to use either column indices or letters. This is achieved by setting the usecols argument, which can take a comma-separated string or a list containing column identifying letters or indices. In this article, we will learn how to import an Excel file into a data frame an
5 min read
How to merge two csv files by specific column using Pandas in Python?
In this article, we are going to discuss how to merge two CSV files there is a function in pandas library pandas.merge(). Merging means nothing but combining two datasets together into one based on common attributes or column. Syntax: pandas.merge() Parameters : data1, data2: Dataframes used for merging.how: {‘left’, ‘right’, ‘outer’, ‘inner’}, def
3 min read
How to Update all the Values of a Specific Column of SQLite Table using Python ?
In this article, we are going to update all the values of a specific column of a given SQLite table using Python. In order to update all the columns of a particular table in SQL, we use the UPDATE query. The UPDATE statement in SQL is used to update the data of an existing table in the database. We can update single columns as well as multiple colu
3 min read
Python SQLite - Update Specific Column
In this article, we will discuss how to update a specific column of a table in SQLite using Python. In order to update a particular column in a table in SQL, we use the UPDATE query. The UPDATE statement in SQL is used to update the data of an existing table in the database. We can update single columns as well as multiple columns using UPDATE stat
3 min read
Rename specific column(s) in Pandas
Given a pandas Dataframe, let's see how to rename specific column(s) names using various methods. First, let's create a Dataframe: C/C++ Code # import pandas package import pandas as pd # defining a dictionary d = {"Name": ["John", "Mary", "Helen"], "Marks": [95, 75, 99], "Roll No": [12, 2
5 min read
Removing duplicate rows based on specific column in PySpark DataFrame
In this article, we are going to drop the duplicate rows based on a specific column from dataframe using pyspark in Python. Duplicate data means the same data based on some condition (column values). For this, we are using dropDuplicates() method: Syntax: dataframe.dropDuplicates(['column 1','column 2','column n']).show() where, dataframe is the in
1 min read
Practice Tags :