auto-increment

Generate an auto increment field in rails

和自甴很熟 提交于 2019-11-28 05:30:20
I have a model Token, which has a field token_number that I need to auto increment (starting from 1001), if and only if the user does not provide it. The problem is that, since the user has the option to provide this field, I can't exactly query the database and ask for the largest token_number. I found one answer on this forum, but I'm quite certain there has to be a better way to do it than to execute an SQL statement? Auto increment a non-primary key field in Ruby on Rails Syed Aslam Interesting question for me. Unfortunately, rails doesn't provide a way to auto-increment columns, so we

Automatically add letters in front of an auto-increment fieild

左心房为你撑大大i 提交于 2019-11-28 05:21:59
问题 Is it possible to attach a letter in front of an auto-increment in MySQL. I have several tables in my database, some of which have unique id's created by auto-increment. I want to be able to distinguish between the auto-generated numbers by a letter at the front. This is not a feature which is absolutely required but it would just help making small tasks easy. 回答1: Unfortunately you cannot do that. At least not in sql. An autoincrement field is of integer type, so adding a letter there

How to Reset an MySQL AutoIncrement using a MAX value from another table?

社会主义新天地 提交于 2019-11-28 05:19:04
I know this won't work, tried it in various forms and failed all times. What is the simplest way to achieve the following result? ALTER TABLE XYZ AUTO_INCREMENT = (select max(ID) from ABC); This is great for automation projects. Thank you! SELECT @max := (max(ID)+1) from ABC; -> This works! select ID from ABC where ID = (@max-1); -> This works! ALTER TABLE XYZ AUTO_INCREMENT = (@max+1); -> This fails :( Why? OMG Ponies Use a Prepared Statement : SELECT @max := MAX(ID)+ 1 FROM ABC; PREPARE stmt FROM 'ALTER TABLE ABC AUTO_INCREMENT = ?'; EXECUTE stmt USING @max; DEALLOCATE PREPARE stmt;

What happens when auto_increment on integer column reaches the max_value in databases?

风格不统一 提交于 2019-11-28 04:27:30
I am implementing a database application and I will use both JavaDB and MySQL as database. I have an ID column in my tables that has integer as type and I use the databases auto_increment-function for the value. But what happens when I get more than 2 (or 4) billion posts and integer is not enough? Is the integer overflowed and continues or is an exception thrown that I can handle? Yes, I could change to long as datatype, but how do I check when that is needed? And I think there is problem with getting the last_inserted_id()-functions if I use long as datatype for the ID-column. Jim Martin's

MySQL: Auto increment temporary column in select statement

ぃ、小莉子 提交于 2019-11-28 03:56:30
How do I create and auto increment a temporary column in my select statement with MySQL? Here is what I have so far: SET @cnt = 0; SELECT (@cnt =@cnt + 1) AS rowNumber, rowID FROM myTable WHERE CategoryID = 1 Which returns: +++++++++++++++++++++ + rowNumber | rowID + +++++++++++++++++++++ + (NULL) | 1 + + (NULL) | 25 + + (NULL) | 33 + + (NULL) | 150 + + (NULL) | 219 + +++++++++++++++++++++ But I need: +++++++++++++++++++++ + rowNumber | rowID + +++++++++++++++++++++ + 1 | 1 + + 2 | 25 + + 3 | 33 + + 4 | 150 + + ... | ... + +++++++++++++++++++++ Kermit This will give you a consecutive row

How to get last inserted row ID from WordPress database?

吃可爱长大的小学妹 提交于 2019-11-28 03:44:36
My WordPress plugin has a table with a AUTO_INCREMENT primary key field called ID. When a new row is inserted into the table, I'd like to get the ID value of the insertion. The feature is to using AJAX to post data to server to insert into DB. The new row ID is returned in the AJAX response to update client status. It is possible that multiple clients are posting data to server at the same time. So, I have to make sure that each AJAX request get the EXACT new row ID in response. In PHP, there is a method called mysql_insert_id for this feature.But, it is valid for race condition only if the

How do I return a new IDENTITY column value from an SQLServer SELECT statement?

空扰寡人 提交于 2019-11-28 03:04:28
问题 I'm inserting into an SQLServer table with an autoincrementing key field. (I believe this is called an IDENTITY column in SQLServer.) In Oracle, I can use the RETURNING keyword to give my INSERT statement a results set like a SELECT query that will return the generated value: INSERT INTO table (foreign_key1, value) VALUES (9, 'text') RETURNING key_field INTO :var; How do I accomplish this in SQLServer? Bonus : Okay, nice answers so far, but how do I put it into a single statement, if possible

Make auto increment fill previously deleted number [duplicate]

*爱你&永不变心* 提交于 2019-11-28 01:33:59
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Using unused primary keys I created a table with an id column with auto increment Now when I deleted the rows with the id's 8,12,30 in that table. Now when I insert a new record I want to insert the next record into 8 then 12 etc.. Is there a way to get MySQL auto increment to do this for me? I tried to set autoincrement to some number, but I don't want to do it that way. 回答1: You don't want to use an auto

Auto increment a value in firebase with javascript

[亡魂溺海] 提交于 2019-11-28 01:04:04
Hello I'm working on some firebase project and i can save my datas via javascript into firebase database. But i couldn't figure it out to auto increment child value (my child value is duyuru, you can see the details in the below) of my database. I'm sharing my code in below can you give me hints to solve this issue. <script> // Initialize Firebase var config = { apiKey: "sample1", authDomain: "sample2", databaseURL: "sample3", storageBucket: "sample4", }; firebase.initializeApp(config); var database = firebase.database(); firebase.initializeApp(config); var database = firebase.database();

Does Postgresql SERIAL work differently?

时光怂恿深爱的人放手 提交于 2019-11-27 22:52:18
I have a postgres table with a SERIAL id. id (serial) name age Insert usually happens from a web application. I inserted manually two new records setting the id as max (id)+1 **** After these 2 insert when the web app inserts 2 record it gives duplicate key error. Just for 2 records. After that everything works fine. The question is - Why didn't my manual insert increment the serial? Are auto increment and serial are different? What am I missing here? Do MySQL or any other SQL have the same issue? When you create a serial or bigserial column, PostgreSQL actually does three things: Creates an