﻿IF OBJECT_ID('FK_4025847_4025846', 'F') IS NOT NULL BEGIN
  ALTER TABLE T_NAPTARINAP_TEVEKENYSEG 
  DROP CONSTRAINT FK_4025847_4025846;   
END
GO

IF OBJECT_ID('FK_4025611_4025610', 'F') IS NOT NULL BEGIN
  ALTER TABLE T_TANITASIORA 
  DROP CONSTRAINT FK_4025611_4025610;   
END
GO

IF OBJECT_ID('PK_T_4025562') IS NOT NULL BEGIN
  ALTER TABLE T_NAPTARINAP 
  DROP CONSTRAINT PK_T_4025562;   
END
GO

IF NOT EXISTS (SELECT 1 FROM sys.indexes WHERE name = 'IX_NAPTARINAP_INTEZMENYID_TANEVID_NAPDATUMA') BEGIN
  CREATE CLUSTERED INDEX IX_NAPTARINAP_INTEZMENYID_TANEVID_NAPDATUMA
  ON T_NAPTARINAP (C_INTEZMENYID, C_TANEVID, C_NAPDATUMA)
END
GO

IF OBJECT_ID('PK_T_4025562') IS NULL BEGIN
  ALTER TABLE T_NAPTARINAP 
  ADD CONSTRAINT PK_T_4025562 PRIMARY KEY (ID);   
END
GO

IF OBJECT_ID('FK_4025847_4025846', 'F') IS NULL BEGIN
  ALTER TABLE T_NAPTARINAP_TEVEKENYSEG 
  ADD CONSTRAINT FK_4025847_4025846
  FOREIGN KEY (C_NAPTARINAPID)
  REFERENCES T_NAPTARINAP (ID)
END
GO

IF OBJECT_ID('FK_4025611_4025610', 'F') IS NULL BEGIN
  ALTER TABLE T_TANITASIORA 
  ADD CONSTRAINT FK_4025611_4025610
  FOREIGN KEY (C_NAPTARINAPID)
  REFERENCES T_NAPTARINAP (ID)
END
GO 

IF EXISTS (SELECT 1 FROM sys.indexes WHERE name = 'IX_NAPTARINAP_INTEZMENYID_TANEVID_NAPDATUMA_ORARENDINAP') BEGIN
  DROP INDEX IX_NAPTARINAP_INTEZMENYID_TANEVID_NAPDATUMA_ORARENDINAP
  ON T_NAPTARINAP
END
GO

IF EXISTS (SELECT 1 FROM sys.indexes WHERE name = 'IX_NAPTARINAP_NAPDATUM_INTEZMENYID') BEGIN
  DROP INDEX IX_NAPTARINAP_NAPDATUM_INTEZMENYID
  ON T_NAPTARINAP
END
GO

IF NOT EXISTS (SELECT 1 FROM sys.indexes WHERE name = 'IX_CSENGETESIRENDORA_INTEZMENYID_CSENGETESIRENDID_ORASZAM') BEGIN
  CREATE NONCLUSTERED INDEX IX_CSENGETESIRENDORA_INTEZMENYID_CSENGETESIRENDID_ORASZAM 
  ON T_CSENGETESIRENDORA (C_INTEZMENYID , C_TANEVID , C_CSENGETESIRENDID, C_ORASZAM)
  INCLUDE (TOROLT, C_KEZDETE, C_VEGE)
END
GO

IF NOT EXISTS (SELECT 1 FROM sys.indexes WHERE name = 'IX_HELYETTESITESIIDOSZAK_INTEZMENYID_HELYETTESITETTORARENDID') BEGIN
  CREATE NONCLUSTERED INDEX IX_HELYETTESITESIIDOSZAK_INTEZMENYID_HELYETTESITETTORARENDID
  ON T_HELYETTESITESIIDOSZAK (C_INTEZMENYID , C_HELYETTESITETTORARENDID)
  INCLUDE (C_HELYETTESITESNAPJA, C_HELYETTESTANAROKID, TOROLT)
END
GO

IF NOT EXISTS (SELECT 1 FROM sys.indexes WHERE name = 'UK_CSENGETESIRENDORA_CSENGETESIRENDID_ORASZAM') BEGIN
  CREATE UNIQUE INDEX UK_CSENGETESIRENDORA_CSENGETESIRENDID_ORASZAM
  ON T_CSENGETESIRENDORA (C_CSENGETESIRENDID, C_ORASZAM)
  WHERE TOROLT = 'F'
END
GO

IF NOT EXISTS (SELECT 1 FROM sys.indexes WHERE name = 'IX_ORARENDIORA_INTEZMENYID_TANEVID_CSENGETESIRENDID_ORAERVENYESSEGVEGE') BEGIN
  CREATE INDEX IX_ORARENDIORA_INTEZMENYID_TANEVID_CSENGETESIRENDID_ORAERVENYESSEGVEGE 
  ON T_ORARENDIORA (C_INTEZMENYID, C_TANEVID, C_CSENGETESIRENDID, C_ORAERVENYESSEGVEGE)
  INCLUDE (TOROLT, C_OSZTALYCSOPORTID)
END
GO

IF NOT EXISTS (SELECT 1 FROM sys.indexes WHERE name = 'IX_FELHASZNALO_INTEZMENYID_ID') BEGIN
  CREATE INDEX IX_FELHASZNALO_INTEZMENYID_ID 
  ON T_FELHASZNALO (C_INTEZMENYID, ID) 
  INCLUDE (C_NYOMTATASINEV, C_TANEVID)
END
GO

IF NOT EXISTS (SELECT 1 FROM sys.indexes WHERE name = 'IX_TEREM_INTEZMENYID_ID') BEGIN
  CREATE INDEX IX_TEREM_INTEZMENYID_ID 
  ON T_TEREM (C_INTEZMENYID, ID) 
  INCLUDE (C_NEV, C_TANEVID)
END
GO

IF NOT EXISTS (SELECT 1 FROM sys.indexes WHERE name = 'IX_TANTARGY_INTEZMENYID_ID') BEGIN
  CREATE INDEX IX_TANTARGY_INTEZMENYID_ID 
  ON T_TANTARGY (C_INTEZMENYID, ID) 
  INCLUDE (C_NEV, C_ROVIDNEV, C_TARGYKATEGORIA, C_TANEVID)
END
GO

IF NOT EXISTS (SELECT 1 FROM sys.indexes WHERE name = 'IX_TANITASIORA_INTEZMENYID_TANEVID_DATUM_TANARID_HELYETTESITOTANARID') BEGIN
  CREATE INDEX IX_TANITASIORA_INTEZMENYID_TANEVID_DATUM_TANARID_HELYETTESITOTANARID 
  ON T_TANITASIORA (C_INTEZMENYID, C_TANEVID, C_DATUM, C_TANARID, C_HELYETTESITOTANARID) 
  INCLUDE (TOROLT)
END
GO

IF NOT EXISTS (SELECT 1 FROM sys.indexes WHERE name = 'IX_TANITASIORA_DATUM_INTEZMENYID') BEGIN
	CREATE NONCLUSTERED INDEX IX_TANITASIORA_DATUM_INTEZMENYID
	ON [dbo].[T_TANITASIORA] ([C_DATUM],[C_INTEZMENYID])
	INCLUDE ([C_ORARENDIORAGROUPID], C_TANEVID)
	WHERE TOROLT = 'F'
END
GO

IF NOT EXISTS (SELECT 1 FROM sys.indexes WHERE name='UK_DICTIONARYITEMBASE_INTEZMENYID_TANEVID_TYPE_NAME' 
  AND object_id = OBJECT_ID('T_DICTIONARYITEMBASE'))
BEGIN
  CREATE UNIQUE INDEX UK_DICTIONARYITEMBASE_INTEZMENYID_TANEVID_TYPE_NAME 
    ON T_DICTIONARYITEMBASE (C_INTEZMENYID, C_TANEVID, C_TYPE, C_NAME) 
    INCLUDE (C_VALUE, C_ORDER) 
    WHERE TOROLT = 'F'
END
