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:
- CREATE TABLE – in this case we explicitly define all parts of the table – data types, primary/alien keys, constraints etc.
- SELECT INTO – no 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.
- To begin with, we use the
CREATE TABLE
statement. - We define the table name, preferably in the format [database].[schema].[table “” not found /]
. - Subsequently, within parentheses, we define all the columns, their data types, and whether they can be empty or not.
- 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 anIDENTITY
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];