SQL CREATE TABLE- How to Create a Database Table (SQL Server)

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] (
   [ID_Employee] INTEGER IDENTITY(1,1) PRIMARY KEY,
   [Name] VARCHAR(255) NOT NULL,
   [Surname] VARCHAR(255) NOT NULL,
   [Job] VARCHAR (255) NOT NULL,
   [FK_Manager] INT NULL,
   [Department] VARCHAR (255) NOT NULL,
   [Salary] INTEGER 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
Category: Handling SQL tables

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

My name is Jan Zednicek, and I have been working as a freelance Data Engineer for roughly 10 years. During this time, I have been publishing case studies and technical guides on this website, targeting professionals, students, and enthusiasts interested in Data Engineering particularly on Microsoft technologies as well as corporate finance and reporting solutions. 🔥 If you found this article helpful, please share it or mention me on your website or Community forum

Leave a Reply

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