Guide to Using ALTER, UPDATE, and DELETE Statements in MySQL

In MySQL, the ALTER, UPDATE, and DELETE statements are essential for modifying and managing the data in your database tables. This guide will provide you with an overview of these SQL statements, along with examples to demonstrate their usage.

Prerequisites

Before you start, make sure you have MySQL installed and have access to a MySQL database. You can use MySQL Command Line Client or any other MySQL database management tool.

1. ALTER Statement

The ALTER statement is used to modify an existing database table’s structure. You can add, modify, or delete columns, change data types, or apply constraints.

Example: Adding a New Column

Suppose you have a table called employees, and you want to add a new column phone_number to store employee phone numbers.

ALTER TABLE employees
ADD COLUMN phone_number VARCHAR(15);

Example: Modifying a Column

If you need to change the data type of an existing column, such as changing the salary column from INT to DECIMAL, you can use the following SQL statement:

ALTER TABLE employees
MODIFY COLUMN salary DECIMAL(10, 2);

2. UPDATE Statement

The UPDATE statement is used to modify existing records in a table. It allows you to change the values of one or more columns for specific rows based on a specified condition.

Example: Updating Records

Suppose you want to update the salary of an employee with employee_id 101 to set it to $60,000:

UPDATE employees
SET salary = 60000
WHERE employee_id = 101;

Example: Updating Multiple Columns

You can update multiple columns in a single UPDATE statement:

UPDATE employees
SET salary = 60000, department = 'HR'
WHERE employee_id = 101;

3. DELETE Statement

The DELETE statement is used to remove rows from a table based on a specified condition.

Example: Deleting Records

Suppose you want to delete all employees who have a salary less than $40,000:

DELETE FROM employees
WHERE salary < 40000;

Example: Deleting All Records

To delete all records from a table, you can use the following statement:

DELETE FROM employees;

Caution: Be extremely careful when using the DELETE statement without a WHERE clause, as it will delete all records in the table.

Conclusion

The ALTER, UPDATE, and DELETE statements are powerful tools for modifying and managing data in your MySQL database. With these SQL statements, you can change table structures, update existing records, and delete unwanted data. It’s essential to use them judiciously and always have a backup of your data before making significant changes to your database.

Remember to test your SQL statements in a safe environment, such as a development or staging database, before applying them to a production database to avoid unintended consequences. These statements are a fundamental part of database management and are crucial for maintaining the integrity and accuracy of your data.

Hello, I’m Anuj. I make and teach software.

My website is free of advertisements, affiliate links, tracking or analytics, sponsored posts, and paywalls.
Follow me on LinkedIn, X (twitter) to get timely updates when I post new articles.
My students are the reason this website exists, crafted with the affection and dedication they’ve shown. ❤️

Feedback Display