【发布时间】:2019-11-19 12:52:24
【问题描述】:
首先是相关代码:
create table customer(
customer_mail_address varchar(255) not null,
subscription_start date not null,
subscription_end date, check (subscription_end !< subcription start)
constraint pk_customer primary key (customer_mail_address)
)
create table watchhistory(
customer_mail_address varchar(255) not null,
watch_date date not null,
constraint pk_watchhistory primary key (movie_id, customer_mail_address, watch_date)
)
alter table watchhistory
add constraint fk_watchhistory_ref_customer foreign key (customer_mail_address)
references customer (customer_mail_address)
on update cascade
on delete no action
go
所以我想使用 UDF 将 watchhistory 中的 watch_date 限制在 subscription_start 和 subscription_end 之间客户。我好像搞不懂。
【问题讨论】:
-
请添加您正在使用的rdms。例如,在 Oracle 上,您不能使用引用另一个表的检查约束:“检查约束的条件可以引用表中的任何列,但不能引用其他表的列。”
-
我正在使用 Microsoft SQL Server Management Studio
-
虽然您可以绕道而行,但更好的问题是您认为应该这样做的原因。 “历史”这个名称表明该表并不是您模型的真正组成部分,而是用于捕获不断变化的信息(可能用于审计或取证目的)。
标签: sql sql-server constraints user-defined-functions