MySQL 编辑和更新包含员工工资在内的记录


MySQL 中使用 UPDATE 命令来更新记录。通过它,SET 命令用于设置新值。我们首先创建一个表 -

mysql> create table DemoTable
(
   EmployeeId int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   EmployeeName varchar(50),
   EmployeeSalary int
);
Query OK, 0 rows affected (0.57 sec)

使用 insert 命令在表中插入一些记录 -

mysql> insert into DemoTable(EmployeeName,EmployeeSalary) values('Chris',56780);
Query OK, 1 row affected (0.14 sec)
mysql> insert into DemoTable(EmployeeName,EmployeeSalary) values('Robert',45670);
Query OK, 1 row affected (0.10 sec)
mysql> insert into DemoTable(EmployeeName,EmployeeSalary) values('Mike',87654);
Query OK, 1 row affected (0.13 sec)
mysql> insert into DemoTable(EmployeeName,EmployeeSalary) values('David',34569);
Query OK, 1 row affected (0.11 sec)

使用 select 语句显示表中的所有记录 -

mysql> select *from DemoTable;

这将产生以下输出 -

+------------+--------------+----------------+
| EmployeeId | EmployeeName | EmployeeSalary |
+------------+--------------+----------------+
|          1 | Chris        |          56780 |
|          2 | Robert       |          45670 |
|          3 | Mike         |          87654 |
|          4 | David        |          34569 |
+------------+--------------+----------------+
4 rows in set (0.00 sec)

以下是更新记录并设置新值的查询 -

mysql> update DemoTable
   set EmployeeSalary=EmployeeSalary+12346;
Query OK, 4 rows affected (0.14 sec)
Rows matched: 4 Changed: 4 Warnings: 0

让我们检查表记录 -

mysql> select *from DemoTable;

这将产生以下输出 -

+------------+--------------+----------------+
| EmployeeId | EmployeeName | EmployeeSalary |
+------------+--------------+----------------+
|          1 | Chris        | 69126          |
|          2 | Robert       | 58016          |
|          3 | Mike         | 100000         |
|          4 | David        | 46915          |
+------------+--------------+----------------+
4 rows in set (0.00 sec)

更新于: 04-Oct-2019

437 次浏览

开启你的 职业 生涯

完成课程即可获得认证

立刻开始
广告