Chapter 8. 配列

Note: This must become a chapter on array behavior. Volunteers? - thomas 1998-01-12

これは配列の振る舞いに関する章にならなくてはなりません. 誰かボランティアいませんか? - thomas 1998-01-12

Postgres allows attributes of an instance to be defined as fixed-length or variable-length multi-dimensional arrays. Arrays of any base type or user-defined type can be created. To illustrate their use, we first create a class with arrays of base types.

Postgresでは,インスタンスの属性を, 固定長または可変長の多次元配列として定義することができます. どのようなベースの型でも,またユーザ定義の型でも生成する ことができます.それらの使用法について説明するために, まずはベース型配列で1つのクラスを作ってみましょう.


CREATE TABLE SAL_EMP (
    name            text,
    pay_by_quarter  int4[],
    schedule        text[][]
);
CREATE TABLE 従業員給与 (
    名前            text,
    四半期毎の給与  int4[],
    スケジュール    text[][]
);

The above query will create a class named SAL_EMP with a text string (name), a one-dimensional array of int4 (pay_by_quarter), which represents the employee's salary by quarter and a two-dimensional array of text (schedule), which represents the employee's weekly schedule. Now we do some INSERTSs; note that when appending to an array, we enclose the values within braces and separate them by commas. If you know C, this is not unlike the syntax for initializing structures.

上記の問い合わせでは,text文字列(名前), 四半期毎の従業員の給与を表すint4の1次元 配列(四半期毎の給与),および従業員の1週間のスケジュールを表す textの2次元配列(スケジュール)という 3つのメンバを持つ,従業員給与という名前のクラスを生成します. ここで,いくつかINSERTSをやってみましょう. 配列に追加する際は,値を{}で囲み,さらにそれらをカンマで区切って やることに注意してください.もしあなたがC をご存知であれば,これは構造体の初期化をするための書式に似ていない こともありません.


INSERT INTO SAL_EMP
    VALUES ('Bill',
    '{10000, 10000, 10000, 10000}',
    '{{"meeting", "lunch"}, {}}');
INSERT INTO 従業員給与
    VALUES ('ビル',
    '{10000, 10000, 10000, 10000}',
    '{{"会議", "昼食"}, {}}');

INSERT INTO SAL_EMP
    VALUES ('Carol',
    '{20000, 25000, 25000, 25000}',
    '{{"talk", "consult"}, {"meeting"}}');
INSERT INTO 従業員給与
    VALUES ('キャロル',
    '{20000, 25000, 25000, 25000}',
    '{{"討議", "協議"}, {"会議"}}');
By default, Postgres uses the "one-based" numbering convention for arrays -- that is, an array of n elements starts with array[1] and ends with array[n]. Now, we can run some queries on SAL_EMP. First, we show how to access a single element of an array at a time. This query retrieves the names of the employees whose pay changed in the second quarter: デフォルトでは,Postgresは配列に 対して " 1 から始まる" 番号付けの規則を使います。つまり、n 個の 要素を持つ配列は,配列[1]から始まり配列[n]で終ります。ここで、 従業員給与に対していくつかの問い合わせをやってみましょう。まず、 一度に配列のひとつの要素だけにアクセスする方法を示します。この 問い合わせは、第 2 四半期に給与が変わる従業員の名前を取り出します。

SELECT name
    FROM SAL_EMP
    WHERE SAL_EMP.pay_by_quarter[1] <>
    SAL_EMP.pay_by_quarter[2];

+------+
|name  |
+------+
|Carol |
+------+
SELECT 名前
    FROM 従業員給与
    WHERE 従業員給与.四半期毎の給与[1] <>
    従業員給与.四半期毎の給与[2];

+--------+
|名前    |
+--------+
|キャロル|
+--------+

This query retrieves the third quarter pay of all employees:

この問い合わせでは,すべての従業員の第 3 四半期の給与を取り出します。


SELECT SAL_EMP.pay_by_quarter[3] FROM SAL_EMP;


+---------------+
|pay_by_quarter |
+---------------+
|10000          |
+---------------+
|25000          |
+---------------+
SELECT 従業員給与.四半期毎の給与[3] FROM 従業員給与;

+---------------+
|四半期毎の給与 |
+---------------+
|10000          |
+---------------+
|25000          |
+---------------+

We can also access arbitrary slices of an array, or subarrays. This query retrieves the first item on Bill's schedule for the first two days of the week.

また、任意の配列の一部や副配列にアクセスすることもできます. この問い合わせは,週の頭 2 日分のビルのスケジュールの中で 最初の項目を取り出します。


SELECT SAL_EMP.schedule[1:2][1:1]
    FROM SAL_EMP
    WHERE SAL_EMP.name = 'Bill';

+-------------------+
|schedule           |
+-------------------+
|{{"meeting"},{""}} |
+-------------------+
SELECT 従業員給与.スケジュール[1:2][1:1]
    FROM 従業員給与
    WHERE 従業員給与.名前 = 'ビル';

+---------------+
|スケジュール   |
+---------------+
|{{"会議"},{""}}|
+---------------+