SQL 基础
SQL 简介
SQL 入门
SQL 语法
SQL 创建数据库
SQL 创建表
SQL Constraints
SQL Insert
SQL Select
SQL Where
SQL AND & OR
SQL IN & Between
SQL Order By
SQL Top/Limit
SQL Distinct
SQL Update
SQL Delete
SQL Truncate Table
SQL Drop
SQL 连接
SQL Joining Tables
SQL Inner Join
SQL Left Join
SQL Right Join
SQL Full Join
SQL Cross Join
高级 SQL
SQL Union
SQL Like
SQL Alter Table
SQL Aliases
SQL Group By
SQL Having
SQL Create View
SQL Create Index
SQL Dates and Times
SQL Cloning Tables
SQL 临时表
SQL 子查询
SQL 注入
SQL 参考
SQL 数据类型
MySQL 数据类型
SQL Server 数据类型
SQL 方法
SQL Update - SQL基础教程 - 笔下光年
网站首页
SQL Update
In this tutorial you will learn how to update records in a database table using SQL. ## Updating Table Data In the previous chapters we've learnt how to insert data as well as how to select data from a database table based on various conditions. In this tutorial we'll learn to perform one more important task which is updating the existing records in a database table. ## Syntax The UPDATE statement is used to update existing data in a table. ```sql UPDATE table_name SET column1_name = value1, column2_name = value2,... WHERE condition; ``` Here, column1_name, column2_name,... are the names of the columns or fields of a database table whose values you want to update. You can also combine multiple conditions using the AND or OR operators, that you've learned in the previous chapters. <div class="callout callout-danger mb-3">Warning: The WHERE clause in the UPDATE statement specifies which record or records should be updated. If you omit the WHERE clause, all the records will be updated.</div> Let's check out some examples that demonstrate how it actually works. Suppose we've an employees table in our database that has following records: ```sql +--------+--------------+------------+--------+---------+ | emp_id | emp_name | hire_date | salary | dept_id | +--------+--------------+------------+--------+---------+ | 1 | Ethan Hunt | 2001-05-01 | 5000 | 1 | | 2 | Tony Montana | 2002-07-15 | 6500 | 5 | | 3 | Sarah Connor | 2005-10-18 | 8000 | 3 | | 4 | Rick Deckard | 2007-01-03 | 7200 | 4 | | 5 | Martin Blank | 2008-06-24 | 5600 | NULL | +--------+--------------+------------+--------+---------+ ``` ## Updating a Single Column The following SQL statement will update the emp_name field of the employees table and set a new value where the employee id i.e. emp_id is equal to 3. ```sql UPDATE employees SET emp_name = 'Sarah Ann Connor' WHERE emp_id = 3; ``` After execution, the resulting table will look something like this: ```sql +--------+------------------+------------+--------+---------+ | emp_id | emp_name | hire_date | salary | dept_id | +--------+------------------+------------+--------+---------+ | 1 | Ethan Hunt | 2001-05-01 | 5000 | 1 | | 2 | Tony Montana | 2002-07-15 | 6500 | 5 | | 3 | Sarah Ann Connor | 2005-10-18 | 8000 | 3 | | 4 | Rick Deckard | 2007-01-03 | 7200 | 4 | | 5 | Martin Blank | 2008-06-24 | 5600 | NULL | +--------+------------------+------------+--------+---------+ ``` ## Updating Multiple Columns Similarly, you can update multiple columns using a list of comma-separated column name and value pair. The following example will update the salary and dept_id field of an existing employee in the employees table whose emp_id is 5. ```sql UPDATE employees SET salary = 6000, dept_id = 2 WHERE emp_id = 5; ``` After execution, the resulting table will look something like this: ```sql +--------+------------------+------------+--------+---------+ | emp_id | emp_name | hire_date | salary | dept_id | +--------+------------------+------------+--------+---------+ | 1 | Ethan Hunt | 2001-05-01 | 5000 | 1 | | 2 | Tony Montana | 2002-07-15 | 6500 | 5 | | 3 | Sarah Ann Connor | 2005-10-18 | 8000 | 3 | | 4 | Rick Deckard | 2007-01-03 | 7200 | 4 | | 5 | Martin Blank | 2008-06-24 | 6000 | 2 | +--------+------------------+------------+--------+---------+ ```
上一篇:
SQL Distinct
下一篇:
SQL Delete