如果 MySQL 中的值相同,UPDATE 是否會覆蓋它們


不,如果值相同,MySQL UPDATE 不會覆蓋值。我們先建立一個表 -

mysql> create table DemoTable
(
   StudentId int,
   StudentMathMarks int,
   StudentMySQLMarks int
);
Query OK, 0 rows affected (0.46 sec)

使用 insert 命令在表中插入一些記錄 -

mysql> insert into DemoTable values(1,56,78);
Query OK, 1 row affected (0.21 sec)
mysql> insert into DemoTable values(2,88,99);
Query OK, 1 row affected (0.15 sec)
mysql> inse
rt into DemoTable values(3,34,98);
Query OK, 1 row affected (0.13 sec)

使用 select 語句顯示錶中的所有記錄 -

mysql> select *from DemoTable;

這將產生以下輸出 -

+-----------+------------------+-------------------+
| StudentId | StudentMathMarks | StudentMySQLMarks |
+-----------+------------------+-------------------+
|         1 |               56 |                78 |
|         2 |               88 |                99 |
|         3 |               34 |                98 |
+-----------+------------------+-------------------+
3 rows in set (0.00 sec)

以下是更新值的查詢。設定的值已存在於特定的列中,即學生 ID 2,數學成績 88,學生 ID 2 的 SQL 成績 99 -

mysql> update DemoTable set StudentId=2,StudentMathMarks=88,StudentMySQLMarks=99 where StudentId=2;
Query OK, 0 rows affected (0.00 sec)
Rows matched: 1 Changed: 0 Warnings: 0

檢視上面的 UPDATE 命令,輸出 0 受影響行表示如果值相同,UPDATE 不會覆蓋值。

更新日期: 04-10-2019

416 次檢視

開啟你的 職業生涯

透過完成課程獲得認證

開始
廣告