UPDATE, ALTER AND MODIFY COMMANDS IN SQL

Kolade Orimolade
4 min readMar 16, 2023

--

Let’s talk about manipulation in SQL. There are several SQL commands used to modify the structure of tables, columns, and data. In this article, we will be discussing three of the most commonly used commands — UPDATE, ALTER, and MODIFY. The UPDATE, ALTER, and MODIFY commands are essential SQL commands used to modify the structure of tables, columns, and data in relational databases. These commands are used in different scenarios by different people, depending on their roles and responsibilities in the database management process.

Here are some scenarios of when and where to use these commands:

  1. UPDATE Command:

The UPDATE command is used to modify existing data in a table. This command is used when there is a need to update the data in the table due to a change in the business logic or some other reason. The following are some scenarios when the UPDATE command is used:

  • When updating customer records with their latest contact details or billing information.
  • When updating inventory records with new stock levels or pricing information.
  • When updating employee records with promotions or salary increases.

2. ALTER Command:

The ALTER command is used to modify the structure of an existing table. This command is used when there is a need to add or remove columns, change the data type of a column, or rename a table. The following are some scenarios when the ALTER command is used:

  • When adding a new column to a table to store additional data.
  • When removing a column that is no longer required or redundant.
  • When changing the data type of a column to optimize the performance of the database.
  • When renaming a table to provide a more meaningful name or reflect a change in the business logic.

3. MODIFY Command:

The MODIFY command is used to modify the data type of an existing column in a table. This command is used when there is a need to change the data type of a column to accommodate new data or improve performance. The following are some scenarios when the MODIFY command is used:

  • When changing the data type of a column from text to numeric to perform calculations or sorting.
  • When changing the data type of a column from numeric to text to store additional data or improve search functionality.
  • When changing the data type of a column from a smaller to a larger data type to accommodate more data.

Now, let’s examine how each of these commands works.

  1. UPDATE command:

UPDATE is used to modify existing data in a table. It is often used in conjunction with the SELECT command to identify which records need to be updated. The syntax for the UPDATE function is as follows:

UPDATE table_name
SET column_name = new_value
WHERE condition;

The table_name parameter specifies the name of the table to update, the column_name parameter specifies the name of the column to update, and the new_value parameter specifies the new value to be set in the specified column. The WHERE clause is optional and is used to specify which records to update based on a certain condition. If the WHERE clause is not specified, all records in the table will be updated.

For example, suppose we have a table named employees with columns id, name, salary, and department. We can use the following UPDATE statement to increase the salary of all employees in the sales department by 10%:

UPDATE employees
SET salary = salary * 1.1
WHERE department = 'sales';

2. ALTER Function:

The ALTER command is used to modify the structure of an existing table. This can involve adding or removing columns, changing the data type of a column, or renaming a table. The syntax for the ALTER function is as follows:

ALTER TABLE table_name
ADD column_name data_type;
ALTER TABLE table_name
DROP column_name;
ALTER TABLE table_name
RENAME TO new_table_name;

The first ALTER statement adds a new column to the specified table. The column_name parameter specifies the name of the new column, and the data_type parameter specifies the data type of the new column.

The second ALTER statement drops a column from the specified table. The column_name parameter specifies the name of the column to be dropped.

The third ALTER statement renames the specified table to a new name. The new_table_name parameter specifies the new name for the table.

For example, suppose we have a table named customers with columns id, name, email, and phone. We can use the following ALTER statement to add a new column named address of type VARCHAR(255):

ALTER TABLE customers
ADD address VARCHAR(255);

3. MODIFY command:

The MODIFY command is used to modify the data type of an existing column in a table. This can be useful when the data type of a column needs to be changed to accommodate new data or to improve performance. The syntax for the MODIFY function is as follows:

ALTER TABLE table_name
MODIFY column_name new_data_type;

The table_name parameter specifies the name of the table, and the column_name parameter specifies the name of the column to modify. The new_data_type parameter specifies the new data type for the column.

For example, suppose we have a table named orders with a column named order_date of type DATE. We can use the following MODIFY statement to change the data type of the order_date column to DATETIME:

ALTER TABLE orders
MODIFY order_date DATETIME;

In conclusion, the UPDATE, ALTER, and MODIFY functions are essential SQL commands used to manage and manipulate data in relational databases. Understanding how to use these functions effectively is critical for anyone working with SQL databases.

Thanks for reading and Happy Data Manipulation ✌✌

--

--