IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'[dbo].[KeySystem]') AND OBJECTPROPERTY(id, N'IsUserTable') = 1)
BEGIN

	CREATE TABLE [dbo].[KeySystem](
		[KeySystemID] [int] IDENTITY(1,1) NOT NULL,
		[Number] [int] NOT NULL,
		[SystemName] [varchar](100) NOT NULL,
	 CONSTRAINT [PK_KeySystem] PRIMARY KEY CLUSTERED 
	(
		[KeySystemID] ASC
	)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
	) ON [PRIMARY]

	CREATE TABLE [dbo].[KeySystemHotelXTAB](
	[FK_KeySystemID] [int] NOT NULL,
	[HotelCustomerNumber] [int] NOT NULL,
	 CONSTRAINT [PK_KeySystemHotelXTAB] PRIMARY KEY CLUSTERED 
	(
		[FK_KeySystemID] ASC,
		[HotelCustomerNumber] ASC
	)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
	) ON [PRIMARY]

	ALTER TABLE [dbo].[KeySystemHotelXTAB]  WITH CHECK ADD  CONSTRAINT [FK_KeySystemHotelXTAB_Customer] FOREIGN KEY([HotelCustomerNumber])
	REFERENCES [dbo].[Customer] ([CustomerNumber])

	ALTER TABLE [dbo].[KeySystemHotelXTAB] CHECK CONSTRAINT [FK_KeySystemHotelXTAB_Customer]

	ALTER TABLE [dbo].[KeySystemHotelXTAB]  WITH CHECK ADD  CONSTRAINT [FK_KeySystemHotelXTAB_KeySystem] FOREIGN KEY([FK_KeySystemID])
	REFERENCES [dbo].[KeySystem] ([KeySystemID])

	ALTER TABLE [dbo].[KeySystemHotelXTAB] CHECK CONSTRAINT [FK_KeySystemHotelXTAB_KeySystem]

	ALTER TABLE dbo.Rooms ADD FK_KeySystemID INT NULL 
	ALTER TABLE [dbo].[Rooms]  WITH CHECK ADD  CONSTRAINT [FK_Rooms_KeySystem] FOREIGN KEY([FK_KeySystemID])
		REFERENCES [dbo].[KeySystem] ([KeySystemID])
END

