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.

Chapter 8: Being Normal: Normalization and Other Basic Design Issues<br />

the table that you’re interested in. The default is column names only, but you should also take an interest<br />

in the choice of Custom as we did in Chapter 5; this or Standard is what you would use when you want<br />

to edit the table from right within the diagram (very nice!).<br />

Adding and Deleting Tables<br />

You can add a new table to the diagram in one of two ways.<br />

If you have a table that already exists in the database (but not in the diagram), but now you want to add<br />

it to your diagram, you simply click the Add Table button on the diagramming window’s toolbar. You’ll<br />

be presented with a list of all the tables in the database — just choose the one that you want to add, and<br />

it will appear along with any relationships it has to other tables in the diagram.<br />

If you want to add a completely new table, click on New Table on the diagramming window’s toolbar or<br />

right-click in the diagram and choose New Table; you’ll be asked for a name for the new table, and the table<br />

will be added to the diagram in Column Properties view. Simply edit the properties to have the column<br />

names, data types, and so on that you want, and you have a new table in the database.<br />

Try It Out Adding Tables from Within the Diagram<br />

238<br />

Let me take a moment to point out a couple of gotchas in this process.<br />

First, don’t forget to add a primary key to your table. <strong>SQL</strong> <strong>Server</strong> does not automatically<br />

do this, nor does it even prompt you (as Access does). This is a somewhat less<br />

than intuitive process. To add a primary key, you must select the columns that you<br />

want to have in the key. Then right-click and choose Set Primary Key.<br />

Next, be aware that your new table is not actually added to the database until you<br />

choose to save — this is also true of any edits that you make along the way.<br />

Let’s go ahead and add a table to our database just to learn how it works.<br />

Start by clicking on the New Table button in the diagramming window’s toolbar. When prompted for a<br />

name, choose a name of CustomerNotes (see Figure 8-11). You should then get a new window table<br />

using the Standard view:<br />

Notice that I’ve added several columns to my table along with a primary key (remember, select the<br />

columns you want to be the primary key, and then right-click and choose Set Primary Key). Before you<br />

click to save this, let’s try something out — open up the Management Studio, and try to run a query<br />

against your new table:<br />

USE AdventureWorks <strong>2008</strong>;<br />

SELECT * FROM CustomerNotes;<br />

Back comes an error message:<br />

Msg 208, Level 16, State 1, Line 1<br />

Invalid object name ‘CustomerNotes’.

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

Saved successfully!

Ooh no, something went wrong!