A database consists of database objects, let's take them by definition:
Table: In relational databases and flat file databases, a table is a set of data elements (values) that is organized using a model of vertical columns.
View: A view is a named, derived table whose definition is a persistent part of the database.
Trigger: A trigger is a named database object that is associated with a table, and that activates when a particular event occurs for the table.
Stored Procedure / Function: Collectively known as Stored Routine, is a set of SQL statements that can be stored in the server. Once this has been done, clients don't need to keep reissuing the individual statements but can refer to the stored routine instead.
To know what objects a particular database consists of, we can inquire the schema with available syntaxes to access the metadata.
A Metadata is data about the data, such as the name of a database or table, the data type of a column, or access privileges.
In mysql we have the information_schema database, storing information about all other databases handled by the server.
So, when we want to know details about databases on the mysql server, we may surely inquire the information_schema. To make this "inquiry" task simple I've written one more database object, a stored procedure, which will inquire information_schema and provide us with summery of database objects.
Following is a simple stored procedure that summaries the objects of a database with a single function.
The procedure gives a quick idea of available Tables, Views, Triggers, Stored Procedures and Functions from a database.
Steps:
1. Create procedure from attached code.
2. Usage:
To list objects from all databases you may give 'DATABASE-NAME' as '%'.
Other ways to look for procedures and function is:
Above will show procedures / functions from all databases. You may also filter it on database basis as follows:
To view object definitions following statements can be used:
Plenty of other information is available through the information_schema, which is not covered here.
For example, to gather further details regarding triggers we can utilize the following fields:
- EVENT_OBJECT_SCHEMA & EVENT_OBJECT_TABLE - contains database and table name with which the trigger is associated.
- EVENT_MANIPULATION - contains one of the values INSERT, DELETE, or UPDATE on which the trigger is activated.
For more details on the above and other possible information, please refer to the information_schema tables along with the mysql documentation. Additionally, here is a diagram for information_schema as a quick reference to help your understanding.