LoginSignup
1
0

More than 1 year has passed since last update.

BigQueryの"primary keyサポート"を検証(2023/02/22時点プレビュー)

Last updated at Posted at 2023-02-23

結論:思ったものと違う(nullも重複も許容される)

  • 嬉しいニュースだと思ったけど、想像とは違った
  • 下記の検証内容だと、"null"も"重複"も許容してデータが格納されてしまう
  • TABLE_CONSTRAINTS view にも記載あり。ここは詳しく見たほうが良さそう
  • 原文
    • Value constraints for primary keys and foreign keys are not enforced. Users need to ensure that values match their respective constraints, otherwise they may get incorrect results.
  • DeepL訳
    • 主キーと外部キーの値に関する制約は強制されません。ユーザーは、値がそれぞれの制約に一致することを確認する必要があります。そうしないと、正しくない結果を得る可能性があります。

プレビュー版でのprimary keyサポートリリース(2023/02/22時点)

検証内容

  • 簡単なテーブルを作成し、insertを実行

テーブル作成

DDL

-- not enforced無し
create table dwh.item_master(
  item_id integer,
  item_name string,
  primary key(item_id)
)
;

→DDL実行でエラー

image.png

-- not enforced指定あり
create table dwh.item_master(
  item_id integer,
  item_name string,
  primary key(item_id) not enforced
)
;

→テーブル作成は成功

image.png

データのinsert

パターン1:nullデータ

  • データのinsert
insert into dwh.item_master values
(1, 'item_A'),
(null, 'item_B')
;
  • 結果

image.png

パターン2:既にテーブルに登録されている重複データ

  • データのinsert
-- パターン1実行後の状態で下記実行
insert into dwh.item_master values
(1, 'item_C')
;
  • 結果

image.png

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0