17.06.2013 Views

Beginning Microsoft SQL Server 2008 ... - S3 Tech Training

Beginning Microsoft SQL Server 2008 ... - S3 Tech Training

Beginning Microsoft SQL Server 2008 ... - S3 Tech Training

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Adding a DEFAULT Constraint to an Existing Table<br />

While this one is still pretty much more of the same, there is a slight twist. We make use of our ALTER<br />

statement and ADD the constraint as before, but we add a FOR operator to tell <strong>SQL</strong> <strong>Server</strong> what column is<br />

the target for the DEFAULT:<br />

ALTER TABLE Customers<br />

ADD CONSTRAINT CN_CustomerDefaultDateInSystem<br />

DEFAULT GETDATE() FOR DateInSystem;<br />

And an extra example:<br />

ALTER TABLE Customers<br />

ADD CONSTRAINT CN_CustomerAddress<br />

DEFAULT ‘UNKNOWN’ FOR Address1;<br />

As with all constraints except for a PRIMARY KEY, we are able to add more than one per table.<br />

You can mix and match any and all of these constraints as you choose — just be careful<br />

not to create constraints that have mutually exclusive conditions. For example, don’t have<br />

one constraint that says that col1 > col2 and another one that says that col2 > col1.<br />

<strong>SQL</strong> <strong>Server</strong> will let you do this, and you wouldn’t see the issues with it until runtime.<br />

Disabling Constraints<br />

Sometimes we want to eliminate constraints, either just for a time or permanently. It probably doesn’t take<br />

much thought to realize that <strong>SQL</strong> <strong>Server</strong> must give us some way of deleting constraints, but <strong>SQL</strong> <strong>Server</strong><br />

also allows us to just deactivate a FOREIGN KEY or CHECK constraint while otherwise leaving it intact.<br />

The concept of turning off a data integrity rule might seem rather ludicrous at first. I mean, why would<br />

you want to turn off the thing that makes sure you don’t have bad data? First, realize that any reason for<br />

turning off a data integrity rule is going to be a temporary thing (otherwise you would be dropping it<br />

entirely). The usual reason to disable a data integrity rule is because you already have bad data. This<br />

data usually falls into two categories:<br />

❑ Data that’s already in your database when you create the constraint<br />

❑ Data that you want to add after the constraint is already built<br />

<strong>SQL</strong> <strong>Server</strong> allows us to turn the integrity check off long enough to deal with the bad data we want to<br />

make an exception for, and then re-enable the integrity check later — all without physically removing<br />

the data integrity check.<br />

You cannot disable PRIMARY KEY or UNIQUE constraints.<br />

Chapter 6: Constraints<br />

175

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!