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

Last modified date:

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

My name is Jan Zedníček and I work as a BI Developer at Kentico Software in Brno. Mostly you can see me there working in the office, but I also work partly as a freelancer. I have been working as a freelancer for many companies for more than 5 years, but Kentico is the matter of my heart. I used to work as a financial controller at companies like Aero Vodochody or Sberbank and I also used to be a bond program manager in Unicapital Investment group. 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.

Category: Handling SQL tables Tags:

About Ing. Jan Zedníček - BI Developer, Finance controller

My name is Jan Zedníček and I work as a BI Developer at Kentico Software in Brno. Mostly you can see me there working in the office, but I also work partly as a freelancer. I have been working as a freelancer for many companies for more than 5 years, but Kentico is the matter of my heart. I used to work as a financial controller at companies like Aero Vodochody or Sberbank and I also used to be a bond program manager in Unicapital Investment group. 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.