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,
ALT开发者_如何学运维ER 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?
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:
ALTER TABLE root_page_embed_codes CHANGE cde_id code_id int, cde_content code_content mediumtext, cde_created code_created timestamp
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 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):
- Select field in the Database Explorer
- Rename it (press F2 or select Rename in popup menu)
- Click on 'Refactor' button in a message box. It will rename selected field and recreate all related foreign keys.
精彩评论