ALTER and TRUNCATE Tables Using MySQL
ALTER and TRUNCATE Tables Using MySQL
In the previous video, the ALTER and TRUNCATE syntax applies to DB2. There are variations in syntax between different databases. This reading
will explore some examples of ALTER and TRUNCATE statements using MySQL.
Objective(s)
At the end of this reading, you will be able to:
ALTER TABLE
ALTER TABLE statements can be used to add or remove columns from a table, to modify the data type of columns, to add or remove keys, and to add or remove
constraints. The syntax of the ALTER TABLE statement is:
Copied!
1. 1
2. 2
Copied!
By default, all the entries are initially assigned the value NULL. You can then use UPDATE statements to add the necessary column values.
For example, to add a telephone_number column to the author table in the library database, the statement will be written as:
1. 1
2. 2
Copied!
Copied!
Sometimes, the data presented may be in a different format than required. In such a case, we need to modify the data_type of the column. For example, using a
numeric data type for telephone_number means you cannot include parentheses, plus signs, or dashes as part of the number. For such entries, the appropriate
about:blank 1/3
02/07/2024, 15:33 about:blank
choice of data_type is CHAR.
1. 1
2. 2
Copied!
The entries can then be updated using UPDATE statements. An updated version of the "author" table is shown below.
TRUNCATE Table
TRUNCATE TABLE statements are used to delete all of the rows in a table. The syntax of the statement is:
1. 1
Copied!
So, to truncate the "author" table, the statement will be written as:
1. 1
Copied!
Note: The TRUNCATE statement will delete the rows and not the table.
Author
D.M.Naidu
Additional Contributor(s)
Abhishek Gagneja
Change Log
Date (YYYY-MM-DD) Version Changed By Change Description
2023-10-07 1.6 Misty Taylor ID Check
2023-09-09 1.6 Abhishek Gagneja Updated instruction set
2023-05-11 1.5 Eric Hao & Vladislav Boyko Updated Page Frames
2023-05-10 1.4 Eric Hao & Vladislav Boyko Updated Page Frames
about:blank 2/3
02/07/2024, 15:33 about:blank
Date (YYYY-MM-DD) Version Changed By Change Description
2023-05-10 1.3 Eric Hao & Vladislav Boyko Updated Page Frames
2023-05-10 1.2 Eric Hao & Vladislav Boyko Updated Page Frames
2022-11-08 1.1 D.M.Naidu Intial version
about:blank 3/3