更改 MySQL 表以在列上添加注释
我一直在检查 MySQL ALTER TABLE 文档 和它似乎不包括向列添加或修改评论的方法。我该怎么做?
-- for table
ALTER TABLE myTable COMMENT 'Hello World'
-- for columns
-- ???
I have been checking the MySQL Documentation for ALTER TABLE and it does not seem to include a way to add or modify a comment to a column. How can I do this?
-- for table
ALTER TABLE myTable COMMENT 'Hello World'
-- for columns
-- ???
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
尝试:
try:
您可以使用
MODIFY COLUMN
来执行此操作。只需...替换:
YourTable
为您的表格名称your_column
为您的评论名称your_previous_column_definition
与列的 column_definition,我建议通过SHOW CREATE TABLE YourTable
命令获取并逐字复制以避免任何陷阱。*您的新评论
加上您想要的专栏评论。例如...
* 每当您在
ALTER TABLE
语句中使用MODIFY
或CHANGE
子句时,我建议您从输出中复制列定义SHOW CREATE TABLE
语句的。这可以防止您因为没有意识到需要将列定义的重要部分包含在MODIFY
或CHANGE
子句中而意外丢失它。例如,如果您MODIFY
一个AUTO_INCRMENT
列,则需要在MODIFY
中再次显式指定AUTO_INCRMENT
修饰符子句,否则该列将不再是AUTO_INCRMENT
列。同样,如果列定义为NOT NULL
或具有DEFAULT
值,则在执行MODIFY
或时需要包含这些详细信息>更改
列,否则它们将丢失。You can use
MODIFY COLUMN
to do this. Just do...substituting:
YourTable
with the name of your tableyour_column
with the name of your commentyour_previous_column_definition
with the column's column_definition, which I recommend getting via aSHOW CREATE TABLE YourTable
command and copying verbatim to avoid any traps.*Your new comment
with the column comment you want.For example...
* Whenever you use
MODIFY
orCHANGE
clauses in anALTER TABLE
statement, I suggest you copy the column definition from the output of aSHOW CREATE TABLE
statement. This protects you from accidentally losing an important part of your column definition by not realising that you need to include it in yourMODIFY
orCHANGE
clause. For example, if youMODIFY
anAUTO_INCREMENT
column, you need to explicitly specify theAUTO_INCREMENT
modifier again in theMODIFY
clause, or the column will cease to be anAUTO_INCREMENT
column. Similarly, if the column is defined asNOT NULL
or has aDEFAULT
value, these details need to be included when doing aMODIFY
orCHANGE
on the column or they will be lost.数据库上所有字段的脚本:
注意:如果您愿意,您可以改进为只有一张表
@Rufinus 给出的解决方案很棒,但如果您有自动递增会破坏它。
Script for all fields on database:
Note: You can improve to only one table if you prefer
The solution given by @Rufinus is great but if you have auto increments it will break it.
信息模式不是处理这些事情的地方(请参阅 DDL 数据库命令)。
添加注释时,您需要更改表结构(表注释)。
来自 MySQL 5.6 文档:
第 21 章 INFORMATION_SCHEMA 表
The information schema isn't the place to treat these things (see DDL database commands).
When you add a comment you need to change the table structure (table comments).
From MySQL 5.6 documentation:
Chapter 21 INFORMATION_SCHEMA Tables
根据文档,您只能在创建表时添加注释。所以必须有表定义。一种自动化方法是使用脚本读取定义并更新注释。
参考:
http://cornempire.net/2010/04/ 15/add-comments-to-column-mysql/
http://bugs .mysql.com/bug.php?id=64439
As per the documentation you can add comments only at the time of creating table. So it is must to have table definition. One way to automate it using the script to read the definition and update your comments.
Reference:
http://cornempire.net/2010/04/15/add-comments-to-column-mysql/
http://bugs.mysql.com/bug.php?id=64439