CREATE TABLE IF NOT EXISTS TN_USER (
    USER_IDNTF_NO VARCHAR(32) NOT NULL,
    USER_ID VARCHAR(100) NOT NULL,
    PSWD VARCHAR(200) NOT NULL,
    USER_NM VARCHAR(100) NOT NULL,
    EML_ADDR VARCHAR(200) NOT NULL,
    USER_STTS_CD VARCHAR(10) NOT NULL DEFAULT 'A',
    USER_TYPE_CD VARCHAR(10) NOT NULL DEFAULT 'ADM',
    JOIN_DT DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    INDVDL_INFO_AGRE_DT DATETIME NULL,
    LOCK_YN CHAR(1) NOT NULL DEFAULT 'N',
    LGN_FAIL_NMTM INT NULL DEFAULT 0,
    LOCK_DT DATETIME NULL,
    LAST_LGN_DT DATETIME NULL,
    PRIMARY KEY (USER_IDNTF_NO),
    UNIQUE (USER_ID)
);

CREATE TABLE IF NOT EXISTS TN_ATCH_FILE (
    ATCH_FILE_IDNTF_NO VARCHAR(32) NOT NULL,
    CRT_DT DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    DEL_YN CHAR(1) NOT NULL DEFAULT 'N',
    PRIMARY KEY (ATCH_FILE_IDNTF_NO)
);

CREATE TABLE IF NOT EXISTS TN_ATCH_FILE_DTL (
    ATCH_FILE_IDNTF_NO VARCHAR(32) NOT NULL,
    FILE_SN INT NOT NULL,
    FILE_STRG_PATH_NM VARCHAR(500) NOT NULL,
    FILE_STRG_NM VARCHAR(255) NOT NULL,
    ORGNL_FILE_NM VARCHAR(255) NOT NULL,
    FILE_EXTN_NM VARCHAR(50) NULL,
    FILE_CN VARCHAR(255) NULL,
    FILE_SZ BIGINT NOT NULL DEFAULT 0,
    PRIMARY KEY (ATCH_FILE_IDNTF_NO, FILE_SN),
    CONSTRAINT FK_TN_ATCH_FILE_DTL__MASTER FOREIGN KEY (ATCH_FILE_IDNTF_NO)
        REFERENCES TN_ATCH_FILE (ATCH_FILE_IDNTF_NO)
);

CREATE TABLE IF NOT EXISTS TN_PORTFOLIO (
    PORTFOLIO_IDNTF_NO VARCHAR(32) NOT NULL,
    TITLE VARCHAR(200) NOT NULL,
    THUMBNAIL_ATCH_FILE_IDNTF_NO VARCHAR(32) NOT NULL,
    THUMBNAIL_FILE_SN INT NOT NULL DEFAULT 1,
    CONTENT TEXT NOT NULL,
    REG_DT DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    INQ_CNT BIGINT NOT NULL DEFAULT 0,
    REG_USER_IDNTF_NO VARCHAR(32) NOT NULL,
    DEL_YN CHAR(1) NOT NULL DEFAULT 'N',
    UPD_DT DATETIME NULL,
    PRIMARY KEY (PORTFOLIO_IDNTF_NO),
    CONSTRAINT FK_TN_PORTFOLIO__USER FOREIGN KEY (REG_USER_IDNTF_NO)
        REFERENCES TN_USER (USER_IDNTF_NO),
    CONSTRAINT FK_TN_PORTFOLIO__THUMBNAIL FOREIGN KEY (THUMBNAIL_ATCH_FILE_IDNTF_NO)
        REFERENCES TN_ATCH_FILE (ATCH_FILE_IDNTF_NO)
);

CREATE TABLE IF NOT EXISTS TN_CONTACT_INQRY (
    CONTACT_IDNTF_NO VARCHAR(32) NOT NULL,
    NM VARCHAR(100) NOT NULL,
    EML_ADDR VARCHAR(200) NOT NULL,
    TELNO VARCHAR(30) NOT NULL,
    TTL VARCHAR(200) NOT NULL,
    CN TEXT NOT NULL,
    SUBMIT_DT DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY (CONTACT_IDNTF_NO)
);

CREATE INDEX IF NOT EXISTS IDX_TN_ATCH_FILE_DTL__PATH ON TN_ATCH_FILE_DTL (FILE_STRG_PATH_NM);
CREATE INDEX IF NOT EXISTS IDX_TN_PORTFOLIO__REG_DT ON TN_PORTFOLIO (REG_DT);
CREATE INDEX IF NOT EXISTS IDX_TN_CONTACT_INQRY__SUBMIT_DT ON TN_CONTACT_INQRY (SUBMIT_DT);
