當插入記錄時,沒有為AUTO_INCREMENT明確指定值,則等同插入NULL值
把0插入AUTO_INCREMENT數據列的效果與插入NULL值一樣。但不建議這樣做,還是以插入NULL值為好。
如果把一個NULL插入到一個AUTO_INCREMENT數據列里去,MySQL將自動生成下一個序 列編號。默認編號從1開始(如果未指定auto_increment開始值,如果指定開始值,則從指定值開始, 比如AUTO_INCREMENT=4),並1為基數遞增。
mysql> create table ss(id int unsigned not null primary key auto_increment, user_name varchar(15) not null);
Query OK, 0 rows affected (0.00 sec)
mysql> insert into ss(id,user_name) values(1, 'jojo');
Query OK, 1 row affected (0.00 sec)
mysql> insert into ss(id,user_name) values(37, 'liz');
Query OK, 1 row affected (0.00 sec)
mysql> select id, user_name from ss;
+----+-----------+
| id | user_name |
+----+-----------+
| 1 | jojo |
| 37 | liz |
+----+-----------+
2 rows in set (0.00 sec)
===========================================
mysql> create table uu(id int unsigned not null primary key auto_increment, user_name varchar(15) not null) auto_increment=100;
Query OK, 0 rows affected (0.01 sec)
=================================
mysql> insert into uu(id,user_name) values(1, 'jojo');
Query OK, 1 row affected (0.00 sec)
mysql> insert into uu(id,user_name) values(37, 'liz');
Query OK, 1 row affected (0.00 sec)
mysql> select id, user_name from uu;
+----+-----------+
| id | user_name |
+----+-----------+
| 1 | jojo |
| 37 | liz |
+----+-----------+
2 rows in set (0.00 sec)
mysql> desc uu
-> ;
+-----------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| user_name | varchar(15) | NO | | NULL | |
+-----------+------------------+------+-----+---------+----------------+
2 rows in set (0.00 sec)
mysql>
當插入記錄時,沒有為AUTO_INCREMENT明確指定值,則等同插入NULL值
把0插入AUTO_INCREMENT數據列的效果與插入NULL值一樣。但不建議這樣做,還是以插入NULL值為好。
如果把一個NULL插入到一個AUTO_INCREMENT數據列里去,MySQL將自動生成下一個序 列編號。編號從1開始,並1為基數遞增。
因為上面指定了auto_increment=100,所以編號從100開始,遞增1。
mysql> insert into uu(user_name) values('Nicky');
Query OK, 1 row affected (0.00 sec)
mysql> select id, user_name from uu;
+-----+-----------+
| id | user_name |
+-----+-----------+
| 1 | jojo |
| 37 | liz |
| 100 | Nicky |
+-----+-----------+
3 rows in set (0.00 sec)
mysql> insert into uu(user_name) values('Lucy');
Query OK, 1 row affected (0.00 sec)
mysql> select id, user_name from uu;
+-----+-----------+
| id | user_name |
+-----+-----------+
| 1 | jojo |
| 37 | liz |
| 100 | Nicky |
| 101 | Lucy |
+-----+-----------+
4 rows in set (0.00 sec)
mysql>
mysql建表設置兩個默認CURRENT_TIMESTAMP的技巧
create table t (ids int not null auto_increment primary key, name1 varchar(20), t1 timestamp default '0000-00-00 00:00:00', t2 timestamp default now() on update now())
insert into t(name1) values ('tom')
insert into t(name1,t1, t2) values ('jerry', null, null)
注意: 第二次插入數據時,t1 插入null,數據庫中實際卻變成了now(), 不知道原因,但是如果修改成
t1 timestamp null,
就是null了