
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
Reorder keys after deleting a record from MySQL table?
For this, use UPDATE command with some mathematical calculations. To delete an id, use the DELETE. Let us first create a table −
mysql> create table DemoTable1476 -> ( -> Id int -> ); Query OK, 0 rows affected (0.81 sec)
Insert some records in the table using insert command −
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)
Display all records from the table using select statement −
mysql> select * from DemoTable1476;
This will produce the following output −
+------+ | Id | +------+ | 10 | | 20 | | 30 | | 40 | +------+ 4 rows in set (0.00 sec)
Following is the query to delete some id from the table −
mysql> delete from DemoTable1476 where Id=30; Query OK, 1 row affected (0.16 sec)
After deleting, the table records are as follows −
mysql> select * from DemoTable1476;
This will produce the following output −
+------+ | Id | +------+ | 10 | | 20 | | 40 | +------+ 3 rows in set (0.00 sec)
Following is the query to reorder keys after deleting from MySQL table −
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
Let us check the table records once again −
mysql> select * from DemoTable1476;
This will produce the following output −
+------+ | Id | +------+ | 10 | | 20 | | 30 | +------+ 3 rows in set (0.00 sec)
- Related Articles
- How to auto increment with 1 after deleting data from a MySQL table?
- How to optimize a MySQL table after deleting some rows?
- Need help in deleting duplicate columns from a table in MySQL?
- Does deleting row from view delete row from base table in MySQL?
- Change the column name from a MySQL table with Student record?
- How to delete last record (on condition) from a table in MySQL?
- How to get the second last record from a table in MySQL?
- Get the last record from a table in MySQL database with Java?
- How can you delete a record from a table using MySQL in Python?
- Insert record in a MySQL table with Java
- Find date record after a particular date from a column with VARCHAR type in MySQL
- Display dates after NOW() + 10 days from a MySQL table?
- Delete a specific record from a MySQL table by using AND in WHERE clause
- Deleting from temporary table in SAP HANA
- Deleting partial data from a field in MySQL?

Advertisements