
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
Getting Information About MySQL Databases and Tables
It is possible for the user to forget the name of the database or table or the structure of table or the name of the columns. This issue can be solved using MySQL since it supports many statements that provide information about the databases and tables which it supports.
The ‘SHOW DATABASES’ query can be used to list all the databases that are managed by the server. To see which database is currently in use, the ‘DATABASE()’ function.
Let us understand this query in the below section −
Query
mysql> SELECT DATABASE();
Output
+---------------------+ | DATABASE() | +---------------------+ | databaseInUse | +---------------------+
If no database is selected, it results in output ‘NULL’.
To see which tables the default database contains, the below query can be used −
Query
mysql> SHOW TABLES;
Output
+-----------------------------------+ | Tables_in_databaseInUse | +-----------------------------------+ | val1 | | val1 | +-----------------------------------+
The column name in the output which is produced by the above query is ‘Tables_in_databaseInUse’, where databaseInUse is the name of the database that is in use/selected.
If the user wants to know more information about the structure of the table, the ‘DESCRIBE’ statement can be used. It will display the information about every table’s columns −
Query
mysql> DESCRIBE pet;
Output
+---------+-------------+-------+------+-----------+--------+ | Field | Type | Null | Key | Default | Extra | +---------+-------------+-------+------+-----------+--------+ | name | varchar(20) | YES | | NULL | | | owner | varchar(20) | YES | | NULL | | +---------+-------------+-------+------+-----------+--------+
The field indicates the column name, ‘Type’ refers to the data type of the column, ‘NULL’ indicates if the column can contain NULL values or not, ‘Key’ refers to whether the column is indexed or not, and ‘Default’ specifies the column’s default value. The ‘Extra’ displays special information about the columns. If a column was created using the ‘AUTO_INCREMENT’ option, the value is ‘auto_increment’, not empty.
- Related Articles
- Getting information from pooled tables in SAP system
- List of non-empty tables in all your MySQL databases?
- How to copy tables or databases from one MySQL server to another MySQL server?
- Finding total number of rows of tables across multiple databases in MySQL?
- How can I display all databases in MySQL and for each database show all tables?
- Explain about triggers and active databases in DBMS
- How to move data between two tables with columns in different MySQL databases?
- How to GRANT SELECT ON all tables in all databases on a server with MySQL?
- Getting File Information in Perl
- Display information about field names in MySQL including TYPE, KEY, etc.
- Making Inferences About Missing Information
- Get a list of MySQL databases and version?
- mysql_upgrade - Check and Upgrade MySQL Tables
- How can I check the tables of databases other than current database?
- Getting information in MS SharePoint workflow from SAP
