Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8819159
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T05:17:07+00:00 2026-06-14T05:17:07+00:00

If I have a table Table { ID int primary key identity, ParentID int

  • 0

If I have a table

Table
{
ID int primary key identity,
ParentID int not null foreign key references Table(ID)
}

how does one insert first row into a table?

From a business logic point of view not null constraint on ParentID should not be dropped.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T05:17:11+00:00Added an answer on June 14, 2026 at 5:17 am

    In SQL Server, a simple INSERT will do:

    create table dbo.Foo
    (
    ID int primary key identity,
    ParentID int not null foreign key references foo(ID)
    )
    go
    
    insert dbo.Foo (parentId) values (1)
    
    select * from dbo.Foo
    

    results in

        ID          ParentID
    ----------- -----------
        1           1
    

    If you’re trying to insert a value that will be different from your identity seed, the insertion will fail.

    UPDATE:

    The question is not too clear on what the context is (i.e. is the code supposed to work in a live production system or just a DB setup script) and from the comments it seems hard-coding the ID might not be an option. While the code above should normally work fine in the DB initialization scripts where the hierarchy root ID might need to be known and constant, in case of a forest (several roots with IDs not known in advance) the following should work as intended:

    create table dbo.Foo
    (
    ID int primary key identity,
    ParentID int not null foreign key references foo(ID)
    )
    go
    
    insert dbo.Foo (parentId) values (IDENT_CURRENT('dbo.Foo'))
    

    Then one could query the last identity as usual (SCOPE_IDENTITY, etc.). To address @usr’s concerns, the code is in fact transactionally safe as the following example demonstrates:

    insert dbo.Foo (parentId) values (IDENT_CURRENT('dbo.Foo'))
    insert dbo.Foo (parentId) values (IDENT_CURRENT('dbo.Foo'))
    insert dbo.Foo (parentId) values (IDENT_CURRENT('dbo.Foo'))
    
    select * from dbo.Foo
    
    select IDENT_CURRENT('dbo.Foo')
    begin transaction   
        insert dbo.Foo (parentId) values (IDENT_CURRENT('dbo.Foo'))
        rollback
    
    select IDENT_CURRENT('dbo.Foo')
    
    insert dbo.Foo (parentId) values (IDENT_CURRENT('dbo.Foo'))
    
    select * from dbo.Foo
    

    The result:

    ID          ParentID
    ----------- -----------
    1           1
    2           2
    3           3
    
    currentIdentity
    ---------------------------------------
    3
    
    currentIdentity
    ---------------------------------------
    4
    
    ID          ParentID
    ----------- -----------
    1           1
    2           2
    3           3
    5           5
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have a table my_table(id int identity(1,1) not null primary key, data
I have two SQL statements: CREATE TABLE legs(legid INT PRIMARY KEY AUTO_INCREMENT NOT NULL,
I have two tables: CREATE TABLE InmarsatZenith.dbo.ClientJob (JobRef int PRIMARY KEY NOT NULL, IntRef
Consider the following design: Company TABLE ( CompanyId int NOT NULL IDENTITY PRIMARY KEY,
create table #Events ( EventID int identity primary key, StartDate datetime not null, EndDate
I have the following tables: create table TableA ( Id int primary key identity,
Say I have a typical customer table; Id Int Identity Primary Key, FirstName varchar(255),
I have something like this: create table account ( id int identity(1,1) primary key,
I have a (simplified) table consisting of three columns: id INT PRIMARY KEY NOT
declare @T table ( ID int identity primary key, FBK_ID BIGINT null, TWT_ID BIGINT

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.