简体   繁体   中英

How to insert Foreignkey value into a table which doesnt exist yet

I have a situation, I have two tables namely ,User and Company . Here is my User table :

CREATE TABLE [dbo].[User](
[UserId] [int] IDENTITY(1,1) NOT NULL,
[UserName] [nvarchar](500) NOT NULL,
[Password] [nvarchar](500) NOT NULL,    
[CompanyId] [int] NULL,
[LastModUserId] [int] NOT NULL,
[LastModDttm] [datetime] NOT NULL,

CONSTRAINT [PK_User] PRIMARY KEY CLUSTERED 
(
  [UserId] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

ALTER TABLE [dbo].[User]  WITH CHECK ADD  CONSTRAINT [FK_User_Company] FOREIGN KEY([CompanyId])
REFERENCES [dbo].[Company] ([CompanyId])
GO
ALTER TABLE [dbo].[User] CHECK CONSTRAINT [FK_User_Company]
GO

ALTER TABLE [dbo].[User]  WITH CHECK ADD  CONSTRAINT [FK_User_User1] FOREIGN KEY([LastModUserId])
REFERENCES [dbo].[User] ([UserId])
GO
ALTER TABLE [dbo].[User] CHECK CONSTRAINT [FK_User_User1]
 GO

Here is my Company Table :

CREATE TABLE [dbo].[Company](
[CompanyId] [int] IDENTITY(1,1) NOT NULL,
[CompanyName] [nvarchar](500) NOT NULL,
[Address1] [nvarchar](500) NULL,
[Address2] [nvarchar](500) NULL,
[LastModUserId] [int] NOT NULL,
[LastModDttm] [datetime] NOT NULL,
CONSTRAINT [PK_Company] PRIMARY KEY CLUSTERED 
(
[CompanyId] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF,       ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
 ) ON [PRIMARY]

GO

ALTER TABLE [dbo].[Company]  WITH CHECK ADD  CONSTRAINT [FK_Company_User] FOREIGN KEY([LastModUserId])
REFERENCES [dbo].[User] ([UserId])
GO

ALTER TABLE [dbo].[Company] CHECK CONSTRAINT [FK_Company_User]
GO

My problem :

I want to create a company record,I haven't created a user yet.When I Insert into the LastModUserId columun it will throw the error "Cannot Insert the value NULL into column 'LastModUserId' ,table MYDb.dbo.Company' , column does not allow nulls.INSERT fails.

If I try to insert a record into the User Table ,there is also the same problem ,there is CompanyId column,which doesn't allow nulls either

How do I handle this situation ?

Disable constraint to enter super record.

ALTER TABLE [User] NOCHECK CONSTRAINT [FK_User_User1];
     GO
     ALTER TABLE [User] NOCHECK CONSTRAINT [FK_User_Company];
     GO
     DECLARE @USERID AS INT, @COMPANYID AS INT;
     SELECT @USERID = 1, @COMPANYID = 1;
     SET IDENTITY_INSERT [User] ON;
     INSERT INTO [User](
     [UserId],
    [UserName],
    [Password],
    [CompanyId],
    [LastModUserId], 
    [LastModDttm]
    )
    select
    @USERID,
    'Super',
    'Passw0rd',
    1,
    1,
    getdate();

    SET IDENTITY_INSERT [User] OFF;

    SET IDENTITY_INSERT [Company] ON;

    insert into [Company](
    [CompanyId],
    [CompanyName],
    [Address1],
    [Address2],
    [LastModUserId],
    [LastModDttm])
    select
    @COMPANYID,
    'cOMANY a',
    'Address 1',
    'Address 2',
    @USERID,
    getdate();

    SET IDENTITY_INSERT [Company] OFF;
    go
     ALTER TABLE [User] CHECK CONSTRAINT [FK_User_User1];
     GO
     ALTER TABLE [User] CHECK CONSTRAINT [FK_User_Company];
     GO

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM