程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> MYSQL數據庫 >> MySQL綜合教程 >> MySQL自增列拔出0值的處理計劃

MySQL自增列拔出0值的處理計劃

編輯:MySQL綜合教程

MySQL自增列拔出0值的處理計劃。本站提示廣大學習愛好者:(MySQL自增列拔出0值的處理計劃)文章只能為提供參考,不一定能成為您想要的結果。以下是MySQL自增列拔出0值的處理計劃正文


在將數據庫從MSSQL遷徙到MySQL的進程中,基於營業邏輯的請求,須要在MySQL的自增列拔出0值。在MSSQL中是如許完成的:

string sql;sql = " set identity_insert dbo.AppUsers on "
+ " insert dbo.AppUsers (Id, IsLocked, IsMustChangeLocalPassword, IsAvailable, Name, Sequence, CreatedBy, CreatedTime, UpdatedBy, UpdatedTime) "
+ " values (0, 1, 0, 0, '[SYSTEM]', 0, 0, GetDate(), 0, GetDate()) "
+ " set identity_insert dbo.AppUsers off "
+ " DBCC CHECKIDENT ('dbo.AppUsers', RESEED, 0) ";
db.Database.ExecuteSqlCommand(sql);

MySQL官方文檔中是如許寫的:

NO_AUTO_VALUE_ON_ZERO affects handling of AUTO_INCREMENT columns. Normally, you generate the next sequence number for the column by inserting either NULL or 0 into it. NO_AUTO_VALUE_ON_ZERO suppresses this behavior for 0 so that only NULL generates the next sequence number.
This mode can be useful if 0 has been stored in a table's AUTO_INCREMENT column. (Storing 0 is not a recommended practice, by the way.) For example, if you dump the table with mysqldump and then reload it, MySQL normally generates new sequence numbers when
it encounters the 0 values, resulting in a table with contents different from the one that was dumped. Enabling NO_AUTO_VALUE_ON_ZERO before reloading the dump file solves this problem. mysqldump now automatically includes in its output a statement that enables NO_AUTO_VALUE_ON_ZERO, to avoid this problem.

年夜致的意思是說:NO_AUTO_VALUE_ON_ZERO會影響自增列,普通情形下,取得下一個序列值的辦法是對自增列拔出0或許NULL值。NO_AUTO_VALUE_ON_ZERO會轉變這個缺省的行動,使得只要拔出NULL值能力獲得下一個序列值。這類方法關於要將0值拔出自增列是有效的。(趁便指出,0值是不推舉應用在自增列的)例如,假如你應用mysqldump備份數據表然後再恢復它,MySQL普通情況下會0值主動發生新的序列值,成果是形成從備份恢單數據毛病。在恢單數據前,啟用NO_AUTO_VALUE_ON_ZERO可以處理這個成績。mysqldump如今會主動在輸入的語句中包括NO_AUTO_VALUE_ON_ZERO來處理這個成績。
在MySQL中須要如許:

sql = " SET SESSION sql_mode='NO_AUTO_VALUE_ON_ZERO'; insert AppUsers (Id, IsLocked, IsMustChangeLocalPassword, IsAvailable, Name, Sequence, CreatedBy, CreatedTime, UpdatedBy, UpdatedTime) "
+ " values (0, 1, 0, 0, '[SYSTEM]', 0, 0, CURRENT_TIMESTAMP, 0, CURRENT_TIMESTAMP) ";

至此成績處理。
跋文:
因為營業邏輯須要,在自增列會存在0值,為了在Windows平台和Linux平台的MySQL之間復制數據,增長全局變量設置,在my.ini和my.cnf平分別添加NO_AUTO_VALUE_ON_ZERO設置到sql-mode行,例如:

//my.ini 該文件在Windows7或Windows2008操作體系中位於 C:\ProgramData\MySQL\MySQL Server 5.6 目次下(采取MSI裝置方法)# Set the SQL mode to strict
sql-mode="STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION,NO_AUTO_VALUE_ON_ZERO"

重啟MySQL辦事後,檢查sql-mode全局變量的方法:
SELECT @@GLOBAL.sql_mode;
>>>>> 版權沒有 >>>>> 迎接轉載 >>>>> 原文地址 >>>>> http://www.cnblogs.com/jlzhou >>>>> 雄鷹在雞窩裡長年夜,就會掉去飛行的本事,野狼在羊群裡生長,也會愛上羊而損失狼性。人生的奇妙就在於與人相處。生涯的美妙則在於送人玫瑰。和聰慧的人在一路,你才會加倍睿智。和優良的人在一路,你才會鶴立雞群。所以,你是誰其實不主要,主要的是,你和誰在一路。
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved