w3resource
php exercises

MySQL Alter Table - Exercises, Practice, Solution

MySQL Alter Table [15 exercises with solution]

1. Write a SQL statement to rename the table countries to country_new.

Click me to see the solution

2. Write a SQL statement to add a column region_id to the table locations.

Click me to see the solution

3. Write a SQL statement to add a columns ID as the first column of the table locations.

Click me to see the solution

4. Write a SQL statement to add a column region_id after state_province to the table locations.

Click me to see the solution

5. Write a SQL statement change the data type of the column country_id to integer in the table locations.

Click me to see the solution

6. Write a SQL statement to drop the column city from the table locations.

Click me to see the solution

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

Click me to see the solution

8. Write a SQL statement to add a primary key for the columns location_id in the locations table.

Here is the sample table employees.

Sample table: employees


Click me to see the solution

9. Write a SQL statement to add a primary key for a combination of columns location_id and country_id.

Click me to see the solution

10. Write a SQL statement to drop the existing primary from the table locations on a combination of columns location_id and country_id.

Click me to see the solution

11. Write a SQL statement to add a foreign key on job_id column of job_history table referencing to the primary key job_id of jobs table.

Click me to see the solution

12. Write a SQL statement to add a foreign key constraint named fk_job_id on job_id column of job_history table referencing to the primary key job_id of jobs table.

Click me to see the solution

13. Write a SQL statement to drop the existing foreign key fk_job_id from job_history table on job_id column which is referencing to the job_id of jobs table.

Click me to see the solution

14. Write a SQL statement to add an index named indx_job_id on job_id column in the table job_history.

Click me to see the solution

15. Write a SQL statement to drop the index indx_job_id from job_history table.

Click me to see the solution

Many of our valued users post comments along with piece of code. Disqus may, false positively, consider those piece of code as Bad or Starnge syntax and send those comments to spam automatically. Even if that happens, we will make sure those useful comments are taken out of spam and approved. It may take a couple of days for that though, but any useful comment will be brought to public view for sure. We regret if that happend to any user. You may write us directly regarding this to - w3resource[at]yahoo[dot]com.