Table normalization 1nf

From CarlosCaPe/octorato · View interactive version

Table normalization 1nf is an AI agent skill from the GitHub repository CarlosCaPe/octorato. Table Normalization (1NF Violations)

How to use this skill

Skill file

Table Normalization (1NF Violations)

What

Breaking repeating column groups (e.g., PreferredDate1, PreferredDate2,

PreferredDate3) into properly normalized child tables with foreign keys.

This resolves First Normal Form (1NF) violations.

Why

Repeating columns create:

A normalized child table (ApplicantPreferredDates) with one row per preference

eliminates all of these problems.

How

Step 1: Identify the repeating group

-- Columns like: PreferredDate1_Start, PreferredDate1_End,
--               PreferredDate2_Start, PreferredDate2_End, ...
SELECT column_name FROM information_schema.columns
WHERE table_name = 'Applicant'
  AND column_name LIKE 'Preferred%'
ORDER BY ordinal_position;

Step 2: Create the child table

CREATE TABLE public."ApplicantPreferredDate" (
    "Id"            bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
    "ApplicantId"   bigint NOT NULL
        REFERENCES public."Applicant"("ApplicantId") ON DELETE CASCADE,
    "Preference"    smallint NOT NULL CHECK ("Preference" BETWEEN 1 AND 3),
    "StartDate"     timestamptz,
    "EndDate"       timestamptz,
    UNIQUE ("ApplicantId", "Preference")
);

Step 3: Migrate existing data

INSERT INTO public."ApplicantPreferredDate"
    ("ApplicantId", "Preference", "StartDate", "EndDate")
SELECT "ApplicantId", 1, "FirstPreferredSD", "FirstPreferredED"
FROM public."Applicant"
WHERE "FirstPreferredSD" IS NOT NULL

UNION ALL

SELECT "ApplicantId", 2, "SecondPreferredSD", "SecondPreferredED"
FROM public."Applicant"
WHERE "SecondPreferredSD" IS NOT NULL

UNION ALL

SELECT "ApplicantId", 3, "ThirdPreferredSD", "ThirdPreferredED"
FROM public."Applicant"
WHERE "ThirdPreferredSD" IS NOT NULL;

Step 4: Update procedures to use child table

Replace direct column references with JOINs or subqueries against the

child table.

Step 5: (Later) Drop the old columns

-- Only after app code is fully migrated
ALTER TABLE public."Applicant"
    DROP COLUMN "FirstPreferredSD",
    DROP COLUMN "FirstPreferredED",
    -- ... etc.

When to Use

Where We Used It

from repeating columns into ApplicantPreferredDate and

ApplicantPreferredLocation child tables

Related Skills

References

-- see "Schema design and data types" section

Gotchas

of the parent's repeating columns


Category: DDL | Origin:

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.