w3resource

MySQL Alter Table Statement Exercises: Change the name of the column state_province to state, keeping the data type and size same

MySQL Alter Table Statement: Exercise-7 with Solution

Write a MySQL statement to change the name of the column state_province to state, keeping the data type and size same.

Here is the structure of the table locations.

mysql> SHOW COLUMNS FROM locations;
+----------------+--------------+------+-----+---------+-------+
| Field          | Type         | Null | Key | Default | Extra |
+----------------+--------------+------+-----+---------+-------+
| LOCATION_ID    | decimal(4,0) | YES  |     | NULL    |       |
| STREET_ADDRESS | varchar(40)  | YES  |     | NULL    |       |
| POSTAL_CODE    | varchar(12)  | YES  |     | NULL    |       |
| CITY           | varchar(30)  | YES  |     | NULL    |       |
| STATE_PROVINCE | varchar(25)  | YES  |     | NULL    |       |
| COUNTRY_ID     | varchar(2)   | YES  |     | NULL    |       |
+----------------+--------------+------+-----+---------+-------+

Code:

-- This SQL statement is used to alter the 'locations' table by removing one column and adding another.
-- The 'state_province' column is being dropped, and a new 'state' column with a varchar(25) data type is being added after the 'city' column.

ALTER TABLE locations

-- Drop the 'state_province' column from the 'locations' table.
DROP state_province,

-- Add a new column named 'state' with a varchar(25) data type after the 'city' column.
ADD state varchar(25)
AFTER city;

Let execute the above code in MySQL command prompt

In that case, if there are no data in the table, the old column will be removed and new column will be create, no problem at all, but if data in the table you can use the following statement :

ALTER TABLE locations
CHANGE state_province state varchar(25);

Now see the structure of the table locations after alteration.

mysql> SHOW COLUMNS FROM locations;
+----------------+--------------+------+-----+---------+-------+
| Field          | Type         | Null | Key | Default | Extra |
+----------------+--------------+------+-----+---------+-------+
| LOCATION_ID    | decimal(4,0) | YES  |     | NULL    |       |
| STREET_ADDRESS | varchar(40)  | YES  |     | NULL    |       |
| POSTAL_CODE    | varchar(12)  | YES  |     | NULL    |       |
| CITY           | varchar(30)  | YES  |     | NULL    |       |
| state          | varchar(25)  | YES  |     | NULL    |       |
| COUNTRY_ID     | varchar(2)   | YES  |     | NULL    |       |
+----------------+--------------+------+-----+---------+-------+

Explanation:

Here's a brief explanation of the above MySQL code:

What is the difficulty level of this exercise?



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/mysql-exercises/alter-table-statement/alter-table-exercise-7.php