title: Checking for a foreign key before adding it with a sql script
body: <p>I have a SQL script which adds a foreign key to a table, as shown below.</p>
  
  <pre><code>ALTER TABLE [security].[Pages] WITH NOCHECK
  ADD CONSTRAINT [FK_Pages_PageClasses] FOREIGN KEY ([PageClassId]) REFERENCES [security].[PageClasses]([PageClassId]) ON DELETE NO ACTION ON UPDATE NO ACTION;
  </code></pre>
  
  <p>Sometimes the table already has this foreign key so an error is shown when it is run from the Management Studio. Is it possible to add a query to check if the key exists before applying it? Is there a better way to do this? This must work for MS SQL 2005 and 2008.</p>