1
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?

【TablePlus】SQL文でダミーデータを挿入する方法

Last updated at Posted at 2025-12-03

はじめに

普段はデータベース(DB)を触ったり、SQL文を書く機会がないのですが、案件で少しだけ触れる機会があったので記載します。

今回はクライアントツール 「Table Plus」 を使用しました。
インストール方法、DB接続方法については割愛します。

目次

環境

MacBook Air Apple M3

Table PlusはVersion 6.7.4(642)を使用しています。

スクリーンショット 2025-12-03 18.20.41.png

テーブル

既に存在するテーブル(t_user)を使用します。

Group 5.png

カラムを確認

次に、t_userテーブルのカラムを確認します。

テーブル名で右クリックし「Open structure」をクリックすると以下の画面に切り替わります。

Group 6.png

ダミーデータを追加

上部のツールバーにある「SQL」をクリックすると、SQL文を記載できる画面に切り替わります。

Group 7.png

SQL文を記載

次にダミーデータを追加するSQL文を記載します。

Group 8.png

赤枠で囲った部分は以下のとおりです。

INSERT INTO public.t_user (
    mail_address,
    name,
    hashed_password,
    user_role_id,
    tenant_id,
    del_flg,
    failed_login_count,
    db_input_date,
    db_update_date
)
SELECT
    'dummy_user_' || i || '@example.com' AS mail_address,
    'Dummy User ' || i AS name,
    'dummy_hashed_password' AS hashed_password,
    1 AS user_role_id,
    NULL AS tenant_id,
    0 AS del_flg,
    0 AS failed_login_count,
    NOW() AS db_input_date,
    NOW() AS db_update_date
FROM generate_series(1, 5) AS i;

最終行を generate_series(1, 100) とすると100件追加できます。
ここでは5件追加し、以下のようになりました。

Group 9.png

カラム 値の内容
mail_address dummy_user_1 @ example.com から dummy_user_5 @ example.com を作成
name Dummy User 1 から Dummy User 5 を作成
hashed_password 固定文字列
user_role_id 1
tenant_id NULL
del_flg 0
failed_login_count 0
db_input_date, db_update_date 現在日時

SQL文の解説

1)

INSERT INTO public.t_user (
    mail_address,
    // 省略
    db_update_date
)

INSERT する対象テーブルとカラムを指定。

2)

generate_series(1,5)

1
2
3
4
5
という5行の数字を返します。

3)

連番「i」を埋め込んだデータを作成

// メールアドレス

'dummy_user_' || i || '@example.com'

iが1〜5なので、生成される値は
dummy_user_1 @ example.com
dummy_user_2 @ example.com
...
dummy_user_5 @ example.com
となります。

1
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
1
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?