SQL UPDATE 语句

SQL UPDATE 语句用来更新数据表中的记录。

SQL UPDATE 语句语法

UPDATE TableName
SET Column1=Value1, Column2=Value2, ...
WHERE conditions

WHERE子句对SQL UPDATE非常重要,如果忽略,所有记录都将会被修改。

SQL UPDATE 范例

数据表: Employees

EmployeeId FirstName LastName Department Salary
203 Mary Fox Finance 78000
204 Joe Lanyon Finance 45800
303 Dona Earl Development 55000
304 Tony Oakes Development 49000

例1
我们想将 Dona Earl 的部门改为Finance:

UPDATE Employees
SET Department='Finance'
WHERE FirstName='Dona' AND LastName='Earl'

Employees 表将会更新为:

EmployeeId FirstName LastName Department Salary
203 Mary Fox Finance 78000
204 Joe Lanyon Finance 45800
303 Dona Earl Finance 55000
304 Tony Oakes Development 49000

例2
我们想将Tony Oakes的工资加$5000:

UPDATE Employees
SET Salary = Salary + 5000
WHERE FirstName='Tony' AND LastName='Oakes'

现在Employees表会如下所示:

EmployeeId FirstName LastName Department Salary
203 Mary Fox Finance 78000
204 Joe Lanyon Finance 45800
303 Dona Earl Finance 55000
304 Tony Oakes Development 54000