﻿TRUNCATE TABLE T_TANULOTANTARGYATLAG
GO

DROP INDEX IF EXISTS [NCU_TanuloSni_TanuloId_IntezmenyId_TanevId]
  ON [dbo].[T_TANULOSNI];
GO

DROP INDEX IF EXISTS NCU_TanuloSni_TanuloId_AlintezmenyId_AltanevId
    ON [dbo].[T_TANULOSNI];
GO

IF NOT EXISTS (SELECT 1 FROM sys.indexes WHERE name='NCU_TanuloSni_TanuloId_IntezmenyId_TanevId') BEGIN
  CREATE UNIQUE INDEX NCU_TanuloSni_TanuloId_IntezmenyId_TanevId 
  ON T_TANULOSNI (C_TANULOID, C_INTEZMENYID, C_TANEVID) 
  WHERE TOROLT = 'F'
WITH (FILLFACTOR = 80)
END
GO

DROP INDEX IF EXISTS NCI_OrarendiOraTulajdonsag_OrarendiOraId ON T_ORARENDIORATULAJDONSAG;
GO
DROP INDEX IF EXISTS NCI_TanitasiOraTulajdonsag_TanitasiOraId ON T_TANITASIORATULAJDONSAG;
GO
DROP INDEX IF EXISTS NCI_OrarendiOraTulajdonsag_IntezmenyId_TanevId_OraTulajdonsagId ON T_ORARENDIORATULAJDONSAG;
GO
DROP INDEX IF EXISTS NCI_TanitasiOraTulajdonsag_IntezmenyId_TanevId_OraTulajdonsagId ON T_TANITASIORATULAJDONSAG;
GO
PRINT N'Dropping [dbo].[FK_OrarendiOraTulajdonsag_OrarendiOraId_REF_OrarendiOra]...';


GO
ALTER TABLE [dbo].[T_ORARENDIORATULAJDONSAG] DROP CONSTRAINT [FK_OrarendiOraTulajdonsag_OrarendiOraId_REF_OrarendiOra];


GO
PRINT N'Dropping [dbo].[FK_OrarendiOraTulajdonsag_OraTulajdonsagId_IntezmenyId_TanevId_REF_OraTulajdonsagTipus]...';


GO
ALTER TABLE [dbo].[T_ORARENDIORATULAJDONSAG] DROP CONSTRAINT [FK_OrarendiOraTulajdonsag_OraTulajdonsagId_IntezmenyId_TanevId_REF_OraTulajdonsagTipus];


GO
PRINT N'Dropping [dbo].[FK_TanitasiOraTulajdonsag_OraTulajdonsagId_IntezmenyId_TanevId_REF_OraTulajdonsagTipus]...';


GO
ALTER TABLE [dbo].[T_TANITASIORATULAJDONSAG] DROP CONSTRAINT [FK_TanitasiOraTulajdonsag_OraTulajdonsagId_IntezmenyId_TanevId_REF_OraTulajdonsagTipus];


GO
PRINT N'Dropping [dbo].[FK_TanitasiOraTulajdonsag_TanitasiOraId_REF_TanitasiOra]...';


GO
ALTER TABLE [dbo].[T_TANITASIORATULAJDONSAG] DROP CONSTRAINT [FK_TanitasiOraTulajdonsag_TanitasiOraId_REF_TanitasiOra];


GO
PRINT N'Dropping [dbo].[FK_TanuloTantargyAtlag_OktatasiNevelesiFeladatId_IntezmenyId_TanevId_REF_OktatasiNevelesiFeladat]...';


GO
ALTER TABLE [dbo].[T_TANULOTANTARGYATLAG] DROP CONSTRAINT [FK_TanuloTantargyAtlag_OktatasiNevelesiFeladatId_IntezmenyId_TanevId_REF_OktatasiNevelesiFeladat];


GO
PRINT N'Altering [dbo].[T_ORARENDIORATULAJDONSAG]...';


GO
ALTER TABLE [dbo].[T_ORARENDIORATULAJDONSAG] ALTER COLUMN [C_ORARENDIORAID] INT NOT NULL;

ALTER TABLE [dbo].[T_ORARENDIORATULAJDONSAG] ALTER COLUMN [C_ORATULAJDONSAGID] INT NOT NULL;


