SQL UPDATE 語句
UPDATE 語句用于更新表中已存在的記錄。
還可以使用AND或OR運算符組合多個條件。
SQL UPDATE 語法
具有WHERE子句的UPDATE查詢的基本語法如下所示:
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
請注意 更新表中的記錄時要小心! 要注意SQL UPDATE 語句中的 WHERE 子句! WHERE子句指定哪些記錄需要更新。如果省略WHERE子句,所有記錄都將更新! |
演示數(shù)據(jù)庫
在本教程中,我們將使用著名的Northwind示例數(shù)據(jù)庫。
以下是 "Customers" 表中的數(shù)據(jù):
CustomerID | CustomerName | ContactName | Address | City | PostalCode | Country |
---|---|---|---|---|---|---|
1
|
Alfreds Futterkiste | Maria Anders | Obere Str. 57 | Berlin | 12209 | Germany |
2 | Ana Trujillo Emparedados y helados | Ana Trujillo | Avda. de la Constitución 2222 | México D.F. | 05021 | Mexico |
3 | Antonio Moreno Taquería | Antonio Moreno | Mataderos 2312 | México D.F. | 05023 | Mexico |
4
|
Around the Horn | Thomas Hardy | 120 Hanover Sq. | London | WA1 1DP | UK |
5 | Berglunds snabbk?p | Christina Berglund | Berguvsv?gen 8 | Lule? | S-958 22 | Sweden |
SQL UPDATE 實例
以下SQL語句為第一個客戶(CustomerID = 1)更新了“ContactName”和“City”:
實例
SET ContactName = 'Alfred Schmidt', City= 'Frankfurt'
WHERE CustomerID = 1;
現(xiàn)在,選自 "Customers" 表的數(shù)據(jù)如下所示:
CustomerID | CustomerName | ContactName | Address | City | PostalCode | Country |
---|---|---|---|---|---|---|
1
|
Alfreds Futterkiste | Alfred Schmidt | Obere Str. 57 | Frankfurt | 12209 | Germany |
2 | Ana Trujillo Emparedados y helados | Ana Trujillo | Avda. de la Constitución 2222 | México D.F. | 05021 | Mexico |
3 | Antonio Moreno Taquería | Antonio Moreno | Mataderos 2312 | México D.F. | 05023 | Mexico |
4
|
Around the Horn | Thomas Hardy | 120 Hanover Sq. | London | WA1 1DP | UK |
5 | Berglunds snabbk?p | Christina Berglund | Berguvsv?gen 8 | Lule? | S-958 22 | Sweden |
更新多個記錄
WHERE子句決定了將要更新的記錄數(shù)量。
以下SQL語句將把國家/地區(qū)為"Mexico"的所有記錄的聯(lián)系人姓名更新為“Juan”:
UPDATE Customers
SET ContactName='Juan'
WHERE Country='Mexico';
“Customers”表中的選擇現(xiàn)在看起來像這樣:
CustomerID | CustomerName | ContactName | Address | City | PostalCode | Country |
---|---|---|---|---|---|---|
1
|
Alfreds Futterkiste | Alfred Schmidt | Obere Str. 57 | Frankfurt | 12209 | Germany |
2 | Ana Trujillo Emparedados y helados | Juan | Avda. de la Constitución 2222 | México D.F. | 05021 | Mexico |
3 | Antonio Moreno Taquería | Juan | Mataderos 2312 | México D.F. | 05023 | Mexico |
4
|
Around the Horn | Thomas Hardy | 120 Hanover Sq. | London | WA1 1DP | UK |
5 | Berglunds snabbk?p | Christina Berglund | Berguvsv?gen 8 | Lule? | S-958 22 | Sweden |
Update 警告!
更新記錄時要小心。如果您省略WHERE子句,所有記錄將被更新!
UPDATE Customers
SET ContactName='Juan';
"Customers" 表將如下所示:
CustomerID | CustomerName | ContactName | Address | City | PostalCode | Country |
---|---|---|---|---|---|---|
1
|
Alfreds Futterkiste | Juan | Obere Str. 57 | Frankfurt | 12209 | Germany |
2 | Ana Trujillo Emparedados y helados | Juan | Avda. de la Constitución 2222 | México D.F. | 05021 | Mexico |
3 | Antonio Moreno Taquería | Juan | Mataderos 2312 | México D.F. | 05023 | Mexico |
4
|
Around the Horn | Juan | 120 Hanover Sq. | London | WA1 1DP | UK |
5 | Berglunds snabbk?p | Juan | Berguvsv?gen 8 | Lule? | S-958 22 | Sweden |
更多建議: