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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

‘Head Cook & Bottle Washer’,<br />

‘123-45-6789’,<br />

100000,<br />

80000,<br />

‘1990-01-01’,<br />

1,<br />

‘Cooking and Bottling’<br />

);<br />

Now that we have a primer row, we can add in our foreign key. In an ALTER situation, this works just the<br />

same as any other foreign key definition. We can now try this out:<br />

ALTER TABLE Employees<br />

ADD CONSTRAINT FK_EmployeeHasManager<br />

FOREIGN KEY (ManagerEmpID) REFERENCES Employees(EmployeeID);<br />

There is one difference with a CREATE statement. You can (but you don’t have to) leave out the FOREIGN KEY<br />

phrasing and just use the REFERENCES clause. We already have our Employees table set up at this point,<br />

but if we were creating it from scratch, the script would appear as follows (pay particular attention to<br />

the foreign key on the ManagerEmpID column):<br />

CREATE TABLE Employees (<br />

EmployeeID int IDENTITY NOT NULL<br />

PRIMARY KEY,<br />

FirstName varchar (25) NOT NULL,<br />

MiddleInitial char (1) NULL,<br />

LastName varchar (25) NOT NULL,<br />

Title varchar (25) NOT NULL,<br />

SSN varchar (11) NOT NULL,<br />

Salary money NOT NULL,<br />

PriorSalary money NOT NULL,<br />

LastRaise AS Salary -PriorSalary,<br />

HireDate smalldatetime NOT NULL,<br />

TerminationDate smalldatetime NULL,<br />

ManagerEmpID int NOT NULL<br />

REFERENCES Employees(EmployeeID),<br />

Department varchar (25) NOT NULL<br />

);<br />

Chapter 6: Constraints<br />

It’s worth noting that, if you try to DROP the Employees table at this point (to run the<br />

second example), you’re going to get an error. Why? Well, when we established the<br />

reference in our Orders table to the Employees table, the two tables became “schemabound;”<br />

that is, the Employees table now knows that it has what is called a dependency<br />

on it. <strong>SQL</strong> <strong>Server</strong> will not let you drop a table that is referenced by another table. You<br />

have to drop the foreign key in the Orders table before <strong>SQL</strong> <strong>Server</strong> will allow you to<br />

delete the Employees table (or the Customers table for that matter).<br />

In addition, doing the self-referencing foreign key in the constraint doesn’t allow us<br />

to get our primer row in, so it’s important that you do it this way only when the column<br />

the foreign key constraint is placed on allows NULLs. That way the first row can<br />

have a NULL in that column and avoid the need for a primer row.<br />

163

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

Saved successfully!

Ooh no, something went wrong!