GO
ALTER TABLE [dbo].[T_ORARENDIORATULAJDONSAG]
    ADD [C_BOOLERTEK] CHAR (1) NOT NULL;


GO
PRINT N'Altering [dbo].[T_ORATULAJDONSAGTIPUS]...';


GO
ALTER TABLE [dbo].[T_ORATULAJDONSAGTIPUS]
    ADD [C_BOOLDEFAULT]   CHAR (1) DEFAULT ('F') NOT NULL,
        [C_ISORARENDIORA] CHAR (1) DEFAULT ('T') NOT NULL;


GO
PRINT N'Altering [dbo].[T_TANITASIORATULAJDONSAG]...';


GO
ALTER TABLE [dbo].[T_TANITASIORATULAJDONSAG] ALTER COLUMN [C_ORATULAJDONSAGID] INT NOT NULL;

ALTER TABLE [dbo].[T_TANITASIORATULAJDONSAG] ALTER COLUMN [C_TANITASIORAID] INT NOT NULL;


GO
ALTER TABLE [dbo].[T_TANITASIORATULAJDONSAG]
    ADD [C_BOOLERTEK] CHAR (1) NOT NULL;


GO
PRINT N'Altering [dbo].[T_TANULO]...';


GO
ALTER TABLE [dbo].[T_TANULO]
    ADD [C_ISERETTSEGIEREDMENYATADHATO] CHAR (1) DEFAULT ('F') NOT NULL;


GO
PRINT N'Altering [dbo].[T_TANULOTANTARGYATLAG]...';


GO
ALTER TABLE [dbo].[T_TANULOTANTARGYATLAG] DROP COLUMN [C_OKTATASINEVELESIFELADATID];


GO
ALTER TABLE [dbo].[T_TANULOTANTARGYATLAG]
    ADD [C_OKTATASNEVELESIFELADATID] INT NOT NULL;


GO
PRINT N'Creating [dbo].[FK_OrarendiOraTulajdonsag_OrarendiOraId_REF_OrarendiOra]...';


GO
ALTER TABLE [dbo].[T_ORARENDIORATULAJDONSAG] WITH NOCHECK
    ADD CONSTRAINT [FK_OrarendiOraTulajdonsag_OrarendiOraId_REF_OrarendiOra] FOREIGN KEY ([C_ORARENDIORAID]) REFERENCES [dbo].[T_ORARENDIORA] ([ID]);


GO
PRINT N'Creating [dbo].[FK_OrarendiOraTulajdonsag_OraTulajdonsagId_IntezmenyId_TanevId_REF_OraTulajdonsagTipus]...';


GO
ALTER TABLE [dbo].[T_ORARENDIORATULAJDONSAG] WITH NOCHECK
    ADD CONSTRAINT [FK_OrarendiOraTulajdonsag_OraTulajdonsagId_IntezmenyId_TanevId_REF_OraTulajdonsagTipus] FOREIGN KEY ([C_ORATULAJDONSAGID], [C_INTEZMENYID], [C_TANEVID]) REFERENCES [dbo].[T_ORATULAJDONSAGTIPUS] ([ID], [C_ALINTEZMENYID], [C_ALTANEVID]);


GO
PRINT N'Creating [dbo].[FK_TanitasiOraTulajdonsag_OraTulajdonsagId_IntezmenyId_TanevId_REF_OraTulajdonsagTipus]...';


GO
ALTER TABLE [dbo].[T_TANITASIORATULAJDONSAG] WITH NOCHECK
    ADD CONSTRAINT [FK_TanitasiOraTulajdonsag_OraTulajdonsagId_IntezmenyId_TanevId_REF_OraTulajdonsagTipus] FOREIGN KEY ([C_ORATULAJDONSAGID], [C_INTEZMENYID], [C_TANEVID]) REFERENCES [dbo].[T_ORATULAJDONSAGTIPUS] ([ID], [C_ALINTEZMENYID], [C_ALTANEVID]);


GO
PRINT N'Creating [dbo].[FK_TanitasiOraTulajdonsag_TanitasiOraId_REF_TanitasiOra]...';


