MySQL Alter Table - Exercises, Practice, Solution
MySQL Alter Table - Exercises, Practice, Solution
3. Write a SQL statement to add a columns ID as the first column of the table
locations.
5. Write a SQL statement change the data type of the column country_id to
integer in the table locations.
6. Write a SQL statement to drop the column city from the table locations.
8. Write a SQL statement to add a primary key for the columns location_id in the
locations table.
10. Write a SQL statement to drop the existing primary from the table locations
on a combination of columns location_id and country_id.
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.
ACCENTURE BATCH 2 LABORATORY
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.
Page | 2 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.
14. Write a SQL statement to add an index named indx_job_id on job_id column
in the table job_history.
15. Write a SQL statement to drop the index indx_job_id from job_history table.
ACCENTURE BATCH 2 LABORATORY
SOLUTION
Copy
Copy
Write a SQL statement to add a columns ID as the first column of the table
Page | 5 locations.
Copy
AFTER state_province;
Copy
Write a SQL statement change the data type of the column country_id to integer
Page | 7 in the table locations.
Copy
Write a SQL statement to drop the column city from the table locations.
Page | 8
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:
ALTER TABLE locations
DROP city;
Copy
DROP state_province,
AFTER city;
Copy
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
Copy
+----------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------------+--------------+------+-----+---------+-------+
| LOCATION_ID | decimal(4,0) | YES | | NULL | |
| STREET_ADDRESS | varchar(40) | YES | | NULL | |
Page | 10 | POSTAL_CODE | varchar(12) | YES | | NULL | |
| CITY | varchar(30) | YES | | NULL | |
| state | varchar(25) | YES | | NULL | |
| COUNTRY_ID | varchar(2) | YES | | NULL | |
+----------------+--------------+------+-----+---------+-------+
ACCENTURE BATCH 2 LABORATORY
Write a SQL statement to add a primary key for the columns location_id in the
Page | 11 locations table.
Copy
Page | 12
Let execute the above code in MySQL 5.6 command prompt
Copy
+-----------+------------+----------+--------------+------------
-+-----------+-------------+----------+--------+------+---------
---+---------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name
| Collation | Cardinality | Sub_part | Packed | Null |
Page | 14 Index_type | Comment |
+-----------+------------+----------+--------------+------------
-+-----------+-------------+----------+--------+------+---------
---+---------+
| locations | 0 | PRIMARY | 1 | LOCATION_ID
| A | NULL | NULL | NULL | | BTREE
| |
| locations | 0 | PRIMARY | 2 | COUNTRY_ID
| A | 0 | NULL | NULL | | BTREE
| |
+-----------+------------+----------+--------------+------------
-+-----------+-------------+----------+--------+------+---------
---+---------+
ACCENTURE BATCH 2 LABORATORY
Write a SQL statement to drop the existing primary from the table locations on a
Page | 15 combination of columns location_id and country_id.
Copy
Write a SQL statement to add a foreign key on job_id column of job_history table
Page | 16 referencing to the primary key job_id of jobs table.
REFERENCES jobs(job_id);
Copy
Now see the structure of the table job_history after being altered.
mysql> SHOW COLUMNS FROM job_history;
+---------------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------------+---------+------+-----+---------+-------+
ACCENTURE BATCH 2 LABORATORY
Write a SQL statement to add a foreign key constraint named fk_job_id on job_id
Page | 18 column of job_history table referencing to the primary key job_id of jobs table.
REFERENCES jobs(job_id)
ON UPDATE RESTRICT
ON DELETE CASCADE;
Copy
Now see the structure of the table locations after being altered.
mysql> SHOW COLUMNS FROM job_history;
+---------------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
Page | 19 +---------------+---------+------+-----+---------+-------+
| EMPLOYEE_ID | int(11) | NO | | NULL | |
| START_DATE | date | YES | | NULL | |
| HIRE_DATE | date | YES | | NULL | |
| JOB_ID | int(11) | NO | MUL | NULL | |
| DEPARTMENT_ID | int(11) | NO | | NULL | |
+---------------+---------+------+-----+---------+-------+
Now see the created index file.
mysql> SHOW INDEX FROM job_history;
+-------------+------------+-----------+--------------+---------
----+-----------+-------------+----------+--------+------+------
------+---------+
| Table | Non_unique | Key_name | Seq_in_index |
Column_name | Collation | Cardinality | Sub_part | Packed | Null
| Index_type | Comment |
+-------------+------------+-----------+--------------+---------
----+-----------+-------------+----------+--------+------+------
------+---------+
| job_history | 1 | fk_job_id | 1 | JOB_ID
| A | NULL | NULL | NULL | | BTREE
| |
+-------------+------------+-----------+--------------+---------
----+-----------+-------------+----------+--------+------+------
------+---------+
ACCENTURE BATCH 2 LABORATORY
Write a SQL statement to drop the existing foreign key fk_job_id from job_history
Page | 20 table on job_id column which is referencing to the job_id of jobs table.
Copy
Now see the structure of the table job_history after being altered.
mysql> SHOW COLUMNS FROM job_history;
+---------------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------------+---------+------+-----+---------+-------+
| EMPLOYEE_ID | int(11) | NO | | NULL | |
| START_DATE | date | YES | | NULL | |
| HIRE_DATE | date | YES | | NULL | |
| JOB_ID | int(11) | NO | MUL | NULL | |
| DEPARTMENT_ID | int(11) | NO | | NULL | |
+---------------+---------+------+-----+---------+-------+
Now see the index file.
+-------------+------------+-----------+--------------+---------
----+-----------+-------------+----------+--------+------+------
------+---------+
ACCENTURE BATCH 2 LABORATORY
Copy
Now see the structure of the table job_history after being altered.
mysql> SHOW COLUMNS FROM job_history;
+---------------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
Page | 23 +---------------+---------+------+-----+---------+-------+
| EMPLOYEE_ID | int(11) | NO | PRI | NULL | |
| START_DATE | date | YES | | NULL | |
| HIRE_DATE | date | YES | | NULL | |
| JOB_ID | int(11) | NO | MUL | NULL | |
| DEPARTMENT_ID | int(11) | NO | | NULL | |
+---------------+---------+------+-----+---------+-------+
Now see the index file.
mysql> SHOW INDEXES FROM job_history;
+-------------+------------+-------------+--------------+-------
------+-----------+-------------+----------+--------+------+----
--------+---------+
| Table | Non_unique | Key_name | Seq_in_index |
Column_name | Collation | Cardinality | Sub_part | Packed | Null
| Index_type | Comment |
+-------------+------------+-------------+--------------+-------
------+-----------+-------------+----------+--------+------+----
--------+---------+
| job_history | 0 | PRIMARY | 1 |
EMPLOYEE_ID | A | 0 | NULL | NULL |
| BTREE | |
| job_history | 1 | indx_job_id | 1 | JOB_ID
| A | 0 | NULL | NULL | | BTREE
| |
+-------------+------------+-------------+--------------+-------
------+-----------+-------------+----------+--------+------+----
--------+---------+
ACCENTURE BATCH 2 LABORATORY
Write a SQL statement to drop the index indx_job_id from job_history table.
Page | 24
Here is the structure of the job_history and index file of the table job_history.
mysql> SHOW COLUMNS FROM job_history;
+---------------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------------+---------+------+-----+---------+-------+
| EMPLOYEE_ID | int(11) | NO | PRI | NULL | |
| START_DATE | date | YES | | NULL | |
| HIRE_DATE | date | YES | | NULL | |
| JOB_ID | int(11) | NO | MUL | NULL | |
| DEPARTMENT_ID | int(11) | NO | | NULL | |
+---------------+---------+------+-----+---------+-------+
Code:
ALTER TABLE job_history
Copy
Now see the structure of the table job_history after being altered.
Page | 25
mysql> SHOW COLUMNS FROM job_history;
+---------------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------------+---------+------+-----+---------+-------+
| EMPLOYEE_ID | int(11) | NO | PRI | NULL | |
| START_DATE | date | YES | | NULL | |
| HIRE_DATE | date | YES | | NULL | |
| JOB_ID | int(11) | NO | | NULL | |
| DEPARTMENT_ID | int(11) | NO | | NULL | |
+---------------+---------+------+-----+---------+-------+
Now see the index file.
mysql> SHOW INDEXES FROM job_history;
+-------------+------------+----------+--------------+----------
---+-----------+-------------+----------+--------+------+-------
-----+---------+
| Table | Non_unique | Key_name | Seq_in_index |
Column_name | Collation | Cardinality | Sub_part | Packed | Null
| Index_type | Comment |
+-------------+------------+----------+--------------+----------
---+-----------+-------------+----------+--------+------+-------
-----+---------+
| job_history | 0 | PRIMARY | 1 |
EMPLOYEE_ID | A | 0 | NULL | NULL |
| BTREE | |
+-------------+------------+----------+--------------+----------
---+-----------+-------------+----------+--------+------+-------
-----+---------+