mysql create table auto_increment


當插入記錄時,沒有為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的技巧

http://stackoverflow.com/questions/267658/having-both-a-created-and-last-updated-timestamp-columns-in-mysql-4-0

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了


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM