您好,登錄后才能下訂單哦!
-- 實例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
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。