--[T_ADDRESS]
ALTER TABLE [AKUZ].[T_ADDRESS] ADD [CdaSectionDocinfoChemicalPoisoning] uniqueidentifier NULL
GO
ALTER TABLE [AKUZ].[T_ADDRESS] ADD [CdaSectionDocinfoAbsenceContactsInfectious] uniqueidentifier NULL
GO
ALTER TABLE [AKUZ].[T_ADDRESS] ADD [CdaSectionWorkMedCertificate] uniqueidentifier NULL
GO
ALTER TABLE [AKUZ].[T_ADDRESS] ADD [CdaWorkplace] uniqueidentifier NULL
GO
ALTER TABLE [AKUZ].[T_ADDRESS] ADD [HospLanamSocanamEdu] uniqueidentifier NULL
GO
ALTER TABLE [AKUZ].[T_ADDRESS] ADD [DeadpatinfoPerinatalDeathPlace] uniqueidentifier NULL
GO
ALTER TABLE [AKUZ].[T_ADDRESS] ADD [DeadpatinfoPerinatalBirthPlace] uniqueidentifier NULL
GO

ALTER TABLE [AKUZ].[T_ADDRESS] ADD CONSTRAINT [FK_T_CDA_SECTION_DOCINFO_CHEMICAL_POISONING_T_ADDRESS_CdaSectionDocinfoChemicalPoisoning]
FOREIGN KEY([CdaSectionDocinfoChemicalPoisoning]) REFERENCES [AKUZ].[T_CDA_SECTION_DOCINFO_CHEMICAL_POISONING] ([CdaSectionID])
GO
ALTER TABLE [AKUZ].[T_ADDRESS] ADD CONSTRAINT [FK_T_CDA_SECTION_DOCINFO_ABSENCE_CONTACTS_INFECTIOUS_T_ADDRESS_CdaSectionDocinfoAbsenceContactsInfectious]
FOREIGN KEY([CdaSectionDocinfoAbsenceContactsInfectious]) REFERENCES [AKUZ].[T_CDA_SECTION_DOCINFO_ABSENCE_CONTACTS_INFECTIOUS] ([CdaSectionID])
GO
ALTER TABLE [AKUZ].[T_ADDRESS] ADD CONSTRAINT [FK_T_CDA_SECTION_WORK_MED_CERTIFICATE_T_ADDRESS_CdaSectionWorkMedCertificate]
FOREIGN KEY([CdaSectionWorkMedCertificate]) REFERENCES [AKUZ].[T_CDA_SECTION_WORK_MED_CERTIFICATE] ([CdaSectionID])
GO
ALTER TABLE [AKUZ].[T_ADDRESS] ADD CONSTRAINT [FK_T_CDA_WORKPLACE_T_ADDRESS_CdaWorkplace]
FOREIGN KEY([CdaWorkplace]) REFERENCES [AKUZ].[T_CDA_WORKPLACE] ([CdaWorkplaceID])
GO
ALTER TABLE [AKUZ].[T_ADDRESS] ADD CONSTRAINT [FK_T_HOSP_LANAM_SOCANAM_EDU_T_ADDRESS_HospLanamSocanamEdu]
FOREIGN KEY([HospLanamSocanamEdu]) REFERENCES [AKUZ].[T_HOSP_LANAM_SOCANAM_EDU] ([HospLanamSocanamEduID])
GO
ALTER TABLE [AKUZ].[T_ADDRESS] ADD CONSTRAINT [FK_T_CDA_SECTION_DEADPATINFO_PERINATAL_T_ADDRESS_DeadpatinfoPerinatalDeathPlace]
FOREIGN KEY([DeadpatinfoPerinatalDeathPlace]) REFERENCES [AKUZ].[T_CDA_SECTION_DEADPATINFO_PERINATAL] ([CdaSectionID])
GO
ALTER TABLE [AKUZ].[T_ADDRESS] ADD CONSTRAINT [FK_T_CDA_SECTION_DEADPATINFO_PERINATAL_T_ADDRESS_DeadpatinfoPerinatalBirthPlace]
FOREIGN KEY([DeadpatinfoPerinatalBirthPlace]) REFERENCES [AKUZ].[T_CDA_SECTION_DEADPATINFO_PERINATAL] ([CdaSectionID])
GO


