auto-increment

Prevent InnoDB auto increment ON DUPLICATE KEY

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-26 16:45:46
I am currently having problems with a primary key ID which is set to auto increment . It keeps incrementing ON DUPLICATE KEY . For Example: ID | field1 | field2 1 | user | value 5 | secondUser | value 86 | thirdUser | value From the description above, you'll notice that I have 3 inputs in that table but due to auto increment on each update, ID has 86 for the third input. Is there anyway to avoid this ? Here's what my mySQL query looks like: INSERT INTO table ( field1, field2 ) VALUES (:value1, :value2) ON DUPLICATE KEY UPDATE field1 = :value1, field2 = :value2 And here's what my table looks

Can you access the auto increment value in MySQL within one statement?

耗尽温柔 提交于 2019-11-26 16:37:39
I have a MySQL database which contains a table of users. The primary key of the table is 'userid', which is set to be an auto increment field. What I'd like to do is when I insert a new user into the table is to use the same value that the auto increment is creating in the 'userid' field in a different field, 'default_assignment'. e.g. I'd like a statement like this: INSERT INTO users ('username','default_assignment') VALUES ('barry', value_of_auto_increment_field()) so I create user 'Barry', the 'userid' is generated as being 16 (for example), but I also want the 'default_assignment' to have

mysql Failed to read auto-increment value from storage engine

拥有回忆 提交于 2019-11-26 16:33:15
问题 I am having mysql table with one id field as auto-increment . When I insert values to the table am getting error as 1467 - Failed to read auto-increment value from storage engine Also the show table status shows me that the field with auto increment has 18446744073709551615 as Auto_increment value. What would be the issue can any one help me ....? 回答1: Problem could absolutely be that: convert 18446744073709551615 to hex and you'll find $FFFF-FFFF-FFFF-FFFF . If your field is an unsigned

Create unique autoincrement field with mongoose

亡梦爱人 提交于 2019-11-26 16:32:51
问题 Given a Schema: var EventSchema = new Schema({ id: { // ... }, name: { type: String }, }); I want to make id unique and autoincrement. I try to realize mongodb implementation but have problems of understanding how to do it right in mongoose. My question is : what is the right way to implement autoincrement field in mongoose without using any plugins and so on? 回答1: Here is a good example of auto-incremented fields implementation using Mongoose: var CounterSchema = Schema({ _id: {type: String,

“Auto increment” alphabet in Java?

醉酒当歌 提交于 2019-11-26 15:45:05
问题 "Auto increment" alphabet in Java - is this possible? From A to Z without a third-party library? 回答1: Yes, you can do it like this: for (char alphabet = 'A'; alphabet <= 'Z'; alphabet++) { System.out.println(alphabet); } It is also possible with typecasting: for (int i = 65; i <= 90; i++) { System.out.println((char)i); } 回答2: Yes, like this: for (int i = 0; i < 26; i++) { char upper = (char) ('A' + i); char lower = (char) ('a' + i); ... } 回答3: for (char c = 'A'; c <= 'Z'; c++) { ... } 回答4:

How to force MySQL to take 0 as a valid auto-increment value

拥有回忆 提交于 2019-11-26 15:36:46
问题 Long story short, I have a SQL file that I want to import as a skel style file, so this will be done repeatedly, programmatically. I can edit the SQL file however I want, but I'd rather not touch the application itself. This application uses userid = 0 to represent the anonymous user. It also has a relevant (blank) entry in the database to represent this 'user'. Hence, the line in my skel.sql looks something like this: INSERT INTO `{{TABLE_PREFIX}}users` VALUES (0, '', '', '', 0, 0, 0, '', ''

How to convert primary key from integer to serial?

别来无恙 提交于 2019-11-26 15:33:30
In a Postgres 9.3 table I have an integer as primary key with automatic sequence to increment, but I have reached the maximum for integer . How to convert it from integer to serial ? I tried: ALTER TABLE my_table ALTER COLUMN id SET DATA TYPE bigint; But the same does not work with the data type serial instead of bigint . Seems like I cannot convert to serial ? Erwin Brandstetter serial is a pseudo data type, not an actual data type. It's an integer underneath with some additional DDL commands executed automatically: Create a sequence (with matching name by default). Set the column NOT NULL

Custom SERIAL / autoincrement per group of values

試著忘記壹切 提交于 2019-11-26 15:29:49
I'm trying to make a blog system of sort and I ran into a slight problem. Simply put, there's 3 columns in my article table: id SERIAL, category VARCHAR FK, category_id INT id column is obviously the PK and it is used as a global identifier for all articles. category column is well .. category. category_id is used as a UNIQUE ID within a category so currently there is a UNIQUE(category, category_id) constraint in place. However, I also want for category_id to auto-increment . I want it so that every time I execute a query like INSERT INTO article(category) VALUES ('stackoverflow'); I want the

How to retrieve the last autoincremented ID from a SQLite table?

删除回忆录丶 提交于 2019-11-26 15:07:30
I have a table Messages with columns ID (primary key, autoincrement) and Content (text). I have a table Users with columns username (primary key, text) and Hash. A message is sent by one Sender (user) to many recipients (user) and a recipient (user) can have many messages. I created a table Messages_Recipients with two columns: MessageID (referring to the ID column of the Messages table and Recipient (referring to the username column in the Users table). This table represents the many to many relation between recipients and messages. So, the question I have is this. The ID of a new message

How to handle fragmentation of auto increament ID column in MySQL

牧云@^-^@ 提交于 2019-11-26 14:43:55
I have a table with an auto_increment field and sometimes rows get deleted so auto_increment leaves gaps. Is there any way to avoid this or if not, at the very least, how to write an SQL query that: Alters the auto_increment value to be the max(current value) + 1 Return the new auto_increment value? I know how to write part 1 and 2 but can I put them in the same query? If that is not possible: how do I "select" (return) the auto_increment value or auto_increment value + 1? Renumbering will cause confusion. Existing reports will refer to record 99, and yet if the system renumbers it may