0

I have a table wich I want to let its ID be specified per person

This is what I have so far:

CREATE TABLE [dbo].[Duiken] (
[Duik_nr]          INT           IDENTITY (1, 1) NOT NULL,    // ID
[datum]            DATE          NULL,
[naam]             VARCHAR (50)  NOT NULL,                    // Person
[buddy]            VARCHAR (50)  NULL,
[locatie]          VARCHAR (50)  NULL,
[zicht]            INT           NULL,
[lucht_temp]       INT           NULL,
[water_temp]       INT           NULL,
[fles_Groote]      VARCHAR (50)  NULL,
[duik_tijd]        INT           NULL,
[max_duik_diepte]  INT           NULL,
[lucht_in]         INT           NULL,
[lucht_uit]        INT           NULL,
[lucht_verbruik]   AS            ([lucht_in]-[lucht_uit]),
[lood]             INT           NULL,
[oefenduik]        BIT           NULL,
[recreatieve_duik] BIT           NULL,
[zoetwater_duik]   BIT           NULL,
[zoutwater_duik]   BIT           NULL,
[stilstaand_water] BIT           NULL,
[stromend_water]   BIT           NULL,
[nacht_duik]       BIT           NULL,
[nitrox_duik]      BIT           NULL,
[zonnig]           BIT           NULL,
[bewolkt]          BIT           NULL,
[regen]            BIT           NULL,
[Opmerkingen]      VARCHAR (MAX) NULL,
PRIMARY KEY ([naam],[Duik_nr])

);

But when I add a new record on a new name it continues with the highest ID in the table.

the result im getting now

So the "test" person should also begin with ID 1 instead of 53.

something like this:

+----+------+
| id | Name |
+----+------+
|  1 | a    |
|  2 | a    |
|  1 | b    |
|  3 | a    |
|  1 | c    |
|  2 | b    |
+----+------+

1 Answers1

1

Identity(1, 1) creates an AUTO_INCREMENT field. If the "test" person begins with ID1, then there will be a conflict between "test" and "Tim" and since you defined [Duik_nr] to be a primary key (there can't be duplicate values). All primary keys can only have one instance of each value. That's why you're getting the error that (2, TIM) already exists. There's already an ID 2 and a Name "Tim" in the table.

I believe you had previous records or your identity seed is set to a higher number, so the AUTO_INCREMENT is starting at a higher number.

Reset identity seed after deleting records in SQL Server

Community
  • 1
  • 1
Felix Guo
  • 2,700
  • 14
  • 20