CREATE TABLE [dbo].[ATDocumentCabinet] (
    [CabinetID]          INT              NOT NULL IDENTITY(1,1),
    [CabinetName]        NVARCHAR (255)   NOT NULL,
    [CabinetDescription] NVARCHAR (255)   NULL,
    [TemplateTypeId]     INT              NOT NULL DEFAULT(1),
    [DbConnectionId]     INT              NULL,
    [DbName]             NVARCHAR (50)    NULL,
    [ObjectId]           UNIQUEIDENTIFIER NOT NULL DEFAULT (NEWID()),
    [CreateDate]         DATETIME         NOT NULL DEFAULT (GETDATE()),
    [ModifyDate]         DATETIME         NOT NULL DEFAULT (GETDATE()),
    CONSTRAINT [PK_ATDocumentCabinet] PRIMARY KEY ([CabinetID] ASC),
    CONSTRAINT [UK_ATDocumentCabinet_ObjectId] UNIQUE ([ObjectId] ASC),
    CONSTRAINT [UK_ATDocumentCabinet_CabinetName] UNIQUE NONCLUSTERED ([CabinetName] ASC),
    CONSTRAINT [FK_ATDocumentCabinet_TemplateTypeId] FOREIGN KEY ([TemplateTypeId]) REFERENCES [dbo].[ATTemplateType] ([Id]),
    CONSTRAINT [FK_ATDocumentCabinet_DbConnectionId] FOREIGN KEY ([DbConnectionId]) REFERENCES [dbo].[ATConnection] ([Id])
);

GO

CREATE NONCLUSTERED INDEX [ATDocumentCabinet_IX_CabinetID]
    ON [dbo].[ATDocumentCabinet]([CabinetID] ASC);

GO

CREATE TRIGGER ATDocumentCabinetModifiedDate
ON  [dbo].[ATDocumentCabinet]
FOR UPDATE
AS BEGIN
   UPDATE [dbo].[ATDocumentCabinet] SET [dbo].[ATDocumentCabinet].[ModifyDate] = GETDATE()
   FROM INSERTED
   WHERE inserted.[CabinetID]=[ATDocumentCabinet].[CabinetID]
END
