SQL Server 2008 supports the
creation of tables using T-SQL, the SQL Server Management Studio (SSMS)
Object Explorer and the SSMS Database Diagram Editor. Regardless of the
tool you choose, creating a table involves naming the table, defining
the columns, and assigning properties to the columns. The visual tools
(such as Object Explorer and database diagrams) are usually the best
starting point for creating tables. These tools offer drop-down boxes
that allow you to choose the data types for your columns and check boxes
that allow you to define their nullability.
Using Object Explorer to Create Tables
The Object Explorer in SSMS has a Tables node under each database listed. You can add tables via the Object Explorer by right-clicking this Tables node. Figure 1 shows the New Table option displayed after you right-click the Tables node in Object Explorer. The top-right side of the screen shown in Figure 1 is the table creation screen that allows you to enter the column name and data type and to set the Allow Nulls option.
The data entry area under
Column Name is a free-form area where you can define a column name. You
can select the data type from the Data Type drop-down, which displays
the data types available with SQL Server. The Allow Nulls option is
Boolean in nature and is either checked or not checked. For each column
selected, a Column Properties section is displayed in SSMS, providing a
myriad of additional properties that you can assign to each column.
Using Database Diagrams to Create Tables
You can use the
database diagrams for a more robust visual representation of your
tables. You view them from within SSMS, and they give you the distinct
advantage of being able to display multiple tables and the relationships
between these tables. The Database Diagram Editor behaves similarly to
other data modeling tools that allow you to move related tables around
in the diagram and group them accordingly.
Figure 2 shows several screens related to database diagrams. The left side of Figure 2 shows the Object Explorer and the resulting New Database Diagram option that is displayed if you right-click the Database Diagrams node. The right side of the screen shows the diagram design window. In this example, the existing Department table from the AdventureWorks2008 database was added to the diagram, and a new Printer
table was added as well. The printer table was added by right-clicking
in the diagram design window and selecting the New Table option.
The column names and related attributes for the new Printer table in Figure 2
were added using the table entry fields. The data entry screen for the
table is similar to the one provided with the Object Explorer. You enter
column names, along with their associated data types and nullability
option.
The advantage of
database diagrams is that you can define relationships and show them
with a visual representation. This visual view provides a much easier
way to view the table structures in a database. In the example shown in Figure 2, the line drawn between the Department and Printer
tables represents a relationship between these two tables. You define
such a foreign key relationship in the database diagram by dragging the
related column from one table to the other related table.
Using T-SQL to Create Tables
Ultimately, all the tables
created with the visual tools can also be created by using T-SQL. As
with many of the SSMS tools, database objects can be resolved or
scripted into T-SQL statements. Let’s examine the T-SQL syntax to better
understand some of the table creation options; then we can discuss the
definition of the columns in each table.
The full T-SQL CREATE TABLE
syntax is extensive. It includes options to define table constraints,
indexes, and index options. Listing 1
shows the basic T-SQL syntax. This syntax is enough to enable you to create a table
with its associated column definitions.
Listing 1. Basic T-SQL CREATE TABLE Syntax
CREATE TABLE
[ database_name . [ schema_name ] . | schema_name . ] table_name
( { <column_definition> | <computed_column_definition> }
[ <table_constraint> ] [ ,...n ] )
[ ON { partition_scheme_name ( partition_column_name ) | filegroup
| "default" } ]
[ { TEXTIMAGE_ON { filegroup | "default" } ]
[ ; ]
<column_definition> ::=
column_name <data_type>
[ COLLATE collation_name ]
[ NULL | NOT NULL ]
[
[ CONSTRAINT constraint_name ] DEFAULT constant_expression ]
| [ IDENTITY [ ( seed ,increment ) ] [ NOT FOR REPLICATION ]
]
[ ROWGUIDCOL ] [ <column_constraint> [ ...n ] ]
<data type> ::=
[ type_schema_name . ] type_name
[ ( precision [ , scale ] | max |
[ { CONTENT | DOCUMENT } ] xml_schema_collection ) ]
|
The number of options in this
basic syntax can be daunting, but the reality is that you can exclude
many of the options and execute a relatively simple statement. Listing 2 is an example of a simple statement you can use to create a table. This listing shows a CREATE TABLE statement that you can use to create the Printers table that was shown in Figure 2.
Listing 2. A Basic T-SQL CREATE TABLE Example
CREATE TABLE Printer
(
PrinterID int NOT NULL,
DepartmentID smallint NOT NULL,
PrinterName varchar(50) NOT NULL,
Manufacturer varchar(50) NOT NULL,
PrinterDescription varchar(250) NULL
)
|
The CREATE TABLE statement in Listing 2
specifies the table to create, followed by an ordered list of columns
to add to the table. The following section describes the specifics
related to defining the columns.
Tip
SSMS provides several methods for generating the T-SQL code to create tables. Therefore, you rarely need to type the CREATE TABLE
statements from scratch. Instead, you can use the friendly graphical
user interface (GUI) screens that enable you to define the table, and
then you can generate the T-SQL script. For example, you can right-click
a new table in the database diagram and select Generate Change Script
to generate the associated T-SQL for the table.
One of the important considerations during table creation is schema assignment. Schemas
allow you to logically group objects (including tables) and define
ownership, independent of the individual users in the database. Schema
enhancements introduced in SQL Server 2005 are still available in SQL
Server 2008 and can play a significant role in the definition of tables
in a database. Consider, for example, the AdventureWorks2008
database that ships with SQL Server 2008. The tables in this database
have been assigned to schemas that group the tables according to their
functional areas. The schemas in the AdventureWorks2008 database include Sales, Purchasing, Person, Production, HumanResources, and dbo. Some sample tables in the Person schema include the Person and Address tables. The Purchasing schema includes tables that relate to purchasing, including the PurchaseOrderHeader and Vendor tables.
The designation of a schema in the CREATE TABLE statement is relatively simple. Listing 3 includes a three-part table name for the creation of a Printer table in the HumanResources schema. The database name (AdventureWorks2008) is the first part of the name, followed by a schema name (HumanResources). The last part of Listing 3 shows a sample SELECT statement against the Printer table that is owned by the HumanResources
schema. The schema name must precede the table, when referenced. The
only exception to this rule is tables that belong to the default schema
assigned to the user executing the query.
Listing 3. Using T-SQL CREATE TABLE in a Schema
CREATE TABLE AdventureWorks2008.HumanResources.Printer
(
PrinterID int NOT NULL,
DepartmentID smallint NOT NULL,
PrinterName varchar(50) NOT NULL,
Manufacturer varchar(50) NOT NULL,
PrinterDescription varchar(250) NULL
)
go
select * from HumanResources.Printer
|
The
creation of schemas and assignment of tables to schemas requires some
forethought.