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 3: The Foundation Statements of T-<strong>SQL</strong><br />

70<br />

For your first insert, we’ll eliminate the optional column list and allow <strong>SQL</strong> <strong>Server</strong> to assume we’re providing<br />

something for every column:<br />

INSERT INTO Stores<br />

VALUES<br />

(‘TEST’, ‘Test Store’, ‘1234 Anywhere Street’, ‘Here’, ‘NY’, ‘00319’);<br />

As stated earlier, unless we provide a different column list (we’ll cover how to provide a column list<br />

shortly), all the values have to be supplied in the same order as the columns are defined in the table. After<br />

executing this query, you should see a statement that tells you that one row was affected by your query.<br />

Now, just for fun, try running the exact same query a second time. You’ll get the following error:<br />

Msg 2627, Level 14, State 1, Line 1<br />

Violation of PRIMARY KEY constraint ‘PK__Stores__1387E197’. Cannot insert duplicate<br />

key in object ‘dbo.Stores’.<br />

The statement has been terminated.<br />

Why did it work the first time and not the second? Because this table has a primary key that does not<br />

allow duplicate values for the StoreCode field. As long as we changed that one field, we could have left<br />

the rest of the columns alone and it would have taken the new row. We’ll see more of primary keys in<br />

the chapters on design and constraints.<br />

So let’s see what we inserted:<br />

SELECT *<br />

FROM Stores<br />

WHERE StoreCode = ‘TEST’;<br />

This query yields us exactly what we inserted:<br />

StoreCode Name Address City State Zip<br />

--------- ------------ ----------------------- -------------------- ----- -----<br />

TEST Test Store 1234 Anywhere Street Here NY 00319<br />

(1 row(s) affected)<br />

Note that I’ve trimmed a few spaces off the end of each column to help it fit on a page neatly, but the<br />

true data is just as we expected it to be.<br />

Now let’s try it again with modifications for inserting into specific columns:<br />

INSERT INTO Stores<br />

(StoreCode, Name, City, State, Zip)<br />

VALUES<br />

(‘TST2’, ‘Test Store’, ‘Here’, ‘NY’, ‘00319’);<br />

Note that on the line with the data values we’ve changed just two things. First, we’ve changed the value<br />

we are inserting into the primary key column so it won’t generate an error. Second, we’ve eliminated the<br />

value that was associated with the Address column, since we have omitted that column in our column list.<br />

There are a few different instances where we can skip a column in a column list and not provide any data

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

Saved successfully!

Ooh no, something went wrong!