mysql update语句的执行过程详解

天天见闻 天天见闻 2022-03-12 教育 阅读: 502
摘要: where子句的工作原理与使用SELECT语句的工作原理完全相同。现在,我们有了带有SET,FROM和Where子句关键字的更新SQL语句。UPDATE语句中修改的行数。下面的示例更新Sales.让我们使用CTE运行相同SQL更新语句以下示例使用链接的服务器更新远程服务器上的数据。Update语句更新行的一些简单方法,以及在使用条件,子句和其他上下文的情况下进行的各种排列。

使用带有Where子句的更新SQL语句 (Using an update SQL statement with a Where clause)

In the following example, we only want to update one row of the Sales.SalesPerson table.In order to do that, we’ll need to use the WHERE clause.The where clause works exactlythe same as they did with the SELECT statements. So, let’s add the keyword WHERE,and set a filterthat specifies which record to modify.In this case, its BusinessEntityID is equal to 280.Now, we have an update SQL statement with SET, FROM and Where clause keywords.

在下面的示例中,我们只想更新Sales.SalesPerson表的一行。 为此,我们需要使用WHERE子句。 where子句的工作原理与使用SELECT语句的工作原理完全相同。 因此,让我们添加关键字WHERE,并设置一个过滤器,该过滤器指定要修改的记录。 在这种情况下,其BusinessEntityID等于280。现在,我们有了带有SET,FROM和Where子句关键字的更新SQL语句。

SELECT *
FROM Sales.SalesPerson S
WHERE BusinessEntityID = 280;
 
USE AdventureWorks2014;  
GO  
UPDATE S
  SET 
      Bonus = 8000, 
      CommissionPct = .30, 
      SalesQuota = NULL
FROM Sales.SalesPerson S
WHERE BusinessEntityID = 280;

And now, we see that the columns Bonus, ComissionPct, and SalesQuota have been changedwith new values.

现在,我们看到Bonus,ComissionPct和SalesQuota列已更改为新值。

SELECT *
FROM Sales.SalesPerson S
WHERE BusinessEntityID = 280;

将SQL Update语句与顶层子句一起使用 (Using an SQL Update statement with a Top Clause)

In the following examples, we can see that the use the TOP clause to limit the number of rows that are modified in the SQL UPDATE statement.

在以下示例中,我们可以看到使用TOP子句来限制在SQL UPDATE语句中修改的行数。

The following example updates the multiple columns of the matching rows in the Sales.SalesPerson table

下面的示例更新Sales.SalesPerson表中匹配行的多个列

USE AdventureWorks2014;  
GO  
UPDATE TOP (5) S
  SET 
      Bonus = 90000, 
      CommissionPct = .06, 
      SalesQuota = NULL
FROM Sales.SalesPerson S

We can see that the SQL update ran over a random selection of rows.

我们可以看到SQL更新遍历了随机选择的行。

 
SELECT *
FROM Sales.SalesPerson S where  CommissionPct = .06

SQL Update Statement

注意: 使用CTE将更新SQL语句与最高条款一起使用 (Using an update SQL statement with a Top Clause using a CTE)

As we all know that the SQL UPDATE statement with a TOP clause doesn’t support an ORDER BY clause but it is possible to get the sorted order of the columns using a CTE (Common Table Expression).

众所周知,带有TOP子句SQL UPDATE语句不支持ORDER BY子句,但是可以使用CTE (公用表表达式)获取列的排序顺序。

批量update sql语句_update语句_sql修改语句update

Let us run the same SQL update statement using a CTE

让我们使用CTE运行相同SQL更新语句

 
WITH CTE
     AS (SELECT TOP 5 *
         FROM Sales.SalesPerson
         ORDER BY BusinessEntityID)
     UPDATE CTE
       SET 
           Bonus = 190000, 
           CommissionPct = .07, 
           SalesQuota = NULL;

The output signifies the update ran over a order collection of BusinessEntityID.

输出表示更新已遍历BusinessEntityID的订单集合。

SELECT *
FROM Sales.SalesPerson where  CommissionPct = .07

