本文章是由一個(gè)簡(jiǎn)單的Mysql自增初始值的一個(gè)例子引導(dǎo)出大量的關(guān)于mysql主鍵獲取 修改及一些常用的例子,下面我們一起來(lái)看看希望例子對(duì)各位會(huì)有幫助.
alter table table_name auto_increment=n;
注意n只能大于已有的auto_increment的整數(shù)值,小于的值無(wú)效.
show table status like 'table_name' 可以看到auto_increment這一列是表現(xiàn)有的值,步進(jìn)值沒(méi)法改變,只能通過(guò)下面提到last_inset_id()函數(shù)變通使用.
Mysql可以使用AUTO_INCREMENT來(lái)設(shè)定主鍵的值為自增長(zhǎng)的,其默認(rèn)值是1,如果想把它的初始值設(shè)置為1000,比較笨的辦法是先插入一條記錄并指定主鍵的值為999,然后delete改行記錄,代碼如下:
insert into test(pk) values(999);delete from test where pk = 999;
更好的方法是使用alter的方法來(lái)直接修改.例如:
alter table test AUTO_INCREMENT = 1000;
1、不控制主鍵的起點(diǎn),代碼如下:
- 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、控制主鍵的起點(diǎn),代碼如下:
- 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;
自增主鍵歸零
方法一:如果曾經(jīng)的數(shù)據(jù)都不需要的話,可以直接清空所有數(shù)據(jù),并將自增字段恢復(fù)從1開(kāi)始計(jì)數(shù).truncate table 表名
方法二:
dbcc checkident (’table_name’, reseed, new_reseed_value) 當(dāng)前值設(shè)置為 new_reseed_value。如果自創(chuàng)建表后沒(méi)有將行插入該表,則在執(zhí)行 DBCC CHECKIDENT 后插入的第一行將使用 new_reseed_value 作為標(biāo)識(shí)。否則,下一個(gè)插入的行將使用 new_reseed_value + 1。如果 new_reseed_value 的值小于標(biāo)識(shí)列中的最大值,以后引用該表時(shí)將產(chǎn)生 2627 號(hào)錯(cuò)誤信息.
方法二不會(huì)清空已有數(shù)據(jù),操作比較靈活,不僅可以將自增值歸零,也適用于刪除大量連續(xù)行后,重新設(shè)置自增值并插入新的數(shù)據(jù);或從新的值開(kāi)始,當(dāng)然不能和已有的沖突,代碼如下:
- $sql="delete from $table_vote";
- mysql_query($sql, $link);
- $sql="alter table $table_vote auto_increment=1";
- mysql_query($sql, $link);
獲取自增主鍵【4種方法】
通常我們?cè)趹?yīng)用中對(duì)mysql執(zhí)行了insert操作后,需要獲取插入記錄的自增主鍵。本文將介紹java環(huán)境下的4種方法獲取insert后的記錄主鍵auto_increment的值:
通過(guò)JDBC2.0提供的insertRow()方式
通過(guò)JDBC3.0提供的getGeneratedKeys()方式
通過(guò)SQL select LAST_INSERT_ID()函數(shù)
通過(guò)SQL @@IDENTITY 變量
1. 通過(guò)JDBC2.0提供的insertRow()方式,自jdbc2.0以來(lái),可以通過(guò)下面的方式執(zhí)行,代碼如下:
- Statement stmt = null;
- ResultSet rs = null;
- try {
- stmt = conn.createStatement(java.sql.ResultSet.TYPE_FORWARD_ONLY, // 創(chuàng)建Statement
- java.sql.ResultSet.CONCUR_UPDATABLE);
- stmt.executeUpdate("DROP TABLE IF EXISTS autoIncTutorial");
- stmt.executeUpdate( // 創(chuàng)建demo表
- "CREATE TABLE autoIncTutorial ("
- + "priKey INT NOT NULL AUTO_INCREMENT, "
- + "dataField VARCHAR(64), PRIMARY KEY (priKey))");
- rs = stmt.executeQuery("SELECT priKey, dataField " // 檢索數(shù)據(jù)
- + "FROM autoIncTutorial");
- rs.moveToInsertRow(); // 移動(dòng)游標(biāo)到待插入行(未創(chuàng)建的偽記錄)
- rs.updateString("dataField", "AUTO INCREMENT here?"); // 修改內(nèi)容
- rs.insertRow(); // 插入記錄
- rs.last(); // 移動(dòng)游標(biāo)到最后一行
- int autoIncKeyFromRS = rs.getInt("priKey"); // 獲取剛插入記錄的主鍵preKey
- rs.close();
- rs = null;
- System.out.println("Key returned for inserted row: "
- + autoIncKeyFromRS);
- } finally {
- // rs,stmt的close()清理
- } //開(kāi)源代碼Vevb.com
2. 通過(guò)JDBC3.0提供的getGeneratedKeys()方式,代碼如下:
- Statement stmt = null;
- ResultSet rs = null;
- try {
- stmt = conn.createStatement(java.sql.ResultSet.TYPE_FORWARD_ONLY,
- java.sql.ResultSet.CONCUR_UPDATABLE);
- // ...
- // 省略若干行(如上例般創(chuàng)建demo表)
- // ... m.survivalescaperooms.com
- stmt.executeUpdate(
- "INSERT INTO autoIncTutorial (dataField) "
- + "values ('Can I Get the Auto Increment Field?')",
- Statement.RETURN_GENERATED_KEYS); // 向驅(qū)動(dòng)指明需要自動(dòng)獲取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時(shí),應(yīng)注意以下幾點(diǎn):
AUTO_INCREMENT是數(shù)據(jù)列的一種屬性,只適用于整數(shù)類型數(shù)據(jù)列.
設(shè)置AUTO_INCREMENT屬性的數(shù)據(jù)列應(yīng)該是一個(gè)正數(shù)序列,所以應(yīng)該把該數(shù)據(jù)列聲明為UNSIGNED,這樣序列的編號(hào)個(gè)可增加一倍.
AUTO_INCREMENT數(shù)據(jù)列必須有唯一索引,以避免序號(hào)重復(fù).
AUTO_INCREMENT數(shù)據(jù)列必須具備NOT NULL屬性.
AUTO_INCREMENT數(shù)據(jù)列序號(hào)的最大值受該列的數(shù)據(jù)類型約束,如TINYINT數(shù)據(jù)列的最大編號(hào)是127,如加上UNSIGNED,則最大為255,一旦達(dá)到上限,AUTO_INCREMENT就會(huì)失效.
新聞熱點(diǎn)
疑難解答
圖片精選