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

5.1. テーブルの基本 #

<title>Table Basics</title>

A table in a relational database is much like a table on paper: It consists of rows and columns. The number and order of the columns is fixed, and each column has a name. The number of rows is variable &mdash; it reflects how much data is stored at a given moment. SQL does not make any guarantees about the order of the rows in a table. When a table is read, the rows will appear in an unspecified order, unless sorting is explicitly requested. This is covered in <xref linkend="queries"/>. Furthermore, SQL does not assign unique identifiers to rows, so it is possible to have several completely identical rows in a table. This is a consequence of the mathematical model that underlies SQL but is usually not desirable. Later in this chapter we will see how to deal with this issue. リレーショナルデータベースのテーブルは、紙に書く表によく似ています。 テーブルは行と列からできています。 列の数と順序は固定されており、それぞれの列に名前が付けられています。 行の数は可変です。 つまり行の数とは、その時点でどれだけのデータが格納されているのかを示すものです。 SQLではテーブル内の行の順序は保証されません。 テーブルを読み込むと、明示的に並べ替えが要求されない限り、行は不特定な順序で返されます。 これについては第7章を参照してください。 さらに、SQLでは行に固有の識別子が割り当てられないので、テーブル内にまったく同一の行がいくつも存在することがあり得ます。 これは、SQLの基礎をなす数学的モデルの帰結ですが、通常は好ましいことではありません。 この問題の対処法については、本章で後述します。

Each column has a data type. The data type constrains the set of possible values that can be assigned to a column and assigns semantics to the data stored in the column so that it can be used for computations. For instance, a column declared to be of a numerical type will not accept arbitrary text strings, and the data stored in such a column can be used for mathematical computations. By contrast, a column declared to be of a character string type will accept almost any kind of data but it does not lend itself to mathematical calculations, although other operations such as string concatenation are available. それぞれの列にデータ型があります。 データ型によって、列に割り当てられる値が制限されます。 また、列に格納されているデータに意味が割り当てられ、データを計算に使用できるようになります。 例えば、数値型と宣言された列は任意のテキスト文字列は受け付けません。 そして、数値型の列に格納されているデータは算術計算に使用できます。 これに対して、文字列型と宣言された列はほとんど全ての種類のデータを受け付けます。 しかし、文字列の結合といった演算には使用できますが、算術計算には使用できません。

<productname>PostgreSQL</productname> includes a sizable set of built-in data types that fit many applications. Users can also define their own data types. Most built-in data types have obvious names and semantics, so we defer a detailed explanation to <xref linkend="datatype"/>. Some of the frequently used data types are <type>integer</type> for whole numbers, <type>numeric</type> for possibly fractional numbers, <type>text</type> for character strings, <type>date</type> for dates, <type>time</type> for time-of-day values, and <type>timestamp</type> for values containing both date and time. PostgreSQLには、様々なアプリケーションに対応した多数のデータ型の集合が組み込まれています。 またユーザが独自のデータ型を定義することも可能です。 組み込みデータ型のほとんどにはわかりやすい名前と意味が付けられているので、詳しい説明はここでは行わず、第8章で行います。 よく使用されるデータ型としては、整数を表すinteger、小数も表すことができるnumeric、文字列を表すtext、日付を表すdate、時刻を表すtime、そして日付と時刻の両方を含むtimestampがあります。

To create a table, you use the aptly named <xref linkend="sql-createtable"/> command. In this command you specify at least a name for the new table, the names of the columns and the data type of each column. For example: テーブルを作成するには、その名の通りCREATE TABLEコマンドを使用します。 このコマンドで最低限指定する必要があるのは、新規テーブル名、列名、各列のデータ型です。 例を示します。

CREATE TABLE my_first_table (
    first_column text,
    second_column integer
);

