標籤:style http ar color os sp java on 資料
建立資料庫,建立表。
Sql代碼
- mysql> create database ssh2;
- Query OK, 1 row affected (0.04 sec)
-
- mysql> use ssh2;
- Database changed
- mysql> create table user(
- -> id integer primary key,
- -> firstname varchar(200) not null,
- -> lastname varchar(200) not null,
- -> age integer
- -> );
- Query OK, 0 rows affected (0.46 sec)
給主鍵增加一個自增的功能:
Java代碼
- 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增加預設值和自增功能。
Java代碼
- 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擷取系統時間:
Java代碼
- 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開始增長。),還要得到系統預設日期:
如果想要從一個表中建立相同資料結構的表:
create table newtbname select * from tab where 0;
這個新表和原表資料結構相同但是沒有設定主鍵自動成長
alter table modify id integer primary key auto_increment
這樣就保證了資料庫中主鍵id自動成長
mysql設定主鍵自動成長