給主鍵增長一個自增的功能:
- mysql> alter table user modify id integer auto_increment ;
- Query OK, 1 row affected (0.28 sec)
- Records: 1 Duplicates: 0 Warnings: 0
這樣,上面的user表裏面的主鍵,id能夠自增了。
給上面的主鍵id增長默認值和自增功能。
- mysql> alter table user modify id integer auto_increment ;
- Query OK, 0 rows affected (0.39 sec)
- Records: 0 Duplicates: 0 Warnings: 0
-
- mysql> alter table user modify id integer default '1';
- Query OK, 0 rows affected (0.16 sec)
- Records: 0 Duplicates: 0 Warnings: 0
-
- mysql> alter table user modify id integer auto_increment ;
- Query OK, 1 row affected (0.28 sec)
- Records: 1 Duplicates: 0 Warnings: 0
MySql獲取系統時間:
- mysql> alter table user add createtime timestamp default current_timestamp;
- Query OK, 2 rows affected (0.17 sec)
- Records: 2 Duplicates: 0 Warnings: 0
MySql設置主鍵不能爲空,還要自動增加(這裏沒有設置默認值,可是默認是1,從1開始增加。),還要獲得系統默認日期:
- mysql> create table dd(
- -> id int primary key not null auto_increment,
- -> name varchar(20),
- -> time timestamp default current_timestamp
- -> );
- Query OK, 0 rows affected (0.10 sec)
-
- mysql> insert into dd(name) values ('fhihgifds');
- Query OK, 1 row affected (0.14 sec)
-
- mysql> insert into dd(name) values ('steven');
- Query OK, 1 row affected (0.08 sec)
-
- mysql> select * from dd;
- +----+-----------+---------------------+
- | id | name | time |
- +----+-----------+---------------------+
- | 1 | fhihgifds | 2011-03-27 01:58:46 |
- | 2 | steven | 2011-03-27 01:59:35 |
- +----+-----------+---------------------+
- 2 rows in set (0.08 sec)
-
- mysql> insert into dd(name) values ('anthony');
- Query OK, 1 row affected (0.09 sec)
-
- mysql> select * from dd;
- +----+-----------+---------------------+
- | id | name | time |
- +----+-----------+---------------------+
- | 1 | fhihgifds | 2011-03-27 01:58:46 |
- | 2 | steven | 2011-03-27 01:59:35 |
- | 3 | anthony | 2011-03-27 02:00:07 |
- +----+-----------+---------------------+
- 3 rows in set (0.00 sec)
-
- mysql>