MySQL自增列插入0值的解決方案
更新時(shí)間:2013年03月19日 16:40:12 作者:
基于業(yè)務(wù)邏輯的要求,需要在MySQL的自增列插入0值,針對(duì)此需求,本文給予詳細(xì)的解決方案,感興趣的你可以參考下哈,希望可以幫助到你
在將數(shù)據(jù)庫(kù)從MSSQL遷移到MySQL的過(guò)程中,基于業(yè)務(wù)邏輯的要求,需要在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.
大致的意思是說(shuō):NO_AUTO_VALUE_ON_ZERO會(huì)影響自增列,一般情況下,獲得下一個(gè)序列值的方法是對(duì)自增列插入0或者NULL值。NO_AUTO_VALUE_ON_ZERO會(huì)改變這個(gè)缺省的行為,使得只有插入NULL值才能獲取下一個(gè)序列值。這種方式對(duì)于要將0值插入自增列是有用的。(順便指出,0值是不推薦使用在自增列的)例如,如果你使用mysqldump備份數(shù)據(jù)表然后再恢復(fù)它,MySQL一般情形下會(huì)0值自動(dòng)產(chǎn)生新的序列值,結(jié)果是造成從備份恢復(fù)數(shù)據(jù)錯(cuò)誤。在恢復(fù)數(shù)據(jù)前,啟用NO_AUTO_VALUE_ON_ZERO可以解決這個(gè)問(wèn)題。mysqldump現(xiàn)在會(huì)自動(dòng)在輸出的語(yǔ)句中包含NO_AUTO_VALUE_ON_ZERO來(lái)解決這個(gè)問(wèn)題。
在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) ";
至此問(wèn)題解決。
后記:
由于業(yè)務(wù)邏輯需要,在自增列會(huì)存在0值,為了在Windows平臺(tái)和Linux平臺(tái)的MySQL之間復(fù)制數(shù)據(jù),增加全局變量設(shè)置,在my.ini和my.cnf中分別添加NO_AUTO_VALUE_ON_ZERO設(shè)置到sql-mode行,例如:
//my.ini 該文件在Windows7或Windows2008操作系統(tǒng)中位于 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服務(wù)后,查看sql-mode全局變量的辦法:
SELECT @@GLOBAL.sql_mode;
>>>>> 版權(quán)沒(méi)有 >>>>> 歡迎轉(zhuǎn)載 >>>>> 原文地址 >>>>> http://www.cnblogs.com/jlzhou >>>>> 雄鷹在雞窩里長(zhǎng)大,就會(huì)失去飛翔的本領(lǐng),野狼在羊群里成長(zhǎng),也會(huì)愛(ài)上羊而喪失狼性。人生的奧妙就在于與人相處。生活的美好則在于送人玫瑰。和聰明的人在一起,你才會(huì)更加睿智。和優(yōu)秀的人在一起,你才會(huì)出類拔萃。所以,你是誰(shuí)并不重要,重要的是,你和誰(shuí)在一起。
復(fù)制代碼 代碼如下:
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官方文檔中是這樣寫的:
復(fù)制代碼 代碼如下:
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.
大致的意思是說(shuō):NO_AUTO_VALUE_ON_ZERO會(huì)影響自增列,一般情況下,獲得下一個(gè)序列值的方法是對(duì)自增列插入0或者NULL值。NO_AUTO_VALUE_ON_ZERO會(huì)改變這個(gè)缺省的行為,使得只有插入NULL值才能獲取下一個(gè)序列值。這種方式對(duì)于要將0值插入自增列是有用的。(順便指出,0值是不推薦使用在自增列的)例如,如果你使用mysqldump備份數(shù)據(jù)表然后再恢復(fù)它,MySQL一般情形下會(huì)0值自動(dòng)產(chǎn)生新的序列值,結(jié)果是造成從備份恢復(fù)數(shù)據(jù)錯(cuò)誤。在恢復(fù)數(shù)據(jù)前,啟用NO_AUTO_VALUE_ON_ZERO可以解決這個(gè)問(wèn)題。mysqldump現(xiàn)在會(huì)自動(dòng)在輸出的語(yǔ)句中包含NO_AUTO_VALUE_ON_ZERO來(lái)解決這個(gè)問(wèn)題。
在MySQL中需要這樣:
復(fù)制代碼 代碼如下:
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) ";
至此問(wèn)題解決。
后記:
由于業(yè)務(wù)邏輯需要,在自增列會(huì)存在0值,為了在Windows平臺(tái)和Linux平臺(tái)的MySQL之間復(fù)制數(shù)據(jù),增加全局變量設(shè)置,在my.ini和my.cnf中分別添加NO_AUTO_VALUE_ON_ZERO設(shè)置到sql-mode行,例如:
復(fù)制代碼 代碼如下:
//my.ini 該文件在Windows7或Windows2008操作系統(tǒng)中位于 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服務(wù)后,查看sql-mode全局變量的辦法:
SELECT @@GLOBAL.sql_mode;
>>>>> 版權(quán)沒(méi)有 >>>>> 歡迎轉(zhuǎn)載 >>>>> 原文地址 >>>>> http://www.cnblogs.com/jlzhou >>>>> 雄鷹在雞窩里長(zhǎng)大,就會(huì)失去飛翔的本領(lǐng),野狼在羊群里成長(zhǎng),也會(huì)愛(ài)上羊而喪失狼性。人生的奧妙就在于與人相處。生活的美好則在于送人玫瑰。和聰明的人在一起,你才會(huì)更加睿智。和優(yōu)秀的人在一起,你才會(huì)出類拔萃。所以,你是誰(shuí)并不重要,重要的是,你和誰(shuí)在一起。
相關(guān)文章
mysql如何判斷同一字段是否有重復(fù)數(shù)據(jù)
這篇文章主要介紹了mysql如何判斷同一字段是否有重復(fù)數(shù)據(jù)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-05-05
Ubuntu 18.04下mysql 8.0 安裝配置方法圖文教程
這篇文章主要為大家詳細(xì)介紹了Ubuntu 18.04下mysql 8.0 安裝配置方法圖文教程,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05
MySQL連接無(wú)法解析HOST主機(jī)名的解決方法
這篇文章主要介紹了MySQL連接無(wú)法解析HOST主機(jī)名的解決方法,需要的朋友可以參考下2014-02-02
mysql數(shù)據(jù)庫(kù)連接失敗常見(jiàn)問(wèn)題小結(jié)
你有沒(méi)有碰到過(guò)mysql數(shù)據(jù)庫(kù)連接不上的問(wèn)題呢?很多的小伙伴表示,經(jīng)常會(huì)時(shí)不時(shí)的出現(xiàn)這些問(wèn)題,下面這篇文章主要給大家介紹了關(guān)于mysql數(shù)據(jù)庫(kù)連接失敗常見(jiàn)問(wèn)題的相關(guān)資料,需要的朋友可以參考下2023-06-06

