SQLServer2012を使用しています
2つのテーブルがPerson
ありCouple
、Person
は「Person」からの外部キーと「Person」からの外部キーCouple
をCouple
持っています。
それぞれにデータを挿入しようとすると、各テーブルに他のテーブルのFKがあり、最初は両方が空であるため、エラーが発生します。
CREATE TABLE [dbo].[Couple](
[CoupleId] [int] IDENTITY(1,1) NOT NULL,
[HusbandPersonId] [int] NOT NULL,
[WifePersonId] [int] NOT NULL,
[StartDate] [date] NOT NULL,
[EndDate] [date] NOT NULL,
CONSTRAINT [PK_Couple] PRIMARY KEY CLUSTERED
(
[CoupleId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
CREATE TABLE [dbo].[Person](
[PersonId] [int] IDENTITY(1,1) NOT NULL,
[ChildOfCoupleId] [int] NOT NULL,
[CityOfBirth] [int] NOT NULL,
[CityOfPermanentResidence] [int] NOT NULL,
[CityOfCurrentResidence] [int] NOT NULL,
[FirstName] [varchar](20) NOT NULL,
[LastName] [varchar](20) NOT NULL,
[BirthDate] [date] NOT NULL,
[DeathDate] [date] NOT NULL,
[IsMale] [bit] NOT NULL,
CONSTRAINT [PK_Person] PRIMARY KEY CLUSTERED
(
[PersonId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY
ALTER TABLE [dbo].[Couple] WITH CHECK ADD CONSTRAINT [FK_Couple_Person] FOREIGN KEY([HusbandPersonId])
REFERENCES [dbo].[Person] ([PersonId])
GO
ALTER TABLE [dbo].[Couple] CHECK CONSTRAINT [FK_Couple_Person]
GO
ALTER TABLE [dbo].[Couple] WITH CHECK ADD CONSTRAINT [FK_Couple_Person1] FOREIGN KEY([WifePersonId])
REFERENCES [dbo].[Person] ([PersonId])
GO
ALTER TABLE [dbo].[Couple] CHECK CONSTRAINT [FK_Couple_Person1]
GO
ALTER TABLE [dbo].[Person] WITH CHECK ADD CONSTRAINT [FK_Person_Couple] FOREIGN KEY([ChildOfCoupleId])
REFERENCES [dbo].[Couple] ([CoupleId])
GO
ALTER TABLE [dbo].[Person] CHECK CONSTRAINT [FK_Person_Couple]
GO
これを解決する方法は?