在向 MySQL 查询中插入新行时,我是否应当包含为 null 的列?
如果你在插入语句中未指定列列表,则可以使用以下语法:
insert into yourTableName values(NULL,yourValue,NULL,NULL,.....N);
我们首先创建一个表:
mysql> create table DemoTable1513 -> ( -> StudentId int, -> StudentName varchar(20) , -> StudentAge int, -> StudentCountryName varchar(20) -> ); Query OK, 0 rows affected (0.58 sec)
使用 insert 命令在表中插入一些记录:
mysql> insert into DemoTable1513 values(NULL,'Chris Brown',NULL,NULL); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable1513 values(101,NULL,NULL,NULL); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable1513 values(NULL,NULL,23,NULL); Query OK, 1 row affected (0.42 sec) mysql> insert into DemoTable1513 values(NULL,NULL,NULL,'US'); Query OK, 1 row affected (0.16 sec)
使用 select 语句显示表中的所有记录:
mysql> select * from DemoTable1513;
这将生成以下输出:
+-----------+-------------+------------+--------------------+ | StudentId | StudentName | StudentAge | StudentCountryName | +-----------+-------------+------------+--------------------+ | NULL | Chris Brown | NULL | NULL | | 101 | NULL | NULL | NULL | | NULL | NULL | 23 | NULL | | NULL | NULL | NULL | US | +-----------+-------------+------------+--------------------+ 4 rows in set (0.00 sec)
广告