給MySQL表格添加一個自動成長的主鍵列

來源:互聯網
上載者:User

曾經給某個Mysql表格添加主鍵時,用了以下SQL語句

  1. alter table `表格名` add column `列名` int not null auto_increment comment '主鍵' before `某一列名`;    

結果遇到如下兩個錯誤 :

  1. Error Code : 1064    
  2. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'before 列名' at line 1    
  3. (0 ms taken)    

這個錯誤是說before附近有語法錯誤

  1. Error Code : 1075    
  2. Incorrect table definition; there can be only one auto column and it must be defined as a key    
  3.  (0 ms taken)    

這個錯誤是說只有主鍵才能指定自動成長列

網上找了下原因,說是要先添加列,再修改列。我覺得這樣挺麻煩的。自己實驗了一下,一句話也能搞定:

  1. alter table `表格名` add column `列名` int not null auto_increment primary key comment '主鍵' first;    

上面語句表示給某個表格添加一個自動成長的主鍵欄位,並且將該欄位放在第一列。

如果你想放到其他列後面,可以將上面sql語句中的 first 換成  after `已經有的列名`

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.