mysql設定主鍵自動成長

來源:互聯網
上載者:User

標籤:style   http   ar   color   os   sp   java   on   資料   

建立資料庫,建立表。 

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開始增長。),還要得到系統預設日期: 


如果想要從一個表中建立相同資料結構的表:

create table newtbname select * from tab where 0;

這個新表和原表資料結構相同但是沒有設定主鍵自動成長

alter table modify id integer primary key auto_increment

這樣就保證了資料庫中主鍵id自動成長




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.