Thursday, June 11, 2026
HomeLanguagesHow to Show All Tables in MySQL using Python?

How to Show All Tables in MySQL using Python?

A connector is employed when we have to use mysql with other programming languages. The work of mysql-connector is to provide access to MySQL Driver to the required language. Thus, it generates a connection between the programming language and the MySQL Server.

In order to make python interact with the MySQL database, we use Python-MySQL-Connector. Here we will try implementing SQL queries which will show the names of all the tables present in the database or server.

Syntax:

To show the name of tables present inside a database:

SHOW Tables;

To show the name of tables present inside a server:

SELECT table_name

FROM information_schema.tables;

Database in use:

Schema of the database used

The following programs implement the same.

Example 1: Display table names present inside a database:

Python3




import mysql.connector
 
mydb = mysql.connector.connect(
    host="localhost",
    user="root",
    password="",
    database="gfg"
)
 
mycursor = mydb.cursor()
 
mycursor.execute("Show tables;")
 
myresult = mycursor.fetchall()
 
for x in myresult:
    print(x)


Output:

Table names in gfg  database

Example 2: Display table names present inside a server:

Python3




import mysql.connector
 
mydb = mysql.connector.connect(
  host="localhost",
  user="root",
  password="",
)
 
mycursor = mydb.cursor()
 
mycursor.execute("SELECT table_name FROM information_schema.tables;")
 
myresult = mycursor.fetchall()
 
for x in myresult:
  print(x)


Output:

Table names in server

RELATED ARTICLES

1 COMMENT

Most Popular

Dominic
32515 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6896 POSTS0 COMMENTS
Nicole Veronica
12012 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12109 POSTS0 COMMENTS
Shaida Kate Naidoo
7019 POSTS0 COMMENTS
Ted Musemwa
7262 POSTS0 COMMENTS
Thapelo Manthata
6976 POSTS0 COMMENTS
Umr Jansen
6963 POSTS0 COMMENTS