從 MySQL 表格中刪除記錄後重新排序鍵?
為此,請使用帶一些數學計算的 UPDATE 命令。要刪除 ID,請使用 DELETE。讓我們首先建立一個表格 -
mysql> create table DemoTable1476 -> ( -> Id int -> ); Query OK, 0 rows affected (0.81 sec)
使用 insert 命令在表格中插入一些記錄 -
mysql> insert into DemoTable1476 values(10); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable1476 values(20); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable1476 values(30); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable1476 values(40); Query OK, 1 row affected (0.12 sec)
使用 select 語句顯示錶格中的所有記錄 -
mysql> select * from DemoTable1476;
這將產生以下輸出 -
+------+ | Id | +------+ | 10 | | 20 | | 30 | | 40 | +------+ 4 rows in set (0.00 sec)
以下是要從表格中刪除一些 ID 的查詢 -
mysql> delete from DemoTable1476 where Id=30; Query OK, 1 row affected (0.16 sec)
刪除後,表格記錄如下 -
mysql> select * from DemoTable1476;
這將產生以下輸出 -
+------+ | Id | +------+ | 10 | | 20 | | 40 | +------+ 3 rows in set (0.00 sec)
以下是刪除 MySQL 表格後重新排序鍵的查詢 -
mysql> update DemoTable1476 set Id=Id-10 where Id > 30; Query OK, 1 row affected (0.14 sec) Rows matched: 1 Changed: 1 Warnings: 0
讓我們再次檢查表格記錄 -
mysql> select * from DemoTable1476;
這將產生以下輸出 -
+------+ | Id | +------+ | 10 | | 20 | | 30 | +------+ 3 rows in set (0.00 sec)
廣告