SQL Update Statement

将SQL Update语句与计算值一起使用 (Using an SQL Update statement with Computed values)

The following example uses computed value in SQL Update statement. The example increases the value of the Bonus column by 100 and ComissionPct column by 0.005 values for all rows of the BusinessEntityID equal to 288.

下面的示例在SQL Update语句中使用计算值。 对于所有等于288的BusinessEntityIDupdate语句,该示例将Bonus列的值增加100,并将ComissionPct列的值增加0.005。

update语句_批量update sql语句_sql修改语句update

USE AdventureWorks2014;  
GO  
UPDATE Sales.SalesPerson
  SET 
      Bonus = Bonus+100, 
      CommissionPct = CommissionPct+0.005 
WHERE BusinessEntityID = 288;

将SQL Update语句与Compound运算符一起使用 (Using an SQL Update statement with Compound operators)

The following example uses the compound operator ‘+=’ and ‘*=’ to add 100 to the Bonus column and multiply 0.002 to CommissionPct column

下面的示例使用复合运算符'+ ='和'* ='将100加到Bonus列中,并将0.002乘以CommissionPct列

USE AdventureWorks2014;  
GO  
SELECT * FROM Sales.SalesPerson WHERE BusinessEntityID = 289;
GO
UPDATE Sales.SalesPerson
  SET 
      Bonus += 100, 
      CommissionPct *= 0.005 
WHERE BusinessEntityID = 289;
GO
SELECT * FROM Sales.SalesPerson WHERE BusinessEntityID = 289;

使用带有默认值SQL Update语句 (Using an SQL Update statement with a default values)

The following example sets the Primary column to its default value ((0)) for all rows that have a value equal to 1

下面的示例将值等于1的所有行的Primary列设置为其默认值((0))

USE AdventureWorks2014;  
GO  
UPDATE Production.ProductProductPhoto  
SET [Primary] = DEFAULT  
WHERE [Primary]=1

批量update sql语句_update语句_sql修改语句update

SELECT name,object_name(object_id),object_definition(default_object_id) Default_Definition
FROM   sys.columns
WHERE  name      ='Primary'
AND    object_id = object_id('Production.ProductProductPhoto')

将SQL Update语句与SQL连接一起使用 (Using an SQL Update statement with a SQL Joins)

This example modifies the Rate column of the entire rows employee where they belong to the ‘Research and Development’ group.

本示例修改了属于“研究与开发”组的整个行员工的“费率”列。

UPDATE EPH
  SET 
      EPH.Rate*=2
FROM HumanResources.EmployeePayHistory EPH
     INNER JOIN HumanResources.Employee EMP ON EMP.BusinessEntityID = EPH.BusinessEntityID
     INNER JOIN HumanResources.EmployeeDepartmentHistory H ON EMP.BusinessEntityID = H.BusinessEntityID
     INNER JOIN HumanResources.Department Dept ON H.DepartmentID = Dept.DepartmentID
WHERE Dept.GroupName = 'Research and Development';

SELECT EMP.JobTitle, 
       EPH.Rate * 2
FROM HumanResources.EmployeePayHistory EPH
     INNER JOIN HumanResources.Employee EMP ON EMP.BusinessEntityID = EPH.BusinessEntityID
     INNER JOIN HumanResources.EmployeeDepartmentHistory H ON EMP.BusinessEntityID = H.BusinessEntityID
     INNER JOIN HumanResources.Department Dept ON H.DepartmentID = Dept.DepartmentID
WHERE Dept.GroupName = 'Research and Development';

使用链接服务器和OPENQUERY的远程表 (Remote tables using a linked server and OPENQUERY)

The following example uses the linked server to update a data on a remote server.

以下示例使用链接的服务器更新远程服务器上的数据。

UPDATE HQDBT01.AdventureWorks2014.HumanResources.Department  
SET GroupName = N'CIS Relations'  
WHERE DepartmentID = 4;
 
 
SELECT GroupName FROM HQDBT01.AdventureWorks2014.HumanResources.Department WHERE DepartmentID = 4

