Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

Following is a table creating script created by Entity Framework using the model shown in http://codereview.stackexchange.com/questions/14077/is-it-proper-tpt-inheritance.

In this script, there are two alter statements. What is the need for the second ALTER statement?

SCRIPT

CREATE TABLE [dbo].[SellingItems_Book](
[Title] [nvarchar](max) NULL,
[Id] [int] NOT NULL,
 CONSTRAINT [PK_SellingItems_Book] PRIMARY KEY CLUSTERED 
(
[Id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, 
ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

First Alter

 ALTER TABLE [dbo].[SellingItems_Book]  WITH CHECK ADD  CONSTRAINT [FK_Book_inherits_SellingItem] FOREIGN KEY([Id])
REFERENCES [dbo].[SellingItems] ([Id])
GO

Second Alter

ALTER TABLE [dbo].[SellingItems_Book] CHECK CONSTRAINT [FK_Book_inherits_SellingItem]
GO
share|improve this question

3 Answers 3

up vote 2 down vote accepted

The second ALTER statement is superfluous because the WITH CHECK ADD will enable it. I believe they do that to make sure the check is enabled, which what the second statement does.

share|improve this answer

There is no need for the second alter statement, for the first one will already enable the constraint. It may be there because the script was automatically generated, to make sure it will be enabled if you've changed the default server settings (which makes new constraint enabled), or if you decide to change the first alter script.

For more information on Alter Statements see http://msdn.microsoft.com/pt-br/library/ms190273.aspx

share|improve this answer

There might be different reasons to explain why second alter statement is in the scripts:

  1. It is checking back that first alter constraint has been added.
  2. That script was automatically added by script generation tool for verification purposes.
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.