auto-increment

How to re-assign AUTO_INCREMENT column for every row in a MySQL table using PHP

我与影子孤独终老i 提交于 2019-11-30 12:44:25
I have an image gallery which website members can upload images to. When an image is uploaded, a MySQL row is written, containing various pieces of information about the image, member, etc. This row uses AUTO_INCREMENT to create its ID, so that getimage.php?id=XX can fetch the image to be displayed. I loop through the IDs with a for-loop to display the images within the gallery. If I delete the 5th row/image, for example, the AUTO_INCREMENT goes from 123456789 to 12346789 . I would like to re-assign the ID to each row in the MySQL table, starting from the ground up. So 12346789 becomes

Auto-increment on Azure Table Storage

我只是一个虾纸丫 提交于 2019-11-30 12:01:33
问题 I am currently developing an application for Azure Table Storage. In that application I have table which will have relatively few inserts (a couple of thousand/day) and the primary key of these entities will be used in another table, which will have billions of rows. Therefore I am looking for a way to use an auto-incremented integer, instead of GUID, as primary key in the small table (since it will save lots of storage and scalability of the inserts is not really an issue). There've been

Query to Re-index Primary Key of MySQL Database

风格不统一 提交于 2019-11-30 11:44:05
问题 I've got a table in MySQL that has a Primary Key Column. Lets say: ID | Value 1 | One 2 | Two 6 | Three 8 | Four 9 | Five How do I get it to be: ID | Value 1 | One 2 | Two 3 | Three 4 | Four 5 | Five There are no other tables. Just the one. I just want the ID to be in a proper series. Any suggestion?? A Query perhaps.. :) 回答1: There is even a simple way to accomplish the result by writing this query SET @newid=0; UPDATE tablename SET primary_key_id=(@newid:=@newid+1) ORDER BY primary_key_id;

Add an auto_increment column in Magento setup script without using SQL

◇◆丶佛笑我妖孽 提交于 2019-11-30 11:36:52
Previously I asked how to ALTER TABLE in Magento setup script without using SQL . There, Ivan gave an excellent answer which I still refer to even now. However I have yet to discover how to use Varien_Db_Ddl_Table::addColumn() to specify an auto_increment column. I think it has something to do with an option called identity but so far have had no luck. Is this even possible or is that functionality incomplete? One can create an autoincrement column like that (at least since Magento 1.6, maybe even earlier): /** @var $table Varien_Db_Ddl_Table */ $table->addColumn( 'id', Varien_Db_Ddl_Table:

Reset autoincrement in Microsoft SQL Server 2008 R2

佐手、 提交于 2019-11-30 10:56:58
问题 I created a primary key to be autoincrement. I added two rows: ID=1, ID=2 I deleted these two rows. I added a new row, but the new row's ID was: ID=3 How can I reset or restart the autoincrement to 1? 回答1: If you use the DBCC CHECKIDENT command: DBCC CHECKIDENT ("YourTableNameHere", RESEED, 1); But use with CAUTION! - this will just reset the IDENTITY to 1 - so your next inserts will get values 1, then 2, and then 3 --> and you'll have a clash with your pre-existing value of 3 here! IDENTITY

Modify Django AutoField start value

北慕城南 提交于 2019-11-30 09:33:18
问题 I have and existing database, which I have migrated with SQLAlchemy to a new PostgreSQL database. I moved all primary keys with the same values as before. Now I have tables filled with data, but the associated sequences starts from 1. I have pk values stored 1 to 2000. Now, when I try to save something with Django, I have the duplicate key value violates unique constraint regarding to the Primary Key. How can I modify the sequence start values or escape this situation? My current solution is:

Mysql auto increment jumps when insert-select

巧了我就是萌 提交于 2019-11-30 08:44:16
I am testing insert-select query and noticed an weird result. CREATE TABLE `test` ( `cnt` int(11) NOT NULL AUTO_INCREMENT, `a` int(11) DEFAULT NULL, `b` int(11) DEFAULT NULL, PRIMARY KEY (`cnt`) ) CREATE TABLE `test_current` ( `a` int(11) DEFAULT NULL, `b` int(11) DEFAULT NULL ) First I created two tables, and insert some values into test_current mysql> insert into test_current (a,b) values (1,1),(2,2); Query OK, 2 rows affected (0.00 sec) Records: 2 Duplicates: 0 Warnings: 0 And I did this query mysql> INSERT INTO test (a,b) SELECT a,b FROM test_current; Query OK, 2 rows affected, 1 warning

UUID versus auto increment number for primary key

坚强是说给别人听的谎言 提交于 2019-11-30 08:34:21
Why should I choose UUID over an auto increment number for my entity's primary key? What are the pros and cons? 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 the tables with foreign keys) have to wait for the other side of the relationship (ie the table the foreign

Entity Framework auto increment with starting value

▼魔方 西西 提交于 2019-11-30 07:39:06
问题 I have a entity as follows [Table("ESS_POS_SE_VERSION")] public class Version { [Key, Column("DETAIL_ID"), DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int Id { get; set; } [Column("DETAIL_TITILE")] public string DetailKey { get; set; } [Column("DETAIL")] public string Detail { get; set; } } SO Id column is auto increment column, but I need to set its starting value to 1000. How can I do it? 回答1: Consider creating a custom database initializer. It will be called each time your

How to add auto increment id according to a group in mysql

淺唱寂寞╮ 提交于 2019-11-30 07:14:31
问题 Here is the format of the table: indexer group name id 1 abc a 2 abc b 3 xyz c 4 abc e 5 xyz d Now i want it to be like, indexer group name id 1 abc a 1 2 abc b 2 3 xyz c 1 4 abc e 3 5 xyz d 2 "id" should auto increment according to "group" 回答1: Try this: update yourtable t1 join ( select tt.indexer, @rowno := if(@grp = `group`, @rowno + 1, 1) as id, @grp := `group` from (select * from yourtable order by `group`, indexer) tt cross join (select @rowno := 0, @grp := null) t ) t2 on t1.indexer =