Example of creating a table

Use the newpubs database you created in the previous section if you want to try these examples. Otherwise, these changes will affect another database, like pubs2 or pubs3.

The simplest form of |create|table| is:

create table table_name 
(column_name datatype)

For example, to create a table named names with one column named some_name, and a fixed length of 11 bytes, enter:

create table names 
(some_name char(11)) 
drop table names

If you have set quoted_identifier on, both the table name and the column names can be delimited identifiers. Otherwise, they must follow the rules for identifiers described under “Identifiers”. Column names must be unique within a table, but you can use the same column name in different tables in the same database.

There must be a datatype for each column. The word “char” after the column name in the example above refers to the datatype of the column—the type of value that column will contain. Datatypes are discussed in Chapter 7, “Using and Creating Datatypes.”

The number in parentheses after the datatype gives the maximum number of bytes that can be stored in the column. You give a maximum length for some datatypes. Others have a system-defined length.

Be sure to put parentheses around the list of column names, and commas after each column definition. The last column definition does not need a comma after it.

NoteA variable cannot be used in a default if the default is part of a create table statement.