0

間隔をテーブルに保存するとします。間隔は、単一の日付または範囲として指定できるため、フィールドは次のようになります。

| id | since | until | at_date |

今、私はsince is null and until is not nullorのような状況since is not null and until is nullが起こらないことを望みますが、 と の両方since is null and until is nullsince is not null and until is not null問題ありません (最初の 1 つは、 が指定されている場合にのみ問題ありat_dateません)。

おそらく、「マージ」untilsinceて 1 つの列にすることは理にかなっていますが、私の状況では、どちらか一方のみを選択することが理にかなっている場合があるため、それらを別々に使用しても問題ないと思います。

編集:

create table menu_availability
(menu_id int not null,
 daily_serving_start time null,
 daily_serving_end time null,
 weekly_service_off tinyint null,
 one_time_service_off date null,
 constraint ch_valid_week_day
    check ((weekly_service_off is null) 
        or (weekly_service_off <= 6 and weekly_service_off >= 0)),
 constraint ch_non_empty_schedule
    check ((daily_serving_start is not null and daily_serving_end is not null)
        or (daily_serving_start is null and daily_serving_end is null)),
 constraint ch_either_week_or_time_set
    check (daily_serving_start is not null 
        or weekly_service_off is not null 
        or one_time_service_off is not null),
 constraint ch_only_week_or_one_time
    check ((weekly_service_off is null and one_time_service_off is not null)
        or (weekly_service_off is not null and one_time_service_off is null)))

これは私がこれまでに持っているものです...しかし、それは本当に面倒です...

4

1 に答える 1

2

すべての間隔でandat_dateのみを削除して使用する必要があると思います。sinceuntil

包括的ではないので、 「2013-03-01」untilになる値は次のように保存されますat_date

since      until
2013-03-01 2013-03-02

アップデート:

null を許可しない case ステートメントを使用して、永続化された計算ビット列を作成できます。

create table YourTable
(
  id int identity primary key,
  since datetime,
  until datetime,
  at_date datetime,
  sn as case when since is null and until is null or
                  since is not null and until is not null then cast(1 as bit)
        end persisted not null
)

またはチェック制約付き

create table T
(
  id int identity primary key,
  since datetime,
  until datetime,
  at_date datetime,
  constraint ch_sn check(since is null and until is null or
                         since is not null and until is not null)
)
于 2013-03-05T10:09:26.820 に答える