﻿CREATE TABLE [dbo].[InterventiMtz] (
    [numIntMtz]             INT             IDENTITY (1, 1) NOT NULL,
    [richiesta]             DATETIME        NOT NULL,
    [dataLav]               DATETIME        NOT NULL,
    [turnoLav]              INT             NOT NULL,
    [matr]                  NVARCHAR (10)   NOT NULL,
    [guasto]                DATETIME        NOT NULL,
    [idxAmbito]             INT             NOT NULL,
    [idxPriorita]           INT             NOT NULL,
    [isFermo]               BIT             CONSTRAINT [DF_InterventiMtz_isFermo] DEFAULT ((1)) NOT NULL,
    [idxTipo]               INT             NOT NULL,
    [idxImpianto]           INT             NOT NULL,
    [idxMacchina]           INT             NOT NULL,
    [descrizione]           NVARCHAR (2500) NOT NULL,
    [idxStato]              INT             NOT NULL,
    [presaInCarico]         DATETIME        NULL,
    [inizioIntervento]      DATETIME        NULL,
    [fineIntervento]        DATETIME        NULL,
    [descrizioneIntervento] NVARCHAR (2500) NULL,
    [isPreventivabile]      BIT             CONSTRAINT [DF_InterventiMtz_isPreventivabile] DEFAULT ((0)) NOT NULL,
    [idxCausale]            INT             NULL,
    [scheduled]             BIT             CONSTRAINT [DF_InterventiMtz_scheduled] DEFAULT ((0)) NULL,
    CONSTRAINT [PK_InterventiMtz] PRIMARY KEY CLUSTERED ([numIntMtz] ASC),
    CONSTRAINT [FK_InterventiMtz_AnagAmbitoGuasto] FOREIGN KEY ([idxAmbito]) REFERENCES [dbo].[AnagAmbitoGuasto] ([idxAmbito]) ON UPDATE CASCADE,
    CONSTRAINT [FK_InterventiMtz_AnagCausali] FOREIGN KEY ([idxCausale]) REFERENCES [dbo].[AnagCausali] ([idxCausale]) ON UPDATE CASCADE,
    CONSTRAINT [FK_InterventiMtz_AnagImpianti] FOREIGN KEY ([idxImpianto]) REFERENCES [dbo].[AnagImpianti] ([idxImpianto]),
    CONSTRAINT [FK_InterventiMtz_AnagMacchine] FOREIGN KEY ([idxMacchina]) REFERENCES [dbo].[AnagMacchine] ([idxMacchina]) ON UPDATE CASCADE,
    CONSTRAINT [FK_InterventiMtz_AnagPriorita] FOREIGN KEY ([idxPriorita]) REFERENCES [dbo].[AnagPriorita] ([idxPriorita]) ON UPDATE CASCADE,
    CONSTRAINT [FK_InterventiMtz_AnagStati] FOREIGN KEY ([idxStato]) REFERENCES [dbo].[AnagStati] ([idxStato]) ON UPDATE CASCADE,
    CONSTRAINT [FK_InterventiMtz_AnagTipoGuasto] FOREIGN KEY ([idxTipo]) REFERENCES [dbo].[AnagTipoGuasto] ([idxTipo]) ON UPDATE CASCADE
);


GO
CREATE NONCLUSTERED INDEX [i_richiesta]
    ON [dbo].[InterventiMtz]([richiesta] ASC);


GO
CREATE NONCLUSTERED INDEX [i_impianto]
    ON [dbo].[InterventiMtz]([idxImpianto] ASC);


GO
CREATE NONCLUSTERED INDEX [i_macchina]
    ON [dbo].[InterventiMtz]([idxMacchina] ASC);


GO
CREATE NONCLUSTERED INDEX [i_stato]
    ON [dbo].[InterventiMtz]([idxStato] ASC);


GO
EXECUTE sp_addextendedproperty @name = N'MS_Description', @value = 'indica se l''intervento sia stato generato da manutenzione programmata/preventiva', @level0type = N'SCHEMA', @level0name = N'dbo', @level1type = N'TABLE', @level1name = N'InterventiMtz', @level2type = N'COLUMN', @level2name = N'scheduled';

