Complete Guide to Viewing Stored Procedures and Functions in MySQL Command Line

Nov 10, 2025 · Programming · 27 views · 7.8

Keywords: MySQL | Stored Procedures | Stored Functions | Command Line | Database Management

Abstract: This article provides a comprehensive overview of methods for viewing and managing stored procedures and functions in MySQL command line environment. By comparing SHOW PROCEDURE STATUS, SHOW FUNCTION STATUS commands with information_schema.routines system table queries, it analyzes their respective application scenarios and output characteristics. The article also explores syntax differences in creating procedures and functions, parameter type characteristics, and permission management requirements, offering complete technical reference for database developers.

Overview of MySQL Stored Procedures and Functions

In MySQL database management system, stored procedures and functions are essential database objects that allow encapsulation of common SQL logic into reusable code blocks. Stored procedures are typically used to execute series of database operations, while functions are designed to return single values. Understanding how to effectively view and manage these objects is crucial for database development and maintenance.

Command Line Viewing Methods

MySQL provides specialized commands to view status information of stored procedures and functions. Among these, SHOW PROCEDURE STATUS and SHOW FUNCTION STATUS are the most direct approaches. These commands return lists containing detailed information such as object names, associated databases, creation times, and more.

-- View all stored procedures
SHOW PROCEDURE STATUS;

-- View all functions
SHOW FUNCTION STATUS;

Similar to SHOW TABLES and SHOW DATABASES, these commands offer concise ways to quickly browse stored program objects within the database. Output typically includes fields like Db (database name), Name (object name), Type (type), Definer (definer), Modified (modification time), and others.

Information Schema Query Methods

In addition to using SHOW commands, more precise information can be obtained by querying the information_schema.routines system table. This method allows filtering and sorting using standard SQL query syntax.

SELECT routine_schema,
       routine_name,
       routine_type
FROM information_schema.routines
WHERE routine_schema = 'your_database_name'
ORDER BY routine_name;

The advantage of this approach lies in the flexibility to add WHERE conditions for filtering, such as searching for stored programs by specific naming patterns or creation time ranges. Query results include three main fields: routine_schema (database schema), routine_name (program name), and routine_type (program type).

Detailed Definition Viewing

To view the specific definition content of stored procedures or functions, SHOW CREATE PROCEDURE and SHOW CREATE FUNCTION commands can be used. These commands return complete creation statements, including parameter lists, return types (for functions), and program body code.

-- View stored procedure definition
SHOW CREATE PROCEDURE procedure_name;

-- View function definition
SHOW CREATE FUNCTION function_name;

The output from these commands is valuable for understanding implementation logic of existing stored programs, conducting code reviews, or preparing for migration work. Definitions contain complete SQL syntax, including parameter declarations, variable definitions, and business logic implementation.

Creation Syntax Differences Analysis

Stored procedures and functions have important differences in creation syntax. Stored procedures use CREATE PROCEDURE statements, can contain IN, OUT, and INOUT parameter types, and support data modification operations. Functions use CREATE FUNCTION statements, must include RETURNS clause to specify return type, and parameters can only be IN type.

-- Stored procedure example
CREATE PROCEDURE citycount (IN country CHAR(3), OUT cities INT)
BEGIN
    SELECT COUNT(*) INTO cities FROM world.city
    WHERE CountryCode = country;
END

-- Function example
CREATE FUNCTION hello (s CHAR(20))
RETURNS CHAR(50) DETERMINISTIC
RETURN CONCAT('Hello, ', s, '!');

Permissions and Security Considerations

Creating and executing stored programs requires appropriate database permissions. CREATE ROUTINE permission is necessary for creating stored procedures or functions, while EXECUTE permission is used for executing existing programs. When defining stored programs, execution security context can be controlled through DEFINER and SQL SECURITY clauses.

By default, stored programs execute using definer (DEFINER) security mode, meaning programs run with definer's permissions. They can also be set to invoker (INVOKER) mode, where programs execute using caller's permissions. This security mechanism is important for permission management in multi-user environments.

Practical Application Scenarios

In actual database development, regularly viewing stored procedure and function lists helps with:

By combining SHOW commands with information_schema queries, comprehensive stored program management strategies can be built, improving efficiency in database development and maintenance.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.