postgresql Primary keys
1658 ワード
1つのテーブルには1つのプライマリ・キーしか存在しませんが、複数の外部キーを持つことができます.pgでは、各テーブルにプライマリ・キーを設定することを推奨しますが、これは強制されません.
プライマリ・キーの作成方法:
1.テーブル制約
上のproduct_no_pkeyは別名です.
2.フィールド制約
フィールドコンストレイントの作成は、プライマリ・キーとして作成する必要があるフィールドの後ろにprimary keyを付けるだけで、同じように別名を付けることもできます.
公式文書には次のような言葉があります.
Technically, a primary key constraint is simply a combination of a unique constraint and a not-null constraint.
翻訳:技術的に言えば、プライマリ・キーは一意の制約と非空の制約の組み合わせです.
上の2つは等価です.
しかし、1つのテーブルにはプライマリ・キーが1つしかありません.
上の2つの例のように、2番目の例は通過できません.エラーが表示されます.
ERROR: multiple primary keys for table "product3"are not allowed
SQLステータス:42 P 16
プライマリ・キーの作成方法:
1.テーブル制約
create table product (
product_no integer,
b integer,
c integer,
price numeric,
name text,
constraint product_no_pkey primary key(product_no)
);
上のproduct_no_pkeyは別名です.
2.フィールド制約
create table product (
product_no integer primary key,
b integer,
c integer
price numeric,
name text
)
フィールドコンストレイントの作成は、プライマリ・キーとして作成する必要があるフィールドの後ろにprimary keyを付けるだけで、同じように別名を付けることもできます.
create table product (
product_no integer constraint test_key primary key, --test_key
b integer,
c integer,
price numeric,
name text
)
公式文書には次のような言葉があります.
Technically, a primary key constraint is simply a combination of a unique constraint and a not-null constraint.
翻訳:技術的に言えば、プライマリ・キーは一意の制約と非空の制約の組み合わせです.
create table product3(
product_no integer unique not null ,
b integer,
c integer,
price numeric,
name text
)
create table product3(
product_no integer primary key,
b integer,
c integer,
price numeric,
name text
)
上の2つは等価です.
しかし、1つのテーブルにはプライマリ・キーが1つしかありません.
create table product3(
product_no integer unique not null ,
b integer unique not null,
c integer,
price numeric,
name text
)
create table product3(
product_no integer primary key ,
b integer primary key,
c integer,
price numeric,
name text
)
上の2つの例のように、2番目の例は通過できません.エラーが表示されます.
ERROR: multiple primary keys for table "product3"are not allowed
SQLステータス:42 P 16