﻿PRINT N'Dropping Index [dbo].[T_TEREM].[NCI_Terem_MukodesiHelyId]...';


GO
DROP INDEX [NCI_Terem_MukodesiHelyId]
    ON [dbo].[T_TEREM];


GO
PRINT N'Dropping Index [dbo].[T_TEREM].[NCI_Terem_ElozotanevirekordId_IntezmenyId]...';


GO
DROP INDEX [NCI_Terem_ElozotanevirekordId_IntezmenyId]
    ON [dbo].[T_TEREM];


GO
PRINT N'Dropping Index [dbo].[T_NEMKOTOTTMUNKAIDO].[NCI_NemKotottMunkaido_TulajdonosId]...';


GO
DROP INDEX [NCI_NemKotottMunkaido_TulajdonosId]
    ON [dbo].[T_NEMKOTOTTMUNKAIDO];


GO
PRINT N'Dropping Index [dbo].[T_ORARENDIORA].[NCI_OrarendiOra_OraTulajdonosId]...';


GO
DROP INDEX [NCI_OrarendiOra_OraTulajdonosId]
    ON [dbo].[T_ORARENDIORA];


GO
PRINT N'Dropping Foreign Key [dbo].[FK_Terem_MukodesiHelyId_REF_MukodesiHely]...';


GO
ALTER TABLE [dbo].[T_TEREM] DROP CONSTRAINT [FK_Terem_MukodesiHelyId_REF_MukodesiHely];


GO
PRINT N'Dropping Foreign Key [dbo].[FK_NemKotottMunkaido_TulajdonosId_REF_Felhasznalo]...';


GO
ALTER TABLE [dbo].[T_NEMKOTOTTMUNKAIDO] DROP CONSTRAINT [FK_NemKotottMunkaido_TulajdonosId_REF_Felhasznalo];


GO
PRINT N'Dropping Foreign Key [dbo].[FK_OrarendiOra_OraTulajdonosId_REF_Felhasznalo]...';


GO
ALTER TABLE [dbo].[T_ORARENDIORA] DROP CONSTRAINT [FK_OrarendiOra_OraTulajdonosId_REF_Felhasznalo];


GO
PRINT N'Altering Table [dbo].[T_NEMKOTOTTMUNKAIDO]...';


GO
ALTER TABLE [dbo].[T_NEMKOTOTTMUNKAIDO] DROP COLUMN [C_TULAJDONOSID];


GO
PRINT N'Altering Table [dbo].[T_ORARENDIORA]...';


GO
ALTER TABLE [dbo].[T_ORARENDIORA] DROP COLUMN [C_ORATULAJDONOSID];


GO
PRINT N'Altering Table [dbo].[T_TEREM]...';


GO
ALTER TABLE [dbo].[T_TEREM] ALTER COLUMN [C_MUKODESIHELYID] INT NULL;


GO
ALTER TABLE [dbo].[T_TEREM]
    ADD [C_DUALISHELYSZINTIPUSID] INT NULL;


GO
PRINT N'Creating Index [dbo].[T_TEREM].[NCI_Terem_MukodesiHelyId]...';


GO
CREATE NONCLUSTERED INDEX [NCI_Terem_MukodesiHelyId]
    ON [dbo].[T_TEREM]([C_MUKODESIHELYID] ASC);


GO
PRINT N'Creating Index [dbo].[T_TEREM].[NCI_Terem_ElozotanevirekordId_IntezmenyId]...';


GO
CREATE NONCLUSTERED INDEX [NCI_Terem_ElozotanevirekordId_IntezmenyId]
    ON [dbo].[T_TEREM]([ELOZOTANEVIREKORDID] ASC, [C_INTEZMENYID] ASC)
    INCLUDE([C_JELLEG], [C_NEV], [C_TEREMFELELOSID], [C_MUKODESIHELYID], [SERIAL], [TOROLT]) WITH (FILLFACTOR = 80);


GO
PRINT N'Creating Index [dbo].[T_TEREM].[NCI_Terem_IntezmenyId_TanevId_DualisHelyszinTipusId]...';


GO
CREATE NONCLUSTERED INDEX [NCI_Terem_IntezmenyId_TanevId_DualisHelyszinTipusId]
    ON [dbo].[T_TEREM]([C_INTEZMENYID] ASC, [C_TANEVID] ASC, [C_DUALISHELYSZINTIPUSID] ASC);


GO
PRINT N'Creating Table [dbo].[T_DUALISHELYSZINTIPUS]...';


GO
CREATE TABLE [dbo].[T_DUALISHELYSZINTIPUS] (
    [ID]                   INT      NOT NULL,
    [C_ALINTEZMENYID]      INT      NOT NULL,
    [C_ALTANEVID]          INT      NOT NULL,
    [TOROLT]               CHAR (1) NOT NULL,
    [SERIAL]               INT      NOT NULL,
    [LASTCHANGED]          DATETIME NOT NULL,
    [CREATED]              DATETIME NOT NULL,
    [MODIFIER]             INT      NULL,
    [CREATOR]              INT      NULL,
    [ELOZOTANEVIREKORDID]  INT      NULL,
    [ELOZOTANEVIREKORDIDA] INT      NULL,
    CONSTRAINT [PK_DualisHelyszinTipus] PRIMARY KEY CLUSTERED ([ID] ASC, [C_ALINTEZMENYID] ASC, [C_ALTANEVID] ASC)
);


GO
PRINT N'Creating Index [dbo].[T_DUALISHELYSZINTIPUS].[NCI_DualisHelyszinTipus_AlIntezmenyId]...';


GO
CREATE NONCLUSTERED INDEX [NCI_DualisHelyszinTipus_AlIntezmenyId]
    ON [dbo].[T_DUALISHELYSZINTIPUS]([C_ALINTEZMENYID] ASC);


GO
PRINT N'Creating Index [dbo].[T_DUALISHELYSZINTIPUS].[NCI_DualisHelyszinTipus_AlTanevId]...';


GO
CREATE NONCLUSTERED INDEX [NCI_DualisHelyszinTipus_AlTanevId]
    ON [dbo].[T_DUALISHELYSZINTIPUS]([C_ALTANEVID] ASC);


GO
PRINT N'Creating Default Constraint unnamed constraint on [dbo].[T_DUALISHELYSZINTIPUS]...';


GO
ALTER TABLE [dbo].[T_DUALISHELYSZINTIPUS]
    ADD DEFAULT ((0)) FOR [SERIAL];


GO
PRINT N'Creating Default Constraint unnamed constraint on [dbo].[T_DUALISHELYSZINTIPUS]...';


GO
ALTER TABLE [dbo].[T_DUALISHELYSZINTIPUS]
    ADD DEFAULT ('F') FOR [TOROLT];


GO
PRINT N'Creating Default Constraint unnamed constraint on [dbo].[T_DUALISHELYSZINTIPUS]...';


GO
ALTER TABLE [dbo].[T_DUALISHELYSZINTIPUS]
    ADD DEFAULT (getdate()) FOR [CREATED];


GO
PRINT N'Creating Default Constraint unnamed constraint on [dbo].[T_DUALISHELYSZINTIPUS]...';


GO
ALTER TABLE [dbo].[T_DUALISHELYSZINTIPUS]
    ADD DEFAULT (getdate()) FOR [LASTCHANGED];


GO
PRINT N'Creating Foreign Key [dbo].[FK_Terem_MukodesiHelyId_REF_MukodesiHely]...';


GO
ALTER TABLE [dbo].[T_TEREM] WITH NOCHECK
    ADD CONSTRAINT [FK_Terem_MukodesiHelyId_REF_MukodesiHely] FOREIGN KEY ([C_MUKODESIHELYID]) REFERENCES [dbo].[T_MUKODESIHELY] ([ID]);


GO
PRINT N'Creating Foreign Key [dbo].[FK_DualisHelyszinTipus_Id_AlintezmenyId_AltanevId_REF_DictionaryItemBase]...';


GO
ALTER TABLE [dbo].[T_DUALISHELYSZINTIPUS] WITH NOCHECK
    ADD CONSTRAINT [FK_DualisHelyszinTipus_Id_AlintezmenyId_AltanevId_REF_DictionaryItemBase] FOREIGN KEY ([ID], [C_ALINTEZMENYID], [C_ALTANEVID]) REFERENCES [dbo].[T_DICTIONARYITEMBASE] ([ID], [C_INTEZMENYID], [C_TANEVID]);


GO
PRINT N'Creating Foreign Key [dbo].[FK_DualisHelyszinTipus_AlIntezmenyId_REF_Intezmeny]...';


GO
ALTER TABLE [dbo].[T_DUALISHELYSZINTIPUS] WITH NOCHECK
    ADD CONSTRAINT [FK_DualisHelyszinTipus_AlIntezmenyId_REF_Intezmeny] FOREIGN KEY ([C_ALINTEZMENYID]) REFERENCES [dbo].[T_INTEZMENY] ([ID]);


GO
PRINT N'Creating Foreign Key [dbo].[FK_DualisHelyszinTipus_AlTanevId_REF_Tanev]...';


GO
ALTER TABLE [dbo].[T_DUALISHELYSZINTIPUS] WITH NOCHECK
    ADD CONSTRAINT [FK_DualisHelyszinTipus_AlTanevId_REF_Tanev] FOREIGN KEY ([C_ALTANEVID]) REFERENCES [dbo].[T_TANEV] ([ID]);


GO
PRINT N'Creating Foreign Key [dbo].[FK_Terem_DualisHelyszinTipusId_IntezmenyId_TanevId_REF_DualisHelyszinTipus]...';


GO
ALTER TABLE [dbo].[T_TEREM] WITH NOCHECK
    ADD CONSTRAINT [FK_Terem_DualisHelyszinTipusId_IntezmenyId_TanevId_REF_DualisHelyszinTipus] FOREIGN KEY ([C_DUALISHELYSZINTIPUSID], [C_INTEZMENYID], [C_TANEVID]) REFERENCES [dbo].[T_DUALISHELYSZINTIPUS] ([ID], [C_ALINTEZMENYID], [C_ALTANEVID]);


GO
PRINT N'Creating Extended Property [dbo].[T_DUALISHELYSZINTIPUS].[ID].[MS_Description]...';


GO
EXECUTE dev.uspAddOrUpdateExtendedProperty @name = N'MS_Description', @value = N'A rekord egyedi azonosítója, generált IDENTITY(1, 1) (kivéve altáblák esetén, ahol a főtábla ID-ját kapja)', @level0type = N'SCHEMA', @level0name = N'dbo', @level1type = N'TABLE', @level1name = N'T_DUALISHELYSZINTIPUS', @level2type = N'COLUMN', @level2name = N'ID';


GO
PRINT N'Creating Extended Property [dbo].[T_DUALISHELYSZINTIPUS].[C_ALINTEZMENYID].[MS_Description]...';


GO
EXECUTE dev.uspAddOrUpdateExtendedProperty @name = N'MS_Description', @value = N'Az intézmény ID-ja, amihez a rekord tartozik (altábla esetén)', @level0type = N'SCHEMA', @level0name = N'dbo', @level1type = N'TABLE', @level1name = N'T_DUALISHELYSZINTIPUS', @level2type = N'COLUMN', @level2name = N'C_ALINTEZMENYID';


GO
PRINT N'Creating Extended Property [dbo].[T_DUALISHELYSZINTIPUS].[C_ALTANEVID].[MS_Description]...';


GO
EXECUTE dev.uspAddOrUpdateExtendedProperty @name = N'MS_Description', @value = N'A tanév ID-ja, amihez a rekord tartozik (altábla esetén)', @level0type = N'SCHEMA', @level0name = N'dbo', @level1type = N'TABLE', @level1name = N'T_DUALISHELYSZINTIPUS', @level2type = N'COLUMN', @level2name = N'C_ALTANEVID';


GO
PRINT N'Creating Extended Property [dbo].[T_DUALISHELYSZINTIPUS].[TOROLT].[MS_Description]...';


GO
EXECUTE dev.uspAddOrUpdateExtendedProperty @name = N'MS_Description', @value = N'A rekord logikai töröltségét jelző flag', @level0type = N'SCHEMA', @level0name = N'dbo', @level1type = N'TABLE', @level1name = N'T_DUALISHELYSZINTIPUS', @level2type = N'COLUMN', @level2name = N'TOROLT';


GO
PRINT N'Creating Extended Property [dbo].[T_DUALISHELYSZINTIPUS].[SERIAL].[MS_Description]...';


GO
EXECUTE dev.uspAddOrUpdateExtendedProperty @name = N'MS_Description', @value = N'Minden C# kódból történő módosítás esetén eggyel nő az értéke. Hogy párhuzamos módosítás esetnán ne íródjanak felül az adatok', @level0type = N'SCHEMA', @level0name = N'dbo', @level1type = N'TABLE', @level1name = N'T_DUALISHELYSZINTIPUS', @level2type = N'COLUMN', @level2name = N'SERIAL';


GO
PRINT N'Creating Extended Property [dbo].[T_DUALISHELYSZINTIPUS].[LASTCHANGED].[MS_Description]...';


GO
EXECUTE dev.uspAddOrUpdateExtendedProperty @name = N'MS_Description', @value = N'  A rekord utolsó módosításának időpontja', @level0type = N'SCHEMA', @level0name = N'dbo', @level1type = N'TABLE', @level1name = N'T_DUALISHELYSZINTIPUS', @level2type = N'COLUMN', @level2name = N'LASTCHANGED';


GO
PRINT N'Creating Extended Property [dbo].[T_DUALISHELYSZINTIPUS].[CREATED].[MS_Description]...';


GO
EXECUTE dev.uspAddOrUpdateExtendedProperty @name = N'MS_Description', @value = N'A rekord létrehozásának időpontja ', @level0type = N'SCHEMA', @level0name = N'dbo', @level1type = N'TABLE', @level1name = N'T_DUALISHELYSZINTIPUS', @level2type = N'COLUMN', @level2name = N'CREATED';


GO
PRINT N'Creating Extended Property [dbo].[T_DUALISHELYSZINTIPUS].[MODIFIER].[MS_Description]...';


GO
EXECUTE dev.uspAddOrUpdateExtendedProperty @name = N'MS_Description', @value = N'A rekordot utoljára módosító felhasználó ID-ja', @level0type = N'SCHEMA', @level0name = N'dbo', @level1type = N'TABLE', @level1name = N'T_DUALISHELYSZINTIPUS', @level2type = N'COLUMN', @level2name = N'MODIFIER';


GO
PRINT N'Creating Extended Property [dbo].[T_DUALISHELYSZINTIPUS].[CREATOR].[MS_Description]...';


GO
EXECUTE dev.uspAddOrUpdateExtendedProperty @name = N'MS_Description', @value = N'A rekordot létrehozó felhasználó ID-ja', @level0type = N'SCHEMA', @level0name = N'dbo', @level1type = N'TABLE', @level1name = N'T_DUALISHELYSZINTIPUS', @level2type = N'COLUMN', @level2name = N'CREATOR';


GO
PRINT N'Creating Extended Property [dbo].[T_DUALISHELYSZINTIPUS].[ELOZOTANEVIREKORDID].[MS_Description]...';


GO
EXECUTE dev.uspAddOrUpdateExtendedProperty @name = N'MS_Description', @value = N'A rekord előző tanévben lévő ID-ja', @level0type = N'SCHEMA', @level0name = N'dbo', @level1type = N'TABLE', @level1name = N'T_DUALISHELYSZINTIPUS', @level2type = N'COLUMN', @level2name = N'ELOZOTANEVIREKORDID';


GO
PRINT N'Creating Extended Property [dbo].[T_DUALISHELYSZINTIPUS].[ELOZOTANEVIREKORDIDA].[MS_Description]...';


GO
EXECUTE dev.uspAddOrUpdateExtendedProperty @name = N'MS_Description', @value = N'A rekord előző tanévben lévő ID-ja (altábla esetén) ', @level0type = N'SCHEMA', @level0name = N'dbo', @level1type = N'TABLE', @level1name = N'T_DUALISHELYSZINTIPUS', @level2type = N'COLUMN', @level2name = N'ELOZOTANEVIREKORDIDA';


GO
PRINT N'Creating Extended Property [dbo].[T_TEREM].[C_DUALISHELYSZINTIPUSID].[MS_Description]...';


GO
EXECUTE dev.uspAddOrUpdateExtendedProperty @name = N'MS_Description', @value = N'Helyszín típusa', @level0type = N'SCHEMA', @level0name = N'dbo', @level1type = N'TABLE', @level1name = N'T_TEREM', @level2type = N'COLUMN', @level2name = N'C_DUALISHELYSZINTIPUSID';

GO
PRINT N'Checking existing data against newly created constraints';



GO
ALTER TABLE [dbo].[T_TEREM] WITH CHECK CHECK CONSTRAINT [FK_Terem_MukodesiHelyId_REF_MukodesiHely];

ALTER TABLE [dbo].[T_DUALISHELYSZINTIPUS] WITH CHECK CHECK CONSTRAINT [FK_DualisHelyszinTipus_Id_AlintezmenyId_AltanevId_REF_DictionaryItemBase];

ALTER TABLE [dbo].[T_DUALISHELYSZINTIPUS] WITH CHECK CHECK CONSTRAINT [FK_DualisHelyszinTipus_AlIntezmenyId_REF_Intezmeny];

ALTER TABLE [dbo].[T_DUALISHELYSZINTIPUS] WITH CHECK CHECK CONSTRAINT [FK_DualisHelyszinTipus_AlTanevId_REF_Tanev];

ALTER TABLE [dbo].[T_TEREM] WITH CHECK CHECK CONSTRAINT [FK_Terem_DualisHelyszinTipusId_IntezmenyId_TanevId_REF_DualisHelyszinTipus];

EXEC dev.uspCreateSchemaViews 'T_TEREM, T_DUALISHELYSZINTIPUS, T_NEMKOTOTTMUNKAIDO, T_ORARENDIORA'
EXEC dev.sp_Global_GenerateAsyncAuditTriggerAll
GO
PRINT N'Update complete.';


GO
