【问题标题】:How can I add comments in MySQL?如何在 MySQL 中添加注释?
【发布时间】:2012-02-24 07:12:28
【问题描述】:

我想在 SQL 代码中添加注释。我怎样才能做到这一点?我正在使用 MySQL。

【问题讨论】:

    标签: mysql database comments


    【解决方案1】:

    几种方式:

    # Comment
    -- Comment
    /* Comment */
    

    记得put the space after --

    the documentation

    【讨论】:

    • 对于何时使用这些不同的语法,是否有任何通用的最佳实践或风格指南?显然最后一个是多线 cmets 的理想选择,但是单线 cmets 有什么经验法则吗?
    • @StockB 不,但与您的编码风格保持一致永远不会有坏处。
    【解决方案2】:

    "可以使用COMMENT 选项指定列的注释。注释由SHOW CREATE TABLESHOW FULL COLUMNS 语句显示。此选项从 MySQL 4.1 开始可操作。(在 MySQL 4.1 中允许但忽略较早的版本。)”

    举个例子

    --
    -- Table structure for table 'accesslog'
    --
    
    CREATE TABLE accesslog (
    aid int(10) NOT NULL auto_increment COMMENT 'unique ID for each access entry', 
    title varchar(255) default NULL COMMENT 'the title of the page being accessed',
    path varchar(255) default NULL COMMENT 'the local path of teh page being accessed',
    ....
    ) TYPE=MyISAM;
    

    【讨论】:

    • 我认为这不是 OP 所要求的。
    • 这就是我要找的 :) 顺便说一句,我发现 COMMENT 参数必须在任何 AFTER 参数之前;显然,顺序很重要。
    【解决方案3】:

    您可以使用单行 cmets:

    -- this is a comment
    # this is also a comment
    

    或多行注释:

    /*
       multiline
       comment
    */
    

    【讨论】:

      【解决方案4】:

      来自here你可以使用

      #  For single line comments
      -- Also for single line, must be followed by space/control character
      /*
          C-style multiline comment
      */
      

      【讨论】:

        【解决方案5】:

        支持三种类型的评论

        1. 使用 # 散列单行注释

          Select * from users ; # this will list users
          
          1. 双破折号评论使用 --

          Select * from users ; -- this will list users

        注意:在 -- 之后有一个空格很重要

        3) 使用 /* */ 进行多行注释

        Select * from users ; /* this will list users */
        

        【讨论】:

          【解决方案6】:
          /* comment here */ 
          

          这里是一个例子:SELECT 1 /* this is an in-line comment */ + 1;

          http://dev.mysql.com/doc/refman/5.0/en/comments.html

          【讨论】:

            猜你喜欢
            • 2018-08-21
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2012-07-01
            • 2017-08-01
            • 2014-11-08
            • 2011-09-01
            • 2023-03-21
            相关资源
            最近更新 更多