Finding tables, views, functions, procedures and triggers in MySQL Database

AID: 1860
  • Status: Published

2010 points

  • BytheGhost_k8
  • TypeGeneral
  • Posted on2009-10-26 at 05:13:53
This article is about identifying database objects using a stored procedure.

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.
DELIMITER $$

DROP PROCEDURE IF EXISTS `xplore` $$
CREATE DEFINER=`root`@`localhost` PROCEDURE `xplore`(IN_DB varchar(100))
BEGIN 
DECLARE DB VARCHAR(100);
DECLARE NO_TABLES INT(10);
DECLARE NO_VIEWS INT(10);
DECLARE NO_FUNCTIONS INT(10);
DECLARE NO_PROCEDURES INT(10);
DECLARE NO_TRIGGERS INT(10);
DECLARE SUMMARY VARCHAR(200); 
SET DB=IN_DB;

drop temporary table if exists objects;
create temporary table objects
(
object_type varchar(100),
object_name varchar(100),
object_schema varchar(100)
)
engine=myisam; 

INSERT INTO objects
/* query for triggers */
(SELECT 'TRIGGER',TRIGGER_NAME ,TRIGGER_SCHEMA
FROM information_schema.triggers
WHERE TRIGGER_SCHEMA like DB)

UNION

/* query for views*/
(SELECT 'VIEW', TABLE_NAME,TABLE_SCHEMA
FROM information_schema.tables
WHERE table_type='VIEW' and TABLE_SCHEMA like DB)

UNION

/* query for procedure*/
(SELECT 'PROCEDURE', SPECIFIC_NAME, ROUTINE_SCHEMA
FROM information_schema.routines
WHERE routine_type='PROCEDURE' and ROUTINE_SCHEMA like DB)

UNION

/* query for function*/
(SELECT 'FUNCTION', SPECIFIC_NAME, ROUTINE_SCHEMA
FROM information_schema.routines
WHERE routine_type='FUNCTION' and ROUTINE_SCHEMA like DB)

UNION

/* query for tables*/
(SELECT concat(ENGINE,' TABLE'), TABLE_NAME, TABLE_SCHEMA
FROM information_schema.tables
WHERE table_type='BASE TABLE' and TABLE_SCHEMA like DB
GROUP BY ENGINE, TABLE_NAME);

/* show gathered information from temporary table */
SELECT object_name,object_type,object_schema
FROM objects;

/* Prepare and show summary */
SELECT object_schema AS `DATABASE`,
SUM(IF(object_type like '%TABLE', 1, 0)) AS 'TABLES',
SUM(IF(object_type='VIEW', 1, 0)) AS 'VIEWS',
SUM(IF(object_type='TRIGGER', 1, 0)) AS 'TRIGGERS',
SUM(IF(object_type='FUNCTION', 1, 0)) AS 'FUNCTIONS',
SUM(IF(object_type='PROCEDURE', 1, 0)) AS 'PROCEDURES'
FROM objects
GROUP BY object_schema;

END $$

DELIMITER ;
                                    
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:

Select allOpen in new window



2. Usage:
call xplore('DATABASE-NAME');
                                    
1:

Select allOpen in new window


To list objects from all databases you may give 'DATABASE-NAME' as '%'.


Other ways to look for procedures and function is:
SHOW PROCEDURE STATUS; 
SHOW FUNCTION STATUS;
                                    
1:
2:

Select allOpen in new window



Above will show procedures / functions from all databases. You may also filter it on database basis as follows:
SHOW PROCEDURE STATUS WHERE db LIKE 'database-name';
                                    
1:

Select allOpen in new window



To view object definitions following statements can be used:
SHOW CREATE FUNCTION function_name;
SHOW CREATE PROCEDURE procedure_name;
SHOW CREATE VIEW view_name;
SHOW CREATE TABLE table_name;
SHOW CREATE TRIGGER trigger_name ;
                                    
1:
2:
3:
4:
5:

Select allOpen in new window



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.
Asked On
2009-10-26 at 05:13:53ID1860
Tags

Search

,

MySQL

,

Database

,

information_schema

Topic

MySQL Server

Views
1436

Comments

Add your Comment

Please Sign up or Log in to comment on this article.

Join Experts Exchange Today

Gain Access to all our Tech Resources

Get personalized answers

Ask unlimited questions

Access Proven Solutions

Search 3.2 million solutions

Read In-Depth How-To Guides

1000+ articles, demos, & tips

Watch Step by Step Tutorials

Learn direct from top tech pros

And Much More!

Your complete tech resource

See Plans and Pricing

30-day free trial. Register in 60 seconds.

Loading Advertisement...

Top MySQL Server Experts

  1. johanntagle

    286,814

    Guru

    6,000 points yesterday

    Profile
    Rank: Sage
  2. Ray_Paseur

    216,557

    Guru

    0 points yesterday

    Profile
    Rank: Savant
  3. DaveBaldwin

    119,595

    Master

    1,400 points yesterday

    Profile
    Rank: Genius
  4. angelIII

    61,340

    Master

    0 points yesterday

    Profile
    Rank: Elite
  5. mwvisa1

    57,185

    Master

    30 points yesterday

    Profile
    Rank: Genius
  6. HainKurt

    41,850

    0 points yesterday

    Profile
    Rank: Genius
  7. ralmada

    39,250

    0 points yesterday

    Profile
    Rank: Genius
  8. Roads_Roads

    33,080

    0 points yesterday

    Profile
    Rank: Genius
  9. arnold

    29,812

    0 points yesterday

    Profile
    Rank: Genius
  10. theGhost_k8

    29,785

    0 points yesterday

    Profile
    Rank: Sage
  11. Kdo

    29,682

    0 points yesterday

    Profile
    Rank: Genius
  12. bportlock

    26,604

    0 points yesterday

    Profile
    Rank: Genius
  13. jason1178

    23,574

    0 points yesterday

    Profile
    Rank: Genius
  14. maeltar

    23,236

    0 points yesterday

    Profile
    Rank: Guru
  15. StingRaY

    21,500

    0 points yesterday

    Profile
    Rank: Wizard
  16. smadeira

    19,968

    0 points yesterday

    Profile
    Rank: Wizard
  17. fundacionrts

    18,200

    0 points yesterday

    Profile
    Rank: Master
  18. gr8gonzo

    17,019

    0 points yesterday

    Profile
    Rank: Sage
  19. ChrisStanyon

    16,964

    0 points yesterday

    Profile
    Rank: Sage
  20. pratima_mcs

    16,614

    0 points yesterday

    Profile
    Rank: Genius
  21. TempDBA

    16,400

    0 points yesterday

    Profile
    Rank: Sage
  22. Sharath_123

    16,268

    0 points yesterday

    Profile
    Rank: Genius
  23. for_yan

    16,000

    0 points yesterday

    Profile
    Rank: Genius
  24. matthewspatrick

    15,800

    0 points yesterday

    Profile
    Rank: Savant
  25. AielloJ

    13,732

    0 points yesterday

    Profile
    Rank: Wizard

Hall Of Fame