IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[OnlineReservationOrder]') AND type in (N'U'))
BEGIN
	CREATE TABLE [dbo].[OnlineReservationOrder](
		[ReservationOrderId] [int] IDENTITY(1,1) NOT NULL,
		[ReferenceNumber] [int] NULL,
		[GuestNumber] [smallint] NULL,
		[DeliveryPlace] [nvarchar](100) NOT NULL,
		[DeliveryTime] [datetime] NULL,
		[CreateTime] [datetime] NOT NULL,
		[CreateTimeLocal] [datetime] NOT NULL,
		[FolioNumber] [int] NULL
	 CONSTRAINT [PK_OnlineReservationOrder] PRIMARY KEY CLUSTERED 
	(
		[ReservationOrderId] ASC
	) ON [PRIMARY]
	) ON [PRIMARY]

	CREATE NONCLUSTERED INDEX [IX_OnlineReservationOrder] ON [dbo].[OnlineReservationOrder]
	(
		[ReferenceNumber] ASC,
		[GuestNumber] ASC
	) ON [PRIMARY]

	ALTER TABLE [dbo].[OnlineReservationOrder]  WITH CHECK ADD  CONSTRAINT [FK_OnlineReservationOrder_Reservations] FOREIGN KEY([ReferenceNumber], [GuestNumber])
	REFERENCES [dbo].[Reservations] ([ReferenceNumber], [GuestNumber])

	ALTER TABLE [dbo].[OnlineReservationOrder]  WITH CHECK ADD  CONSTRAINT [FK_OnlineReservationOrder_Folios] FOREIGN KEY([FolioNumber])
	REFERENCES [dbo].[Folios] ([FolioNumber])

	ALTER TABLE [dbo].[OnlineReservationOrder] CHECK CONSTRAINT [FK_OnlineReservationOrder_Reservations]

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 = 'FolioNumber'
		WHERE SysObjects.Name = 'OnlineReservationOrder'
	)
	BEGIN
		ALTER TABLE [dbo].[OnlineReservationOrder] ALTER COLUMN [ReferenceNumber] [int] NULL
		ALTER TABLE [dbo].[OnlineReservationOrder] ALTER COLUMN [GuestNumber] [smallint] NULL
		ALTER TABLE [dbo].[OnlineReservationOrder] ADD [FolioNumber] [int] NULL
		ALTER TABLE [dbo].[OnlineReservationOrder]  WITH CHECK ADD  CONSTRAINT [FK_OnlineReservationOrder_Folios] FOREIGN KEY([FolioNumber])
		REFERENCES [dbo].[Folios] ([FolioNumber])
	END
END

IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[OnlineReservationOrderItem]') AND type in (N'U'))
BEGIN
	CREATE TABLE [dbo].[OnlineReservationOrderItem](
		[ReservationOrderId] [int] NOT NULL,
		[TransactionNumber] [int] NOT NULL,
	 CONSTRAINT [PK_OnlineReservationOrderItem] PRIMARY KEY CLUSTERED 
	(
		[ReservationOrderId] ASC,
		[TransactionNumber] ASC
	) ON [PRIMARY]
	) ON [PRIMARY]
	ALTER TABLE [dbo].[OnlineReservationOrderItem]  WITH CHECK ADD  CONSTRAINT [FK_OnlineReservationOrderItem_OnlineReservationOrder] FOREIGN KEY([ReservationOrderId])
	REFERENCES [dbo].[OnlineReservationOrder] ([ReservationOrderId])

	ALTER TABLE [dbo].[OnlineReservationOrderItem] CHECK CONSTRAINT [FK_OnlineReservationOrderItem_OnlineReservationOrder]

	ALTER TABLE [dbo].[OnlineReservationOrderItem]  WITH CHECK ADD  CONSTRAINT [FK_OnlineReservationOrderItem_Transactions] FOREIGN KEY([TransactionNumber])
	REFERENCES [dbo].[Transactions] ([TransactionNumber])

	ALTER TABLE [dbo].[OnlineReservationOrderItem] CHECK CONSTRAINT [FK_OnlineReservationOrderItem_Transactions]
END
