赞
踩
SQL databases provide structured data storage capabilities. We can store data in tables with columns and rows. Other useful features for SQL databases are update capabilities. We can update SQL database data in different ways and constraints. In this tutorial, we will learn how to update database table data with an SQL UPDATE statement and query a single record, multiple records, or conditional situations with examples.
SQL数据库提供结构化的数据存储功能。 我们可以将数据存储在具有列和行的表中。 SQL数据库的其他有用功能是更新功能。 我们可以采用不同的方式和约束来更新SQL数据库数据。 在本教程中,我们将学习如何使用SQL UPDATE语句更新数据库表数据以及如何通过示例查询单个记录,多个记录或条件情况。
SQL Update statement or query has the following syntax with the given values.
SQL Update语句或查询具有以下具有给定值的语法。
- update TABLE_NAME
- set COLUMN1=VALUE1, COLUMN2=VALUE2, ... , COLUMNN=VALUEN
- where CONDITION;
TABLE_NAME
is the table in which we want to update its records.
TABLE_NAME
是我们要在其中更新其记录的表。
COLUMN1
is the column name we want to update.
COLUMN1
是我们要更新的列名。
VALUE1
is the value we want to set the value.
VALUE1
是我们要设置的值。
COLUMN
and VALUE
can be set for multiple times without a problem.
可以多次设置COLUMN
和VALUE
。
CONDITION
is used to select the record or row to update.
CONDITION
用于选择要更新的记录或行。
During this tutorial, we will use the following database table in order to update it in different ways. We try to make the table simple in order to prevent misunderstanding and decrease complexity. In this table, we have the following columns. The table is named Users
.
在本教程中,我们将使用以下数据库表以不同方式对其进行更新。 我们尝试使表格简单化,以防止误解并降低复杂性。 在此表中,我们具有以下列。 该表名为Users
。
ID | Name | City | Age | Country |
1 | Joachim Löw | Berlin | 20 | Germany |
2 | Ahmet Ali Baydan | Ankara | 35 | Turkey |
3 | Antonio Moreno Taquería | México | 40 | Mexico |
4 | James Bond | London | 25 | UK |
5 | Zlatan İbrahimovic | Lulea | 60 | Sweden |
ID | 名称 | 市 | 年龄 | 国家 |
1个 | 约阿希姆·洛(JoachimLöw) | 柏林 | 20 | 德国 |
2 | 艾哈迈德·阿里·拜丹(Ahmet Ali Baydan) | 安卡拉 | 35 | 火鸡 |
3 | 安东尼奥·莫雷诺·塔克里亚 | 墨西哥 | 40 | 墨西哥 |
4 | 占士邦 | 伦敦 | 25 | 英国 |
5 | Zlatanİbrahimovic | 露蕾亚 | 60 | 瑞典 |
We will start with a simple update table example. We will only update the single record single value. We will update the age value of the Zlatan İbrahimovic by using its ID. We will set the age of the Zlatan İbrahimovic to the 38.
我们将从一个简单的更新表示例开始。 我们将只更新单个记录单个值。 我们将使用其ID更新Zlatanİbrahimovic的年龄值。 我们将Zlatanİbrahimovic的年龄设定为38岁。
- UPDATE Users
- SET Age=38
- WHERE ID=5;
Update records can be used to update multiple records in a single Update query execution. We have to specify some conditions which will match multiple records on the given table and update the given columns. In this example, we will the Country of the users whose ages are over 30.
更新记录可用于在单个Update查询执行中更新多个记录。 我们必须指定一些条件,这些条件将匹配给定表上的多个记录并更新给定的列。 在此示例中,我们将选择年龄超过30岁的用户所在的国家/地区。
- UPDATE Users
- SET Country='Turkey'
- WHERE Age>30;
We can update multiple columns for the given condition. Here we need to specify the column and value pairs by delimiting them with a comma. In this example, we will update the Country and City values of the users whose ages are over 30. We will set Country as Turkey
and City as Ankara
.
我们可以针对给定条件更新多个列。 在这里,我们需要通过用逗号定界来指定列和值对。 在此示例中,我们将更新年龄超过30岁的用户的“国家/地区”和“城市”值。我们将“国家/地区”设置为“ Turkey
,将“城市”设置为“ Ankara
。
- UPDATE Users
- SET Country='Turkey', City='Ankara'
- WHERE Age>30;
In some cases, we may need to update the whole table or all rows in a table for a specific column. In this case, we do not need a condition because we will match all table rows. In this example, we will set all users table records Age
value to the 30
.
在某些情况下,我们可能需要为特定列更新整个表或表中的所有行。 在这种情况下,我们不需要条件,因为我们将匹配所有表行。 在此示例中,我们将所有用户表记录的Age
值设置为30
。
- UPDATE Users
- SET Age=30;
During the application development, we generally write some SQL queries. The update SQL query is one of the most used ones. Almost every application has some tables which have the ID
column. We generally use ID values in order to update the table. In this example, we will show how to update according to ID.
在应用程序开发期间,我们通常会编写一些SQL查询。 更新SQL查询是最常用的查询之一。 几乎每个应用程序都有一些带有ID
列的表。 我们通常使用ID值来更新表。 在此示例中,我们将展示如何根据ID更新。
- UPDATE Users
- SET Age=30
- WHERE ID=1;
OR we can update records those ID numbers higher than 3 .
或者,我们可以更新记录,这些ID编号大于3。
- UPDATE Users
- SET Age=30
- WHERE ID>1;
Another useful scenario for the SQL Update is using with a BETWEEN clause or condition to match records to be updated. We can specify range by using BETWEEN
where the record values are between this range will be updated. In this example, we will update the Country to Turkey where Age is between 30 and 40.
SQL Update的另一个有用方案是使用BETWEEN子句或条件来匹配要更新的记录。 我们可以使用BETWEEN
来指定范围,其中记录值在此范围之间将被更新。 在此示例中,我们将国家/地区更新为年龄在30到40岁之间的土耳其。
- UPDATE Users
- SET Country='Turkey'
- WHERE Age BETWEEN 30 AND 40;
Up to now we have updated data by providing explicitly and directly in a SQL query. In complex databases and applications, data can be provided from other tables. We can update data by fetching it from other tables by using the UPDATE SQL statement. We will use an external table named Cities where we will update the Country value of the Users table records according to the Cities table.
到目前为止,我们已经通过显式和直接在SQL查询中提供数据来更新数据。 在复杂的数据库和应用程序中,可以从其他表中提供数据。 我们可以通过使用UPDATE SQL语句从其他表中获取数据来更新数据。 我们将使用一个名为Cities的外部表,在该表中,我们将根据Cities表更新Users表记录的Country值。
- UPDATE Users
- SET Country=(SELECT Country FROM Cities WHERE Cities.City = Users.City)
- WHERE (SELECT Country FROM Cities WHERE Cities.City=Users.City);
In the previous example, we have updated the Users table from another table named Cities. We can do this by using an INNER JOIN SQL statement. The scenario is the same as the previous example. We will use an external table named Cities where we will update the Country value of the Users table records according to the Cities table.
在前面的示例中,我们从另一个名为Cities的表中更新了Users表。 我们可以使用INNER JOIN SQL语句来做到这一点。 该方案与前面的示例相同。 我们将使用一个名为Cities的外部表,在该表中,我们将根据Cities表更新Users表记录的Country值。
- UPDATE Users
- SET Users.Country = Cities.Country
- FROM Users
- INNER JOIN Cities
- ON Users.City =Cities.City;
翻译自: https://www.poftut.com/sql-update-statement-and-query-with-examples/
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。