IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'[dbo].[FoliosExternalRefXTab]') AND OBJECTPROPERTY(id, N'IsUserTable') = 1)
BEGIN
  CREATE TABLE [dbo].[FoliosExternalRefXTab]
  (
    [FK_FolioNumber] [int] NOT NULL,
    [FK_ExternalRefId] [int] NOT NULL,
    [CreatedDateTime] [datetime] NULL,
    [UpdatedDateTime] [datetime] NULL,
    [FK_UserId] [int] NOT NULL,
    CONSTRAINT [PK_FoliosExternalRefXTab] PRIMARY KEY CLUSTERED 
    (
      [FK_FolioNumber] ASC,
      [FK_ExternalRefId] ASC
    )
  ) ON [PRIMARY]

  ALTER TABLE [dbo].[FoliosExternalRefXTab]  WITH CHECK ADD  CONSTRAINT [FK_FoliosExternalRefXTab_ExternalReference] FOREIGN KEY([FK_ExternalRefId])
  REFERENCES [dbo].[ExternalReference] ([ID])

  ALTER TABLE [dbo].[FoliosExternalRefXTab] CHECK CONSTRAINT [FK_FoliosExternalRefXTab_ExternalReference]

  ALTER TABLE [dbo].[FoliosExternalRefXTab]  WITH CHECK ADD  CONSTRAINT [FK_FoliosExternalRefXTab_Folios] FOREIGN KEY([FK_FolioNumber])
  REFERENCES [dbo].[Folios] ([FolioNumber])

  ALTER TABLE [dbo].[FoliosExternalRefXTab] CHECK CONSTRAINT [FK_FoliosExternalRefXTab_Folios]

  ALTER TABLE [dbo].[FoliosExternalRefXTab]  WITH CHECK ADD  CONSTRAINT [FK_FoliosExternalRefXTab_Personel] FOREIGN KEY([FK_UserId])
  REFERENCES [dbo].[Personel] ([UserIDNumber])

  ALTER TABLE [dbo].[FoliosExternalRefXTab] CHECK CONSTRAINT [FK_FoliosExternalRefXTab_Personel]
END