CREATE TABLE [TOEPASBAREREGELBESTAND] (
    [TOEPASBAREREGELBESTAND_ID] INT PRIMARY KEY,
    [DATUMEINDEGELDIGHEID] DATE,
    [DATUMSTART] DATE
);

CREATE TABLE [ACTIVITEIT] (
    [ACTIVITEIT_ID] INT PRIMARY KEY,
    [BOVENLIGGENDE_ACTIVITEIT_ACTIVITEIT_ID] INT,
    [GERELATEERDE_ACTIVITEIT_ACTIVITEIT_ID] INT,
    [GROEP] VARCHAR(200),
    [NAAM] VARCHAR(200),
    [NEN3610ID] VARCHAR(80)
);

CREATE TABLE [JURIDISCHE_REGEL] (
    [JURIDISCHE_REGEL_ID] INT PRIMARY KEY,
    [DATUMBEKEND] DATE,
    [DATUMEINDEGELDIGHEID] DATE,
    [DATUMINWERKING] DATE,
    [DATUMSTART] DATE,
    [OMSCHRIJVING] NVARCHAR(MAX),
    [REGELTEKST] VARCHAR(200),
    [THEMA] VARCHAR(200)
);

CREATE TABLE [LOCATIE] (
    [LOCATIE_ID] INT PRIMARY KEY,
    [HOOGTE] INT,
    [NAAM] VARCHAR(200),
    [NEN3610ID] VARCHAR(80)
);

CREATE TABLE [TOEPASBARE_REGEL] (
    [TOEPASBARE_REGEL_ID] INT PRIMARY KEY,
    [BETREFT_ACTIVITEIT_ID] INT,
    [DATUMBEGINGELDIGHEID] DATE,
    [DATUMEINDEGELDIGHEID] DATE,
    [DOMEIN] VARCHAR(255),
    [HEEFT_TOEPASBAREREGELBESTAND_ID] INT,
    [NAAM] VARCHAR(200),
    [OMSCHRIJVING] NVARCHAR(MAX),
    [SOORTAANSLUITPUNT] VARCHAR(200),
    [TOESTEMMING] VARCHAR(200)
);

CREATE TABLE [KP_JRDSCH_RGL_ACTVTT] (
    [KP_JRDSCH_RGL_ACTVTT_ID] INT PRIMARY KEY,
    [JURIDISCHE_REGEL_ID] INT,
    [ACTIVITEIT_ID] INT
);

CREATE TABLE [KP_ACTVTT_LCT] (
    [KP_ACTVTT_LCT_ID] INT PRIMARY KEY,
    [ACTIVITEIT_ID] INT,
    [LOCATIE_ID] INT
);

CREATE TABLE [KP_JRDSCH_RGL_LCT] (
    [KP_JRDSCH_RGL_LCT_ID] INT PRIMARY KEY,
    [JURIDISCHE_REGEL_ID] INT,
    [LOCATIE_ID] INT
);

CREATE TABLE [MAATREGELEN] (
    [TOEPASBARE_REGEL_ID] INT PRIMARY KEY
);

CREATE TABLE [KP_TPSBR_RGL_LCT] (
    [KP_TPSBR_RGL_LCT_ID] INT PRIMARY KEY,
    [TOEPASBARE_REGEL_ID] INT,
    [LOCATIE_ID] INT
);

CREATE TABLE [CONCLUSIE] (
    [TOEPASBARE_REGEL_ID] INT PRIMARY KEY
);

CREATE TABLE [INDIENINGSVEREISTEN] (
    [TOEPASBARE_REGEL_ID] INT PRIMARY KEY
);

CREATE TABLE [KP_TPSBR_RGL_JRDSCH_RGL] (
    [KP_TPSBR_RGL_JRDSCH_RGL_ID] INT PRIMARY KEY,
    [TOEPASBARE_REGEL_ID] INT,
    [JURIDISCHE_REGEL_ID] INT
);

CREATE TABLE [UITVOERINGSREGEL] (
    [UITVOERINGSREGEL_ID] INT PRIMARY KEY,
    [BEVAT_TOEPASBAREREGELBESTAND_ID] INT,
    [HEEFT_TOEPASBARE_REGEL_ID] INT,
    [NAAM] VARCHAR(200),
    [OMSCHRIJVING] NVARCHAR(MAX),
    [REGEL] NVARCHAR(MAX)
);

CREATE TABLE [KP_MTRGLN_TPSBR_RGL] (
    [KP_MTRGLN_TPSBR_RGL_ID] INT PRIMARY KEY,
    [MAATREGELEN_ID] INT,
    [TOEPASBARE_REGEL_ID] INT
);

CREATE TABLE [KP_CNCLS_TPSBR_RGL] (
    [KP_CNCLS_TPSBR_RGL_ID] INT PRIMARY KEY,
    [CONCLUSIE_ID] INT,
    [TOEPASBARE_REGEL_ID] INT
);

CREATE TABLE [KP_INDNNGSVRSTN_TPSBR_RGL] (
    [KP_INDNNGSVRSTN_TPSBR_RGL_ID] INT PRIMARY KEY,
    [INDIENINGSVEREISTEN_ID] INT,
    [TOEPASBARE_REGEL_ID] INT
);

