That??™s only the first part. The last part is to
add a foreign key constraint on the second table. A constraint is a rule used to maintain data integrity of
a table.
To create the actual relationship you tell the HumanResources.Employee table that its ContactID column
is a foreign key to the ContactID column in the Person.Contact table. This can be accomplished by the
following:
ALTER TABLE [HumanResources].[Employee] ADD CONSTRAINT
[FK_Employee_Contact_ContactID]
FOREIGN KEY([ContactID])
REFERENCES [Person].[Contact] ([ContactID])
This T-SQL code creates a foreign key constraint on the ContactID column referencing the ContactID
column in the Person.Contact table. The constraint is used to determine the action on the values in the
related tables. For example, if a value that is used in one or more related tables is deleted, the constraint
determines whether the value in the related table is also deleted, left in, or set to null.
To see this in action, open SQL Server Management Studio and execute the following in a query window:
SELECT ContactID, FirstName, LastName
FROM Person.
Pages:
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351