Database Reference
In-Depth Information
ALTER TABLE visitorbook
CHANGE
firstname firstname VARCHAR(40)
However, the next command shows an easier way to change the datatype if that is the
only change you wish to make to the column.
ALTER TABLE MODIFY
MySQL has given us the ALTER TABLE MODIFY query that saves us a bit of typing if we are
only going to change the datatype. This has the following basic format:
ALTER TABLE tablename
MODIFY
columnname datatype
To use MODIFY to change the datatype of a column we no longer have to type the col-
umn name twice, so to change the firstname column we can use:
ALTER TABLE visitorbook
MODIFY
firstname VARCHAR(40)
We have not looked at the format of our table for a while, so Figure 8.9 shows our
changes to date with a describe visitorbook query.
ALTER TABLE DROP
The ALTER TABLE DROP query has the following basic format:
ALTER TABLE tablename
DROP
columnname, columnname
You can delete one column or several by separating the column names with commas.
Looking at Figure 8.9 may make you wonder why we did not change the datatype of the
middlename column. This column is awaiting a worse fate! If you turn back to Figure 8.7
you will notice that there is no data being stored at all in the middlename column, and with
hindsight, we have no real need to obtain that information from our website visitors.
Currently that will be just wasting space within our database. We can remove the middle-
name column by using the following script:
ALTER TABLE visitorbook
DROP
middlename
If you run that script you may be startled by how quickly it drops the column. Bear in
mind that when you remove columns it is not like a modern interface which happily asks
Search WWH ::




Custom Search