In the following example, rows on the remote table is updated using the OPENQUERYrowset function

在以下示例中,使用OPENQUERY行集功能更新远程表上的行

UPDATE OPENQUERY(HQDBT01, 'SELECT GroupName FROM AdventureWorks2014.HumanResources.Department WHERE DepartmentID = 4')
  SET 
      GroupName = 'Sales and Marketing';

摘要 (Summary)

Thus far, we’ve discussed some of simple methods of updating rows using a SQL Update statement in SQL Server and various permutations using conditions, clauses and in other contexts. I hope you enjoyed reading this article and for any questions, please feel to comment below…

到目前为止,我们已经讨论了在SQL Server中使用SQL Update语句更新行的一些简单方法,以及在使用条件update语句,子句和其他上下文的情况下进行的各种排列。 希望您喜欢阅读本文,如有任何疑问,请在下面发表评论……

翻译自:

sql update 语句

其他相关
常见的MySQL语句

常见的MySQL语句

作者: 天天见闻 时间:2023-03-15 阅读: 141
...
告诉老默:这47个SQL性能优化技巧,赶紧收藏了!

告诉老默:这47个SQL性能优化技巧,赶紧收藏了!

作者: 天天见闻 时间:2023-02-20 阅读: 136
调用API接口调用存储引擎来查询数据,(1)所有数据库对象名称必须使用小写字母并用下划线分割,(1)所有数据库对象名称禁止使用mysql保留关键字;(5)所有存储相同数据的列名和列类型必须一致,3、所有表必须使用Innodb存储引擎,所有表必须使用Innodb存储引擎(mysql5.5之前默认使用Myisam,(2)不要使用UUID、MD5、HASH、字符串列作为主键(无法保证数据的顺序增长)...
windows update和microsoft update有什么区别,用microsoft update后是不是就可以不用windows update了?

windows update和microsoft update有什么区别,用microsoft update后是不是就可以不用windows update了?

作者: 天天见闻 时间:2022-07-15 阅读: 399
如果你采用microsoft update,可以同时搜索检测WINDOWS和OFFICE的所有关键更新并下载安装。windows update是microsoft update的一部分,某些盗版的Windows无法通过microsoft update升级....
Sql Update语句使用表别名的方法

Sql Update语句使用表别名的方法

作者: 天天见闻 时间:2022-05-04 阅读: 237
在编写Sql脚本时通过表别名可以大大缩减Sql代码,同时表别名也是解决同表多次引用的手段之一。在中使用表别名大家应该都很熟悉了:中使用表别名可能就没那么多人知道了。这是因为Sql引擎无法知道你在where子句中的到底指的是要的表还是from后面的表。#p#如果不对后面的使用别名的话,我们只能通过以下方法来实现。使用别名可以得到更简洁的写法:查询表的字段名的sql语句写法...
独特的deadlock(仅update语句也能造成死锁)

独特的deadlock(仅update语句也能造成死锁)

作者: 天天见闻 时间:2022-04-23 阅读: 296
(d)去掉,然后运行同样的语句循环,死锁就不会发生。为什么两条一模一样的语句,会相互死锁呢?为什么索引上去掉一个选项,或者改掉一个数据类型,就不会死锁了呢?index上的字段去掉,能够解决死锁问题呢(测试2)?表格上的索引越多、数据类型越复杂,执行计划也会越复杂,从而导致遇到阻塞或者死锁的几率增加。对于这个死锁案例本身,将(max)包含在一个索引里,是不太妥当的。...

MySQL中一条update语句是怎么执行的

作者: 天天见闻 时间:2022-03-16 阅读: 298
SQL语句的执行过程如果通过key能够查找到这条SQL语句,直接返回SQL的执行结果。接下来,分析器会经过语法分析和词法分析,知道了这是一条更新语句后,优化器决定要使用哪一个索引,然后执行器负责具体的执行,先找到这一行,然后做更新。InnoDB引擎部分在执行这个简单的update语句的时候的内部流程哪些是update语句执行之后做的,哪些是commit语句执行之后做的?...
我来说两句

年度爆文