Adding Comments to MySQL Table Columns
The MySQL documentation for ALTER TABLE may not explicitly mention a method to add or modify column comments, but this is indeed possible. Here's how you can achieve it:
For Table-Level Comments:
<code class="sql">ALTER TABLE myTable COMMENT 'Hello World';</code>
This statement adds a comment to the entire table, providing a high-level description or any relevant information about the table.
For Column-Level Comments:
To add or modify a comment on a specific column, use the following syntax:
<code class="sql">ALTER TABLE tableName CHANGE columnName newColumnName dataType COMMENT 'new column comment';</code>
For instance, to add a comment to the id column of the user table:
<code class="sql">ALTER TABLE `user` CHANGE `id` `id` INT( 11 ) COMMENT 'id of user';</code>
This statement changes the column name to newColumnName, but you can keep the same column name if desired. The dataType parameter specifies the data type of the column, and the COMMENT parameter provides the desired column comment.
By utilizing these techniques, you can enhance the clarity and documentation of your MySQL tables and columns, making it easier to understand and maintain your database schema.
The above is the detailed content of How Can I Add Comments to MySQL Table Columns?. For more information, please follow other related articles on the PHP Chinese website!