从 MySQL 表格中删除数据后,如何自动增量 1?
为此,可以使用 TRUNCATE TABLE 命令。我们先创建一个表 −
mysql> create table DemoTable1796 ( StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY, StudentName varchar(20) ); Query OK, 0 rows affected (0.00 sec)
使用 insert 命令向表中插入一些记录−
mysql> insert into DemoTable1796(StudentName) values('Chris Brown'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1796(StudentName) values('David Miller'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1796(StudentName) values('John Doe'); Query OK, 1 row affected (0.00 sec)
使用 select 语句显示表中的所有记录
mysql> select * from DemoTable1796;
这将生成以下输出
+-----------+--------------+ | StudentId | StudentName | +-----------+--------------+ | 1 | Chris Brown | | 2 | David Miller | | 3 | John Doe | +-----------+--------------+ 3 rows in set (0.00 sec)
这是从表中删除记录的查询−
mysql> delete from DemoTable1796; Query OK, 3 rows affected (0.00 sec)
现在向表中插入一条记录
mysql> insert into DemoTable1796(StudentName) values('John Doe'); Query OK, 1 row affected (0.00 sec) mysql> select * from DemoTable1796; +-----------+-------------+ | StudentId | StudentName | +-----------+-------------+ | 4 | John Doe | +-----------+-------------+ 1 row in set (0.00 sec)
现在可以使用 TRUNCATE TABLE 命令
mysql> truncate table DemoTable1796; Query OK, 0 rows affected (0.00 sec)
现在,让我们向表中插入一些记录
mysql> insert into DemoTable1796(StudentName) values('John Doe'); Query OK, 1 row affected (0.00 sec) mysql> select * from DemoTable1796; +-----------+-------------+ | StudentId | StudentName | +-----------+-------------+ | 1 | John Doe | +-----------+-------------+ 1 row in set (0.00 sec)
广告