在 MySQL 中创建表时设置 AUTO_INCREMENT?
mysqlmysqli database
首先让我们创建一个表。我们在创建表时使用了 AUTO_INCREMENT 来为 StudentId 设置自动增量 −
mysql> create table DemoTable -> ( -> StudentId int NOT NULL AUTO_INCREMENT, -> StudentFirstName varchar(100), -> StudentLastName varchar(100), -> StudentAge int, -> StudentCountryName varchar(100), -> PRIMARY KEY(StudentId) -> )AUTO_INCREMENT=30; Query OK, 0 rows affected (0.69 sec)
使用 insert 命令在表中插入一些记录 −
mysql> insert into DemoTable(StudentFirstName,StudentLastName,StudentAge,StudentCountryName) values('John','Smith',21,'US'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable(StudentFirstName,StudentLastName,StudentAge,StudentCountryName) values('Chris','Brown',20,'AUS'); Query OK, 1 row affected (0.19 sec)
使用 select 语句显示表中的所有记录 −
mysql> select *from DemoTable;
输出
+-----------+------------------+-----------------+------------+--------------------+ | StudentId | StudentFirstName | StudentLastName | StudentAge | StudentCountryName | +-----------+------------------+-----------------+------------+--------------------+ | 30 | John | Smith | 21 | US | | 31 | Chris | Brown | 20 | AUS | +-----------+------------------+-----------------+------------+--------------------+ 2 rows in set (0.00 sec)