Adding unique int key using max

  • I am trying to add multiple records to my table (insert/select).

    INSERT INTO Users

    ( User_id ,

    Name

    )

    SELECT ( SELECT MAX(User_id) + 1

    FROM Users

    ) ,

    Name

    But I get the error:

    Violation of PRIMARY KEY constraint 'PK_Users'. Cannot insert duplicate key in object 'dbo.Users'.

    But I am using the max User_id + 1, so it can't be duplicate

    This would insert about 20 records.

    Why the error?

    Thanks,

    Tom

  • tshad (7/31/2014)


    I am trying to add multiple records to my table (insert/select).

    INSERT INTO Users

    ( User_id ,

    Name

    )

    SELECT ( SELECT MAX(User_id) + 1

    FROM Users

    ) ,

    Name

    But I get the error:

    Violation of PRIMARY KEY constraint 'PK_Users'. Cannot insert duplicate key in object 'dbo.Users'.

    But I am using the max User_id + 1, so it can't be duplicate

    This would insert about 20 records.

    Why the error?

    Thanks,

    Tom

    Try something like (untested but certain it works ๐Ÿ˜‰

    ๐Ÿ˜Ž

    INSERT INTO Users

    ( User_id ,

    Name

    )

    SELECT ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) + ( SELECT MAX(User_id)

    FROM Users

    ) ,

    Name

  • To further explain things (hopefully correctly), the reason your SQL was failing was that it was determining the MAX value once, and adding 1 to it once, and attempting to use that same value for each of the 20 new records you were inserting. Make sense?

    I'd guess that if you used your method to insert 1 record, it would work fine.


    "If I had been drinking out of that toilet, I might have been killed." -Ace Ventura

  • That worked fine.

    Not sure what this means:

    ROW_NUMBER() OVER (ORDER BY (SELECT NULL))

    How does that work?

  • tshad (8/1/2014)


    That worked fine.

    Not sure what this means:

    ROW_NUMBER() OVER (ORDER BY (SELECT NULL))

    How does that work?

    This adds an incremental number to the output, by stating (SELECT NULL), one indicates that the order is not implied by any values but only the order of appearance. So for each row returned, the id value will be the MAX(KEY) (static throughout the batch) + the incremental row number.

    ๐Ÿ˜Ž

  • CELKO (8/19/2014)


    First learn that a row is not a record; this is a fundamental concept.

    Next, look at CREATE SEQUENCE.

    Hey Joe,

    would you care to share a definition of the difference of the two?

    ๐Ÿ˜Ž

  • CELKO (8/19/2014)


    First learn that a row is not a record; this is a fundamental concept.

    Next, look at CREATE SEQUENCE.

    depends on what you are referring to;

    taken at face value Wikipedia disagrees with that statement in the very first sentence of the definition

    http://en.wikipedia.org/wiki/Row_(database)


    http://en.wikipedia.org/wiki/Row_(database)

    In the context of a relational database, a rowโ€”also called a record or tuple โ€”represents a single, implicitly structured data item in a table. In simple terms, a database table can be thought of as consisting of rows and columns or fields.[1] Each row in a table represents a set of related data, and every row in the table has the same structure.

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

  • CELKO (8/19/2014)


    would you care to share a definition of the difference of the two?

    You'll be sorry; this is the short version ๐Ÿ˜€

    Thank you for this and not at all sorry as this is what was missing from your previous post ๐Ÿ˜‰ In my opinion, complete answers such as this one help the OP's building a better understanding of the subject. It also aids in battling certain growing ambiguity.

    ๐Ÿ˜Ž

Viewing 8 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic. Login to reply