Creating a table in the SQL Server database is a basic skill. While creating it using SQL command CREATE TABLE we define:

  • Name of the table together with database definition and scheme
  • List of the columns with appropriate data types, constraints and definition if the column can be empty (NULL vs. NOT NULL)

Table can be created in two ways using SQL commands:

  1. CREATE TABLEin this case we explicitly define all parts of the table – data types, primary/alien keys, constraints etc.
  2. SELECT INTOno need to define parts of the table. It is automatically created based on list of columns in SELECT part of the script. This variation of table creation will not set up constraints/indexes into new table from the parent table.

SQL Server CREATE TABLE command Syntax

The syntax for creating a table is quite straightforward.

  1. To begin with, we use the CREATE TABLE statement.
  2. We define the table name, preferably in the format [database].[schema].[table “” not found /]
  3. Subsequently, within parentheses, we define all the columns, their data types, and whether they can be empty or not.
  4. We can directly define an auto-increment column of type IDENTITY(1,1), a calculated column (a column containing some formula), and so on. The properties of an IDENTITY column are specifically discussed in the article “SQL IDENTITY (Auto increment) – Automatic value incrementation,” where the command arguments are described.
CREATE TABLE [Database].[Scheme].[New_Table] (
[Column 1] Data_type NULL,
[Column 2] Data_type NOT NULL,

Example of CREATE TABLE Command in SQL Server

Understanding the sql CREATE TABLE Syntax is the easiest thing on this example. As a test, we will create a table with employees which will consist info about them, where:

  • ID_Employee will be primary key to the table – unique identification of integer type
  • Name, Surname, Job, Department – will be of VARCHAR type, value must be always filled (NOT NULL)
  • FK_Manager – foreign key to manager of given employee – done as a self seferenced foreign key – column with manager will be reference to primary key – ID_Employee. Cannot be empty (NOT NULL)
  • Employed_From, Employed_To – date column Employed_To can be empty (NULL)
  • Is_Employed – identifier if the employee is currently employed – reaches values 1,0. Cannot be empty (NOT NULL)

SQL Script – Table Employees:

CREATE TABLE [dbo].[Employees] (
   [Name] VARCHAR(255) NOT NULL,
   [Surname] VARCHAR(255) NOT NULL,
   [Job] VARCHAR (255) NOT NULL,
   [FK_Manager] INT NULL,
   [Department] VARCHAR (255) NOT NULL,
   [Employed_From] DATE NOT NULL,
   [Employed_To] DATE NULL,
   [Is_Employed] INT NOT NULL

ALTER TABLE [dbo].[Employees]
ADD CONSTRAINT [FK_Manager_SF] FOREIGN KEY ([FK_Manager]) REFERENCES [dbo].[Employees] ([ID_Employee]);

SELECT INTO Command Syntax

This method is used for scenarios where we don’t need to specifically define column properties, constraints, and the like. Through the SELECT INTO statement, a table is created for us, which inherits its data types from the SELECT statement, but nothing else from the source table (such as indexes, SQL triggers, etc.).

It’s a good way to create temporary auxiliary tables that we can clean up afterward. Temporary tables are typically handled differently, as discussed in the article SQL Temporary Table – Difference between # and ## Temp Tables, so there aren’t many scenarios where I would recommend using SELECT INTO.

SELECT [Column 1], [Column 2]
INTO [Database].[Scheme].[New_Table]
FROM [Database].[Scheme].[Table];

Rate this post

Ing. Jan Zedníček - Data Engineer & Controlling

My name is Jan Zedníček and I have been working as a freelancer for many companies for more than 10 years. I used to work as a financial controller, analyst and manager at many different companies in field of banking and manufacturing. When I am not at work, I like playing volleyball, chess, doing a workout in the gym.

🔥 If you found this article helpful, please share it or mention me on your website

Leave a Reply

Your email address will not be published. Required fields are marked *