在 MySQL 中移动行的值来更改现有行的现有 id 值?
mysqlmysqli database
首先我们创建一个表 −
mysql> create table DemoTable -> ( -> StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> StudentName varchar(20) -> ); Query OK, 0 rows affected (1.07 sec)
使用 insert 命令在表中插入一些记录 −
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)
使用 select 语句显示表中的所有记录 −
mysql> select *from DemoTable;
这将产生以下输出 −
+-----------+-------------+ | StudentId | StudentName | +-----------+-------------+ | 1 | Chris | | 2 | Robert | | 3 | David | | 4 | Mike | +-----------+-------------+ 4 rows in set (0.00 sec)
这是在 MySQL 中移动现有行的 id 值的查询−
mysql> update DemoTable set StudentId=StudentId+1000; Query OK, 4 rows affected (0.18 sec) Rows matched: 4 Changed: 4 Warnings: 0
让我们再次检查表记录 −
mysql> select *from DemoTable;
这将产生以下输出 −
+-----------+-------------+ | StudentId | StudentName | +-----------+-------------+ | 1001 | Chris | | 1002 | Robert | | 1003 | David | | 1004 | Mike | +-----------+-------------+ 4 rows in set (0.00 sec)