开发者

Strange constraints in generated SQL

开发者 https://www.devze.com 2022-12-15 14:33 出处:网络
Note: This question is not related to Visual Paradigm. Anyone thatknows SQL could answer it. I am using Visual Paradigm to model a database in our project (using ER diagrams). When Visual Paradigm

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

精彩评论

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