检查连接约束
Check constraint on join
CREATE TABLE Calendar (
id_calendar INT IDENTITY(1,1) PRIMARY KEY,
year int NOT NULL,
month int NULL,
day_of_month int NULL,
day_of_week int NULL,
week_of_year int NULL
);
CREATE TABLE Param(
id_param INT IDENTITY(1,1) PRIMARY KEY,
param varchar(30),
periode varchar(20),
CONSTRAINT check_periode CHECK (periode IN ('Day','Week','Month','Year'))
);
CREATE TABLE Event (
id_calendar INT,
id_param INT
);
每当向事件中插入新行时,我想检查它是否符合参数的周期。
如果周期='Day',那么年月none,day_of_month,day_of_week,week_of_year 应为 NULL。
如果periode = 'Week',那么只有day_of_month和day_of_week应该是NULL,
...
所以我需要同时加入 FK'S
并从那里构建一个 CHECK 约束。是否可以将 id_calendar 和 id_param 传递给 UDF
并从 CHECK 调用它?
出于性能原因,我不想使用全局查询,只想使用涉及受影响的行及其外键的检查。
是的。您可以定义一个函数来验证值并在 CHECK 约束中引用它。
CREATE FUNCTION CheckEvent(@id_Calendar INT, @id_param INT)
RETURNS BIT
AS
BEGIN
---LOGIC
END
添加检查约束以利用函数
ALTER TABLE Event ADD CONSTRAINT CHK_Event CHECK( dbo.CheckEvent(id_param , id_calendar) = 1)
为 periode
创建实体并在相关时引用它。使用组合键来表示 "the same periode
" 约束。
CREATE TABLE Periodes (
periode varchar(20),
CONSTRAINT check_periode CHECK (periode IN ('Day','Week','Month','Year'));
INSERT ...
CREATE TABLE Calendar (
id_calendar INT IDENTITY(1,1) PRIMARY KEY,
periode varchar(20) not null, -- FK to Periodes.Period
-- + check nulls according to period
-- + unique (id_calendar,periode)
year int NOT NULL,
month int NULL,
day_of_month int NULL,
day_of_week int NULL,
week_of_year int NULL
);
CREATE TABLE Param(
id_param INT IDENTITY(1,1) PRIMARY KEY,
param varchar(30),
periode varchar(20) not null, -- FK to Periodes.Period
-- + unique(id_param, periode)
);
CREATE TABLE Event (
id_calendar INT,
id_param INT,
periode varchar(20) not null -- + FK(id_calendar, periode) => Calendar(id_calendar, periode)
-- + FK(id_param, periode) => Param(id_param, periode)
);
CREATE TABLE Calendar (
id_calendar INT IDENTITY(1,1) PRIMARY KEY,
year int NOT NULL,
month int NULL,
day_of_month int NULL,
day_of_week int NULL,
week_of_year int NULL
);
CREATE TABLE Param(
id_param INT IDENTITY(1,1) PRIMARY KEY,
param varchar(30),
periode varchar(20),
CONSTRAINT check_periode CHECK (periode IN ('Day','Week','Month','Year'))
);
CREATE TABLE Event (
id_calendar INT,
id_param INT
);
每当向事件中插入新行时,我想检查它是否符合参数的周期。
如果周期='Day',那么年月none,day_of_month,day_of_week,week_of_year 应为 NULL。
如果periode = 'Week',那么只有day_of_month和day_of_week应该是NULL,
...
所以我需要同时加入 FK'S
并从那里构建一个 CHECK 约束。是否可以将 id_calendar 和 id_param 传递给 UDF
并从 CHECK 调用它?
出于性能原因,我不想使用全局查询,只想使用涉及受影响的行及其外键的检查。
是的。您可以定义一个函数来验证值并在 CHECK 约束中引用它。
CREATE FUNCTION CheckEvent(@id_Calendar INT, @id_param INT)
RETURNS BIT
AS
BEGIN
---LOGIC
END
添加检查约束以利用函数
ALTER TABLE Event ADD CONSTRAINT CHK_Event CHECK( dbo.CheckEvent(id_param , id_calendar) = 1)
为 periode
创建实体并在相关时引用它。使用组合键来表示 "the same periode
" 约束。
CREATE TABLE Periodes (
periode varchar(20),
CONSTRAINT check_periode CHECK (periode IN ('Day','Week','Month','Year'));
INSERT ...
CREATE TABLE Calendar (
id_calendar INT IDENTITY(1,1) PRIMARY KEY,
periode varchar(20) not null, -- FK to Periodes.Period
-- + check nulls according to period
-- + unique (id_calendar,periode)
year int NOT NULL,
month int NULL,
day_of_month int NULL,
day_of_week int NULL,
week_of_year int NULL
);
CREATE TABLE Param(
id_param INT IDENTITY(1,1) PRIMARY KEY,
param varchar(30),
periode varchar(20) not null, -- FK to Periodes.Period
-- + unique(id_param, periode)
);
CREATE TABLE Event (
id_calendar INT,
id_param INT,
periode varchar(20) not null -- + FK(id_calendar, periode) => Calendar(id_calendar, periode)
-- + FK(id_param, periode) => Param(id_param, periode)
);