0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

生成AIで遊ぼAdvent Calendar 2024

Day 16

現行システムのカオスなテーブル設計を、生成AI(V0)を使って正規化してみたい

Posted at

はじめに

以下検証をいろいろなモデルでやってみます。

検証環境情報

・DB:PostgreSQL
・生成AIモデル:V0

やってみよう

DDLををインプットにしてみる

ER図

is_subordinate_of って何ですか?

image.png

へ~

DDL

-- accounts テーブル
CREATE TABLE accounts (
    account_id SERIAL PRIMARY KEY,
    username VARCHAR(50) UNIQUE NOT NULL,
    email VARCHAR(100) UNIQUE NOT NULL,
    password VARCHAR(255) NOT NULL,
    name VARCHAR(100) NOT NULL,
    birth_date DATE,
    address TEXT,
    phone_number VARCHAR(20),
    role VARCHAR(50) NOT NULL,
    status VARCHAR(20) DEFAULT 'active' NOT NULL,
    created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP
);

-- shipping_addresses テーブル
CREATE TABLE shipping_addresses (
    address_id SERIAL PRIMARY KEY,
    account_id INTEGER NOT NULL,
    postal_code VARCHAR(10),
    prefecture VARCHAR(20),
    city VARCHAR(50),
    building VARCHAR(100),
    FOREIGN KEY (account_id) REFERENCES accounts(account_id)
);

-- organizations テーブル
CREATE TABLE organizations (
    organization_id SERIAL PRIMARY KEY,
    account_id INTEGER NOT NULL,
    company VARCHAR(100),
    department VARCHAR(100),
    group_name VARCHAR(100),
    position VARCHAR(100),
    FOREIGN KEY (account_id) REFERENCES accounts(account_id)
);

-- subordinates テーブル
CREATE TABLE subordinates (
    subordinate_id SERIAL PRIMARY KEY,
    account_id INTEGER NOT NULL,
    subordinate_account_id INTEGER NOT NULL,
    FOREIGN KEY (account_id) REFERENCES accounts(account_id),
    FOREIGN KEY (subordinate_account_id) REFERENCES accounts(account_id)
);

テーブル設計書

image.png

image.png

image.png

image.png

まとめ

どのモデルもパスワードやロールを別テーブルには持たないんですね。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?