Database Reference
In-Depth Information
Therefore, to rename the visitorbook table to visitorsbook , we would execute the follow-
ing query:
ALTER TABLE visitorbook
RENAME
visitorsbook
And to change it back:
ALTER TABLE visitorsbook
RENAME
visitorbook
RENAME only allows you to change the name of the whole table. One way that you can
change the name of a column is by using CHANGE
ALTER TABLE CHANGE
The ALTER TABLE CHANGE query allows you to change the name of a column as well as
its datatype. It has the following basic format:
ALTER TABLE tablename
CHANGE
oldcolumnname newcolumnname datatype
CHANGE requires you to specify the old and new column names even if you are not
changing the name of the column, in which case they can both be the same. Likewise, it
requires you to specify the datatype even if you are not changing it.
For example, our column lastname could be confusing to some cultures, so we will
change it to familyname instead. To do this, run the following query:
ALTER TABLE visitorbook
CHANGE
lastname familyname TEXT
Notice how we still have to specify the datatype even though we are not changing it.
While we are looking at that column though, do you see a problem with the datatype? We
are using a TEXT field, which can contain strings of varying length, to store a string that in
all probability is only tens of characters long. Let us make the arbitrary decision that a fam-
ily name will never be longer than 40 characters, so change the type to a VARCHAR using
the following script:
ALTER TABLE visitorbook
CHANGE
familyname familyname VARCHAR(40)
The CHANGE command allows you to change the column name and datatype at the
same time as well by specifying the two different column names and the new datatype.
You could change the firstname column to a VARCHAR as well with the following:
Search WWH ::




Custom Search