ALTER TABLE [CONCLUSIE] ADD CONSTRAINT FK_CONCLUSIE_GEN_TOEPASBARE_REGEL FOREIGN KEY ([TOEPASBARE_REGEL_ID]) REFERENCES [TOEPASBARE_REGEL] ([TOEPASBARE_REGEL_ID]);
-- Generalization to TOEPASBARE_REGEL

ALTER TABLE [INDIENINGSVEREISTEN] ADD CONSTRAINT FK_INDIENINGSVEREISTEN_GEN_TOEPASBARE_REGEL FOREIGN KEY ([TOEPASBARE_REGEL_ID]) REFERENCES [TOEPASBARE_REGEL] ([TOEPASBARE_REGEL_ID]);
-- Generalization to TOEPASBARE_REGEL

ALTER TABLE [MAATREGELEN] ADD CONSTRAINT FK_MAATREGELEN_GEN_TOEPASBARE_REGEL FOREIGN KEY ([TOEPASBARE_REGEL_ID]) REFERENCES [TOEPASBARE_REGEL] ([TOEPASBARE_REGEL_ID]);
-- Generalization to TOEPASBARE_REGEL

ALTER TABLE [KP_TPSBR_RGL_LCT] ADD CONSTRAINT FK_KP_TPSBR_RGL_LCT_1 FOREIGN KEY ([TOEPASBARE_REGEL_ID]) REFERENCES [TOEPASBARE_REGEL] ([TOEPASBARE_REGEL_ID]);
-- TOEPASBARE_REGEL (many-to-many junction, Connector_ID: 653)

ALTER TABLE [KP_TPSBR_RGL_LCT] ADD CONSTRAINT FK_KP_TPSBR_RGL_LCT_2 FOREIGN KEY ([LOCATIE_ID]) REFERENCES [LOCATIE] ([LOCATIE_ID]);
-- LOCATIE (many-to-many junction, Connector_ID: 653)

ALTER TABLE [KP_MTRGLN_TPSBR_RGL] ADD CONSTRAINT FK_KP_MTRGLN_TPSBR_RGL_1 FOREIGN KEY ([MAATREGELEN_ID]) REFERENCES [MAATREGELEN] ([TOEPASBARE_REGEL_ID]);
-- MAATREGELEN (many-to-many junction, Connector_ID: 654)

ALTER TABLE [KP_MTRGLN_TPSBR_RGL] ADD CONSTRAINT FK_KP_MTRGLN_TPSBR_RGL_2 FOREIGN KEY ([TOEPASBARE_REGEL_ID]) REFERENCES [TOEPASBARE_REGEL] ([TOEPASBARE_REGEL_ID]);
-- TOEPASBARE_REGEL (many-to-many junction, Connector_ID: 654)

ALTER TABLE [KP_INDNNGSVRSTN_TPSBR_RGL] ADD CONSTRAINT FK_KP_INDNNGSVRSTN_TPSBR_RGL_1 FOREIGN KEY ([INDIENINGSVEREISTEN_ID]) REFERENCES [INDIENINGSVEREISTEN] ([TOEPASBARE_REGEL_ID]);
-- INDIENINGSVEREISTEN (many-to-many junction, Connector_ID: 655)

ALTER TABLE [KP_INDNNGSVRSTN_TPSBR_RGL] ADD CONSTRAINT FK_KP_INDNNGSVRSTN_TPSBR_RGL_2 FOREIGN KEY ([TOEPASBARE_REGEL_ID]) REFERENCES [TOEPASBARE_REGEL] ([TOEPASBARE_REGEL_ID]);
-- TOEPASBARE_REGEL (many-to-many junction, Connector_ID: 655)

ALTER TABLE [KP_CNCLS_TPSBR_RGL] ADD CONSTRAINT FK_KP_CNCLS_TPSBR_RGL_1 FOREIGN KEY ([CONCLUSIE_ID]) REFERENCES [CONCLUSIE] ([TOEPASBARE_REGEL_ID]);
-- CONCLUSIE (many-to-many junction, Connector_ID: 656)

ALTER TABLE [KP_CNCLS_TPSBR_RGL] ADD CONSTRAINT FK_KP_CNCLS_TPSBR_RGL_2 FOREIGN KEY ([TOEPASBARE_REGEL_ID]) REFERENCES [TOEPASBARE_REGEL] ([TOEPASBARE_REGEL_ID]);
-- TOEPASBARE_REGEL (many-to-many junction, Connector_ID: 656)

ALTER TABLE [KP_TPSBR_RGL_JRDSCH_RGL] ADD CONSTRAINT FK_KP_TPSBR_RGL_JRDSCH_RGL_1 FOREIGN KEY ([TOEPASBARE_REGEL_ID]) REFERENCES [TOEPASBARE_REGEL] ([TOEPASBARE_REGEL_ID]);
-- TOEPASBARE_REGEL (many-to-many junction, Connector_ID: 667)

