Reset Identity within Compound Key

  • Hi, first post here.

    I'm returning to SQL after an absence of many years and things have changed a bit since I last used it.

    I'm using SQL Server Management Studio 2008.

    I have 2 tables, Questions and Answers, pseudo definition below:

    Questions

    +++++++

    Id int Not null Primary key identity(1,1)

    other columns

    Answers

    ++++++

    QuestionID int not null Primary Key references Questions.Id

    Id int Not null Primary key identity(1,1)

    other columns

    So there can be mutiple answer options to a question. What i want to know is how to make the Id of the Answers table reset to 1 within the question id:

    e.g.

    QuestionId, Id

    1,1 - Question 1, Option 1

    1,2 - Question 1, Option 2

    1,3

    1,4

    2,1 - Question 2, Option 1

    2,2

    2,3

    3,1

    Will sql know this automatically or will it do this:

    1,1 - Question 1, Option 1

    1,2 - Question 1, Option 2

    1,3

    1,4

    2,5

    2,6

    2,7

    3,8

    I hope that makes sense, it's been so long since I've done relational stuff I can barely remember the terminology.

  • You could do that by doing an DBCC CHECKIDENT() (See BOL for details), but it is really not the best way to do it. I'd be more likely to roll my own solution for that.

Viewing 2 posts - 1 through 1 (of 1 total)

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