You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在MySQL中显示函数定义中的注释(仅用于开发)

How to Keep Function Comments Visible Only in Development Environments

Great question! When you create a function in MySQL, inline -- comments and block /* ... */ comments don’t get saved as part of the function’s stored definition—that’s exactly why SHOW CREATE FUNCTION foo; doesn’t display them. Here are a few practical, dev-friendly ways to retain those comments without cluttering your production environment:

  • Track comments in a dedicated dev metadata table
    Set up a simple table in your development database to store comments alongside your function names. This keeps comments separate from production data but easily accessible for devs:

    -- Create the metadata table (only in dev!)
    CREATE TABLE IF NOT EXISTS dev_function_comments (
        function_name VARCHAR(64) PRIMARY KEY,
        full_comments TEXT NOT NULL,
        updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
    );
    
    -- After creating your function, add its comments here
    INSERT INTO dev_function_comments (function_name, full_comments)
    VALUES ('foo', '-- comment1 -- comment2 -- comment3\n/* information here */')
    ON DUPLICATE KEY UPDATE full_comments = VALUES(full_comments);
    

    To view comments alongside the function definition, just run a query like:

    SHOW CREATE FUNCTION foo;
    SELECT full_comments FROM dev_function_comments WHERE function_name = 'foo';
    
  • Use a deployment script to strip comments for production
    Keep your fully commented function code in your dev repo (e.g., in .sql files). Before deploying to production, use a simple script to strip out all comments. For example, a quick sed command:

    # Remove -- line comments and /* ... */ block comments
    sed -e 's/--.*$//' -e '/\/\*/,/\*\//d' your_dev_function.sql > production_ready_function.sql
    

    This way, devs work with the commented version, while production gets a clean, comment-free function.

  • Leverage MySQL’s COMMENT clause (with a deployment caveat)
    MySQL lets you add an official comment to a function using the COMMENT keyword when creating it. While this comment is stored in system tables (so it will exist in production unless you remove it), it’s an option if you’re willing to strip it during deployment:

    DROP FUNCTION IF EXISTS `foo`;
    delimiter //
    CREATE FUNCTION foo() 
    RETURNS int(1)
    COMMENT '-- comment1 -- comment2 -- comment3\n/* information here */'
    BEGIN
        SET @var1 = @var1 + 1;
        RETURN @var1;
    END; //
    delimiter ;
    

    To retrieve the comment in dev, run:

    SELECT COMMENT FROM INFORMATION_SCHEMA.ROUTINES 
    WHERE ROUTINE_NAME = 'foo' AND ROUTINE_TYPE = 'FUNCTION';
    

    Remember to remove the COMMENT clause from the SQL before deploying to production if you don’t want it there.

内容的提问来源于stack exchange,提问作者Jarod Sun

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 04:03:23