SQLUPDATE语句和⽰例查询
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语法 (SQL UPDATE Syntax)
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⽤于选择要更新的记录或⾏。
数据库表数据⽰例 (Example of Database Table Data)
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
ID
Name
名称
City
Age
年龄
Country
国家
sql中update什么意思
ID Name City Age Country
1Joachim Löw Berlin20Germany
2Ahmet Ali Baydan Ankara35Turkey
3Antonio Moreno Taquería México40Mexico
4James Bond London25UK
5Zlatan İbrahimovic Lulea60Sweden
ID名称市年龄国家
1个约阿希姆·洛(JoachimLöw)柏林20德国
2艾哈迈德·阿⾥·拜丹(Ahmet Ali Baydan)安卡拉35⽕鸡
3安东尼奥·莫雷诺·塔克⾥亚墨西哥40墨西哥
4占⼠邦伦敦25英国
5Zlatanİbrahimovic露蕾亚60瑞典
更新表(Update Table)
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İbr
ahimovic的年龄值。 我们将Zlatanİbrahimovic的年龄设定为38岁。
UPDATE Users
SET Age=38
WHERE ID=5;
更新多个记录 (Update Multiple Records)
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;
更新多列 (Update Multiple Columns)
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;
更新特定列的所有表⾏/记录 (Update All Table Rows/Records For Specific Column)
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;
根据ID列更新 (Update According To ID Column)
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;
使⽤BETWEEN⼦句更新 (Update Using BETWEEN Clause)
Another useful scenario for the SQL Update is using with a BETWEEN clause or condition to match r
ecords 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;
使⽤SQL Select从其他表更新表 (Update Table From Other Table with SQL Select)
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);
使⽤SQL INNER JOIN更新 (Update Using SQL INNER JOIN)
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;
了解更多如何将PostgreSQL服务器安装到Linux,Debian,Ubuntu,CentOS,Mint,Fedora,Redhat中?

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。