views:

26

answers:

2

Hi,

MySql:: if field is primary key + auto increment should he also defined as unsigned or mysql doing that by default?

Edit:

  1. I asking because integer unsigned save 2* space vs integer.
  2. I asking about INNODB. Thanks
+1  A: 

MySQL doesn't define it to be unsigned by default. The value won't wrap round into negative values, if that's what you are worried about. Inserts will start to fail when you have 'burst' the column type. A good idea would be to use a column type that is 'enormous' compared to the dataset at hand and then you'll never have to worry about it.

More here about auto-increment... http://dev.mysql.com/doc/refman/5.1/en/example-auto-increment.html

Brian Hooper
I worring about memory space that can be saved when field defined as unsigned
Yosef
@Yosef; making the column unsigned doesn't save any memory. It allows twice as many rows to be accommodated for the same INTEGER type, but that doesn't matter until the number of rows reached 2,000,000,000 or so.
Brian Hooper
I stiil think it save because if we write int(8) without unsigned, with unsigned it we can write int(7)
Yosef
I think you'll find the underlying type is the same whichever you have.
Brian Hooper
+2  A: 

MySQL does not make auto increment fields unsigned by default, and you can keep it signed if you like.

The consequence of auto increment is that MySQL keeps a counter for this table which is increased whenever you insert a row without specifying a value for the identity column, and the current value is used as a default for that column. However, if you explicitly specify a value for the identity column on insert, the auto increment status will remain untouched. If the column is of a signed type, this means that you can insert negative values as the ID. In most cases however, you want to let MySQL handle auto increment values completely.

Of course, due to the primary key constraint, the insert will still fail if you try to insert identity values that are in use already, because a primary key also implies a unique constraint.

tdammers
See my edit please
Yosef