﻿GO
SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, CONCAT_NULL_YIELDS_NULL, QUOTED_IDENTIFIER ON;

SET NUMERIC_ROUNDABORT OFF;


GO

--The column [dbo].[T_FOGLALKOZAS].[C_TANAROKID] on table [dbo].[T_FOGLALKOZAS] must be added, but the column has no default value and does not allow NULL values. If the table contains data, the ALTER script will not work. To avoid this issue you must either: add a default value to the column, mark it as allowing NULL values, or enable the generation of smart-defaults as a deployment option.


--IF EXISTS (select top 1 1 from [dbo].[T_FOGLALKOZAS])
--    RAISERROR (N'Rows were detected. The schema update is terminating because data loss might occur.', 16, 127) WITH NOWAIT

--GO

--Table [dbo].[T_FOGLALKOZASOK_TANAROK] is being dropped.  Deployment will halt if the table contains data.


--IF EXISTS (select top 1 1 from [dbo].[T_FOGLALKOZASOK_TANAROK])
--    RAISERROR (N'Rows were detected. The schema update is terminating because data loss might occur.', 16, 127) WITH NOWAIT


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


IF EXISTS (SELECT 1 FROM sys.foreign_keys where parent_object_id = OBJECT_ID(N'dbo.T_ORARENDIORA') AND name = 'FK_4025554_4025552')
BEGIN
	ALTER TABLE [dbo].[T_ORARENDIORA] DROP CONSTRAINT [FK_4025554_4025552];
END


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


GO
ALTER TABLE [dbo].[T_TANULOTARGYMULASZTASSTATISZT] DROP CONSTRAINT [FK_4026077_4026075];


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


GO
ALTER TABLE [dbo].[T_TEREM_FOGLALKOZAS] DROP CONSTRAINT [FK_4025949_4025947];


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


GO
ALTER TABLE [dbo].[T_FOGLALKOZAS] DROP CONSTRAINT [FK_4025422_4025423];


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


GO
ALTER TABLE [dbo].[T_FOGLALKOZAS] DROP CONSTRAINT [FK_4025428_4025426];


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


GO
ALTER TABLE [dbo].[T_FOGLALKOZAS] DROP CONSTRAINT [FK_4025431_4025429];


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


GO
ALTER TABLE [dbo].[T_FOGLALKOZAS] DROP CONSTRAINT [FK_402542202_402542200];


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


GO
ALTER TABLE [dbo].[T_FOGLALKOZAS] DROP CONSTRAINT [FK_402542205_402542203];


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


GO
ALTER TABLE [dbo].[T_FOGLALKOZASOK_TANAROK] DROP CONSTRAINT [FK_4025436_4025435];


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


GO
ALTER TABLE [dbo].[T_TANITASIORA] DROP CONSTRAINT [FK_4025623_4025622];


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


GO
ALTER TABLE [dbo].[T_FOGLALKOZASOK_TANAROK] DROP CONSTRAINT [FK_4025437_4025435];


GO
PRINT N'Starting rebuilding table [dbo].[T_FOGLALKOZAS]...';


GO
BEGIN TRANSACTION;

SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;

SET XACT_ABORT ON;

CREATE TABLE [dbo].[tmp_ms_xx_T_FOGLALKOZAS] (
    [ID]                  INT             IDENTITY (1, 1) NOT NULL,
    [C_FOGLALKOZASTIPUSA] INT             NOT NULL,
    [C_ORASZAM]           NUMERIC (10, 2) NOT NULL,
    [C_NEV]               NVARCHAR (255)  NULL,
    [C_ERTEKELESKELL]     CHAR (1)        DEFAULT ('F') NULL,
    [C_MULASZTASKELL]     CHAR (1)        DEFAULT ('F') NULL,
    [C_TANARFELVEHETI]    CHAR (1)        DEFAULT ('T') NULL,
    [C_IMPORTALT]         CHAR (1)        DEFAULT ('F') NULL,
    [C_TANTARGYID]        INT             NULL,
    [C_OSZTALYCSOPORTID]  INT             NULL,
    [C_TANAROKID]         INT             NOT NULL,
    [C_INTEZMENYID]       INT             NOT NULL,
    [C_TANEVID]           INT             NOT NULL,
    [TOROLT]              CHAR (1)        DEFAULT ('F') NOT NULL,
    [SERIAL]              INT             DEFAULT ((0)) NOT NULL,
    [LASTCHANGED]         DATETIME        DEFAULT (getdate()) NOT NULL,
    [CREATED]             DATETIME        DEFAULT (getdate()) NOT NULL,
    [MODIFIER]            INT             NULL,
    [CREATOR]             INT             NULL,
    [ELOZOTANEVIREKORDID] INT             NULL,
    [NNID]                INT             NULL,
    CONSTRAINT [tmp_ms_xx_constraint_PK_T_40254221] PRIMARY KEY CLUSTERED ([ID] ASC)
);

