mySQL 中主索引值自動增加

來源:互聯網
上載者:User

標籤:

轉  http://stevenjohn.iteye.com/blog/976397 MySql 主鍵自動成長部落格分類:
     
  • DataBase
MySQLSQL 建立資料庫,建立表。 

Sql代碼  
  1. mysql> create database ssh2;  
  2. Query OK, 1 row affected (0.04 sec)  
  3.   
  4. mysql> use ssh2;  
  5. Database changed  
  6. mysql> create table user(  
  7.     -> id integer primary key,  
  8.     -> firstname varchar(200) not null,  
  9.     -> lastname varchar(200) not null,  
  10.     -> age integer  
  11.     -> );  
  12. Query OK, 0 rows affected (0.46 sec)  

給主鍵增加一個自增的功能: 
Java代碼  
  1. mysql> alter table user modify id integer auto_increment ;  
  2. Query OK, 1 row affected (0.28 sec)  
  3. Records: 1  Duplicates: 0  Warnings: 0  



這樣,上面的user表裡面的主鍵,id可以自增了。 









給上面的主鍵id增加預設值和自增功能。 
Java代碼  
  1. mysql> alter table user modify id integer auto_increment ;  
  2. Query OK, 0 rows affected (0.39 sec)  
  3. Records: 0  Duplicates: 0  Warnings: 0  
  4.   
  5. mysql> alter table user modify id integer default ‘1‘;  
  6. Query OK, 0 rows affected (0.16 sec)  
  7. Records: 0  Duplicates: 0  Warnings: 0  
  8.   
  9. mysql> alter table user modify id integer auto_increment ;  
  10. Query OK, 1 row affected (0.28 sec)  
  11. Records: 1  Duplicates: 0  Warnings: 0  






MySql擷取系統時間: 
Java代碼  
  1. mysql> alter table user add createtime timestamp default current_timestamp;  
  2. Query OK, 2 rows affected (0.17 sec)  
  3. Records: 2  Duplicates: 0  Warnings: 0  












MySql設定主鍵不可為空,還要自動成長(這裡沒有設定預設值,但是預設是1,從1開始增長。),還要得到系統預設日期: 
Java代碼  
  1. mysql> create table dd(  
  2.     -> id int primary key not null auto_increment,  
  3.     -> name varchar(20),  
  4.     -> time timestamp default current_timestamp  
  5.     -> );  
  6. Query OK, 0 rows affected (0.10 sec)  
  7.   
  8. mysql> insert into dd(name) values (‘fhihgifds‘);  
  9. Query OK, 1 row affected (0.14 sec)  
  10.   
  11. mysql> insert into dd(name) values (‘steven‘);  
  12. Query OK, 1 row affected (0.08 sec)  
  13.   
  14. mysql> select * from dd;  
  15. +----+-----------+---------------------+  
  16. | id | name      | time                |  
  17. +----+-----------+---------------------+  
  18. |  1 | fhihgifds | 2011-03-27 01:58:46 |  
  19. |  2 | steven    | 2011-03-27 01:59:35 |  
  20. +----+-----------+---------------------+  
  21. 2 rows in set (0.08 sec)  
  22.   
  23. mysql> insert into dd(name) values (‘anthony‘);  
  24. Query OK, 1 row affected (0.09 sec)  
  25.   
  26. mysql> select * from dd;  
  27. +----+-----------+---------------------+  
  28. | id | name      | time                |  
  29. +----+-----------+---------------------+  
  30. |  1 | fhihgifds | 2011-03-27 01:58:46 |  
  31. |  2 | steven    | 2011-03-27 01:59:35 |  
  32. |  3 | anthony   | 2011-03-27 02:00:07 |  
  33. +----+-----------+---------------------+  
  34. 3 rows in set (0.00 sec)  
  35.   
  36. mysql>  

mySQL 中主索引值自動增加

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.