传递 NULL 给 MySQL 以自动递增?
是的,我们可以按照下面的语法传递 NULL −
insert into yourTableName values(NULL,yourValue1,yourValue2,...N);
让我们首先创建一个表格 -
mysql> create table DemoTable1503 -> ( -> ClientId int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> ClientName varchar(20), -> ClientAge int -> ); Query OK, 0 rows affected (0.45 sec)
使用 insert 命令在表中插入一些记录。由于我们已经设置了 NON NULL,它不会影响 auto_increment −
mysql> insert into DemoTable1503 values(NULL,'Chris',25); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable1503 values(NULL,'David',28); Query OK, 1 row affected (0.37 sec) mysql> insert into DemoTable1503 values(NULL,'Bob',45); Query OK, 1 row affected (0.09 sec) mysql> insert into DemoTable1503 values(NULL,'Mike',57); Query OK, 1 row affected (0.15 sec)
使用 select 语句在表中显示所有记录 −
mysql> select * from DemoTable1503;
这将生成下面的输出。自动递增以相同的方式打印(非空) -
+----------+------------+-----------+ | ClientId | ClientName | ClientAge | +----------+------------+-----------+ | 1 | Chris | 25 | | 2 | David | 28 | | 3 | Bob | 45 | | 4 | Mike | 57 | +----------+------------+-----------+ 4 rows in set (0.00 sec)
广告