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.

And then we moved on to a more specific start (indeed, it’s the first line of our statement that will create<br />

the table) on creating a table called Customers:<br />

CREATE TABLE Customers<br />

Our Customers table is going to be the first table in a database we will be putting together to track our<br />

company’s accounting. We’ll be looking at designing a database in a couple of chapters, but we’ll go<br />

ahead and get started on our database by building a couple of tables to learn our CREATE TABLE statement.<br />

We’ll look at most of the concepts of table construction in this section, but we’ll save a few for later<br />

on in the book. That being said, let’s get started building the first of several tables.<br />

I’m going to add in a USE line prior to my CREATE code so that I’m sure that, when I<br />

run the script, the table is created in the proper database. We’ll then follow up that first line that we’ve<br />

already seen with a few columns.<br />

Any script you create for regular use with a particular database should include a USE command with<br />

the name of that database. This ensures that you really are creating, altering, and dropping the objects<br />

in the database you intend. More than once have I been the victim of my own stupidity when I blindly<br />

opened up a script and executed it only to find that the wrong database was current, and any tables<br />

with the same name had been dropped (thus losing all data) and replaced by a new layout. You can also<br />

tell when other people have done this by taking a look around the master database — you’ll often find<br />

several extraneous tables in that database from people running CREATE scripts that were meant to go<br />

somewhere else.<br />

USE Accounting<br />

CREATE TABLE Customers<br />

(<br />

CustomerNo int IDENTITY NOT NULL,<br />

CustomerName varchar(30) NOT NULL,<br />

Address1 varchar(30) NOT NULL,<br />

Address2 varchar(30) NOT NULL,<br />

City varchar(20) NOT NULL,<br />

State char(2) NOT NULL,<br />

Zip varchar(10) NOT NULL,<br />

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

Phone char(15) NOT NULL,<br />

FedIDNo varchar(9) NOT NULL,<br />

DateInSystem smalldatetime NOT NULL<br />

)<br />

This is a somewhat simplified table vs. what we would probably use in real life, but there’s plenty of<br />

time to change it later (and we will).<br />

Once we’ve built the table, we want to verify that it was indeed created, and that it has all the columns<br />

and types that we expect. To do this, we can make use of several commands, but perhaps the best is one<br />

that will seem like an old friend before you’re done with this book: sp_help. The syntax is simple:<br />

EXEC sp_help <br />

To specify the table object that we just created, try executing the following code:<br />

EXEC sp_help Customers<br />

Chapter 5: Creating and Altering Tables<br />

131

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

Saved successfully!

Ooh no, something went wrong!