CREATE TABLE [dbo].[ATTaskInstance] (
    [TaskID]              INT            NOT NULL,
    [WorkItemReferenceID] INT            NOT NULL,
    [Status]              INT            NOT NULL,
    [CompletedBy]         NVARCHAR (255) NULL,
    [DateCompleted]       DATETIME       NULL,
    CONSTRAINT [PK_TaskInstance] PRIMARY KEY NONCLUSTERED ([TaskID] ASC, [WorkItemReferenceID] ASC),
    CONSTRAINT [ATTaskInstance_FK1] FOREIGN KEY ([TaskID]) REFERENCES [dbo].[ATTaskDefinition] ([TaskID]),
    CONSTRAINT [ATtaskInstance_FK2] FOREIGN KEY ([WorkItemReferenceID]) REFERENCES [dbo].[ATWorkItemReference] ([WorkItemReferenceId]),
    CONSTRAINT [ATTaskInstance_FK3] FOREIGN KEY ([CompletedBy]) REFERENCES [dbo].[ATUser] ([UserName])
);


GO
CREATE NONCLUSTERED INDEX [ATTaskinstance_IX_WIRId]
    ON [dbo].[ATTaskInstance]([WorkItemReferenceID] ASC);

