Database Reference
In-Depth Information
Altering a table
We can spend hours, days, and even weeks getting our tables defined just the way
we want them, but chances are at some point we'll need to make some changes. This
is where the ALTER TABLE command comes into play.
The basic syntax for the ALTER TABLE command is as follows:
ALTER TABLE table_name <alter_definition>[, alter_definition] ...;
An <alter_definition> command can ADD , MODIFY , and DROP (delete) columns
from tables, among other things. Multiple alter definitions in a single ALTER TABLE
command are separated by commas. Because we can have multiple alter definitions
in one ALTER TABLE command, the syntax examples in the next four sections will
not contain the beginning ALTER TABLE table_name part that must begin an ALTER
TABLE command. The examples that demonstrate actual usage will.
Adding a column
An <alter_definition> attribute of an ALTER TABLE command is used to add a
column has the following pattern:
ADD <column_name> <column_definition> [FIRST | AFTER <column_name> ]
The FIRST and AFTER parts are optional. We can use one, but not both. The FIRST
option puts the new column as the first column of the row. The AFTER option lets us
specify, which column the new column appears after.
For example, the following will create a new username column and place it after the
pref_name column:
ALTER TABLE employees ADD username varchar(20) AFTER pref_name;
Modifying a column
An alter definition of an ALTER TABLE command to modify a column has the
following pattern:
MODIFY <column_name> <column_definition>
For example, the following ALTER TABLE command will change the pref_name
column to varchar(25) :
ALTER TABLE employees MODIFY pref_name varchar(25);
 
Search WWH ::




Custom Search