MySQL auto-increment column inserts 0 values

Source: Internet
Author: User

MySQL auto-increment column inserts 0 values. when migrating a database from MSSQL to MySQL, You need to insert 0 values in the auto-increment column of MySQL based on the requirements of the business logic. This is done in MSSQL: www.2cto.com 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, 0, '[SYSTEM]', 0, 0, GetDate (), 0, GetDate () "+" set identity_insert dbo. appUsers off "+" dbcc checkident ('dbo. appUsers ', RESEED, 0) "; db. data Base. executeSqlCommand (SQL); this is written in the MySQL official document: 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 whenit 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 between des I N its output a statement that enables NO_AUTO_VALUE_ON_ZERO, to avoid this problem. generally, NO_AUTO_VALUE_ON_ZERO will affect the auto-incrementing column. Generally, the method to obtain the next sequence value is to insert 0 or NULL values to the auto-incrementing column. NO_AUTO_VALUE_ON_ZERO will change this default behavior so that only NULL values can be inserted to obtain the next sequence value. This method is useful for inserting 0 values into the auto-incrementing column. (By The Way, 0 values are not recommended for auto-incrementing columns.) For example, if you use mysqldump to back up the data table and restore it, in general, MySQL automatically generates a new sequence value with a value of 0, resulting in an error in data recovery from backup. Enable NO_AUTO_VALUE_ON_ZERO Before restoring data. Mysqldump will now automatically include NO_AUTO_VALUE_ON_ZERO in the output statement to solve this problem. In MySQL, You Need To: 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) "; the problem is resolved.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.