ALTER TABLE [KP_TPSBR_RGL_JRDSCH_RGL] ADD CONSTRAINT FK_KP_TPSBR_RGL_JRDSCH_RGL_2 FOREIGN KEY ([JURIDISCHE_REGEL_ID]) REFERENCES [JURIDISCHE_REGEL] ([JURIDISCHE_REGEL_ID]);
-- JURIDISCHE_REGEL (many-to-many junction, Connector_ID: 667)

ALTER TABLE [KP_JRDSCH_RGL_LCT] ADD CONSTRAINT FK_KP_JRDSCH_RGL_LCT_1 FOREIGN KEY ([JURIDISCHE_REGEL_ID]) REFERENCES [JURIDISCHE_REGEL] ([JURIDISCHE_REGEL_ID]);
-- JURIDISCHE_REGEL (many-to-many junction, Connector_ID: 668)

ALTER TABLE [KP_JRDSCH_RGL_LCT] ADD CONSTRAINT FK_KP_JRDSCH_RGL_LCT_2 FOREIGN KEY ([LOCATIE_ID]) REFERENCES [LOCATIE] ([LOCATIE_ID]);
-- LOCATIE (many-to-many junction, Connector_ID: 668)

ALTER TABLE [KP_JRDSCH_RGL_ACTVTT] ADD CONSTRAINT FK_KP_JRDSCH_RGL_ACTVTT_1 FOREIGN KEY ([JURIDISCHE_REGEL_ID]) REFERENCES [JURIDISCHE_REGEL] ([JURIDISCHE_REGEL_ID]);
-- JURIDISCHE_REGEL (many-to-many junction, Connector_ID: 677)

ALTER TABLE [KP_JRDSCH_RGL_ACTVTT] ADD CONSTRAINT FK_KP_JRDSCH_RGL_ACTVTT_2 FOREIGN KEY ([ACTIVITEIT_ID]) REFERENCES [ACTIVITEIT] ([ACTIVITEIT_ID]);
-- ACTIVITEIT (many-to-many junction, Connector_ID: 677)

ALTER TABLE [KP_ACTVTT_LCT] ADD CONSTRAINT FK_KP_ACTVTT_LCT_1 FOREIGN KEY ([ACTIVITEIT_ID]) REFERENCES [ACTIVITEIT] ([ACTIVITEIT_ID]);
-- ACTIVITEIT (many-to-many junction, Connector_ID: 680)

ALTER TABLE [KP_ACTVTT_LCT] ADD CONSTRAINT FK_KP_ACTVTT_LCT_2 FOREIGN KEY ([LOCATIE_ID]) REFERENCES [LOCATIE] ([LOCATIE_ID]);
-- LOCATIE (many-to-many junction, Connector_ID: 680)

ALTER TABLE [UITVOERINGSREGEL] ADD CONSTRAINT FK_UITVOERINGSREGEL_BEVAT FOREIGN KEY ([BEVAT_TOEPASBAREREGELBESTAND_ID]) REFERENCES [TOEPASBAREREGELBESTAND] ([TOEPASBAREREGELBESTAND_ID]);
-- BEVAT (1 to 0..*, Connector_ID: 650)

ALTER TABLE [TOEPASBARE_REGEL] ADD CONSTRAINT FK_TOEPASBARE_REGEL_HEEFT FOREIGN KEY ([HEEFT_TOEPASBAREREGELBESTAND_ID]) REFERENCES [TOEPASBAREREGELBESTAND] ([TOEPASBAREREGELBESTAND_ID]);
-- HEEFT (0..* to 1, Connector_ID: 651)

ALTER TABLE [UITVOERINGSREGEL] ADD CONSTRAINT FK_UITVOERINGSREGEL_HEEFT FOREIGN KEY ([HEEFT_TOEPASBARE_REGEL_ID]) REFERENCES [TOEPASBARE_REGEL] ([TOEPASBARE_REGEL_ID]);
-- HEEFT (1 to 0..*, Connector_ID: 652)

ALTER TABLE [TOEPASBARE_REGEL] ADD CONSTRAINT FK_TOEPASBARE_REGEL_BETREFT FOREIGN KEY ([BETREFT_ACTIVITEIT_ID]) REFERENCES [ACTIVITEIT] ([ACTIVITEIT_ID]);
-- BETREFT (0..* to 1, Connector_ID: 679)

ALTER TABLE [ACTIVITEIT] ADD CONSTRAINT FK_ACTIVITEIT_BOVENLIGGENDE_ACTIVITEIT FOREIGN KEY ([BOVENLIGGENDE_ACTIVITEIT_ACTIVITEIT_ID]) REFERENCES [ACTIVITEIT] ([ACTIVITEIT_ID]);
-- BOVENLIGGENDE_ACTIVITEIT (1 to 0..1, Connector_ID: 681)

ALTER TABLE [ACTIVITEIT] ADD CONSTRAINT FK_ACTIVITEIT_GERELATEERDE_ACTIVITEIT FOREIGN KEY ([GERELATEERDE_ACTIVITEIT_ACTIVITEIT_ID]) REFERENCES [ACTIVITEIT] ([ACTIVITEIT_ID]);
-- GERELATEERDE_ACTIVITEIT (1 to 0..1, Connector_ID: 682)

