Altering table queries
In addition to creating tables, it is also possible to modify existing tables. This can be done using an ALTER
query. An ALTER
query uses the following syntax:
ALTER TABLE [table_name] [alter_options]
ALTER
queries can be used for a number of purposes. One common reason is to change how a field in a table is defined. For example, suppose we have a customer table that contains a field for username. Currently, it allows for a VARCHAR
value of size 15
, but we want to extend this to be size 30
. To do this, we can use an ALTER
query, as follows:
ALTER TABLE customer MODIFY username VARCHAR(30);
We can also use the ALTER
query to add an index to our table. To do this, we first need to create the index using a CREATE
query. So, for example, suppose we now wanted to add an index to the username of our customer table. First, we create the index for username
:
CREATE UNIQUE INDEX 'idx_username' ON customer('username')
The next exercise...