July 15, 2010 at 9:51 am
Chris Stewart-397033 (7/15/2010)
Also in BOL under the CREATE TABLE entry is the following (partial) definition of IDENTITY (emphasis mine):IDENTITY
Indicates that the new column is an identity column. When a new row is added to the table, the Database Engine provides a unique, incremental value for the column. ... The IDENTITY property can be assigned to tinyint, smallint, int, bigint, decimal(p,0), or numeric(p,0) columns.
increment
Is the incremental value added to the identity value of the previous row loaded.
Since the engine creates a unique, incremental value, it must be a whole number value which fits in the bounds of the data type, and is other than zero.
But what about that first inserted row, in this case with the value of -1?:-D
July 15, 2010 at 9:58 am
correct answer is :
Create Table MyTable
(
mainkey int not null identity (0,1)
)
July 15, 2010 at 10:22 am
Tom Garth (7/15/2010)
Chris Stewart-397033 (7/15/2010)
Also in BOL under the CREATE TABLE entry is the following (partial) definition of IDENTITY (emphasis mine):IDENTITY
Indicates that the new column is an identity column. When a new row is added to the table, the Database Engine provides a unique, incremental value for the column. ... The IDENTITY property can be assigned to tinyint, smallint, int, bigint, decimal(p,0), or numeric(p,0) columns.
increment
Is the incremental value added to the identity value of the previous row loaded.
Since the engine creates a unique, incremental value, it must be a whole number value which fits in the bounds of the data type, and is other than zero.
But what about that first inserted row, in this case with the value of -1?:-D
I'm guessing that the engine first checks for an accurate construct, regardless of what the seed value is. 😉
July 15, 2010 at 10:51 am
Nice question, took a little looking at it to figure out what was going on. Thanks!
July 15, 2010 at 1:06 pm
dbowlin (7/15/2010)
Not being able to use an increment of 0 makes sense, so an error makes sense.
What about situations when you need table with the only row 🙂
Logically thinking system must allow you insert at least 1 row...
BTW: identity() is just a function to generate some numbers according rules you need, if you need to manage those generated numbers then table constrains must be used
July 15, 2010 at 5:37 pm
Thanks for the question. I think this serves as a good reminder to people that an increment must be some value other than 0.
Jason...AKA CirqueDeSQLeil
_______________________________________________
I have given a name to my pain...MCM SQL Server, MVP
SQL RNNR
Posting Performance Based Questions - Gail Shaw[/url]
Learn Extended Events
July 16, 2010 at 2:34 am
Good question. I was thrown by the 'does not have permission' option, and thought that maybe if the user did not have permission to create tabbles then this is the message that would be given, so chose the 'cannot tell' answer.
Of course when I tried it, even for a user without permission, it failed with the syntax error rather than the security error 🙂
July 16, 2010 at 3:15 am
I really, really knew the answer to this, but it seemed so simple and obvious I thought there must be a catch and didn't answer it.
I think I am suffering a pavlovian reaction to QOTD.
---------------------------------------------------------------------
September 18, 2010 at 12:55 pm
Good question.
I think the question demonstrates a sense of humour.
Some of the comments though seem to attribute mystical properties to the identity property.
Tom
Viewing 9 posts - 16 through 23 (of 23 total)
You must be logged in to reply to this topic. Login to reply