mysql> alter table user modify id integer auto_increment ;
query ok, 1 row affected (0.28 sec)
records: 1 duplicates: 0 warnings: 0
給主鍵增加乙個自增的功能:
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)
這樣,上面的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> 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>
MySql 主鍵自動增長
建立資料庫,建立表。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,f...
Mysql主鍵自動增長
mysql create database ssh2 query ok,1 row affected 0.04 sec mysql usessh2 database changed mysql create table user id integer primary key,firstname va...
Oracle主鍵自動增長
oracle主鍵自動增長 這幾天搞oracle,想讓表的主鍵實現自動增長,查網路實現如下 create table simon example id number 4 not null primary key,name varchar2 25 建立序列 create sequence create ...