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 />

Dropping Rules<br />

If you want to completely eliminate a rule from your database, you use the same DROP syntax that we’ve<br />

already become familiar with for tables:<br />

Defaults<br />

DROP RULE <br />

Defaults are even more similar to their cousin — a default constraint — than a rule is to a CHECK constraint.<br />

Indeed, they work identically, with the only real differences being in the way that they are<br />

attached to a table and the default’s (the object, not the constraint) support for a user-defined data type.<br />

The syntax for defining a default works much as it did for a rule:<br />

CREATE DEFAULT <br />

AS <br />

Therefore, to define a default of zero for our Salary:<br />

CREATE DEFAULT SalaryDefault<br />

AS 0;<br />

Again, a default is worthless without being bound to something. To bind it, we make use of sp_bindefault,<br />

which is, other than the procedure name, identical in syntax to the sp_bindrule procedure:<br />

EXEC sp_bindefault ‘SalaryDefault’, ‘Employees.Salary’;<br />

To unbind the default from the table, we use sp_unbindefault:<br />

EXEC sp_unbindefault ‘Employees.Salary’;<br />

Keep in mind that the futureonly_flag also applies to this stored procedure; it is just not used here.<br />

Dropping Defaults<br />

182<br />

The concept of defaults vs. DEFAULT constraints is wildly difficult for a lot of people<br />

to grasp. After all, they have almost the same name. If we refer to “default,” then we<br />

are referring to either the object-based default (what we’re talking about in this section),<br />

or a shorthand to the actual default value (that will be supplied if we don’t<br />

provide an explicit value). If we refer to a “DEFAULT constraint,” then we are talking<br />

about the non-object-based solution — the solution that is an integral part of the<br />

table definition.<br />

If you want to completely eliminate a default from your database, you use the same DROP syntax that<br />

we’ve already become familiar with for tables and rules:<br />

DROP DEFAULT

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

Saved successfully!

Ooh no, something went wrong!