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.

Chapter 6: Constraints<br />

166<br />

This is going to expose you to one of the hassles of relational database work — dependency chains. A<br />

dependency chain exists when you have something that is, in turn, dependent on something else, which<br />

may yet be dependent on something else, and so on. There’s really nothing you can do about this. It’s<br />

just something that comes along with database work. You have to start at the top of the chain and work<br />

your way down to what you need inserted. Fortunately, the records you need are often already there,<br />

save one or two dependency levels.<br />

OK, so in order to get our row into our OrderDetails table, we must also have a record already in the<br />

Orders table. Unfortunately, getting a row into the Orders table requires that we have one in the Customers<br />

table (remember that foreign key we built on Orders?). So, let’s take care of it a step at a time:<br />

INSERT INTO Customers -- Our Customer.<br />

-- Remember that CustomerNo is<br />

-- an Identity column<br />

VALUES<br />

(‘Billy Bob’‘s Shoes’,<br />

‘123 Main St.’,<br />

‘ ‘,<br />

‘Vancouver’,<br />

‘WA’,<br />

‘98685’,<br />

‘Billy Bob’,<br />

‘(360) 555-1234’,<br />

‘931234567’,<br />

GETDATE()<br />

);<br />

Now we have a customer, so let’s select against the record we just inserted to be sure:<br />

Customer No 1<br />

Customer Name Billy Bob’s Shoes<br />

Address 1 123 Main Street<br />

Address 2<br />

City Vancouver<br />

State WA<br />

Zip 98685<br />

Contact Billy Bob<br />

Phone (360) 555-1234<br />

FedIDNo 931234567<br />

DateInSystem 2000-07-1021:17:00

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

Saved successfully!

Ooh no, something went wrong!