IF OBJECT_ID(N'[dbo].[FK_PL_ViewLink_PL_Link]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[PL_ViewLink] DROP CONSTRAINT [FK_PL_ViewLink_PL_Link];
GO

-- Creating foreign key on [PL_Link_LinkID] in table 'PL_ViewLink'
ALTER TABLE [dbo].[PL_ViewLink]
ADD CONSTRAINT [FK_PL_ViewLink_PL_Link]
    FOREIGN KEY ([PL_Link_LinkID])
    REFERENCES [dbo].[PL_Link]
        ([LinkID])
    ON DELETE CASCADE ON UPDATE NO ACTION;
GO

IF OBJECT_ID(N'[dbo].[FK_PL_ViewLink_PL_View]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[PL_ViewLink] DROP CONSTRAINT [FK_PL_ViewLink_PL_View];
GO

-- Creating foreign key on [PL_View_ViewID] in table 'PL_ViewLink'
ALTER TABLE [dbo].[PL_ViewLink]
ADD CONSTRAINT [FK_PL_ViewLink_PL_View]
    FOREIGN KEY ([PL_View_ViewID])
    REFERENCES [dbo].[PL_View]
        ([ViewID])
    ON DELETE CASCADE ON UPDATE NO ACTION;
GO

IF OBJECT_ID(N'[dbo].[FK_PL_LinkCategory]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[PL_Link] DROP CONSTRAINT [FK_PL_LinkCategory];
GO

-- Creating foreign key on [CategoryID] in table 'PL_Link'
ALTER TABLE [dbo].[PL_Link]
ADD CONSTRAINT [FK_PL_LinkCategory]
    FOREIGN KEY ([CategoryID])
    REFERENCES [dbo].[PL_Category]
        ([CategoryID])
    ON DELETE CASCADE ON UPDATE NO ACTION;
GO

IF OBJECT_ID(N'[dbo].[FK_PL_LinkPL_ViewLinkDenied]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[PL_ViewLinkDenied] DROP CONSTRAINT [FK_PL_LinkPL_ViewLinkDenied];
GO

-- Creating foreign key on [LinkID] in table 'PL_ViewLinkDenied'
ALTER TABLE [dbo].[PL_ViewLinkDenied]
ADD CONSTRAINT [FK_PL_LinkPL_ViewLinkDenied]
    FOREIGN KEY ([LinkID])
    REFERENCES [dbo].[PL_Link]
        ([LinkID])
    ON DELETE CASCADE ON UPDATE NO ACTION;
GO

ALTER TABLE [dbo].[PL_Link]
ALTER COLUMN [CategoryID] int  NULL

GO