﻿IF EXISTS (SELECT 1 FROM sys.foreign_keys WHERE name = 'FK_4028090_4028089')  BEGIN
  EXEC sp_rename N'T_DICTIONARYITEMBASE.FK_4028090_4028089', N'FK_DictionaryItemBase_DictionaryTypeId_IntezmenyId_TanevId_REF_DictionaryType'
END 
ELSE BEGIN
  IF NOT EXISTS (SELECT 1 FROM sys.foreign_keys WHERE name = 'FK_DictionaryItemBase_DictionaryTypeId_IntezmenyId_TanevId_REF_DictionaryType') BEGIN
    ALTER TABLE [dbo].[T_DICTIONARYITEMBASE] WITH NOCHECK
    ADD CONSTRAINT [FK_DictionaryItemBase_DictionaryTypeId_IntezmenyId_TanevId_REF_DictionaryType] 
    FOREIGN KEY ([C_DICTIONARYTYPEID], [C_INTEZMENYID], [C_TANEVID]) 
    REFERENCES [dbo].[T_DICTIONARYTYPE] (ID, C_INTEZMENYID, C_TANEVID);
  END
END
GO

IF EXISTS (SELECT 1 FROM sys.indexes WHERE name = 'PK_T_4025043') AND NOT EXISTS (SELECT 1 FROM sys.indexes WHERE name = 'PK_EntityHistory') BEGIN
  EXEC sp_rename N'T_ENTITYHISTORY.PK_T_4025043', N'PK_EntityHistory', N'INDEX'
END
GO

IF NOT EXISTS (
    SELECT 1 
    FROM sys.indexes uk 
    INNER JOIN sys.tables tbl ON tbl.object_id = uk.object_id
    WHERE tbl.name = 'T_ENTITYHISTORY' AND uk.is_primary_key = 1)
BEGIN
  ALTER TABLE T_ENTITYHISTORY
   ADD CONSTRAINT PK_EntityHistory PRIMARY KEY CLUSTERED (ID);
END
GO

IF EXISTS (SELECT 1 FROM sys.indexes WHERE name = 'PK_T_4025080') AND NOT EXISTS (SELECT 1 FROM sys.indexes WHERE name = 'PK_EntityAttributeHistory') BEGIN
  EXEC sp_rename N'T_ENTITYATTRIBUTEHISTORY.PK_T_4025080', N'PK_EntityAttributeHistory', N'INDEX'
END
GO

IF NOT EXISTS (
    SELECT 1 
    FROM sys.indexes uk 
    INNER JOIN sys.tables tbl ON tbl.object_id = uk.object_id
    WHERE tbl.name = 'T_ENTITYATTRIBUTEHISTORY' AND uk.is_primary_key = 1)
BEGIN
  ALTER TABLE T_ENTITYATTRIBUTEHISTORY
  ADD CONSTRAINT PK_EntityAttributeHistory PRIMARY KEY CLUSTERED (ID);
END
GO


IF EXISTS (SELECT 1 FROM sys.foreign_keys WHERE name = 'FK_4025091_4025089')  BEGIN
  EXEC sp_rename N'T_ENTITYATTRIBUTEHISTORY.FK_4025091_4025089', N'FK_EntityAttributeHistory_EntityId_REF_EntityHistory'
END 
ELSE BEGIN
  IF NOT EXISTS (SELECT 1 FROM sys.foreign_keys WHERE name = 'FK_EntityAttributeHistory_EntityId_REF_EntityHistory') BEGIN
    ALTER TABLE [dbo].T_ENTITYATTRIBUTEHISTORY WITH NOCHECK
    ADD CONSTRAINT FK_EntityAttributeHistory_EntityId_REF_EntityHistory 
    FOREIGN KEY ([C_ENTITYHISTORYID]) 
    REFERENCES [dbo].[T_ENTITYHISTORY] (ID);
  END
END
GO

ALTER TABLE T_ENTITYHISTORY DROP CONSTRAINT IF EXISTS FK_402504302_402504300;
ALTER TABLE T_ENTITYHISTORY DROP CONSTRAINT IF EXISTS FK_402504305_402504303;
GO

IF EXISTS (SELECT 1 FROM sys.indexes WHERE name = 'I_T_402811502_402811500') AND NOT EXISTS (SELECT 1 FROM sys.indexes WHERE name = 'NCI_HittanTipus_AlIntezmenyId') BEGIN
  EXEC sp_rename N'T_HITTANTIPUS.I_T_402811502_402811500', N'NCI_HittanTipus_AlIntezmenyId', N'INDEX'
END
GO
IF EXISTS (SELECT 1 FROM sys.indexes WHERE name = 'I_T_402811505_402811503') AND NOT EXISTS (SELECT 1 FROM sys.indexes WHERE name = 'NCI_HittanTipus_AlTanevId') BEGIN
  EXEC sp_rename N'T_HITTANTIPUS.I_T_402811505_402811503', N'NCI_HittanTipus_AlTanevId', N'INDEX'
END
GO
-- Ilyen index már létezik C_ALINTEZMENYID
DROP INDEX IF EXISTS I_T_402841902_402841900 ON T_KKGYOGYPEDVEGZETTSEG
GO

IF EXISTS (SELECT 1 FROM sys.indexes WHERE name = 'PK_T_4028423') AND NOT EXISTS (SELECT 1 FROM sys.indexes WHERE name = 'PK_KKKlasszikusZene') BEGIN
  EXEC sp_rename N'T_KKKLASSZIKUSZENE.PK_T_4028423', N'PK_KKKlasszikusZene', N'INDEX'
END
GO
