H2 数据库 - Update 查询
UPDATE 查询用于更新或修改表中的现有记录。 我们可以使用 WHERE 子句和 UPDATE 查询来更新选定的行,否则所有行都会受到影响。
语法
以下是 UPDATE 查询的基本语法。
UPDATE tableName [ [ AS ] newTableAlias ] SET { { columnName = { DEFAULT | expression } } [,...] } | { ( columnName [,...] ) = ( select ) } [ WHERE expression ] [ ORDER BY order [,...] ] [ LIMIT expression ]
在此 UPDATE 语法中,我们可以使用 AND 或 OR 子句组合多个条件。
示例
考虑具有以下记录的 CUSTOMER 表。
+----+----------+-----+-----------+----------+ | ID | NAME | AGE | ADDRESS | SALARY | +----+----------+-----+-----------+----------+ | 1 | Ramesh | 32 | Ahmedabad | 2000.00 | | 2 | Khilan | 25 | Delhi | 1500.00 | | 3 | kaushik | 23 | Kota | 2000.00 | | 4 | Chaitali | 25 | Mumbai | 6500.00 | | 5 | Hardik | 27 | Bhopal | 8500.00 | | 6 | Komal | 22 | MP | 4500.00 | | 7 | Muffy | 24 | Indore | 10000.00 | +----+----------+-----+-----------+----------+
如果您想获取客户表以及给定数据,请执行以下查询。
CREATE TABLE CUSTOMER (id number, name varchar(20), age number, address varchar(20), salary number); INSERT into CUSTOMER values (1, 'Ramesh', 32, 'Ahmedabad', 2000); INSERT into CUSTOMER values (2, 'Khilan', 25, 'Delhi', 1500); INSERT into CUSTOMER values (3, 'kaushik', 23, 'Kota', 2000); INSERT into CUSTOMER values (4, 'Chaitali', 25, 'Mumbai', 6500); INSERT into CUSTOMER values (5, 'Hardik', 27, 'Bhopal', 8500); INSERT into CUSTOMER values (6, 'Komal', 22, 'MP', 4500); INSERT into CUSTOMER values (7, 'Muffy', 24, 'Indore', 10000);
The following command is an example, which would update ADDRESS for a customer whose ID is 6 −
UPDATE CUSTOMERS SET ADDRESS = 'Pune' WHERE ID = 6;
现在,CUSTOMERS 表将具有以下记录。 我们可以通过执行以下查询来检查客户表记录。
SELECT * FROM CUSTOMERS;
The above query produces the following result.
+----+----------+-----+-----------+----------+ | ID | NAME | AGE | ADDRESS | SALARY | +----+----------+-----+-----------+----------+ | 1 | Ramesh | 32 | Ahmedabad | 2000.00 | | 2 | Khilan | 25 | Delhi | 1500.00 | | 3 | kaushik | 23 | Kota | 2000.00 | | 4 | Chaitali | 25 | Mumbai | 6500.00 | | 5 | Hardik | 27 | Bhopal | 8500.00 | | 6 | Komal | 22 | Pune | 4500.00 | | 7 | Muffy | 24 | Indore | 10000.00 | +----+----------+-----+-----------+----------+
要修改 CUSTOMERS 表中的所有 ADDRESS 和 SALARY 列值,我们不需要使用 WHERE 子句。 UPDATE 查询如下 −
UPDATE CUSTOMERS SET ADDRESS = 'Pune', SALARY = 1000.00;
现在,CUSTOMERS 表将具有以下记录。 我们可以通过执行以下查询来检查客户表记录。
SELECT * FROM CUSTOMERS;
上面的查询产生以下结果 −
+----+----------+-----+---------+---------+ | ID | NAME | AGE | ADDRESS | SALARY | +----+----------+-----+---------+---------+ | 1 | Ramesh | 32 | Pune | 1000.00 | | 2 | Khilan | 25 | Pune | 1000.00 | | 3 | kaushik | 23 | Pune | 1000.00 | | 4 | Chaitali | 25 | Pune | 1000.00 | | 5 | Hardik | 27 | Pune | 1000.00 | | 6 | Komal | 22 | Pune | 1000.00 | | 7 | Muffy | 24 | Pune | 1000.00 | +----+----------+-----+---------+---------+