-- 实例1: 创建财政年度表 (约束)

-- 新建表:

Create table FiscalYears
(
fiscal_year INTEGER NOT NULL PRIMARY KEY,
start_date DATE NOT NULL,
constraint valid_start_date
CHECK(
year(start_date) = fiscal_year -1
AND MONTH(start_date)=10
AND DAY(start_date)=01
),

end_date date NOT NULL,
constraint valid_end_date
CHECK(
year(end_date) = fiscal_year
AND MONTH(end_date)=10
AND DAY(end_date)=01
)
);

-- 删除约束 valid_end_date

Alter table FiscalYears
drop constraint valid_end_date

--新建约束
Alter table FiscalYears
add constraint valid_end_date
CHECK(
year(end_date) = fiscal_year
AND MONTH(end_date)=09
AND DAY(end_date)=30
)

--或:

ALTER TABLE [dbo].[FiscalYears]
WITH CHECK
ADD CONSTRAINT [valid_end_date]
CHECK(
(
datepart(year,[end_date])=[fiscal_year]
AND datepart(month,[end_date])=(9)
AND datepart(day,[end_date])=(30)
)
)
GO

--插入数据到表 FiscalYears
insert into dbo.FiscalYears
values(
'1990',
'1989-10-01',
'1990-09-30'
)

--参考: http://www.xuebuyuan.com/53328.html ;

-- http://m.blog.csdn.net/blog/zhaoyh0530/4535987