
- 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
Shifting values of rows in MySQL to change the existing id values for existing rows?
Let us first create a table −
mysql> create table DemoTable -> ( -> StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> StudentName varchar(20) -> ); Query OK, 0 rows affected (1.07 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable(StudentName) values('Chris'); Query OK, 1 row affected (0.23 sec) mysql> insert into DemoTable(StudentName) values('Robert'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable(StudentName) values('David'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable(StudentName) values('Mike'); Query OK, 1 row affected (0.10 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+-----------+-------------+ | StudentId | StudentName | +-----------+-------------+ | 1 | Chris | | 2 | Robert | | 3 | David | | 4 | Mike | +-----------+-------------+ 4 rows in set (0.00 sec)
Here is the query to shift id values of existing rows in MySQL −
mysql> update DemoTable set StudentId=StudentId+1000; Query OK, 4 rows affected (0.18 sec) Rows matched: 4 Changed: 4 Warnings: 0
Let us check the table records once again −
mysql> select *from DemoTable;
This will produce the following output −
+-----------+-------------+ | StudentId | StudentName | +-----------+-------------+ | 1001 | Chris | | 1002 | Robert | | 1003 | David | | 1004 | Mike | +-----------+-------------+ 4 rows in set (0.00 sec)
- Related Articles
- How can we use existing values of the rows to provide new values in the SET clause of UPDATE statement?
- How to add +1 to existing MySQL values?
- Adding characters in values for an existing int column in MySQL?
- How add unique key to existing table (with non-unique rows)?
- How to change the password in MongoDB for existing user?
- MySQL query to multiply values of two rows and add the result
- MySQL query to sum up values of rows and sort the result?
- MySQL - SUM rows with same ID?
- How can we add columns with default values to an existing MySQL table?
- How to return rows that have the same column values in MySQL?
- How to update multiple rows and left pad values in MySQL?
- How to fetch random rows in MySQL with comma separated values?
- Fix a specific column value and display random values for rest of the rows in MySQL
- SET only two values for all the rows in a MySQL table based on conditions?
- MySQL query to compare and display only the rows with NULL values

Advertisements