Tuesday, October 22, 2019
Change a Column Name in MySQL
Change a Column Name in MySQL If you already created your MySQL database, and you decide after the fact that one of the columns is named incorrectly, you dont need to remove it and add a replacement; you can simply rename it. Renaming a Database Column You rename a column in MySQL using theà ALTER TABLE and CHANGEà commands together to change an existing column. For example, say theà column is currently named Soda, but you decide that Beverage isà a more appropriate title. The column is located on the table entitled Menu. Here is an example of how to change it: ALTER TABLE menu CHANGE soda beverage varchar(10) ; In a genericà form, where you substitute your terms, this is: ALTER TABLE tablename CHANGE oldname newname varchar(10) ; About VARCHAR The VARCHAR(10) in the examples can change to be appropriate for your column. VARCHAR is a character string of variable length. The maximum length- in this exampleà it is 10- indicates the maximum number of characters you want to store in the column. VARCHAR(25) could store up to 25 characters. Other Uses for ALTER TABLE The ALTER TABLEà command can also be used to add a new column to a table or to remove an entire column and all its data from a table. For example, to add a column use: ALTER TABLE table_nameADD column_name datatype To delete a column, use: ALTER TABLE table_nameDROP COLUMN column_nameà You can also make changes to a columns size and type in MySQL.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.