Questions tagged [auto-increment]

a database constraint that automatically increases from the last record when making an INSERT

AUTO_INCREMENT is a flag set on a field in a database table that forces the RDBMS to create a unique identifier for the record. This is useful when no other obvious primary key field is apparent in a record.

Useful questions

Related tags

2677 questions
11
votes
3 answers

PHP function to increment variable by 1 each time

I have started writing a PHP script for a game about creatures, there are 4 yes/no questions and what I am trying to do is write a function that will display 2 buttons that say yes and no and give then different names each time I run the function,…
Harry12345
  • 1,144
  • 6
  • 20
  • 47
10
votes
2 answers

An autoincrementing callable?

I have a method within which I need to pass an ever-increasing integer to another function. I can do this like so: def foo(i): print i def bar(): class Incrementer(object): def __init__(self, start=0): self.i = start …
Timmy O'Mahony
  • 53,000
  • 18
  • 155
  • 177
10
votes
2 answers

How to add auto increment primary key based on an order of column?

I need to add an auto increment id to an already existing table. I did: ALTER TABLE table_name ADD column_name INT NOT NULL AUTO_INCREMENT FIRST , ADD PRIMARY KEY (column_name) However, the auto numbering wasnt based on any particular column order.…
jerrymouse
  • 16,964
  • 16
  • 76
  • 97
10
votes
1 answer

MySQL's AUTO_INCREMENT behavior in a multiple row insert

I think the answer to my question is obvious but since I could not find any documentation to support it, I thought it's worth asking. At least for the record. As we all know AUTO_INCREMENT fields are incremented each time an INSERT statement is…
Mehran
  • 15,593
  • 27
  • 122
  • 221
10
votes
2 answers

Why am I unable to find a record by _id in mongodb

I am trying to find a record in mongoDB by it's MongoID "_id" field. I have found examples on how to do it, but can not get it to work. Example: $recID = "010101010101011"; //would be a valid mongodb _id $recID = new MongoId((string)$recID); //…
Scott Szretter
  • 3,938
  • 11
  • 57
  • 76
10
votes
3 answers

auto_increment by group

Is there a way with MySQL (5.0 specifically) to have an auto_increment field who's value is based on a grouping column? Example: id name group_field 1 test 1 2 test2 1 1 test3 2 2 test4 2 1 test5 3 2 test6 3 I'd like to not…
Glen Solsberry
  • 11,960
  • 15
  • 69
  • 94
10
votes
2 answers

mysql auto_increment column increments by a random value

Today I've encountered one of the strangest things with MySQL I've seen. I have a trivial table: CREATE TABLE `features` ( `feature_id` mediumint(6) unsigned NOT NULL AUTO_INCREMENT, `feature_name` varchar(100) CHARACTER SET latin1 COLLATE…
Artur
  • 7,038
  • 2
  • 25
  • 39
10
votes
1 answer

PostgreSQL auto increment column by scope of another column value

In PostgreSQL we can define a column as SERIAL to auto increment it with insert query. Can this auto increment happen by scoping to another column value ? I will explain my question with below example. In a multi tenant SaaS application, there…
dp7
  • 6,651
  • 1
  • 18
  • 37
10
votes
7 answers

ID Best Practices for Databases

I was wondering what the best practices were for building and storing IDs. A few years ago, a professor told me about the dangers of a poorly constructed ID system, using the Social Security Number as an example. In particular, because SSNs do not…
10
votes
1 answer

How to convert primary key from integer to serial?

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…
Damir
  • 54,277
  • 94
  • 246
  • 365
10
votes
2 answers

Can AUTO_INCREMENT be safely used in a BEFORE TRIGGER in MySQL

Instagram's Postgres method of implementing custom Ids for Sharding is great, but I need the implementation in MySQL. So, I converted the method found at the bottom of this blog, here:…
jsidlosky
  • 407
  • 1
  • 5
  • 17
10
votes
4 answers

Change the step auto_increment fields increment by

How do I change the amount auto_increment fields in MySQL increment by from the default (1) to n?
sansknwoledge
  • 4,209
  • 9
  • 38
  • 61
10
votes
2 answers

Mongoose auto-increment

I would like to create auto increment ID with Mongoose for MongoDB. I was already looking for some examples, but there aren't anything useful other the MongoDB's documentation.
user1257255
  • 1,161
  • 8
  • 26
  • 55
10
votes
2 answers

Copy autoincrement value to another column on insert?

Basically I have a table that versions products, so it has two columns of interest, id | product_id id is an autoincrement column, product_id is just an int When a product is first created the product_id comes from the id, When the product is edited…
Hailwood
  • 89,623
  • 107
  • 270
  • 423
9
votes
2 answers

REST - How would a PUT request handle auto-incremented resource identifiers

According to the HTTP 1.1. spec: If the Request-URI does not point to an existing resource, and that URI is capable of being defined as a new resource by the requesting user agent, the origin server can create the resource with that URI. So in…
James
  • 80,725
  • 18
  • 167
  • 237