auto-increment

MySQL table incrementing by 10 for some reason

末鹿安然 提交于 2019-12-19 19:43:37
问题 The id field in a mysql table is incrementing by 10 (11, 21, 31) for some reason. Here is the table definition: CREATE TABLE `clients` ( `id` int(11) NOT NULL auto_increment, `first_name` varchar(255) default NULL, `last_name` varchar(255) default NULL, ) ENGINE=InnoDB AUTO_INCREMENT=52 DEFAULT CHARSET=utf8; If I do a simple insert statement in SQL the next ID will be 41. 回答1: You have auto_increment_increment set to 10, change it back to 1. 来源: https://stackoverflow.com/questions/993800

Resetting AUTO_INCREMENT is taking a long time in MySQL

家住魔仙堡 提交于 2019-12-19 17:33:01
问题 ALTER TABLE tablename AUTO_INCREMENT = 10000000 This query is taking long time to update. Why? I need to optimize this query. 回答1: ALTER TABLE causes a rebuild of the entire table - if your table contains many rows,this can take ages. If you just need to bump up the value of the auto_increment value, the quickest way is to insert a dummy row (and then delete that roow if need be). This will only take a fraction of a second, whereas ALTER TABLE can take days for a large table. For example,

composite (alphanumeric) primary key and auto increment

巧了我就是萌 提交于 2019-12-19 11:45:32
问题 Is it possible to make an auto-increment function in MySQL that combines alpha elements and numeric? I have an existing system with keys like QAb99, QAb101, QAd9003, etc. The max numeric portion ranges from 1 to 9999, while the letters begin with QA and range from QAa to QAd, etc. and will eventually pass QAd9999 to QAe1, etc. Is it better to manage generating new keys in SQL or outside of SQL (i.e. php script)? thanks. 回答1: I asked this a while ago. Mysql doesn't do this unfortunately. I'd

Re-Indexing MySQL INT Primary Keys & Reset AUTO_INCREMENT

两盒软妹~` 提交于 2019-12-19 09:58:04
问题 So I have a MySQL database that I'm using with a PHP site. Over the course of weeks of development, I'm left with something like: Table: users id | name ----------- 12| Bob 34| Jen 155| John 154| Kyle Except this goes on for hundreds of records and the ids are in the thousands. I'm looking for a script I can run to re-name the keys to their lowest value (preserving their respective rows), and then reset the AUTO_INCREMENT to the next id The desired output would be: Table: users id | name ----

Auto increment a non-primary key field in Ruby on Rails

…衆ロ難τιáo~ 提交于 2019-12-19 05:45:14
问题 In a RoR migration, how do I auto increment a non-primary-key field? I'd like to do this in the db definition, and not in the model. 回答1: You need to execute an SQL statement. statement = "ALTER TABLE `users` CHANGE `id` `id` SMALLINT( 5 ) UNSIGNED NOT NULL AUTO_INCREMENT" ActiveRecord::Base.connection.execute(statement) you can entry manually in your migration Note this is just an example. The final SQL statement syntax depends on the database. 来源: https://stackoverflow.com/questions/3220473

How can I have two columns in SQL Server auto increment?

两盒软妹~` 提交于 2019-12-19 04:37:06
问题 I have two columns in a table of a SQL server DB that I would like to autoincrement when new fields are added. However, Managment Studio wont allow me to set two columns to IDENTITY. Is there some other way to do this? 回答1: You could make the second field a calculated field based on the first. Or make an INSERT trigger than programatically generates the value for the second. 回答2: If you wanted the 2nd column to basically be a mirror of the first: ALTER TABLE dbo.myTable ADD foo AS [rowid] GO

Primary key id reaching limit of bigint data type

女生的网名这么多〃 提交于 2019-12-18 13:27:43
问题 I have a table that is exposed to large inserts and deletes on a regular basis (and because of this there are large gaps in the number sequence of the primary id column). It had a primary id column of type 'int' that was changed to 'bigint'. Despite this change, the limit of this datatype will also inevitably be exceeded at some point in the future (current usage would indicate this to be the case within the next year or so). How do you handle this scenario? I'm wondering (shock horror)

UUID versus auto increment number for primary key

爷,独闯天下 提交于 2019-12-18 13:02:44
问题 Why should I choose UUID over an auto increment number for my entity's primary key? What are the pros and cons? 回答1: Andrey and Mjg both had good points, but I would add a related performance issue that is significant. With the decoupling of database and key generation also allows applications that have complex relationships between objects to create them all with the keys in place, so that bulk inserts are possible. In the case of auto-increment, all of the objects that own relationships (ie

What is the difference between “++” and “+= 1 ” operators?

我的梦境 提交于 2019-12-18 10:27:22
问题 In a loop in C++, I usually encounter situations to use ++ or +=1 , but I can't tell their difference. For instance, if I have an integer int num = 0; and then in a loop I do: num ++; or num += 1; they both increase the value of num , but what is their difference? I doubt num++ could work faster than num+=1 , but how? Is this difference subtle enough to be ignored? 回答1: num += 1 is rather equivalent to ++num . All those expressions ( num += 1 , num++ and ++num ) increment the value of num by

What is the difference between “++” and “+= 1 ” operators?

天涯浪子 提交于 2019-12-18 10:27:12
问题 In a loop in C++, I usually encounter situations to use ++ or +=1 , but I can't tell their difference. For instance, if I have an integer int num = 0; and then in a loop I do: num ++; or num += 1; they both increase the value of num , but what is their difference? I doubt num++ could work faster than num+=1 , but how? Is this difference subtle enough to be ignored? 回答1: num += 1 is rather equivalent to ++num . All those expressions ( num += 1 , num++ and ++num ) increment the value of num by