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

By | 18. 12. 2019

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.

CREATE TABLE Syntax

CREATE TABLE [Database].[Scheme].[New_Table] (
[Column 1] Data_type NULL,
[Column 2] Data_type NOT NULL,
...)

SELECT INTO Syntax

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

Example of Table Creation

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]);

Rate this post
Category: Handling SQL tables Tags:

About Jan Zedníček

My name is Jan Zedníček and I work as a freelancer. I have been working as a freelancer for many companies for more than 5 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 and I enjoy tasting of best quality rums. I am trying to summarize all my knowledge on this website not to forget them (because of the rum effect, you know =) and to put them forward to anyone. Don´t worry about asking for help or writing some comments.

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.