IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'[dbo].[WareSpaTablePeriod]') AND OBJECTPROPERTY(id, N'IsUserTable') = 1)
BEGIN
  CREATE TABLE [dbo].[WareSpaTablePeriod]
  (
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [FK_WareNumber] [int] NOT NULL,
    [SpaFromTime] [datetime] NOT NULL,
    [SpaToTime] [datetime] NOT NULL,
    [TableFromTime] [datetime] NOT NULL,
    [TableToTime] [datetime] NOT NULL,
    CONSTRAINT [PK_WareSpaTableTimes] PRIMARY KEY CLUSTERED 
    (
      [Id] ASC
    )
  ) ON [PRIMARY]

  ALTER TABLE [dbo].[WareSpaTablePeriod]  WITH CHECK ADD  CONSTRAINT [FK_WareSpaTablePeriod_Ware] FOREIGN KEY([FK_WareNumber])
  REFERENCES [dbo].[Ware] ([WareNumber])

  ALTER TABLE [dbo].[WareSpaTablePeriod] CHECK CONSTRAINT [FK_WareSpaTablePeriod_Ware]
END