How to get the selected database & the database Information?

How to get the selected database & the database Information?


2






Rahul Kumar (@rahul)

Let's assume that you forgot the name of a database or table, or what the structure of a given table is (for example, what its columns are called)? MySQL addresses this problem through several statements that provide information about the databases and tables it supports. Let's learn this quickly..

How to get the list of databases?

How to get the list of databases?

      mysql> SELECT DATABASE();
+------------+
| DATABASE() |
+------------+
| college  |
| university  |
+------------+
    

How to get the current database?

You have previously seen SHOW DATABASES, which lists the databases managed by the server. To find out which database is currently selected, use the DATABASE() function:

How to get the current databases?

      mysql> SELECT DATABASE();
+------------+
| DATABASE() |
+------------+
| college  |
+------------+


    

If you have not yet selected any database, the result is NULL.

How to get the list of tables of the selected database?

To find out what tables the default database contains (for example, when you are not sure about the name of a table), use this statement:

How to get the list of tables of selected database?

      mysql> SHOW TABLES;
+---------------------+
| Tables_in_college |
+---------------------+
| student               |
| course                 |
+---------------------+
    

If you want to find more information about the table and its structure then you can use the following key

Get structure of the table

      mysql> DESCRIBE pet;
+---------+-------------+------+-----+---------+-------+
| Field   | Type        | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| name    | varchar(20) | YES  |     | NULL    |       |
| owner   | varchar(20) | YES  |     | NULL    |       |
| species | varchar(20) | YES  |     | NULL    |       |
| sex     | char(1)     | YES  |     | NULL    |       |
| birth   | date        | YES  |     | NULL    |       |
| death   | date        | YES  |     | NULL    |       |
+---------+-------------+------+-----+---------+-------+
    

Add a thoughtful comment...

✨ Explore more tech insights and coding wonders with @dsabyte! Your journey in innovation has just begun. Keep learning, keep sharing, and let's continue to code a brighter future together. Happy exploring! 🚀❤️

  • #mysql
  • #query
  • #mysqli
  • #dbms
  • #rdbms
  • #database

Subscribe to our newsletter.

Join the "News Later" community by entering your email. It's quick, it's easy, and it's your key to unlocking future tech revelations.

Weekly Updates

Every week, we curate and deliver a collection of articles, blogs and chapters directly to your inbox. Stay informed, stay inspired, and stay ahead in the fast-paced world of technology.

No spam

Rest assured, we won't clutter your inbox with unnecessary emails. No spam, only meaningful insights, and valuable content designed to elevate your tech experience.

© 2024 @dsabyte. All rights reserved