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

How It Works<br />

Our latest constraint works just as the last one did — the physical table definition is aware of the rules<br />

placed on the data it is to contain. Just as it would not allow string data to be inserted into a numeric column,<br />

now it will not allow a row to be inserted into the Orders table where the referenced employee in<br />

charge of that order is not a valid EmployeeID. If someone attempts to add a row that doesn’t match<br />

with an employee record, the insertion into Orders will be rejected in order to maintain the integrity of<br />

the database.<br />

Note that while we’ve added two foreign keys, there is still a line down at the bottom of our<br />

sp_helpconstraint results (or under the Messages tab if you have Results in Grid selected) that<br />

says No foreign keys reference this table. This is telling us that, while we do have foreign<br />

keys in this table that reference other tables, there are no other tables out there that reference this table. If<br />

you want to see the difference, just run sp_helpconstraint on the Customers or Employees tables<br />

at this point, and you’ll see that each of these tables is now referenced by our new Orders table.<br />

Making a Table Self-Referencing<br />

162<br />

What if the column you want to refer to isn’t in another table, but is actually right within the table in<br />

which you are building the reference? Can a table be both the referencing and the referenced table? You<br />

bet! Indeed, while this is far from the most common of situations, it is actually used with regularity.<br />

Before we actually create this self-referencing constraint that references a required (non-nullable) field<br />

that’s based on an identity column, it’s rather critical that we get at least one row in the table prior to the<br />

foreign key being added. Why? Well, the problem stems from the fact that the identity value is chosen and<br />

filled in after the foreign key has already been checked and enforced. That means that you don’t have a<br />

value yet for that first row to reference when the check happens. The only other option here is to go ahead<br />

and create the foreign key, but then disable it when adding the first row. We’ll learn about disabling constraints<br />

a little later in this chapter.<br />

OK — because this is a table that’s referencing a column based on an identity column, we need to get a<br />

primer row into the table before we add our constraint:<br />

INSERT INTO Employees<br />

(<br />

FirstName,<br />

LastName,<br />

Title,<br />

SSN,<br />

Salary,<br />

PriorSalary,<br />

HireDate,<br />

ManagerEmpID,<br />

Department<br />

)<br />

VALUES<br />

(<br />

‘Billy Bob’,<br />

‘Boson’,

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

Saved successfully!

Ooh no, something went wrong!