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

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

  ALTER TABLE [dbo].[TransactionExternalRefXTab] CHECK CONSTRAINT [FK_TransactionExternalRefXTab_ExternalReference]

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

  ALTER TABLE [dbo].[TransactionExternalRefXTab] CHECK CONSTRAINT [FK_TransactionExternalRefXTab_Personel]

  ALTER TABLE [dbo].[TransactionExternalRefXTab]  WITH CHECK ADD  CONSTRAINT [FK_TransactionExternalRefXTab_Transactions] FOREIGN KEY([FK_TransactionNumber])
  REFERENCES [dbo].[Transactions] ([TransactionNumber])

  ALTER TABLE [dbo].[TransactionExternalRefXTab] CHECK CONSTRAINT [FK_TransactionExternalRefXTab_Transactions]
END