如何更改表列名称的前缀?
我有一个如下所示的表,
Field Type Collation Attributes Null Default
cde_id int(10) No 0
cde_title varchar(255) utf8_general_ci Yes NULL
cde_content mediumtext utf8_general_ci Yes NULL
pg_id varchar(255) utf8_general_ci Yes 0
cde_created timestamp No 0000-00-00 00:00:00
但我想将列名称的前缀从“cde”更改为“code”。
我使用此查询来更改名称,
ALTER TABLE root_page_embed_codes
CHANGE cde_id code_id int(10)
然后我发现它不够动态,因为列 types
彼此不同,并且我有一个外键
- < code>pg_id 也是如此。
我可以使用任何查询方法以更简单的方式更改前缀吗?
I have a table like this below,
Field Type Collation Attributes Null Default
cde_id int(10) No 0
cde_title varchar(255) utf8_general_ci Yes NULL
cde_content mediumtext utf8_general_ci Yes NULL
pg_id varchar(255) utf8_general_ci Yes 0
cde_created timestamp No 0000-00-00 00:00:00
But I want to change the columns name's prefix from 'cde' to 'code'.
I use this query to change the name,
ALTER TABLE root_page_embed_codes
CHANGE cde_id code_id int(10)
Then I find that it is not dynamic enough because the column types
are different from one to another and I have a foreign key
- pg_id
as well.
Any query method that I can use to change the prefix in a easier way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
mysql 中的 int 始终是 int。
(10)
部分只是向 MySQL 提示应显示多少位数字。无论您想要 1 位数字还是 10 位数字,内部的整数值定义仍然相同。你到底是什么意思,“不够动态”?更改 cde_id 字段的名称不会以任何方式影响 pg_id 字段。
后续:您可以在单个更改查询中链接多个字段更改:
但是,您无法避免必须指定“新”字段类型,因为 MySQL 不够聪明,无法意识到您只是重命名字段而不是想要更改除名称以外的任何内容。不幸的是,字段没有像表和数据库那样的“重命名”。
An int in mysql is an int, always. The
(10)
portion is merely a hint to MySQL as to how many digits it should display. Regardless if whether you want 1 digit or 10, it's still going to be the same integer value definition internally.What exactly do you mean, "not dynamic enough"? Changing the cde_id field's name won't affect pg_id field in any way.
followup: You can chain multiple field changes in a single alter query:
You can't get around having to specify the "new" field type, however, as MySQL is not smart enough to realize you're just renaming the field and not wanting to change anything BUT the name. There's no "rename" for fields as there is for tables and databases, unfortunately.
当字段重命名时,MySQL 不关心依赖的 FK。正如 Luc M 建议的那样 - 你应该重新创建 FK。
另外,您可以尝试在 dbForge Studio for MySQL (Express Edition) 中重命名此字段:
MySQL does not care about dependent FK when field is being renamed. As Luc M suggested - you should recreate the FK.
Also, you can try to rename this field in dbForge Studio for MySQL (Express Edition):