ALTER DOMAIN — change the definition of a domain ドメイン定義を変更する
ALTER DOMAINname{ SET DEFAULTexpression| DROP DEFAULT } ALTER DOMAINname{ SET | DROP } NOT NULL ALTER DOMAINnameADDdomain_constraint[ NOT VALID ] ALTER DOMAINnameDROP CONSTRAINT [ IF EXISTS ]constraint_name[ RESTRICT | CASCADE ] ALTER DOMAINnameRENAME CONSTRAINTconstraint_nameTOnew_constraint_nameALTER DOMAINnameVALIDATE CONSTRAINTconstraint_nameALTER DOMAINnameOWNER TO {new_owner| CURRENT_ROLE | CURRENT_USER | SESSION_USER } ALTER DOMAINnameRENAME TOnew_nameALTER DOMAINnameSET SCHEMAnew_schema<phrase>where <replaceable class="parameter">domain_constraint</replaceable> is:</phrase> ここでdomain_constraintは以下の通りです。 [ CONSTRAINTconstraint_name] { NOT NULL | CHECK (expression) }
<command>ALTER DOMAIN</command> changes the definition of an existing domain.
There are several sub-forms:
ALTER DOMAINは既存ドメインの定義を変更します。
以下に示す副構文があります。
SET/DROP DEFAULT
These forms set or remove the default value for a domain. Note
that defaults only apply to subsequent <command>INSERT</command>
commands; they do not affect rows already in a table using the domain.
この構文はドメインのデフォルト値の設定または削除を行います。
指定したデフォルト値は、その後のINSERTコマンドのみに適用されることに注意してください。
そのドメインを使用したテーブルの既存の行には影響を与えません。
SET/DROP NOT NULL
These forms change whether a domain is marked to allow NULL
values or to reject NULL values. You can only <literal>SET NOT NULL</literal>
when the columns using the domain contain no null values.
この構文はドメインがNULL値を持つことができるかどうかを変更します。
SET NOT NULLを実行できるのは、ドメインを使用する列にNULL値が含まれていない場合のみです。
ADD domain_constraint [ NOT VALID ]
This form adds a new constraint to a domain.
When a new constraint is added to a domain, all columns using that
domain will be checked against the newly added constraint. These
checks can be suppressed by adding the new constraint using the
<literal>NOT VALID</literal> option; the constraint can later be made
valid using <command>ALTER DOMAIN ... VALIDATE CONSTRAINT</command>.
Newly inserted or updated rows are always checked against all
constraints, even those marked <literal>NOT VALID</literal>.
<literal>NOT VALID</literal> is only accepted for <literal>CHECK</literal> constraints.
この構文はドメインに新しい制約を付与します。
新しい制約がドメインに追加された時、そのドメインを使用するすべての列が新しく追加された制約に対して検査されます。
NOT VALIDオプションを使用して新しい制約を追加することでこれらの検査を抑制することができます。
ALTER DOMAIN ... VALIDATE CONSTRAINTを使用することで、後で制約を有効にすることができます。
新しく挿入または更新される行については、NOT VALIDと印が付いていたとしても、常にすべての制約が検査されます。
NOT VALIDはCHECK制約でのみ受け付けられます。
DROP CONSTRAINT [ IF EXISTS ]
This form drops constraints on a domain.
If <literal>IF EXISTS</literal> is specified and the constraint
does not exist, no error is thrown. In this case a notice is issued instead.
この構文はドメイン上の制約を削除します。
IF EXISTSが指定された場合、制約が存在しなくてもエラーになりません。
この場合は代わりに注意メッセージが発生します。
RENAME CONSTRAINTThis form changes the name of a constraint on a domain. この構文はドメイン上の制約名を変更します。
VALIDATE CONSTRAINT
This form validates a constraint previously added as
<literal>NOT VALID</literal>, that is, it verifies that all values in
table columns of the domain type satisfy the specified constraint.
この構文は、以前にNOT VALIDとして追加された制約を検証します。
つまり、そのドメイン型のテーブル列の値すべてが指定された制約を満たすかどうかを検証します。
OWNERThis form changes the owner of the domain to the specified user. この構文はドメインの所有者を指定したユーザに変更します。
RENAMEThis form changes the name of the domain. この構文はドメインの名前を変更します。
SET SCHEMAThis form changes the schema of the domain. Any constraints associated with the domain are moved into the new schema as well. この構文はドメインのスキーマを変更します。 ドメインに関連する制約もすべて新しいスキーマに移動します。
You must own the domain to use <command>ALTER DOMAIN</command>.
To change the schema of a domain, you must also have
<literal>CREATE</literal> privilege on the new schema.
To alter the owner, you must be able to <literal>SET ROLE</literal> to the
new owning role, and that role must have <literal>CREATE</literal> privilege
on the domain's schema. (These restrictions enforce that altering the owner
doesn't do anything you couldn't do by dropping and recreating the domain.
However, a superuser can alter ownership of any domain anyway.)
ALTER DOMAINを使用するにはドメインを所有していなければなりません。
ドメインのスキーマを変更するには、新しいスキーマにおけるCREATE権限も持たなければなりません。
所有者を変更するには、新しい所有者ロールに対してSET ROLEができなければなりません。また、そのロールはドメインのスキーマにおいてCREATE権限を持たなければなりません。
(この制限により、所有者の変更により、ドメインの削除と再作成でできないことは何もできないようにしています。
しかし、スーパーユーザはすべてのドメインの所有者を変更することができます。)
nameThe name (possibly schema-qualified) of an existing domain to alter. 変更対象となる既存のドメインの名前です(スキーマ修飾名も可)。
domain_constraintNew domain constraint for the domain. ドメイン用の新しいドメイン制約です。
constraint_nameName of an existing constraint to drop or rename. 削除または名前を変更する既存の制約名です。
NOT VALIDDo not verify existing stored data for constraint validity. 既存の格納されたデータについて制約の妥当性を検証しません。
CASCADEAutomatically drop objects that depend on the constraint, and in turn all objects that depend on those objects (see <xref linkend="ddl-depend"/>). その制約に依存するオブジェクトを自動的に削除し、さらにそれらのオブジェクトに依存するすべてのオブジェクトも削除します(5.15参照)。
RESTRICTRefuse to drop the constraint if there are any dependent objects. This is the default behavior. 依存するオブジェクトがある場合、制約の削除要求を拒否します。 これがデフォルトの動作です。
new_nameThe new name for the domain. ドメインの新しい名前です。
new_constraint_nameThe new name for the constraint. 制約の新しい名前です。
new_ownerThe user name of the new owner of the domain. ドメインの新しい所有者となるユーザの名前です。
new_schemaThe new schema for the domain. ドメインの新しいスキーマです。
Although <command>ALTER DOMAIN ADD CONSTRAINT</command> attempts to verify
that existing stored data satisfies the new constraint, this check is not
bulletproof, because the command cannot <quote>see</quote> table rows that
are newly inserted or updated and not yet committed. If there is a hazard
that concurrent operations might insert bad data, the way to proceed is to
add the constraint using the <literal>NOT VALID</literal> option, commit
that command, wait until all transactions started before that commit have
finished, and then issue <command>ALTER DOMAIN VALIDATE
CONSTRAINT</command> to search for data violating the constraint. This
method is reliable because once the constraint is committed, all new
transactions are guaranteed to enforce it against new values of the domain
type.
ALTER DOMAIN ADD CONSTRAINTは、既存の格納されたデータが新しい制約を満たすか検証しようとしますが、この確認は万全なものではありません。このコマンドが新しく挿入または更新されてまだコミットされていないテーブル行を「見る」ことはできないからです。
同時に実行される操作が不正なデータを挿入する危険があり、処理方法がNOT VALIDオプションを使った制約を追加することであるなら、そのコマンドをコミットして、そのコミットよりも前に開始したトランザクションがすべて完了するのを待ってから、制約に違反するデータを探すためにALTER DOMAIN VALIDATE CONSTRAINTを発行してください。
制約が一度コミットされれば、新しいトランザクションはすべてドメイン型の新しい値に対してその制約を強制していることが保証されますので、この方法は信頼できます。
Currently, <command>ALTER DOMAIN ADD CONSTRAINT</command>, <command>ALTER
DOMAIN VALIDATE CONSTRAINT</command>, and <command>ALTER DOMAIN SET NOT
NULL</command> will fail if the named domain or any derived domain is used
within a container-type column (a composite, array, or range column) in
any table in the database. They should eventually be improved to be able
to verify the new constraint for such nested values.
今のところ、ALTER DOMAIN ADD CONSTRAINT、ALTER DOMAIN VALIDATE CONSTRAINTおよびALTER DOMAIN SET NOT NULLは、指定ドメインまたは任意の派生ドメインがデータベース内のいずれかのテーブルのコンテナ型の列(複合型、配列型もしくは範囲型の列)で使用されていた場合、失敗します。
これは将来的には、こうした入れ子になった値に対する新しい制約を検証できるように改良されるべきです。
To add a <literal>NOT NULL</literal> constraint to a domain:
ドメインにNOT NULL制約を付与します。
ALTER DOMAIN zipcode SET NOT NULL;
To remove a <literal>NOT NULL</literal> constraint from a domain:
ドメインからNOT NULL制約を削除します。
ALTER DOMAIN zipcode DROP NOT NULL;
To add a check constraint to a domain: ドメインにCHECK制約を付与します。
ALTER DOMAIN zipcode ADD CONSTRAINT zipchk CHECK (char_length(VALUE) = 5);
To remove a check constraint from a domain: ドメインからCHECK制約を削除します。
ALTER DOMAIN zipcode DROP CONSTRAINT zipchk;
To rename a check constraint on a domain: ドメイン上の検査制約の名前を変更します。
ALTER DOMAIN zipcode RENAME CONSTRAINT zipchk TO zip_check;
To move the domain into a different schema: ドメインを新しいスキーマに移動します。
ALTER DOMAIN zipcode SET SCHEMA customers;
<command>ALTER DOMAIN</command> conforms to the <acronym>SQL</acronym>
standard, except for the <literal>OWNER</literal>, <literal>RENAME</literal>, <literal>SET SCHEMA</literal>, and
<literal>VALIDATE CONSTRAINT</literal> variants, which are
<productname>PostgreSQL</productname> extensions. The <literal>NOT VALID</literal>
clause of the <literal>ADD CONSTRAINT</literal> variant is also a
<productname>PostgreSQL</productname> extension.
ALTER DOMAIN文は標準SQLに準拠しています。
ただし、OWNER、RENAME、SET SCHEMA、VALIDATE CONSTRAINT構文は例外で、PostgreSQLの拡張です。
ADD CONSTRAINT構文のNOT VALID句もPostgreSQLの拡張です。