SET NOCOUNT ON

IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'[dbo].[Newsletter]') AND OBJECTPROPERTY(id, N'IsUserTable') = 1)
BEGIN
  CREATE TABLE [dbo].[Newsletter]
  (
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [Name] [varchar](50) NOT NULL,
    [Description] [text] NULL,
    [SortNumber] [int] NULL,
    CONSTRAINT [PK_Newsletter] PRIMARY KEY CLUSTERED 
    (
      [Id] ASC
    )
  ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
END
GO

IF EXISTS(SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.GDPRApprove') AND OBJECTPROPERTY(id, N'IsUserTable') = 1)
BEGIN
  IF NOT EXISTS
  (
    SELECT SysColumns.Name 
    FROM SysObjects WITH (NOLOCK) 
    INNER JOIN SysColumns WITH (NOLOCK) ON SysColumns.Id = SysObjects.ID 
    AND SysColumns.Name = 'FK_NewsletterId'
    WHERE SysObjects.Name = 'GDPRApprove'
  )
  BEGIN
    ALTER TABLE GDPRApprove ADD FK_NewsletterId INT NULL

    ALTER TABLE [dbo].[GDPRApprove]  WITH CHECK ADD  CONSTRAINT [FK_GDPRApprove_Newsletter] FOREIGN KEY([FK_NewsletterId])  REFERENCES [dbo].[Newsletter] ([Id])

    ALTER TABLE [dbo].[GDPRApprove] CHECK CONSTRAINT [FK_GDPRApprove_Newsletter]

  END
END
GO