-- V9: 공통 코드

CREATE TABLE common_code_group (
    group_code   VARCHAR(20)  PRIMARY KEY,
    group_name   VARCHAR(100) NOT NULL,
    description  VARCHAR(300),
    is_system    CHAR(1)      NOT NULL DEFAULT 'N',
    is_active    CHAR(1)      NOT NULL DEFAULT 'Y',
    sort_order   INT          NOT NULL DEFAULT 0,
    created_at   TIMESTAMP    NOT NULL DEFAULT NOW(),
    created_by   BIGINT,
    updated_at   TIMESTAMP,
    updated_by   BIGINT
);

CREATE TABLE common_code (
    code_id        SERIAL PRIMARY KEY,
    group_code     VARCHAR(20) NOT NULL REFERENCES common_code_group(group_code),
    code           VARCHAR(30) NOT NULL,
    code_name      VARCHAR(100) NOT NULL,
    code_name_en   VARCHAR(100),
    code_desc      VARCHAR(300),
    attr1          VARCHAR(100),
    attr2          VARCHAR(100),
    attr3          VARCHAR(100),
    ref_code       VARCHAR(50),
    sort_order     INT          NOT NULL DEFAULT 0,
    is_active      CHAR(1)      NOT NULL DEFAULT 'Y',
    created_at     TIMESTAMP    NOT NULL DEFAULT NOW(),
    created_by     BIGINT,
    updated_at     TIMESTAMP,
    updated_by     BIGINT,
    UNIQUE (group_code, code)
);
CREATE INDEX idx_cc_group_active ON common_code(group_code, is_active, sort_order);
