alter table table_name auto_increment=n;
注意n只能大於已有的auto_increment的整數值,小於的值無效.
show table status like 'table_name' 可以看到auto_increment這一列是表現有的值.
步進值沒法改變.只能通過下面提到last_inset_id()函數變通使用
Mysql可以使用AUTO_INCREMENT來設定主鍵的值為自增長的,其預設值是1,如果想把它的初始值設定為1000,比較笨的辦法是先插入一條記錄並指定主鍵的值為999,然後delete改行記錄,例如:
| 代碼如下 |
複製代碼 |
insert into test(pk) values(999); delete from test where pk = 999; |
更好的方法是使用alter的方法來直接修改,例如:
| 代碼如下 |
複製代碼 |
alter table test AUTO_INCREMENT = 1000; |
例子
1、不控制主鍵的起點
| 代碼如下 |
複製代碼 |
create table emb_t_dictBusType ( emb_c_busTypeID int not null auto_increment, emb_c_busTypeEnName varchar(255) not null, emb_c_busTypeZhName varchar(255) not null, primary key(emb_c_busTypeID) )engine=INNODB default charset=gbk; |
2、控制主鍵的起點
| 代碼如下 |
複製代碼 |
create table emb_t_dictBusType ( emb_c_busTypeID int not null auto_increment, emb_c_busTypeEnName varchar(255) not null, emb_c_busTypeZhName varchar(255) not null, primary key(emb_c_busTypeID) )engine=INNODB auto_increment=1001 default charset=gbk; |
自增主鍵歸零
方法一:
如果曾經的資料都不需要的話,可以直接清空所有資料,並將自增欄位恢複從1開始計數
truncate table 表名
方法二:
dbcc checkident (’table_name’, reseed, new_reseed_value) 當前值設定為 new_reseed_value。如果自建立表後沒有將行插入該表,則在執行 DBCC CHECKIDENT 後插入的第一行將使用 new_reseed_value 作為標識。否則,下一個插入的行將使用 new_reseed_value + 1。如果 new_reseed_value 的值小於識別欄位中的最大值,以後引用該表時將產生 2627 號錯誤資訊。 www.111cn.net
方法二不會清空已有資料,操作比較靈活,不僅可以將自增值歸零,也適用於刪除大量連續行後,重新設定自增值並插入新的資料;或從新的值開始,當然不能和已有的衝突。
| 代碼如下 |
複製代碼 |
$sql="delete from $table_vote"; mysql_query($sql, $link); $sql="alter table $table_vote auto_increment=1"; mysql_query($sql, $link); |
擷取自增主鍵【4種方法】
通常我們在應用中對mysql執行了insert操作後,需要擷取插入記錄的自增主鍵。本文將介紹java環境下的4種方法擷取insert後的記錄主鍵auto_increment的值:
通過JDBC2.0提供的insertRow()方式
通過JDBC3.0提供的getGeneratedKeys()方式
通過SQL select LAST_INSERT_ID()函數
通過SQL @@IDENTITY 變數
1. 通過JDBC2.0提供的insertRow()方式
自jdbc2.0以來,可以通過下面的方式執行。
| 代碼如下 |
複製代碼 |
Statement stmt = null; ResultSet rs = null; try { stmt = conn.createStatement(java.sql.ResultSet.TYPE_FORWARD_ONLY, // 建立Statement java.sql.ResultSet.CONCUR_UPDATABLE); stmt.executeUpdate("DROP TABLE IF EXISTS autoIncTutorial"); stmt.executeUpdate( // 建立demo表 "CREATE TABLE autoIncTutorial (" + "priKey INT NOT NULL AUTO_INCREMENT, " + "dataField VARCHAR(64), PRIMARY KEY (priKey))"); rs = stmt.executeQuery("SELECT priKey, dataField " // 檢索資料 + "FROM autoIncTutorial"); rs.moveToInsertRow(); // 移動遊標到待插入行(未建立的偽記錄) rs.updateString("dataField", "AUTO INCREMENT here?"); // 修改內容 rs.insertRow(); // 插入記錄 rs.last(); // 移動遊標到最後一行 int autoIncKeyFromRS = rs.getInt("priKey"); // 擷取剛插入記錄的主鍵preKey rs.close(); rs = null; System.out.println("Key returned for inserted row: " + autoIncKeyFromRS); } finally { // rs,stmt的close()清理 } |
2. 通過JDBC3.0提供的getGeneratedKeys()方式
| 代碼如下 |
複製代碼 |
Statement stmt = null; ResultSet rs = null; try { stmt = conn.createStatement(java.sql.ResultSet.TYPE_FORWARD_ONLY, java.sql.ResultSet.CONCUR_UPDATABLE); // ... // 省略若干行(如上例般建立demo表) // ... www.111cn.net stmt.executeUpdate( "INSERT INTO autoIncTutorial (dataField) " + "values ('Can I Get the Auto Increment Field?')", Statement.RETURN_GENERATED_KEYS); // 向驅動指明需要自動擷取generatedKeys! int autoIncKeyFromApi = -1; rs = stmt.getGeneratedKeys(); // 擷取自增主鍵! if (rs.next()) { autoIncKeyFromApi = rs.getInt(1); } else { // throw an exception from here } rs.close(); rs = null; System.out.println("Key returned from getGeneratedKeys():" + autoIncKeyFromApi); } finally { ... } |
使用AUTO_INCREMENT時,應注意以下幾點:
AUTO_INCREMENT是資料列的一種屬性,只適用於整數類型資料列。
設定AUTO_INCREMENT屬性的資料列應該是一個正數序列,所以應該把該資料列聲明為UNSIGNED,這樣序列的編號個可增加一倍。
AUTO_INCREMENT資料列必須有唯一索引,以避免序號重複。
AUTO_INCREMENT資料列必須具備NOT NULL屬性。
AUTO_INCREMENT資料列序號的最大值受該列的資料類型約束,如TINYINT資料列的最大編號是127,如加上UNSIGNED,則最大為255。一旦達到上限,AUTO_INCREMENT就會失效。