-- [T_HOSP_LANAM_SOCANAM_EDU]
IF  EXISTS (SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID(N'[AKUZ].[T_ADDRESS]') AND name = N'UQ_T_ADDRESS_HospLanamSocanamEdu' and is_unique_constraint <> 1)
DROP INDEX [UQ_T_ADDRESS_HospLanamSocanamEdu] ON [AKUZ].[T_ADDRESS]
IF  EXISTS (SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID(N'[AKUZ].[T_ADDRESS]') AND name = N'UQ_T_ADDRESS_HospLanamSocanamEdu' and is_unique_constraint = 1)
ALTER TABLE [AKUZ].[T_ADDRESS] DROP CONSTRAINT [UQ_T_ADDRESS_HospLanamSocanamEdu]
IF  EXISTS (SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[AKUZ].[T_ADDRESS]') AND name = N'HospLanamSocanamEdu_UQ')
ALTER TABLE [AKUZ].[T_ADDRESS] DROP COLUMN [HospLanamSocanamEdu_UQ]
GO
ALTER TABLE [AKUZ].[T_ADDRESS] ADD [HospLanamSocanamEdu_UQ] AS (CASE WHEN [HospLanamSocanamEdu] IS NOT NULL THEN NULL ELSE [AddressID] END)
GO
CREATE UNIQUE NONCLUSTERED INDEX [UQ_T_ADDRESS_HospLanamSocanamEdu] ON [AKUZ].[T_ADDRESS] ([HospLanamSocanamEdu] ASC, [HospLanamSocanamEdu_UQ] ASC) ON [PRIMARY]
GO

--[T_CDA_WORKPLACE]
ALTER TABLE [AKUZ].[T_CDA_WORKPLACE] ALTER COLUMN [Address] varchar(255) NULL
GO
ALTER TABLE [AKUZ].[T_CDA_WORKPLACE] ALTER COLUMN [EhrRusJ5PHS5F9RA] uniqueidentifier NULL
GO

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

ALTER TABLE [AKUZ].[T_ADDRESS] ADD [CdaWorkplace_UQ] AS (CASE WHEN [CdaWorkplace] IS NOT NULL THEN NULL ELSE [AddressID] END)
GO
CREATE UNIQUE NONCLUSTERED INDEX [UQ_T_ADDRESS_CdaWorkplace] ON [AKUZ].[T_ADDRESS] ([CdaWorkplace] ASC, [CdaWorkplace_UQ] ASC) ON [PRIMARY]
GO

--[T_CDA_SECTION_WORK_MED_CERTIFICATE]
ALTER TABLE [AKUZ].[T_CDA_SECTION_WORK_MED_CERTIFICATE] ALTER COLUMN [EhrRusJ5PHS5F9RA] uniqueidentifier NULL
GO
ALTER TABLE [AKUZ].[T_CDA_SECTION_WORK_MED_CERTIFICATE] ALTER COLUMN [TextAddress] varchar(255) NULL
GO

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

ALTER TABLE [AKUZ].[T_ADDRESS] ADD [CdaSectionWorkMedCertificate_UQ] AS (CASE WHEN [CdaSectionWorkMedCertificate] IS NOT NULL THEN NULL ELSE [AddressID] END)
GO
CREATE UNIQUE NONCLUSTERED INDEX [UQ_T_ADDRESS_CdaSectionWorkMedCertificate] ON [AKUZ].[T_ADDRESS] ([CdaSectionWorkMedCertificate] ASC, [CdaSectionWorkMedCertificate_UQ] ASC) ON [PRIMARY]
GO


--[T_CDA_SECTION_DOCINFO_CHEMICAL_POISONING]

ALTER TABLE [AKUZ].[T_CDA_SECTION_DOCINFO_CHEMICAL_POISONING] ALTER COLUMN [FullADDR] uniqueidentifier NULL
GO
ALTER TABLE [AKUZ].[T_CDA_SECTION_DOCINFO_CHEMICAL_POISONING] ALTER COLUMN [FlatNumber] varchar(8) NULL
GO

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

ALTER TABLE [AKUZ].[T_ADDRESS] ADD [CdaSectionDocinfoChemicalPoisoning_UQ] AS (CASE WHEN [CdaSectionDocinfoChemicalPoisoning] IS NOT NULL THEN NULL ELSE [AddressID] END)
GO
CREATE UNIQUE NONCLUSTERED INDEX [UQ_T_ADDRESS_CdaSectionDocinfoChemicalPoisoning] ON [AKUZ].[T_ADDRESS] ([CdaSectionDocinfoChemicalPoisoning] ASC, [CdaSectionDocinfoChemicalPoisoning_UQ] ASC) ON [PRIMARY]
GO

--[T_CDA_SECTION_DOCINFO_ABSENCE_CONTACTS_INFECTIOUS]

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

ALTER TABLE [AKUZ].[T_ADDRESS] ADD [CdaSectionDocinfoAbsenceContactsInfectious_UQ] AS (CASE WHEN [CdaSectionDocinfoAbsenceContactsInfectious] IS NOT NULL THEN NULL ELSE [AddressID] END)
GO
CREATE UNIQUE NONCLUSTERED INDEX [UQ_T_ADDRESS_CdaSectionDocinfoAbsenceContactsInfectious] ON [AKUZ].[T_ADDRESS] ([CdaSectionDocinfoAbsenceContactsInfectious] ASC, [CdaSectionDocinfoAbsenceContactsInfectious_UQ] ASC) ON [PRIMARY]
GO

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

ALTER TABLE [AKUZ].[T_ADDRESS]ADD [DeadpatinfoPerinatalDeathPlace_UQ] AS (CASE WHEN [DeadpatinfoPerinatalDeathPlace] IS NOT NULL THEN NULL ELSE [AddressID] END)
GO
CREATE UNIQUE NONCLUSTERED INDEX [UQ_T_ADDRESS_DeadpatinfoPerinatalDeathPlace] ON [AKUZ].[T_ADDRESS] ([DeadpatinfoPerinatalDeathPlace] ASC, [DeadpatinfoPerinatalDeathPlace_UQ] ASC) ON [PRIMARY]
GO

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

ALTER TABLE [AKUZ].[T_ADDRESS]ADD [DeadpatinfoPerinatalBirthPlace_UQ] AS (CASE WHEN [DeadpatinfoPerinatalBirthPlace] IS NOT NULL THEN NULL ELSE [AddressID] END)
GO
CREATE UNIQUE NONCLUSTERED INDEX [UQ_T_ADDRESS_DeadpatinfoPerinatalBirthPlace] ON [AKUZ].[T_ADDRESS] ([DeadpatinfoPerinatalBirthPlace] ASC, [DeadpatinfoPerinatalBirthPlace_UQ] ASC) ON [PRIMARY]
GO

