IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[AKUZ].[T_CDA_SECTION_PRIVILEGES_REC]') AND type in (N'U'))
CREATE TABLE [AKUZ].[T_CDA_SECTION_PRIVILEGES_REC]
(
    [CdaSectionPrivilegesRecID] uniqueidentifier NOT NULL,
    [CdaOnkoConsultationProtocolV4] uniqueidentifier NOT NULL
)
ON [PRIMARY]
GO

IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[AKUZ].[PK_T_CDA_SECTION_PRIVILEGES_REC]') AND type in (N'PK'))
ALTER TABLE [AKUZ].[T_CDA_SECTION_PRIVILEGES_REC] ADD CONSTRAINT [PK_T_CDA_SECTION_PRIVILEGES_REC]
PRIMARY KEY CLUSTERED ([CdaSectionPrivilegesRecID]) ON [PRIMARY]
GO

IF  EXISTS (SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID(N'[AKUZ].[T_CDA_SECTION_PRIVILEGES_REC]') AND name = N'UQ_T_CDA_SECTION_PRIVILEGES_REC_CdaOnkoConsultationProtocolV4' and is_unique_constraint <> 1)
DROP INDEX [UQ_T_CDA_SECTION_PRIVILEGES_REC_CdaOnkoConsultationProtocolV4] ON [AKUZ].[T_CDA_SECTION_PRIVILEGES_REC]
IF  EXISTS (SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID(N'[AKUZ].[T_CDA_SECTION_PRIVILEGES_REC]') AND name = N'UQ_T_CDA_SECTION_PRIVILEGES_REC_CdaOnkoConsultationProtocolV4' and is_unique_constraint = 1)
ALTER TABLE [AKUZ].[T_CDA_SECTION_PRIVILEGES_REC] DROP CONSTRAINT [UQ_T_CDA_SECTION_PRIVILEGES_REC_CdaOnkoConsultationProtocolV4]
IF  EXISTS (SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[AKUZ].[T_CDA_SECTION_PRIVILEGES_REC]') AND name = N'CdaOnkoConsultationProtocolV4_UQ')
ALTER TABLE [AKUZ].[T_CDA_SECTION_PRIVILEGES_REC] DROP COLUMN [CdaOnkoConsultationProtocolV4_UQ]
GO

/*** ÂÍÈÌÀÍÈÅ! ÑÎÇÄÀÅÒÑß ÓÍÈÊÀËÜÍÛÉ ÈÍÄÅÊÑ, ÏÎËÅ ÍÅ ÌÎÆÅÒ ÁÛÒÜ NULLABLE ***/
CREATE UNIQUE NONCLUSTERED INDEX [UQ_T_CDA_SECTION_PRIVILEGES_REC_CdaOnkoConsultationProtocolV4] ON [AKUZ].[T_CDA_SECTION_PRIVILEGES_REC] ([CdaOnkoConsultationProtocolV4] ASC) ON [PRIMARY]
GO

IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[AKUZ].[FK_T_VIMIS_CDA_DOCUMENT_BASE_T_CDA_SECTION_PRIVILEGES_REC_CdaOnkoConsultationProtocolV4]') AND type in (N'F'))
ALTER TABLE [AKUZ].[T_CDA_SECTION_PRIVILEGES_REC] ADD CONSTRAINT [FK_T_VIMIS_CDA_DOCUMENT_BASE_T_CDA_SECTION_PRIVILEGES_REC_CdaOnkoConsultationProtocolV4]
FOREIGN KEY([CdaOnkoConsultationProtocolV4]) REFERENCES [AKUZ].[T_VIMIS_CDA_DOCUMENT_BASE] ([CdaDocumentID])
GO

IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[AKUZ].[T_CDA_PREFERENTIAL_CATEGOR]') AND type in (N'U'))
CREATE TABLE [AKUZ].[T_CDA_PREFERENTIAL_CATEGOR]
(
    [CdaPreferentialCategorID] uniqueidentifier NOT NULL,
    [EhrRusSdo8u3n5ro] int NOT NULL,
    [CdaSectionPrivilegesRec] uniqueidentifier NULL
)
ON [PRIMARY]
GO

IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[AKUZ].[PK_T_CDA_PREFERENTIAL_CATEGOR]') AND type in (N'PK'))
ALTER TABLE [AKUZ].[T_CDA_PREFERENTIAL_CATEGOR] ADD CONSTRAINT [PK_T_CDA_PREFERENTIAL_CATEGOR]
PRIMARY KEY CLUSTERED ([CdaPreferentialCategorID]) ON [PRIMARY]
GO

IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[AKUZ].[FK_T_EHR_RUS_SDO8U3N5RO_T_CDA_PREFERENTIAL_CATEGOR_EhrRusSdo8u3n5ro]') AND type in (N'F'))
ALTER TABLE [AKUZ].[T_CDA_PREFERENTIAL_CATEGOR] ADD CONSTRAINT [FK_T_EHR_RUS_SDO8U3N5RO_T_CDA_PREFERENTIAL_CATEGOR_EhrRusSdo8u3n5ro]
FOREIGN KEY([EhrRusSdo8u3n5ro]) REFERENCES [EHR].[T_EHR_RUS_SDO8U3N5RO] ([EhrRusID])
GO

IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[AKUZ].[FK_T_CDA_SECTION_PRIVILEGES_REC_T_CDA_PREFERENTIAL_CATEGOR_CdaSectionPrivilegesRec]') AND type in (N'F'))
ALTER TABLE [AKUZ].[T_CDA_PREFERENTIAL_CATEGOR] ADD CONSTRAINT [FK_T_CDA_SECTION_PRIVILEGES_REC_T_CDA_PREFERENTIAL_CATEGOR_CdaSectionPrivilegesRec]
FOREIGN KEY([CdaSectionPrivilegesRec]) REFERENCES [AKUZ].[T_CDA_SECTION_PRIVILEGES_REC] ([CdaSectionPrivilegesRecID])
GO