Description:
Creates new database table.
The table structure itself consists of individual columns. Each column has its own name, data type and possible other attributes. Each table in the SQL Server must have its own primary key (PRIMARY KEY attribute). The data are processed by rows in the table.
Syntax:
CREATE TABLE table_name
(
column_name data_type [
NULL |
NOT NULL ] [
PRIMARY KEY |
UNIQUE ] [
IDENTITY ] [
DEFAULT default_expression ] [
CHECK check_expression ]
[, ...]
)
table_name |
Name of the table. |
column_name |
Column name. |
data_type |
Column data type (bigint, int, smallint, tinyint, bit, decimal, numeric, money, smallmoney, float, real, datetime, smalldatetime, char, varchar, ntext, binary, varbinary, image). |
NULL |
Enable using the NULL value in the column. |
NOT NULL |
Disable using the NULL value in the column. |
PRIMARY KEY |
Specifies column as the primary key of the table (UNIQUE by default but UNIQUE mustn't be stated). |
UNIQUE |
Values in this column must be unique (writing the record with the value that already exists in this column, fails). |
IDENTITY |
column (often primary key) whose unique value is supplied by the SQL server itself on writing (only 1 such column is enabled in the table). |
DEFAULT |
Definition of the default value, used on inserting new record, if the value for this column is not stated. |
default_expression |
the default value is the result of this expression. |
CHECK |
On inserting or editing the value its check will be performed. If the check fails, then the whole operation fails. |
check_expression |
The check itself where the expression result specifies whether the operation is valid or not. |
Example:
Creates new table data with columns time (time, primary key, must not be NULL), 'flags' (integer, must not be NULL, default 0) and 'value' (real number, must not be NULL, default 0).
CREATE TABLE data
(
time datetime PRIMARY KEY NOT NULL,
flags smallint NOT NULL DEFAULT 0,
value float NOT NULL DEFAULT 0
)