CHECK (((((datepart('month'<REDACTED>text, birthdate))<REDACTED>integer =…
Share
Download ▾
CHECK (((((date_part('month'text, birthdate))integer = (date_part('month'text, recurring_birthday))integer) AND ((date_part('day'text, birthdate))integer = (date_part('day'text, recurring_birthday))integer)) OR ((((date_part('year'text, birthdate))integer % 4) = 0) AND ((((date_part('year'text, birthdate))integer % 100) <> 0) OR (((date_part('year'text, birthdate))integer % 400) = 0)) AND ((date_part('month'text, birthdate))integer = 2) AND ((date_part('day'text, birthdate))integer = 29) AND ((NOT ((((date_part('year'text, recurring_birthday))integer % 4) = 0) AND ((((date_part('year'text, recurring_birthday))integer % 100) <> 0) OR (((date_part('year'text, recurring_birthday))integer % 400) = 0)))) AND ((date_part('month'text, recurring_birthday))integer = 3) AND ((date_part('day'text, recurring_birthday))integer = 1)))))
このCHECK制約を解説
Assistant
このCHECK制約は、**誕生日(birthdate)と毎年繰り返す記念日(recurring_birthday)**が適切に一致しているか、またはうるう年(2月29日生まれ)に対して特別な扱いがされているかを検証するものです。長くて複雑ですが、丁寧に分解して説明します。
このCHECK制約は以下のどちらかが真であることを要求します:
birthdateとrecurring_birthdayの「月日(MM-DD)」が一致している
birthdateが「うるう年の2月29日」であり、recurring_birthdayが「非うるう年の3月1日」になっている(特別処理)
birthdateとrecurring_birthdayの「月日(MM-DD)」が一致している
birthdateが「うるう年の2月29日」であり、recurring_birthdayが「非うるう年の3月1日」になっている(特別処理)
────────────────────────────────────────────────────────────────────
sqlCopy code((date_part('month', birthdate)integer = date_part('month', recurring_birthday)integer) AND
(date_part('day', birthdate)integer = date_part('day', recurring_birthday)integer))
これはシンプルに、birthdateとrecurring_birthdayの月と日が同じであることをチェックしています。
✅ 条件2: 2月29日 → 3月1日 の特別扱い(うるう年対応)
────────────────────────────────────────────────────────────────────
sqlCopy code(((birthdateの年がうるう年) AND
birthdateが2月29日であり AND
recurring_birthdayの年がうるう年でない AND
recurring_birthdayが3月1日である))
────────────────────────────────────────────────────────────────────
sqlCopy code(date_part('year', birthdate)integer % 4 = 0) AND
((date_part('year', birthdate)integer % 100 <> 0) OR (date_part('year', birthdate)integer % 400 = 0))
────────────────────────────────────────────────────────────────────
sqlCopy code(date_part('month', birthdate)integer = 2) AND
(date_part('day', birthdate)integer = 29)
🔹recurring_birthdayがうるう年でない
────────────────────────────────────────────────────────────────────
sqlCopy codeNOT (
(date_part('year', recurring_birthday)integer % 4 = 0) AND
((date_part('year', recurring_birthday)integer % 100 <> 0) OR
(date_part('year', recurring_birthday)integer % 400 = 0))
)
🔹recurring_birthdayが3月1日である
────────────────────────────────────────────────────────────────────
sqlCopy code(date_part('month', recurring_birthday)integer = 3) AND
(date_part('day', recurring_birthday)integer = 1)
このCHECK制約は以下のどちらかを満たしていればOKです:
birthdateとrecurring_birthdayの月日が一致している
birthdateがうるう年の2月29日で、recurring_birthdayがうるう年ではない3月1日である
birthdateとrecurring_birthdayの月日が一致している
birthdateがうるう年の2月29日で、recurring_birthdayがうるう年ではない3月1日である
うるう年でない年には2月29日が存在しないため、2月29日生まれの人の記念日を3月1日として扱うのは実務的な配慮です。このような日付変換ロジックは、誕生日リマインダーや自動メッセージ送信のアプリでよく使われます。
もしこの制約をもっと読みやすくしたい場合、ビューや関数に切り出すのも一案です。希望があればその書き換えもできますよ。
Dataset releases
Not yet included in a published release.