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.

Chapter 6: Constraints<br />

The INSERT statement conflicted with the CHECK constraint<br />

“CN_CustomerDateInSystem”. The conflict occurred in database “Accounting”, table<br />

“dbo.Customers”, column ‘DateInSystem’.<br />

The statement has been terminated.<br />

Now if we change things to use a DateInSystem that meets the criterion used in the CHECK (anything<br />

with today’s date or earlier), the INSERT works fine.<br />

DEFAULT Constraints<br />

This will be the first of two different types of data integrity tools that will be called something to do with<br />

“default.” This is, unfortunately, very confusing, but I’ll do my best to make it clear (and I think it will<br />

become so).<br />

We’ll see the other type of default when we look at rules and defaults later in the chapter.<br />

A DEFAULT constraint, like all constraints, becomes an integral part of the table definition. It defines<br />

what to do when a new row is inserted that doesn’t include data for the column on which you have<br />

defined the default constraint. You can either define it as a literal value (say, setting a default salary to<br />

zero or “UNKNOWN” for a string column) or as one of several system values such as GETDATE().<br />

The main things to understand about a DEFAULT constraint are that:<br />

❑ Defaults are only used in INSERT statements. They are ignored for UPDATE and DELETE<br />

statements.<br />

❑ If any value is supplied in the INSERT, then the default is not used.<br />

❑ If no value is supplied, the default will always be used.<br />

Defaults are only made use of in INSERT statements. I cannot express enough how much this confuses<br />

many <strong>SQL</strong> <strong>Server</strong> beginners. Think about it this way: When you are first inserting the record, <strong>SQL</strong> <strong>Server</strong><br />

doesn’t have any kind of value for your column except what you supplied (if anything) or the default. If<br />

neither of these is supplied, then <strong>SQL</strong> <strong>Server</strong> will either insert a NULL (essentially amounting to “I don’t<br />

know”), or if your column definition says NOT NULL, then <strong>SQL</strong> <strong>Server</strong> will reject the record. After that<br />

first insert, however, <strong>SQL</strong> <strong>Server</strong> already has some value for that column. If you are updating that column,<br />

then it has your new value. If the column in question isn’t part of an UPDATE statement, then <strong>SQL</strong><br />

<strong>Server</strong> just leaves what is already in the column.<br />

If a value was provided for the column, then there is no reason to use the default. The supplied value<br />

is used.<br />

If no value is supplied, then the default will always be used. Now this seems simple enough until you<br />

think about the circumstance where a NULL value is what you actually want to go into that column for a<br />

record. If you don’t supply a value on a column that has a default defined, then the default will be used.<br />

What do you do if you really wanted it to be NULL? Say so — insert NULL as part of your INSERT statement.<br />

173

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

Saved successfully!

Ooh no, something went wrong!