IF EXISTS (SELECT TOP 1 1 
           FROM   [dbo].[T_FOGLALKOZAS])
    BEGIN
        SET IDENTITY_INSERT [dbo].[tmp_ms_xx_T_FOGLALKOZAS] ON;
        INSERT INTO [dbo].[tmp_ms_xx_T_FOGLALKOZAS] ([ID], [C_FOGLALKOZASTIPUSA], [C_ORASZAM], [C_NEV], [C_ERTEKELESKELL], [C_MULASZTASKELL], [C_TANARFELVEHETI], [C_IMPORTALT], [C_TANTARGYID], [C_OSZTALYCSOPORTID], [C_TANAROKID], [C_INTEZMENYID], [C_TANEVID], [TOROLT], [SERIAL], [LASTCHANGED], [CREATED], [MODIFIER], [CREATOR], [ELOZOTANEVIREKORDID], [NNID])
        SELECT   [ID],
                 [C_FOGLALKOZASTIPUSA],
                 [C_ORASZAM],
                 [C_NEV],
                 [C_ERTEKELESKELL],
                 [C_MULASZTASKELL],
                 [C_TANARFELVEHETI],
                 [C_IMPORTALT],
                 [C_TANTARGYID],
                 [C_OSZTALYCSOPORTID],
				 ft.[C_TANAROKID],
                 f.[C_INTEZMENYID],
                 f.[C_TANEVID],
                 f.[TOROLT],
                 f.[SERIAL],
                 f.[LASTCHANGED],
                 f.[CREATED],
                 f.[MODIFIER],
                 f.[CREATOR],
                 f.[ELOZOTANEVIREKORDID],
                 f.[NNID]
        FROM     [dbo].[T_FOGLALKOZAS] f
		  INNER JOIN T_FOGLALKOZASOK_TANAROK ft ON ft.C_FOGLALKOZASOKID = ID
        ORDER BY [ID] ASC;
        SET IDENTITY_INSERT [dbo].[tmp_ms_xx_T_FOGLALKOZAS] OFF;
    END

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


GO
DROP TABLE [dbo].[T_FOGLALKOZASOK_TANAROK];


DROP TABLE [dbo].[T_FOGLALKOZAS];

EXECUTE sp_rename N'[dbo].[tmp_ms_xx_T_FOGLALKOZAS]', N'T_FOGLALKOZAS';

EXECUTE sp_rename N'[dbo].[tmp_ms_xx_constraint_PK_T_40254221]', N'PK_T_4025422', N'OBJECT';

COMMIT TRANSACTION;

SET TRANSACTION ISOLATION LEVEL READ COMMITTED;


GO
PRINT N'Creating [dbo].[T_FOGLALKOZAS].[I_T_4025422_4025423]...';


GO
CREATE NONCLUSTERED INDEX [I_T_4025422_4025423]
    ON [dbo].[T_FOGLALKOZAS]([C_INTEZMENYID] ASC, [C_TANEVID] ASC, [C_FOGLALKOZASTIPUSA] ASC);


GO
PRINT N'Creating [dbo].[T_FOGLALKOZAS].[I_T_4025428_4025426]...';


GO
CREATE NONCLUSTERED INDEX [I_T_4025428_4025426]
    ON [dbo].[T_FOGLALKOZAS]([C_TANTARGYID] ASC);


GO
PRINT N'Creating [dbo].[T_FOGLALKOZAS].[I_T_4025431_4025429]...';


GO
CREATE NONCLUSTERED INDEX [I_T_4025431_4025429]
    ON [dbo].[T_FOGLALKOZAS]([C_OSZTALYCSOPORTID] ASC);


GO
PRINT N'Creating [dbo].[T_FOGLALKOZAS].[I_T_4025437_4025435]...';


GO
CREATE NONCLUSTERED INDEX [I_T_4025437_4025435]
    ON [dbo].[T_FOGLALKOZAS]([C_TANAROKID] ASC);


GO
PRINT N'Creating [dbo].[T_FOGLALKOZAS].[I_T_402542202_402542200]...';


GO
CREATE NONCLUSTERED INDEX [I_T_402542202_402542200]
    ON [dbo].[T_FOGLALKOZAS]([C_INTEZMENYID] ASC);


GO
PRINT N'Creating [dbo].[T_FOGLALKOZAS].[I_T_402542205_402542203]...';


GO
CREATE NONCLUSTERED INDEX [I_T_402542205_402542203]
    ON [dbo].[T_FOGLALKOZAS]([C_TANEVID] ASC);


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


GO
ALTER TABLE [dbo].[T_TANULOTARGYMULASZTASSTATISZT] WITH NOCHECK
    ADD CONSTRAINT [FK_4026077_4026075] FOREIGN KEY ([C_TARGYMULASZTASSTATISZTIKAIID]) REFERENCES [dbo].[T_FOGLALKOZAS] ([ID]);


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


GO
ALTER TABLE [dbo].[T_TEREM_FOGLALKOZAS] WITH NOCHECK
    ADD CONSTRAINT [FK_4025949_4025947] FOREIGN KEY ([C_FOGLALKOZASID]) REFERENCES [dbo].[T_FOGLALKOZAS] ([ID]);


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


GO
ALTER TABLE [dbo].[T_FOGLALKOZAS] WITH NOCHECK
    ADD CONSTRAINT [FK_4025422_4025423] FOREIGN KEY ([C_FOGLALKOZASTIPUSA], [C_INTEZMENYID], [C_TANEVID]) REFERENCES [dbo].[T_FOGLALKOZASTIPUS] ([ID], [C_ALINTEZMENYID], [C_ALTANEVID]);


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


GO
ALTER TABLE [dbo].[T_FOGLALKOZAS] WITH NOCHECK
    ADD CONSTRAINT [FK_4025428_4025426] FOREIGN KEY ([C_TANTARGYID]) REFERENCES [dbo].[T_TANTARGY] ([ID]);


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


GO
ALTER TABLE [dbo].[T_FOGLALKOZAS] WITH NOCHECK
    ADD CONSTRAINT [FK_4025431_4025429] FOREIGN KEY ([C_OSZTALYCSOPORTID]) REFERENCES [dbo].[T_OSZTALYCSOPORT] ([ID]);


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


GO
ALTER TABLE [dbo].[T_FOGLALKOZAS] WITH NOCHECK
    ADD CONSTRAINT [FK_402542202_402542200] FOREIGN KEY ([C_INTEZMENYID]) REFERENCES [dbo].[T_INTEZMENY] ([ID]);


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


GO
ALTER TABLE [dbo].[T_FOGLALKOZAS] WITH NOCHECK
    ADD CONSTRAINT [FK_402542205_402542203] FOREIGN KEY ([C_TANEVID]) REFERENCES [dbo].[T_TANEV] ([ID]);


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


GO
ALTER TABLE [dbo].[T_FOGLALKOZAS] WITH NOCHECK
    ADD CONSTRAINT [FK_4025437_4025435] FOREIGN KEY ([C_TANAROKID]) REFERENCES [dbo].[T_ALKALMAZOTT] ([ID]);


GO
PRINT N'Refreshing [dbo].[T_FOGLALKOZAS_OSSZES]...';


GO
EXECUTE sp_refreshsqlmodule N'[dbo].[T_FOGLALKOZAS_OSSZES]';


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


GO
ALTER TABLE [dbo].[T_TANULOTARGYMULASZTASSTATISZT] WITH CHECK CHECK CONSTRAINT [FK_4026077_4026075];

ALTER TABLE [dbo].[T_TEREM_FOGLALKOZAS] WITH CHECK CHECK CONSTRAINT [FK_4025949_4025947];

ALTER TABLE [dbo].[T_FOGLALKOZAS] WITH CHECK CHECK CONSTRAINT [FK_4025422_4025423];

ALTER TABLE [dbo].[T_FOGLALKOZAS] WITH CHECK CHECK CONSTRAINT [FK_4025428_4025426];

ALTER TABLE [dbo].[T_FOGLALKOZAS] WITH CHECK CHECK CONSTRAINT [FK_4025431_4025429];

ALTER TABLE [dbo].[T_FOGLALKOZAS] WITH CHECK CHECK CONSTRAINT [FK_402542202_402542200];

ALTER TABLE [dbo].[T_FOGLALKOZAS] WITH CHECK CHECK CONSTRAINT [FK_402542205_402542203];

ALTER TABLE [dbo].[T_FOGLALKOZAS] WITH CHECK CHECK CONSTRAINT [FK_4025437_4025435];


GO
PRINT N'Update complete.';


GO
