MySQL自增列插入0值的解決方案_MySQL教程
推薦:Mysql中的find_in_set的使用方法介紹Mysql中的find_in_set的使用方法介紹,需要的朋友可以參考一下
在將數(shù)據(jù)庫從MSSQL遷移到MySQL的過程中,基于業(yè)務邏輯的要求,需要在MySQL的自增列插入0值。在MSSQL中是這樣完成的:復制代碼 代碼如下:www.hl5o.cn
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官方文檔中是這樣寫的:
復制代碼 代碼如下:www.hl5o.cn
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備份數(shù)據(jù)表然后再恢復它,MySQL一般情形下會0值自動產(chǎn)生新的序列值,結果是造成從備份恢復數(shù)據(jù)錯誤。在恢復數(shù)據(jù)前,啟用NO_AUTO_VALUE_ON_ZERO可以解決這個問題。mysqldump現(xiàn)在會自動在輸出的語句中包含NO_AUTO_VALUE_ON_ZERO來解決這個問題。
在MySQL中需要這樣:
復制代碼 代碼如下:www.hl5o.cn
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) ";
至此問題解決。
后記:
由于業(yè)務邏輯需要,在自增列會存在0值,為了在Windows平臺和Linux平臺的MySQL之間復制數(shù)據(jù),增加全局變量設置,在my.ini和my.cnf中分別添加NO_AUTO_VALUE_ON_ZERO設置到sql-mode行,例如:
復制代碼 代碼如下:www.hl5o.cn
//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服務后,查看sql-mode全局變量的辦法:
SELECT @@GLOBAL.sql_mode;
>>>>> 版權沒有 >>>>> 歡迎轉(zhuǎn)載 >>>>> 原文地址 >>>>> http://www.cnblogs.com/jlzhou >>>>> 雄鷹在雞窩里長大,就會失去飛翔的本領,野狼在羊群里成長,也會愛上羊而喪失狼性。人生的奧妙就在于與人相處。生活的美好則在于送人玫瑰。和聰明的人在一起,你才會更加睿智。和優(yōu)秀的人在一起,你才會出類拔萃。所以,你是誰并不重要,重要的是,你和誰在一起。
分享:如何用cmd連接Mysql數(shù)據(jù)庫如何用cmd連接Mysql數(shù)據(jù)庫,需要的朋友可以參考一下
相關MySQL教程:
- MSSQL清空日志刪除日志文件
- 關于數(shù)據(jù)庫中保留小數(shù)位的問題
- 解析mysql與Oracle update的區(qū)別
- mysql 導入導出數(shù)據(jù)庫以及函數(shù)、存儲過程的介紹
- MySQL——修改root密碼的4種方法(以windows為例)
- 解決MYSQL出現(xiàn)Can''t create/write to file ''#sql_5c0_0.MYD''的問題
- 深入理解SQL的四種連接-左外連接、右外連接、內(nèi)連接、全連接
- 解析:內(nèi)聯(lián),左外聯(lián),右外聯(lián),全連接,交叉連接的區(qū)別
- mysql出現(xiàn)“Incorrect key file for table”處理方法
- mysql重裝后出現(xiàn)亂碼設置為utf8可解決
- 淺析一個MYSQL語法(在查詢中使用count)的兼容性問題
- 解析MySQL中INSERT INTO SELECT的使用
- 相關鏈接:
- 教程說明:
MySQL教程-MySQL自增列插入0值的解決方案
。