Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

MySQL Metadata

MySQL Metadata

MySQL metadata is information about the database and its objects (such as tables, columns, indexes, etc.).

Metadata is stored in system tables, which are located in the information_schema database of MySQL. By querying these system tables, you can obtain detailed information about the database structure, objects, and other related information.

You may want to know the following three kinds of information in MySQL:

At the MySQL command prompt, we can easily obtain the above server information. But if you use a scripting language such as Perl or PHP, you need to call specific interface functions to obtain it. We will introduce this in detail next.

The following are some commonly used MySQL metadata queries:

View all databases:



SHOW DATABASES;

Select a database:



USE database_name;

View all tables in a database:



SHOW TABLES;

View the structure of a table:



DESC table_name;

View the indexes of a table:



SHOW INDEX FROM table_name;

View the creation statement of a table:



SHOW CREATE TABLE table_name;

View the number of rows in a table:



SELECT COUNT(*) FROM table_name;

View column information:



SELECT COLUMN_NAME, DATA_TYPE, IS_NULLABLE, COLUMN_KEY

FROM INFORMATION_SCHEMA.COLUMNS

WHERE TABLE_SCHEMA = 'your_database_name'

AND TABLE_NAME = 'your_table_name';

In the above SQL statements, 'your_database_name' and 'your_table_name' are your database name and table name, respectively.

View foreign key information:



SELECT

    TABLE_NAME,

    COLUMN_NAME,

    CONSTRAINT_NAME,

    REFERENCED_TABLE_NAME,

    REFERENCED_COLUMN_NAME

FROM

    INFORMATION_SCHEMA.KEY_COLUMN_USAGE

WHERE

    TABLE_SCHEMA = 'your_database_name'

    AND TABLE_NAME = 'your_table_name'

    AND REFERENCED_TABLE_NAME IS NOT NULL;

Please replace 'your_database_name' and 'your_table_name' in the above SQL statements with the actual database name and table name.


The information_schema Database

information_schema is a system database in MySQL that contains metadata information about the database server, which is stored in the form of tables in the information_schema database.

The SCHEMATA Table

Stores information about databases, such as database name, character set, collation, etc.



SELECT * FROM information_schema.SCHEMATA;

The TABLES Table

Contains information about all tables in a database, such as table name, database name, engine, number of rows, etc.



SELECT * FROM information_schema.TABLES WHERE TABLE_SCHEMA = 'your_database_name';

The COLUMNS Table

Contains information about the columns in a table, such as column name, data type, whether NULL is allowed, etc.



SELECT * FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = 'your_database_name' AND TABLE_NAME = 'your_table_name';

The STATISTICS Table

Provides statistical information about table indexes, such as index name, column name, uniqueness, etc.



SELECT * FROM information_schema.STATISTICS WHERE TABLE_SCHEMA = 'your_database_name' AND TABLE_NAME = 'your_table_name';

The KEY_COLUMN_USAGE Table

Contains information about foreign keys in a table, such as foreign key name, column name, related table, etc.



SELECT * FROM information_schema.KEY_COLUMN_USAGE WHERE TABLE_SCHEMA = 'your_database_name' AND TABLE_NAME = 'your_table_name';

The REFERENTIAL_CONSTRAINTS Table

Stores information about foreign key constraints, such as constraint name, related table, etc.



SELECT * FROM information_schema.REFERENTIAL_CONSTRAINTS WHERE CONSTRAINT_SCHEMA = 'your_database_name' AND TABLE_NAME = 'your_table_name';

These tables provide rich metadata information that can be used to query database structure, table information, column information, index information, etc.

Please note that you need to replace 'your_database_name' and 'your_table_name' in the queries with the actual database name and table name.


Getting the Number of Records Affected by a Query

PERL Example

In a DBI script, the number of records affected by a statement is returned by the do( ) or execute( ) function:



# 方法 1

# Execute $query using do()

my $count = $dbh->do ($query);

# If an error occurs, it will output 0

printf "%d 条数据被影响\n", (defined ($count) ? $count : 0);



# 方法 2

# Execute $query using prepare() and execute()

my $sth = $dbh->prepare ($query);

my $count = $sth->execute ( );

printf "%d 条数据被影响\n", (defined ($count) ? $count : 0);

PHP Example

In PHP, you can use the mysqli_affected_rows( ) function to get the number of records affected by a query.



$result_id = mysqli_query ($conn_id, $query);

# If query fails, return

$count = ($result_id ? mysqli_affected_rows ($conn_id) : 0);

print ("$count 条数据被影响\n");


Lists of Databases and Tables

You can easily obtain lists of databases and tables on a MySQL server. If you do not have sufficient privileges, the result will return null.

You can also use the SHOW TABLES or SHOW DATABASES statements to obtain lists of databases and tables.

PERL Example



# Get all available tables in the current database.

my @tables = $dbh->tables ( );

foreach $table (@tables ){

   print "表名 $table\n";

}

PHP Example

The following example outputs all databases on the MySQL server:

View all databases

Database . "
" ; } mysqli_close ( $conn ) ; ?>

Getting Server Metadata

The following commands can be used at the MySQL command prompt, or in scripts, such as PHP scripts.

Command Description
SELECT VERSION( ) Server version information
SELECT DATABASE( ) Current database name (or returns empty)
SELECT USER( ) Current username
SHOW STATUS Server status
SHOW VARIABLES Server configuration variables