Wednesday, March 4, 2020
How to Change a Column Size or Type in MySQL
How to Change a Column Size or Type in MySQL Just because you made a MySQL column one type or size doesnt mean that it has to stay that way. Changing the column type or size in an existing database is simple.ââ¬â¹ Changing a Database Column Size and Type You change a column size or type in MySQL using theà ALTER TABLEà andà MODIFY commands together to make the change.à Lets say, for example, that you have a column named State on a table named Address and you previously set it up to hold two characters, expecting people to use 2-character state abbreviations. You find that several people entered entire names instead of 2-character abbreviations, and you want to allow them to do this. You need to make this column larger to allow the full state names to fit. Here is how you do it: à ALTER TABLE address MODIFY state VARCHAR(20) ; In generic terms, you use the ALTER TABLE command followed by the table name, then theà MODIFY command followed by the column name and new type and size. Here is an example: à ALTER TABLE tablenameà MODIFY columnnameà VARCHAR(20) ; The maximum width of the column is determined by the number in parentheses. The type is identifiedà by VARCHAR as being a variable character field. About VARCHAR The VARCHAR(20) in the examples can change to whatever number is appropriate for your column. VARCHAR is a character string of variable length. The maximum length- in this exampleà it is 20- 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_name à ADD column_name datatype To delete a column, use: à ALTER TABLE table_name à DROP COLUMN column_name
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.