MySQL Tutorial

MySQL Installation and Configuration

MySQL Database Operations

Database Design

MySQL Data Types

MySQL Storage Engines

MySQL Basic Operations of Tables

MySQL Constraints

MySQL Operators

MySQL Function

MySQL Manipulate Table Data

MySQL View

MySQL Indexes

MySQL Stored Procedure

MySQL Trigger

MySQL Transactions

MySQL Character Set

MySQL User Management

MySQL Database Backup and Recovery

MySQL Log

MySQL Performance Optimization

MySQL Comments: Single-line And Multi-line Comments

Comments in MySQL are used to explain sections of SQL statements, or to prevent execution of SQL commands.

In MySQL, there are three ways to include comments:

  1. # comment: The # symbol can be used to start a single-line comment. Everything from the # symbol to the end of the line is considered a comment and is ignored by MySQL.

    Example:

    # This is a single-line comment
    SELECT * FROM users;
    
  2. -- comment: The -- (double-dash with space) can also be used to start a single-line comment. Everything from the -- to the end of the line is considered a comment and is ignored by MySQL.

    Example:

    -- This is another single-line comment
    SELECT * FROM users;
    
  3. /*...*/ comment: The /*...*/ symbols can be used to start and end a multi-line comment. Everything between /* and */ is considered a comment and is ignored by MySQL.

    Example:

    /* This is a
    multi-line comment */
    SELECT * FROM users;
    

Note: Be careful when commenting out multiple lines with # or -- as they only comment out a single line.

In general, comments can be very useful for explaining your code to others, or leaving notes to yourself for future reference. They can also be helpful for debugging, by temporarily disabling certain lines of code.

  1. Using Single-Line Comments in MySQL Scripts:

    • Single-line comments begin with -- and extend to the end of the line.
    • Example:
      -- This is a single-line comment
      SELECT * FROM mytable;
      
  2. Adding Comments to SQL Statements in MySQL:

    • Place comments after the -- to annotate specific SQL statements.
    • Example:
      SELECT column1, column2 -- Selecting specific columns
      FROM mytable;
      
  3. Commenting Out Code in MySQL Queries:

    • Comments can be used to temporarily disable or comment out parts of a query.
    • Example:
      -- SELECT * FROM mytable; -- This line is commented out
      
  4. Multi-Line Comment Examples in MySQL:

    • Multi-line comments use /* to start and */ to end, allowing comments spanning multiple lines.
    • Example:
      /*
        This is a multi-line comment
        spanning multiple lines
      */
      SELECT * FROM mytable;
      
  5. Disabling Parts of SQL Queries with Comments in MySQL:

    • Temporarily remove or disable specific parts of a query using comments.
    • Example:
      SELECT *
      -- , column2
      FROM mytable;
      
  6. MySQL Comments in Stored Procedures and Functions:

    • Comments are valuable in stored procedures and functions to document logic and behavior.
    • Example:
      DELIMITER //
      CREATE PROCEDURE myprocedure()
      BEGIN
        -- Procedure logic here
      END //
      DELIMITER ;