sql update 语句_SQL Update语句概述

sql update 语句

In this article, we’ll walk-through the SQL update statement to modify one or more existing rows in the table. 

在本文中,我们将逐步介绍SQL更新语句,以修改表中的一个或多个现有行。

In order to modify data in a table, we’ll use an Update statement, a DML (data manipulation language) statement. A SQL update statement comes with a SET clause where we define the column-and-value as a pair of items. In addition, you can enforce the conditional clause. In order to limit the number of rows, we’ll need to set up a where clause. The condition is defined in the where clause that identifies what rows to modify in the table.

为了修改表中的数据,我们将使用Update语句,DML(数据操作语言)语句。 SQL Update语句带有SET子句,其中我们将列和值定义为一对项目。 另外,您可以强制执行条件子句。 为了限制行数,我们需要设置一个where子句。 该条件在where子句中定义,该子句标识表中要修改的行。

After reading this article, you’ll understand the following topics covering how to use a simple SQL update statement

阅读本文之后,您将了解以下主题,涉及如何使用简单SQL更新语句

  1. on multiple columns

    在多列上
  2. with computed value

    具有计算值
  3. with the compound operator

    与复合运算符
  4. with the defaults

    使用默认值
  5. with SQL joins

    与SQL联接
  6. with the Where clause

    带有Where子句
  7. on a remote table

    在远程表上
  8. with use Top(n) clause

    使用use Top(n)子句
  9. with CTE (Common-Table-Expression) statements

    与CTE(Common-Table-Expression)语句

运行一个简单SQL更新语句 (Running a simple SQL update statement)

For this example, we’ll work with Person.Person , so, let’s take a look at the data first. In this case, let’s say, hypothetically, we wanted to change the data of the ModifiedDate column for all rows of the table with the current datetimestamp value.

对于此示例,我们将使用Person.Person ,因此,让我们首先看一下数据。 假设,在这种情况下,我们想更改具有当前datetimestamp值的表的所有行的ModifiedDate列的数据。

Let us use the keyword UPDATE, and then the name of the table Person.Person, then use the keyword SET, and after that list the column name ModifiedDate and then the value, in this case, it’s current date timestamp.

让我们使用关键字UPDATE,然后使用表Person.Person的名称,然后使用关键字SET,然后在此之后列出列名称ModifiedDate ,然后是值(在这种情况下为当前日期时间戳)。

USE AdventureWorks2014;  
GO  
UPDATE Person.Person  
SET ModifiedDate = GETDATE();

对多列使用更新SQL语句 (Using an update SQL statement with Multiple columns)

Here, we’ve to come up with a pair of items, one being the column name, and one being the value, separated by an equal sign. The following example updates the columns Bonus with the value 8000, CommissionPct with the value .30, and SalesQuota by NULL for all rows in the Sales.SalesPerson table.

在这里,我们要提出一对项目,一个是列名,另一个是值,以等号分隔。 下面的示例对Sales.SalesPerson表中的所有行将Bonus列的值更新为8000,将CommissionPct列的值更新为.30,将SalesQuota列更新为NULL。

USE AdventureWorks2014;  
GO  
UPDATE Sales.SalesPerson
  SET 
      Bonus = 8000, 
      CommissionPct = .10, 
      SalesQuota = NULL

In this example, the above SQL update statement can be re-written using FROM clause and table alias.

在此示例中,可以使用FROM子句和表别名来重写上述SQL更新语句

你可能感兴趣的:(java,mysql,sql,python,数据库)