GO
ALTER TABLE [dbo].[T_TANITASIORATULAJDONSAG] WITH NOCHECK
    ADD CONSTRAINT [FK_TanitasiOraTulajdonsag_TanitasiOraId_REF_TanitasiOra] FOREIGN KEY ([C_TANITASIORAID]) REFERENCES [dbo].[T_TANITASIORA] ([ID]);


GO
PRINT N'Creating [dbo].[FK_TanuloTantargyAtlag_OktatasNevelesiFeladatId_IntezmenyId_TanevId_REF_OktatasiNevelesiFeladat]...';


GO
ALTER TABLE [dbo].[T_TANULOTANTARGYATLAG] WITH NOCHECK
    ADD CONSTRAINT [FK_TanuloTantargyAtlag_OktatasNevelesiFeladatId_IntezmenyId_TanevId_REF_OktatasiNevelesiFeladat] FOREIGN KEY ([C_OKTATASNEVELESIFELADATID], [C_INTEZMENYID], [C_TANEVID]) REFERENCES [dbo].[T_OKTATASINEVELESIFELADAT] ([ID], [C_ALINTEZMENYID], [C_ALTANEVID]);


GO
PRINT N'Altering [dbo].[tr_AsyncAuditORARENDIORATULAJDONSAG]...';


GO




GO
ALTER TABLE [dbo].[T_ORARENDIORATULAJDONSAG] WITH CHECK CHECK CONSTRAINT [FK_OrarendiOraTulajdonsag_OrarendiOraId_REF_OrarendiOra];

ALTER TABLE [dbo].[T_ORARENDIORATULAJDONSAG] WITH CHECK CHECK CONSTRAINT [FK_OrarendiOraTulajdonsag_OraTulajdonsagId_IntezmenyId_TanevId_REF_OraTulajdonsagTipus];

ALTER TABLE [dbo].[T_TANITASIORATULAJDONSAG] WITH CHECK CHECK CONSTRAINT [FK_TanitasiOraTulajdonsag_OraTulajdonsagId_IntezmenyId_TanevId_REF_OraTulajdonsagTipus];

ALTER TABLE [dbo].[T_TANITASIORATULAJDONSAG] WITH CHECK CHECK CONSTRAINT [FK_TanitasiOraTulajdonsag_TanitasiOraId_REF_TanitasiOra];

ALTER TABLE [dbo].[T_TANULOTANTARGYATLAG] WITH CHECK CHECK CONSTRAINT [FK_TanuloTantargyAtlag_OktatasNevelesiFeladatId_IntezmenyId_TanevId_REF_OktatasiNevelesiFeladat];
GO
CREATE INDEX NCI_OrarendiOraTulajdonsag_OrarendiOraId ON T_ORARENDIORATULAJDONSAG (C_ORARENDIORAID)
GO
CREATE INDEX NCI_TanitasiOraTulajdonsag_TanitasiOraId ON T_TANITASIORATULAJDONSAG (C_TANITASIORAID)
GO

CREATE INDEX NCI_OrarendiOraTulajdonsag_IntezmenyId_TanevId_OraTulajdonsagId ON T_ORARENDIORATULAJDONSAG (C_INTEZMENYID, C_TANEVID, C_ORATULAJDONSAGID)
GO
CREATE INDEX NCI_TanitasiOraTulajdonsag_IntezmenyId_TanevId_OraTulajdonsagId ON T_TANITASIORATULAJDONSAG (C_INTEZMENYID, C_TANEVID, C_ORATULAJDONSAGID)
PRINT N'Update complete.';


GO



EXEC dev.uspCreateSchemaViews 'T_TANULOTANTARGYATLAG, T_TANITASIORATULAJDONSAG, T_ORARENDIORATULAJDONSAG, T_TANULO, T_ORATULAJDONSAGTIPUS';
GO

EXEC dev.sp_Global_GenerateAsyncAuditTriggerAll
GO

ALTER QUEUE auditLog.[AuditQueue]
WITH ACTIVATION (
  STATUS = ON,
  PROCEDURE_NAME = auditLog.usp_AuditProcessing,
  MAX_QUEUE_READERS = 10,
  EXECUTE AS 'Kreta_tech_user'
);
GO
