概述
用途:更新表中原有數據
單獨使用,使用where匹配欄位
set後面,更新欄位值,既可以一次一項,也可以一次多項
例如1,
Update table_name Set column_name = new_value Where column_name = some_value
例:
“Person”表中的原始數據:
LastName FirstName Address City
Nilsen Fred Kirkegt 56 Stavanger
Rasmussen Storgt 67
運行下面的SQL將Person表中LastName欄位為”Rasmussen”的FirstName更新為”Nina”:
UPDATE Person SET FirstName = 'Nina' WHERE LastName = 'Rasmussen'
更新後”Person”表中的數據為:
LastName FirstName Address City
Nilsen Fred Kirkegt 56 Stavanger
Rasmussen Nina Storgt 67
同樣的,用UPDATE語句也可以同時更新多個欄位:
例如2,
UPDATE Person SET Address = 'Stien 12', City = 'Stavanger' WHERE LastName = 'Rasmussen'
更新後”Person”表中的數據為:
LastName FirstName Address City
Nilsen Fred Kirkegt 56 Stavanger
Rasmussen Nina Stien 12 Stavanger
語法
UPDATE 表名稱 SET 列名稱 = 新值 WHERE 列名稱 = 某值
套用
Person:
LastName | FirstName | Address | City |
Gates | Bill | Xuanwumen 10 | Beijing |
Wilson | Champs-Elysees |
更新某一行中的一個列
我們為 lastname 是 "Wilson" 的人添加 firstname:
UPDATE Person SET FirstName = 'Fred' WHERE LastName = 'Wilson' 結果:
LastName | FirstName | Address | City |
Gates | Bill | Xuanwumen 10 | Beijing |
Wilson | Fred | Champs-Elysees |
更新某一行中的若干列
我們會修改地址(address),並添加城市名稱(city):
UPDATE Person SET Address = 'Zhongshan 23', City = 'Nanjing'WHERE LastName = 'Wilson'結果:
LastName | FirstName | Address | City |
Gates | Bill | Xuanwumen 10 | Beijing |
Wilson | Fred | Zhongshan 23 | Nanjing |
與select
update用來更新用select選擇出的數據
例如3,
update 表名 set 欄位名=‘值’select .....