This creates a table named <literal>my_first_table</literal> with two columns. The first column is named <literal>first_column</literal> and has a data type of <type>text</type>; the second column has the name <literal>second_column</literal> and the type <type>integer</type>. The table and column names follow the identifier syntax explained in <xref linkend="sql-syntax-identifiers"/>. The type names are usually also identifiers, but there are some exceptions. Note that the column list is comma-separated and surrounded by parentheses. これで2列からなるmy_first_tableという名前のテーブルが作成されます。 最初の列の名前はfirst_columnで、そのデータ型はtextです。 2番目の列の名前はsecond_columnで、そのデータ型はintegerです。 テーブル名および列名は、4.1.1で説明した識別子の構文に従います。 型名も通常は識別子ですが、例外もあります。 列リストはカンマで区切り、括弧で囲むことに注意してください。

Of course, the previous example was heavily contrived. Normally, you would give names to your tables and columns that convey what kind of data they store. So let's look at a more realistic example: 先ほどの例は、説明が目的であるため現実的ではありません。 通常、テーブルおよび列の名前は、どのようなデータが格納されているかわかるような名前にします。 以下に、より現実的な例を示します。

CREATE TABLE products (
    product_no integer,
    name text,
    price numeric
);

(The <type>numeric</type> type can store fractional components, as would be typical of monetary amounts.) numeric型は小数を格納することができ、金額を扱う場合はこれが一般的です。)

ヒント

When you create many interrelated tables it is wise to choose a consistent naming pattern for the tables and columns. For instance, there is a choice of using singular or plural nouns for table names, both of which are favored by some theorist or other. 相関するテーブルを数多く作成する場合は、テーブルと列の命名規則を一貫させるのが賢明です。 例えば、テーブル名に単数形あるいは複数形どちらの名詞を使用するかという選択肢があります(これは論者によって好みが分かれています)。

There is a limit on how many columns a table can contain. Depending on the column types, it is between 250 and 1600. However, defining a table with anywhere near this many columns is highly unusual and often a questionable design. テーブルに含めることができる列の数には制限があります。 制限は、列の型に応じて250〜1600の間となります。 しかし、これほど多くの列を使用することは稀ですし、そのような場合は設計に問題があることも多いのです。

If you no longer need a table, you can remove it using the <xref linkend="sql-droptable"/> command. For example: 必要のないテーブルができた場合は、DROP TABLEコマンドを使用してそのテーブルを削除できます。 例を示します。

DROP TABLE my_first_table;
DROP TABLE products;

Attempting to drop a table that does not exist is an error. Nevertheless, it is common in SQL script files to unconditionally try to drop each table before creating it, ignoring any error messages, so that the script works whether or not the table exists. (If you like, you can use the <literal>DROP TABLE IF EXISTS</literal> variant to avoid the error messages, but this is not standard SQL.) 存在しないテーブルを削除しようとすると、エラーになります。 もっともテーブルが存在するかどうかに関係なくスクリプト全体を動作させることができるように、テーブルを作成する前に、エラーメッセージを無視して無条件に削除操作を行うことは、SQLスクリプトファイルではよく行われることです。 (この操作を行いたければ、エラーメッセージの出力を防ぐDROP TABLE IF EXISTSという構文を使用できます。 しかし、これは標準SQLではありません。)

If you need to modify a table that already exists, see <xref linkend="ddl-alter"/> later in this chapter. 既に存在するテーブルを変更する方法については、本章で後述する5.6を参照してください。

With the tools discussed so far you can create fully functional tables. The remainder of this chapter is concerned with adding features to the table definition to ensure data integrity, security, or convenience. If you are eager to fill your tables with data now you can skip ahead to <xref linkend="dml"/> and read the rest of this chapter later. これまでに説明したツールを使用して、十分に機能するテーブルを作成できます。 本章の残りでは、テーブル定義に機能を追加して、データの整合性、安全性、利便性を確実にする方法について述べていきます。 この時点でテーブルにデータを入力したければ、本章の残りを後回しにして第6章に進んでも構いません。