开发者

Strange constraints in generated SQL

Note: This question is not related to Visual Paradigm. Anyone that knows SQL could answer it.


I am using Visual Paradigm to model a database in our project (using ER diagrams). When Visual Paradigm generates the SQL equivalent for the database and I import it in MSSQL it works pretty.

I took a look in generated SQL code to make sure anything is right and I saw something strange!:

For tblContracts I defined a constraint named EndAfterStart to make sure the value of endDate is always bigger than startDate. The generated SQL code for this constraint is here:

IF NOT EXISTS (SELECT * FROM sys.check_constraints WHERE object_id=OBJECT_ID(N'[dbo].[EndAfterStart]'))
ALTER TABLE [dbo].[tblContracts] WITH CHECK ADD CONSTRAINT [EndAfterStart] CHECK (([startDate]<=[endDate]))
GO
ALTER TABLE [dbo].[tblContracts] CHECK CONSTRAINT [EndAfterStart]
GO

And the questions:

  1. Why tblContracts is altered twice to add this开发者_Go百科 constraint?!
  2. Isn't first two lines enough?
  3. What is different between second line and forth line?


First and second lines create EndAfterStart constraint if it doesn't exist. Fourth line enables EndAfterStart constraint.


The second line adds the constraint to the table; the fourth line enables the constraint.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