auto-increment

How to handle fragmentation of auto increament ID column in MySQL

情到浓时终转凉″ 提交于 2019-11-26 05:56:42
问题 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? 回答1: Renumbering

Auto increment in MongoDB to store sequence of Unique User ID

纵然是瞬间 提交于 2019-11-26 05:36:18
问题 I am making a analytics system, the API call would provide a Unique User ID, but it\'s not in sequence and too sparse. I need to give each Unique User ID an auto increment id to mark a analytics datapoint in a bitarray/bitset. So the first user encounters would corresponding to the first bit of the bitarray, second user would be the second bit in the bitarray, etc. So is there a solid and fast way to generate incremental Unique User IDs in MongoDB? 回答1: You can, but you should not https://web

Prevent InnoDB auto increment ON DUPLICATE KEY

久未见 提交于 2019-11-26 04:55:48
问题 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 (

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

旧巷老猫 提交于 2019-11-26 04:52:00
问题 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

MySQL AUTO_INCREMENT does not ROLLBACK

三世轮回 提交于 2019-11-26 04:27:57
I'm using MySQL's AUTO_INCREMENT field and InnoDB to support transactions. I noticed when I rollback the transaction, the AUTO_INCREMENT field is not rollbacked? I found out that it was designed this way but are there any workarounds to this? Let me point out something very important: You should never depend on the numeric features of autogenerated keys. That is, other than comparing them for equality (=) or unequality (<>), you should not do anything else. No relational operators (<, >), no sorting by indexes, etc. If you need to sort by "date added", have a "date added" column. Treat them as

SQL: Creating a relation table with 2 different auto_increment

一曲冷凌霜 提交于 2019-11-26 03:58:37
问题 I have 2 tables, both with their own auto incremented IDs, which are of course primary keys. When I want to create a 3rd table to establish the relation between these 2 tables, I always have an error. First one is about that you can have only 1 automatically-incremented column, the second one occurs when I delete the auto_increment statement from those 2, therefore sql doesn\'t allow me to make them foreign keys, because of the type matching failure. Is there a way that I can create a

How to convert primary key from integer to serial?

笑着哭i 提交于 2019-11-26 03:45:38
问题 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 ? 回答1: serial is a pseudo data type, not an actual data type. It's an integer underneath with some additional DDL commands executed

Custom SERIAL / autoincrement per group of values

时光怂恿深爱的人放手 提交于 2019-11-26 03:44:05
问题 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

PostgreSQL Autoincrement

老子叫甜甜 提交于 2019-11-26 03:17:55
问题 I\'m switching from MySQL to PostgreSQL and was wondering how I can do autoincrement values. I saw in the PostgreSQL docs a datatype \"serial\", but I get syntax errors when using it (in v8.0). 回答1: Yes, SERIAL is the equivalent function. CREATE TABLE foo ( id SERIAL, bar varchar); INSERT INTO foo (bar) values ('blah'); INSERT INTO foo (bar) values ('blah'); SELECT * FROM foo; 1,blah 2,blah SERIAL is just a create table time macro around sequences. You can not alter SERIAL onto an existing

Mongoose auto increment

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-26 02:56:33
问题 According to this mongodb article it is possible to auto increment a field and I would like the use the counters collection way. The problem with that example is that I don\'t have thousands of people typing the data in the database using the mongo console. Instead I am trying to use mongoose. So my schema looks something like this: var entitySchema = mongoose.Schema({ testvalue:{type:String,default:function getNextSequence() { console.log(\'what is this:\',mongoose);//this is mongoose var