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.

for it in the INSERT statement. For now, we’re just taking advantage of the fact that the Address column<br />

is not a required column — that is, it accepts NULLs. Since we’re not providing a value for this column and<br />

since it has no default (we’ll see more on defaults later on), this column will be set to NULL when we perform<br />

our INSERT. Let’s verify that by rerunning our test SELECT statement with one slight modification:<br />

SELECT *<br />

FROM Stores<br />

WHERE StoreCode = ‘TST2’;<br />

Now we see something a little different:<br />

StoreCode Name Address City State Zip<br />

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

TST2 Test Store NULL Here NY 00319<br />

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

Notice that a NULL was inserted for the column that we skipped.<br />

Note that the columns have to be nullable in order to do this. What does that mean? Pretty much what it<br />

sounds like: It means that you are allowed to have NULL values for that column. Believe me, we will be<br />

discussing the nullability of columns at great length in this book, but for now, just realize that some columns<br />

allow NULLs and some don’t. We can always skip providing information for columns that allow NULLs.<br />

If, however, the column is not nullable, then one of three conditions must exist, or we will receive an<br />

error and the INSERT will be rejected:<br />

❑ The column has been defined with a default value. A default is a constant value that is inserted if<br />

no other value is provided. We will learn how to define defaults in Chapter 7.<br />

❑ The column is defined to receive some form of system-generated value. The most common of<br />

these is an IDENTITY value (covered more in the design chapter), where the system typically<br />

starts counting first at one row, increments to two for the second, and so on. These aren’t really<br />

row numbers, as rows may be deleted later on and numbers can, under some conditions, get<br />

skipped, but they serve to make sure each row has its own identifier.<br />

❑ We supply a value for the column.<br />

Just for completeness, let’s perform one more INSERT statement. This time, we’ll insert a new sale into<br />

the Sales table. To view the properties of the Sales table, we can either open its Properties dialog as we<br />

did with the Stores table, or we can run a system-stored procedure called sp_help. sp_help will<br />

report information about any database object, user-defined data type, or <strong>SQL</strong> <strong>Server</strong> data type. The syntax<br />

for using sp_help is as follows:<br />

EXEC sp_help <br />

To view the properties of the Sales table, we just have to type the following into the Query window:<br />

EXEC sp_help Sales;<br />

Chapter 3: The Foundation Statements of T-<strong>SQL</strong><br />

71

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

Saved successfully!

Ooh no, something went wrong!