バージョンごとのドキュメント一覧

2.4. テーブルに行を挿入 #

<title>Populating a Table With Rows</title>

The <command>INSERT</command> statement is used to populate a table with rows: 以下のように、INSERT文を使用して、テーブルに行を挿入します。

INSERT INTO weather VALUES ('San Francisco', 46, 50, 0.25, '1994-11-27');

Note that all data types use rather obvious input formats. Constants that are not simple numeric values usually must be surrounded by single quotes (<literal>'</literal>), as in the example. The <type>date</type> type is actually quite flexible in what it accepts, but for this tutorial we will stick to the unambiguous format shown here. 全てのデータ型でどちらかといえばわかりやすい入力書式を使用していることに注意してください。 通常、単純な数値以外の定数は、この例のように、単一引用符(')で括らなければなりません。 date型で受け付けられるものは実際はかなり柔軟です。 しかし、このチュートリアルの段階では、曖昧さがない書式にこだわることにします。

The <type>point</type> type requires a coordinate pair as input, as shown here: point型では、入力として次のような座標の組み合わせが必要です。

INSERT INTO cities VALUES ('San Francisco', '(-194.0, 53.0)');

The syntax used so far requires you to remember the order of the columns. An alternative syntax allows you to list the columns explicitly: ここまでの構文では、列の順番を覚えておく必要がありました。 以下に示す他の構文では、列のリストを明示的に与えることができます。

INSERT INTO weather (city, temp_lo, temp_hi, prcp, date)
    VALUES ('San Francisco', 43, 57, 0.0, '1994-11-29');

You can list the columns in a different order if you wish or even omit some columns, e.g., if the precipitation is unknown: リスト内の列は好きな順番で指定できます。 また、一部の列を省略することもできます。 例えば、降水量がわからない場合は以下のようにできます。

INSERT INTO weather (date, city, temp_hi, temp_lo)
    VALUES ('1994-11-29', 'Hayward', 54, 37);

Many developers consider explicitly listing the columns better style than relying on the order implicitly. 多くの開発者は、暗黙的な順番に依存するよりも、列のリストを明示的に指定する方が良いやり方だと考えています。

Please enter all the commands shown above so you have some data to work with in the following sections. 次節でもデータを使用しますので、上のコマンドを全て入力してください。

You could also have used <command>COPY</command> to load large amounts of data from flat-text files. This is usually faster because the <command>COPY</command> command is optimized for this application while allowing less flexibility than <command>INSERT</command>. An example would be: また、COPYを使用して大量のデータを平文テキストファイルからロードすることもできます。 COPYコマンドはINSERTよりも柔軟性はありませんが、この目的に特化していますので、通常、より高速になります。 以下に例を示します。

COPY weather FROM '/home/user/weather.txt';

where the file name for the source file must be available on the machine running the backend process, not the client, since the backend process reads the file directly. You can read more about the <command>COPY</command> command in <xref linkend="sql-copy"/>. ここで元となるファイルを表すファイル名は、クライアントではなく、バックエンドプロセスを動かしているマシンで利用できるものでなければなりません。 バックエンドプロセスがこのファイルを直接読み込むからです。 COPYにはCOPYコマンドについてのより詳しい説明があります。