Ticker

6/recent/ticker-posts

Update Statement in SQL

Update Statement in SQL


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

Syntax The basic syntax of the UPDATE statement is as follows:

sql
UPDATE table_name SET column1 = value1, column2 = value2, ... WHERE condition;
  • table_name: Specifies the name of the table that you want to update.
  • column1, column2, ...: Names of the columns you want to update.
  • value1, value2, ...: New values to be assigned to the columns.
  • WHERE condition: Optional condition to specify which rows should be updated. If omitted, all rows in the table will be updated.

Example Consider a table named "employees" with the following structure:

sql
CREATE TABLE employees ( id INT PRIMARY KEY, name VARCHAR(50), age INT, salary DECIMAL(10,2) );

To update the salary of an employee with a specific ID, you can use the following UPDATE statement:

sql
UPDATE employees SET salary = 50000 WHERE id = 123;

In the above example, the salary of the employee with ID 123 will be updated to 50000.

You can also update multiple columns simultaneously:

sql
UPDATE employees SET age = 30, salary = 60000 WHERE id = 123;

In this case, both the age and salary of the employee with ID 123 will be updated.

If you want to update multiple rows based on certain conditions, you can use a WHERE clause. For example, to update the salaries of all employees older than 40:

sql
UPDATE employees SET salary = salary + 10000 WHERE age > 40;

In the above query, the salary of all employees older than 40 will be increased by 10000.

Conclusion The UPDATE statement in SQL allows you to modify existing records in a database table. It provides a flexible way to update one or more columns for one or more rows based on specified conditions. By using the UPDATE statement effectively, you can make necessary changes to the data in your database tables.

Post a Comment

0 Comments