2009-04-23 112 views
2

我想自動爲CREATE_TABLE軌遞增ID列命令開始如何設置值初始AUTO_INCREMENT ID在SQL在500如何在create_table上設置初始ID值?

我發現的例子:

CREATE TABLE student (id int(2) NOT NULL auto_increment, name varchar(50) NOT NULL default '', class varchar(10) NOT NULL default '', mark int(3) NOT NULL default '0', sex varchar(6) NOT NULL default 'male', UNIQUE KEY id (id)) auto_increment=100000 TYPE=MyISAM; 

所以我看了看Rails的API,並看到了CREATE_TABLE方法這些選項:

The options hash can include the following keys: 

:id 
    Whether to automatically add a primary key column. Defaults to true. Join tables for has_and_belongs_to_many should set :id => false. 
:primary_key 
    The name of the primary key, if one is to be added automatically. Defaults to id. 
:options 
    Any extra options you want appended to the table definition. 
:temporary 
    Make a temporary table. 
:force 
    Set to true to drop the table before creating it. Defaults to false. 

不是我所需要......我想這沒有成功:

:options => {:auto_increment => 500} 

任何人都知道如何獲得自增長ID列對於這一說法在500開始:

create_table :contents do |t| 
    t.string :type,    :null => false 
    t.string :name,    :null => false 
end 

回答

4

試試這個:

create_table(:student, :options => 'AUTO_INCREMENT = 500') do |t| 
    t.column :name, :string, :limit => 50 
    # Other fields here 
end 
+0

這做到了!謝謝! – Matthew 2009-04-24 17:26:46

0

這只是一種猜測,但你嘗試過在表中插入(然後刪除)一行ID爲499?

2

的SQL語句來設置,這將是:

ALTER TABLE student AUTO_INCREMENT = 500; 
2

有許多方法可以做到這一點,某些特定於RDBMS(你沒有指定你使用的是什麼RBDMS,但MyISAM表表示MySQL)。

但它有一種難聞的氣味;除了獨特和單調增加之外,你不應該關心合成關鍵字的任何內容。你的關心表明可能存在更大的問題。