如何在 MySQL 中将自己的值插入 auto_increment 列?
mysqlmysqli database
您可以借助 INSERT 语句来实现这一点,即您可以像普通插入一样简单地插入它。语法如下 −
INSERT INTO yourTableName (yourIdColumnName,yourColumnName) values(value1,'value2'); Let us first create a table: mysql> create table InsertValueInAutoIncrement -> ( -> UserId int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> UserName varchar(20) -> ); Query OK, 0 rows affected (0.59 sec)
现在,您可以使用 insert 命令在表中插入一些记录。在这里,我们还为 auto_increment 字段 UserId 插入了自己的值。查询如下 −
mysql> insert into InsertValueInAutoIncrement(UserName) values('John'); Query OK, 1 row affected (0.19 sec) mysql> insert into InsertValueInAutoIncrement(UserName) values('Carol'); Query OK, 1 row affected (0.17 sec) mysql> insert into InsertValueInAutoIncrement(UserName) values('Sam'); Query OK, 1 row affected (0.11 sec) mysql> insert into InsertValueInAutoIncrement(UserName) values('Bob'); Query OK, 1 row affected (0.18 sec) mysql> insert into InsertValueInAutoIncrement(UserId,UserName) values(100,'Maxwell'); Query OK, 1 row affected (0.18 sec) mysql> insert into InsertValueInAutoIncrement(UserName) values('James'); Query OK, 1 row affected (0.20 sec) mysql> insert into InsertValueInAutoIncrement(UserId,UserName) values(1000,'Larry'); Query OK, 1 row affected (0.12 sec)
使用 select 语句显示表中的所有记录。查询如下 −
mysql> select *from InsertValueInAutoIncrement;
下表为 −
+--------+----------+ | UserId | UserName | +--------+----------+ | 1 | John | | 2 | Carol | | 3 | Sam | | 4 | Bob | | 100 | Maxwell | | 101 | James | | 1000 | Larry | +--------+----------+ 7 rows in set (0.00 sec)