IF NOT EXISTS (SELECT * FROM sys.tables WHERE name = 'PriceInterval')
BEGIN
  CREATE TABLE [dbo].[PriceInterval]
  (
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [FromAmount] [money] NOT NULL,
    [ToAmount] [money] NOT NULL,
    [Price] [money] NOT NULL,
    [InPercent] [bit] NOT NULL,
    CONSTRAINT [PK_PriceInterval] PRIMARY KEY CLUSTERED 
    (
      [Id] ASC
    )
  ) ON [PRIMARY]
END

IF NOT EXISTS (SELECT * FROM sys.tables WHERE name = 'WarePriceIntervalXTab')
BEGIN
  CREATE TABLE [dbo].[WarePriceIntervalXTab]
  (
    [FK_WareNumber] [int] NOT NULL,
    [FK_PriceIntervalId] [int] NOT NULL,
    CONSTRAINT [PK_WarePriceIntervalXTab] PRIMARY KEY CLUSTERED 
    (
      [FK_WareNumber] ASC,
      [FK_PriceIntervalId] ASC
    )
  ) ON [PRIMARY]

  ALTER TABLE [dbo].[WarePriceIntervalXTab]  WITH CHECK ADD  CONSTRAINT [FK_WarePriceIntervalXTab_PriceInterval] FOREIGN KEY([FK_PriceIntervalId])
  REFERENCES [dbo].[PriceInterval] ([Id])
  ON DELETE CASCADE

  ALTER TABLE [dbo].[WarePriceIntervalXTab] CHECK CONSTRAINT [FK_WarePriceIntervalXTab_PriceInterval]

  ALTER TABLE [dbo].[WarePriceIntervalXTab]  WITH CHECK ADD  CONSTRAINT [FK_WarePriceIntervalXTab_Ware] FOREIGN KEY([FK_WareNumber])
  REFERENCES [dbo].[Ware] ([WareNumber])
  ON DELETE CASCADE

  ALTER TABLE [dbo].[WarePriceIntervalXTab] CHECK CONSTRAINT [FK_WarePriceIntervalXTab_Ware]
END