SQL UPDATE 語(yǔ)句
UPDATE 語(yǔ)句用于更新表中已存在的記錄。
還可以使用AND或OR運(yùn)算符組合多個(gè)條件。
SQL UPDATE 語(yǔ)法
具有WHERE子句的UPDATE查詢(xún)的基本語(yǔ)法如下所示:
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
請(qǐng)注意 更新表中的記錄時(shí)要小心! 要注意SQL UPDATE 語(yǔ)句中的 WHERE 子句! WHERE子句指定哪些記錄需要更新。如果省略WHERE子句,所有記錄都將更新! |
演示數(shù)據(jù)庫(kù)
在本教程中,我們將使用著名的Northwind示例數(shù)據(jù)庫(kù)。
以下是 "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 實(shí)例
以下SQL語(yǔ)句為第一個(gè)客戶(hù)(CustomerID = 1)更新了“ContactName”和“City”:
實(shí)例
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 |
更新多個(gè)記錄
WHERE子句決定了將要更新的記錄數(shù)量。
以下SQL語(yǔ)句將把國(guó)家/地區(qū)為"Mexico"的所有記錄的聯(lián)系人姓名更新為“Juan”:
UPDATE Customers
SET ContactName='Juan'
WHERE Country='Mexico';
“Customers”表中的選擇現(xiàn)在看起來(lái)像這樣:
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 警告!
更新記錄時(shí)要小心。如果您省略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 |
更多建議: