I have a table with One-To-Many reflexive association.
I need insert the first value disabling temporary the constraint. Any idea how to do it?
I use MS SQL 2008, thanks guys for your support!
CREATE TABLE dbo.CmsCategories
(
CategoryId int NOT NULL IDENTITY (0,1) -- Seed = 0 and Increment= 1
CONSTRAINT PK_CmsCategories_CategoryId PRIMARY KEY,
ParentOf int NOT NULL
CONSTRAINT DF_CmsCategories_开发者_如何学PythonParentOf DEFAULT 0
);
ALTER TABLE dbo.CmsCategories
ADD CONSTRAINT FK_CmsCategories_ParentOf FOREIGN KEY (ParentOf) REFERENCES dbo.CmsCategories(CategoryId); -- One-to-many Reflexive association
GO
INSERT INTO dbo.CmsCategories
(ParentOf)
VALUES
(0);
i found out the solution, tell me what do you think thanks!
-- CmsCategories: Disable constraint one-to-many reflexive association to add first row
ALTER TABLE dbo.CmsCategories
NOCHECK
CONSTRAINT
FK_CmsCategories_ParentOf;
---- CmsCategories: Insert first row - Root value
INSERT INTO dbo.CmsCategories
(Title, MetaDescription, MetaKeyword, Summary, IsPublished, ParentOf)
VALUES
('Homepage','Homepage','Homepage','Homepage',1,0);
-- CmsCategories: Enable constraint one-to-many reflexive association
ALTER TABLE dbo.CmsCategories
CHECK
CONSTRAINT
FK_CmsCategories_ParentOf;
精彩评论