IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'[dbo].[OnlinePayment]') AND OBJECTPROPERTY(id, N'IsUserTable') = 1)
BEGIN
  CREATE TABLE [dbo].[OnlinePayment]
  (
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [OnlinePaymentId] [varchar](100) NULL,
    [OnlineTransactionId] [varchar](100) NULL,
    [OnlinePaymentType] [int] NULL,
  CONSTRAINT [PK_OnlinePayment] PRIMARY KEY CLUSTERED 
    (
      [Id] ASC
    )
  ) ON [PRIMARY]
END
ELSE
BEGIN
  IF NOT EXISTS
  (
    SELECT SysColumns.Name 
    FROM SysObjects WITH (NOLOCK) 
      INNER JOIN SysColumns WITH (NOLOCK) ON SysColumns.Id = SysObjects.ID 
      AND SysColumns.Name = 'OnlinePaymentType'
      WHERE SysObjects.Name = 'OnlinePayment'
  )
  BEGIN
    ALTER TABLE OnlinePayment ADD OnlinePaymentType INT NULL
  END
END

IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'[dbo].[ReservationOnlinePaymentXtab]') AND OBJECTPROPERTY(id, N'IsUserTable') = 1)
BEGIN
  CREATE TABLE [dbo].[ReservationOnlinePaymentXtab]
  (
    [FK_ReferenceNumber] [int] NOT NULL,
    [FK_GuestNumber] [smallint] NOT NULL,
    [FK_OnlinePaymentId] [int] NOT NULL,
    CONSTRAINT [PK_ReservationOnlinePaymentXtab] PRIMARY KEY CLUSTERED 
    (
      [FK_ReferenceNumber] ASC,
      [FK_GuestNumber] ASC,
      [FK_OnlinePaymentId] ASC
    )
  ) ON [PRIMARY]

  ALTER TABLE [dbo].[ReservationOnlinePaymentXtab]  WITH CHECK ADD  CONSTRAINT [FK_ReservationOnlinePaymentXtab_OnlinePayment] FOREIGN KEY([FK_OnlinePaymentId])
  REFERENCES [dbo].[OnlinePayment] ([Id])

  ALTER TABLE [dbo].[ReservationOnlinePaymentXtab] CHECK CONSTRAINT [FK_ReservationOnlinePaymentXtab_OnlinePayment]

  ALTER TABLE [dbo].[ReservationOnlinePaymentXtab]  WITH CHECK ADD  CONSTRAINT [FK_ReservationOnlinePaymentXtab_ReservationOnlinePaymentXtab] FOREIGN KEY([FK_ReferenceNumber], [FK_GuestNumber])
  REFERENCES [dbo].[Reservations] ([ReferenceNumber], [GuestNumber])

  ALTER TABLE [dbo].[ReservationOnlinePaymentXtab] CHECK CONSTRAINT [FK_ReservationOnlinePaymentXtab_ReservationOnlinePaymentXtab]
END

IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'[dbo].[TransactionOnlinePaymentXtab]') AND OBJECTPROPERTY(id, N'IsUserTable') = 1)
BEGIN
  CREATE TABLE [dbo].[TransactionOnlinePaymentXtab]
  (
    [FK_TransactionNumber] [int] NOT NULL,
    [FK_OnlinePaymentId] [int] NOT NULL,
    CONSTRAINT [PK_TransactionOnlinePaymentXtab] PRIMARY KEY CLUSTERED 
    (
      [FK_TransactionNumber] ASC,
      [FK_OnlinePaymentId] ASC
    )
  ) ON [PRIMARY]

  ALTER TABLE [dbo].[TransactionOnlinePaymentXtab]  WITH CHECK ADD  CONSTRAINT [FK_TransactionOnlinePaymentXtab_OnlinePayment] FOREIGN KEY([FK_OnlinePaymentId])
  REFERENCES [dbo].[OnlinePayment] ([Id])

  ALTER TABLE [dbo].[TransactionOnlinePaymentXtab] CHECK CONSTRAINT [FK_TransactionOnlinePaymentXtab_OnlinePayment]

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

  ALTER TABLE [dbo].[TransactionOnlinePaymentXtab] CHECK CONSTRAINT [FK_TransactionOnlinePaymentXtab_Transactions]
END