The column_specification in a CREATE TABLE statement describes the characteristics of the column.
This statement has the following format:
column_name datatype
[[WITH] DEFAULT default_spec | WITH DEFAULT | NOT DEFAULT]
[WITH NULL | NOT NULL]
[[CONSTRAINT constraint_name] column_constraint
{ [CONSTRAINT constraint_name] column_constraint}]
[COLLATE collation_name]
Assigns a valid name to the column.
Assigns a valid data type to the column. If CREATE TABLE...AS SELECT is specified, the new table takes its column names and formats from the results of the SELECT clause of the subselect specified in the AS clause (unless different column names are specified).
Specifies whether the column is mandatory, as described in Default Clause.
Specifies whether the column accept nulls, as described in Null Clause.
Specifies checks to be performed on the contents of the column to ensure appropriate data values, as described in Constraints.
Specifies a collation sequence to be used on the column, as one of the following:
Specifies collation for columns containing Unicode data (nchar and nvarchar data types). This is the default collation for Unicode columns.
Specifies case insensitive collation for columns containing Unicode data (nchar and nvarchar data types).
Specifies the collation for columns containing char, C, varchar, and text data. This is the default collation for non-Unicode columns.
The WITH|NOT DEFAULT clause in the column specification specifies whether a column requires an entry.
This clause has the following format:
[WITH] DEFAULT default_spec | WITH DEFAULT | NOT DEFAULT
Indicates the column is mandatory (requires an entry).
Indicates that if no value is provided, the DBMS Server inserts 0 for numeric and money columns, or an empty string for character and date columns.
Indicates that if no value is provided (because none is required), the DBMS Server inserts the default value. The default value must be compatible with the data type of the column.
For character columns, valid default values include the following constants:
The following is an example of using the DEFAULT clause:
CREATE TABLE DEPT(dname CHAR(10),
location CHAR(10) DEFAULT 'NY',
creation DATE DEFAULT '01/01/03',
budget MONEY DEFAULT 10000);
The following considerations and restrictions apply when specifying a default value for a column:
To specify whether a column accepts null values, specify the WITH|NOT NULL clause in the column specification.
This clause has the following format:
WITH NULL | NOT NULL
Indicates that the column accepts nulls. If no value is supplied by the user, null is inserted. This is the default for all data types except a system_maintained logical key.
Indicates that the column does not accept nulls.
The WITH|NOT NULL clause works in combination with the WITH|NOT DEFAULT clause, as follows:
The column accepts nulls. If no value is provided, the DBMS Server inserts a null.
The column accepts null values. If no value is provided, the DBMS Server inserts a 0 or blank string, depending on the data type.
The column accepts null values. The user must provide a value (mandatory column).
The column does not accept nulls. If no value is provided, the DBMS Server inserts 0 for numeric and money columns, or an empty string for character and date columns.
The column is mandatory and does not accept nulls, which is typical for primary key columns.