In this tutorial, we learn how to Create Table in SQL Server. We can create Tables either using the Create Table
statement or using the Table designer option in SSMS. We learn both of these methods of Creating the SQL Server tables in the tutorial.
Table of Contents
Before Creating the tables
Table Names
The fully qualified table names consists of three parts. database name, schema name & the actual table name. The fully qualified table name must be UNIQUE in a SQL Server instance.
1 2 3 | database_name.schema_name.table_name |
Note that, whenever we create the database, the SQL Server automatically creates the default schema dbo
. We can create more schema using the create schema <schema_name>
query.
The table name must be unique under a schema.
The name must follow the naming rules for the identifiers. Also note that you should avoid using the reserved keywords. If you use them then you must always enclose the table in square brackets as delimited identifiers.
Columns
We must also give a name to each column, which also must follow the naming rules for the identifiers. The column name must be UNIQUE within the table. Again avoid using the reserved keywords, else you need to follow the delimited identifiers rules when referring the columns in Query.
Data Types
It is also necessary to assign data type to each column. The data type defines the type of the data that the column is going to hold. This can be a number, string , date and Boolean etc.
Primary Key
Each table must also have primary key (although SQL Server allows you to create table without Primary Key). Primary key is a column (or columns), whose value we can use to uniquely identify a row.
NULL/NOT NULL
We also need to specify whether the column accepts the NULL value or not using the keyword NULL
(allows NULL) or NOT NULL
(does not allows NULL).
Using NULL allows us to insert a row to the table, without providing any value to the column
Create Table Syntax
The Simplified syntax of creating the table as shown below
1 2 3 4 5 6 7 8 | CREATE TABLE [database_name.][schema_name.]table_name ( column_1 data_type PRIMARY KEY, <- column definitions column_2 data_type, <- column definitions ..., column_n data_type <- column definitions ); |
The syntax starts with CREATE TABLE
. We follow it up with the name of the table table_name
.
Next inside the brackets we specify the column definitions. Each column definition must start with column name (column_1
, column_2
etc.) followed by data type (data_type
) of the column.
We also need to designate at least one column as primary key by using the PRIMARY KEY
keyword after the column name.
Create Table Example Query
The following is the example Create Table Query for a table with the name employee
. We have added 7 columns in the table.
1 2 3 4 5 6 7 8 9 10 11 | CREATE TABLE Employee ( EmployeeID int NOT NULL PRIMARY KEY, FirstName varchar(50) NOT NULL, LastName varchar(50) NOT NULL, Department varchar(20) NOT NULL, Designation varchar(15) NOT NULL, Salary decimal(10, 0) NOT NULL, Email varchar(50) NULL, ) |
The EmployeeID
is the Primary Key with the data type as int
, which stands for integer value. You can only store numbers in this column. We also mark this as PRIMARY KEY
.
The Salary
column has the data type decimal(10,0)
,
The other columns are FirstName
, LastName
, Department
, Designation
& Email
. All of them has the data type as Varchar
of various lengths. The varchar data type accepts string data. We specify the max length of the string can accept as the argument to varchar.
The columns can be either NULL
or NOT NULL
. If we do not specify anything, then the SQL Server treats the column as NULL
.
Create table Using SQL Statement
Now, let us create the table using the above query.
Open SSMS. If you have not installed it, then refer to the install SSMS tutorial. Connect to the SQL Server instance.
First, we need to create a database to hold our table. Create a new database HR
. You can refer to the tutorial how to create the database.
- Select the newly created database i.e.
HR
. - On the menu select
File -> New -> Query with current connection
- This will open the Query window
- Paste the following query to the Query window
- Click on Execute to run the query
- Commands completed successfully message appears
1 2 3 4 5 6 7 8 9 10 11 | CREATE TABLE Employee ( EmployeeID int NOT NULL PRIMARY KEY, FirstName varchar(50) NOT NULL, LastName varchar(50) NOT NULL, Department varchar(20) NOT NULL, Designation varchar(15) NOT NULL, Salary decimal(10, 0) NOT NULL, Email varchar(50) NULL, ) |
You can view the employee table from the object explorer.
Select the tables under HR
database under database
node. Right click and refresh. You will see the employee
table. Expand it and then expand the columns node to view the columns.
Create Table Using Table Designer
We can also create the table using the Table Designer
- Open SSMS and connect to the database instance.
- Select Database node and expand it.
- Select the database, where you want to create the new table (
HR
in the example below). - Select Tables node and right click and select
new -> tables
. - Type column names, choose data types from the drop down, and choose whether to allow nulls for each column.
- Select the
EmployeeID
Column and click on the icon the toolbar to mark it as Primary key. (You can also use theTable Designer -> Set Primary Key
Option. - Click on Save Icon in the tool bar or
file -> Save
to save the table. - Enter the table name as Employee and click on Ok
Syntax of Create Table Command
The create table syntax is much more complex. The complete Syntax is as shown below. We will learn them in other tutorials.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 | CREATE TABLE { database_name.schema_name.table_name | schema_name.table_name | table_name } [ AS FileTable ] ( { <column_definition> | <computed_column_definition> | <column_set_definition> | [ <table_constraint> ] [ ,... n ] | [ <table_index> ] } [ ,...n ] [ PERIOD FOR SYSTEM_TIME ( system_start_time_column_name , system_end_time_column_name ) ] ) [ ON { partition_scheme_name ( partition_column_name ) | filegroup | "default" } ] [ TEXTIMAGE_ON { filegroup | "default" } ] [ FILESTREAM_ON { partition_scheme_name | filegroup | "default" } ] [ WITH ( <table_option> [ ,...n ] ) ] [ ; ] <column_definition> ::= column_name <data_type> [ FILESTREAM ] [ COLLATE collation_name ] [ SPARSE ] [ MASKED WITH ( FUNCTION = ' mask_function ') ] [ [ CONSTRAINT constraint_name ] DEFAULT constant_expression ] [ IDENTITY [ ( seed,increment ) ] [ NOT FOR REPLICATION ] [ GENERATED ALWAYS AS ROW { START | END } [ HIDDEN ] ] [ NULL | NOT NULL ] [ ROWGUIDCOL ] [ ENCRYPTED WITH ( COLUMN_ENCRYPTION_KEY = key_name , ENCRYPTION_TYPE = { DETERMINISTIC | RANDOMIZED } , ALGORITHM = 'AEAD_AES_256_CBC_HMAC_SHA_256' ) ] [ <column_constraint> [, ...n ] ] [ <column_index> ] <data_type> ::= [ type_schema_name . ] type_name [ ( precision [ , scale ] | max | [ { CONTENT | DOCUMENT } ] xml_schema_collection ) ] <column_constraint> ::= [ CONSTRAINT constraint_name ] { { PRIMARY KEY | UNIQUE } [ CLUSTERED | NONCLUSTERED ] [ WITH FILLFACTOR = fillfactor | WITH ( < index_option > [ , ...n ] ) ] [ ON { partition_scheme_name ( partition_column_name ) | filegroup | "default" } ] | [ FOREIGN KEY ] REFERENCES [ schema_name . ] referenced_table_name [ ( ref_column ) ] [ ON DELETE { NO ACTION | CASCADE | SET NULL | SET DEFAULT } ] [ ON UPDATE { NO ACTION | CASCADE | SET NULL | SET DEFAULT } ] [ NOT FOR REPLICATION ] | CHECK [ NOT FOR REPLICATION ] ( logical_expression ) } <column_index> ::= INDEX index_name [ CLUSTERED | NONCLUSTERED ] [ WITH ( <index_option> [ ,... n ] ) ] [ ON { partition_scheme_name (column_name ) | filegroup_name | default } ] [ FILESTREAM_ON { filestream_filegroup_name | partition_scheme_name | "NULL" } ] <computed_column_definition> ::= column_name AS computed_column_expression [ PERSISTED [ NOT NULL ] ] [ [ CONSTRAINT constraint_name ] { PRIMARY KEY | UNIQUE } [ CLUSTERED | NONCLUSTERED ] [ WITH FILLFACTOR = fillfactor | WITH ( <index_option> [ , ...n ] ) ] [ ON { partition_scheme_name ( partition_column_name ) | filegroup | "default" } ] | [ FOREIGN KEY ] REFERENCES referenced_table_name [ ( ref_column ) ] [ ON DELETE { NO ACTION | CASCADE } ] [ ON UPDATE { NO ACTION } ] [ NOT FOR REPLICATION ] | CHECK [ NOT FOR REPLICATION ] ( logical_expression ) ] <column_set_definition> ::= column_set_name XML COLUMN_SET FOR ALL_SPARSE_COLUMNS < table_constraint > ::= [ CONSTRAINT constraint_name ] { { PRIMARY KEY | UNIQUE } [ CLUSTERED | NONCLUSTERED ] (column [ ASC | DESC ] [ ,...n ] ) [ WITH FILLFACTOR = fillfactor |WITH ( <index_option> [ , ...n ] ) ] [ ON { partition_scheme_name (partition_column_name) | filegroup | "default" } ] | FOREIGN KEY ( column [ ,...n ] ) REFERENCES referenced_table_name [ ( ref_column [ ,...n ] ) ] [ ON DELETE { NO ACTION | CASCADE | SET NULL | SET DEFAULT } ] [ ON UPDATE { NO ACTION | CASCADE | SET NULL | SET DEFAULT } ] [ NOT FOR REPLICATION ] | CHECK [ NOT FOR REPLICATION ] ( logical_expression ) < table_index > ::= { { INDEX index_name [ UNIQUE ] [ CLUSTERED | NONCLUSTERED ] (column_name [ ASC | DESC ] [ ,... n ] ) | INDEX index_name CLUSTERED COLUMNSTORE | INDEX index_name [ NONCLUSTERED ] COLUMNSTORE (column_name [ ,... n ] ) } [ WITH ( <index_option> [ ,... n ] ) ] [ ON { partition_scheme_name (column_name ) | filegroup_name | default } ] [ FILESTREAM_ON { filestream_filegroup_name | partition_scheme_name | "NULL" } ] } <table_option> ::= { [DATA_COMPRESSION = { NONE | ROW | PAGE } [ ON PARTITIONS ( { <partition_number_expression> | <range> } [ , ...n ] ) ]] [ FILETABLE_DIRECTORY = <directory_name> ] [ FILETABLE_COLLATE_FILENAME = { <collation_name> | database_default } ] [ FILETABLE_PRIMARY_KEY_CONSTRAINT_NAME = <constraint_name> ] [ FILETABLE_STREAMID_UNIQUE_CONSTRAINT_NAME = <constraint_name> ] [ FILETABLE_FULLPATH_UNIQUE_CONSTRAINT_NAME = <constraint_name> ] [ SYSTEM_VERSIONING = ON [ ( HISTORY_TABLE = schema_name . history_table_name [, DATA_CONSISTENCY_CHECK = { ON | OFF } ] ) ] ] [ REMOTE_DATA_ARCHIVE = { ON [ ( <table_stretch_options> [,...n] ) ] | OFF ( MIGRATION_STATE = PAUSED ) } ] [ DATA_DELETION = ON {( FILTER_COLUMN = column_name, RETENTION_PERIOD = { INFINITE | number {DAY | DAYS | WEEK | WEEKS | MONTH | MONTHS | YEAR | YEARS } )} ] } <table_stretch_options> ::= { [ FILTER_PREDICATE = { null | table_predicate_function } , ] MIGRATION_STATE = { OUTBOUND | INBOUND | PAUSED } } <index_option> ::= { PAD_INDEX = { ON | OFF } | FILLFACTOR = fillfactor | IGNORE_DUP_KEY = { ON | OFF } | STATISTICS_NORECOMPUTE = { ON | OFF } | STATISTICS_INCREMENTAL = { ON | OFF } | ALLOW_ROW_LOCKS = { ON | OFF } | ALLOW_PAGE_LOCKS = { ON | OFF } | OPTIMIZE_FOR_SEQUENTIAL_KEY = { ON | OFF } | COMPRESSION_DELAY= {0 | delay [Minutes]} | DATA_COMPRESSION = { NONE | ROW | PAGE | COLUMNSTORE | COLUMNSTORE_ARCHIVE } [ ON PARTITIONS ( { <partition_number_expression> | <range> } [ , ...n ] ) ] } <range> ::= <partition_number_expression> TO <partition_number_expression> |
Reference
Read More
Great concepts. This would be really helpful. I appreciate you sharing.