4

Following is a table creating script created by Entity Framework using the model shown in https://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
2

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.

1

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.
1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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