SQL Server Create Table


You can create a table using the SSMS GUI, or you can use T-SQL code. This post describes how to use the T-SQL code to create a table.

The general form for creating a table is:

CREATE TABLE table_name (
    column1 datatype,
    column2 datatype,
    column3 datatype,
   ....
);

Here is an example from w3schools.com

CREATE TABLE Persons (
    PersonID int,
    LastName varchar(255),
    FirstName varchar(255),
    Address varchar(255),
    City varchar(255) 
);

Primary Key

CREATE TABLE [dbo].[Persons](
	[PersonID] [int] NOT NULL,
	[LastName] [varchar](255) NULL,
	[FirstName] [varchar](255) NULL,
	[Address] [varchar](255) NULL,
	[City] [varchar](255) NULL,
 CONSTRAINT [PK_Persons] PRIMARY KEY CLUSTERED 
 ([PersonID] ASC)
 )

After creating this table we can use SSMS to script the table for us. If we script the table with CREATE To we get this:

CREATE TABLE [dbo].[Persons](
	[PersonID] [int] NOT NULL,
	[LastName] [varchar](255) NULL,
	[FirstName] [varchar](255) NULL,
	[Address] [varchar](255) NULL,
	[City] [varchar](255) NULL,
 CONSTRAINT [PK_Persons] PRIMARY KEY CLUSTERED 
(
	[PersonID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO

Identity

CREATE TABLE [dbo].[Persons](
	[PersonID] [int] IDENTITY(1,1) NOT NULL,
	[LastName] [varchar](255) NULL,
	[FirstName] [varchar](255) NULL,
	[Address] [varchar](255) NULL,
	[City] [varchar](255) NULL
) ON [PRIMARY]
GO

Can you also designate your Identity as a primary key?

Unique Constraint

This is a sports box score example. Here we have an identity and a unique constraint. In this table we will have the box scores of several games. Each player will appear in the table many times, just as the game date will be repeated many times, however, the combination of a Player and a Game Date can only appear at most once (no duplicates). You can’t have the same player playing in the same game more than one time.

CREATE TABLE [dbo].[Box](
	[UnqId] [int] IDENTITY(1,1) NOT NULL,
	[Player] [varchar](50) NOT NULL,
	[MinPlayed] [time](7) NULL,
	[Points] [int] NULL,
	[GameDate] [date] NOT NULL,
	[Opponent] [varchar](20) NOT NULL,
 CONSTRAINT [UQ_PlayerGameDate] UNIQUE NONCLUSTERED 
 ( [Player] ASC, [GameDate] ASC )
)

When you add a unique constraint to a table, SQL Server will enforce it using a nonclustered index unless you specify the clustered keyword. With a clustered index the table is organized as a B-tree and without one it is organized as a heap. A clustered index physically stores the data itself in order. Nonclustered indexes have a structure separate from the data rows. A nonclustered index contains the nonclustered index key values and each key value entry has a pointer to the data row that contains the key value.

T-SQL Code to Create Primary Key

After the table has been created you can alter the table and add a primary key. Player would not be a good choice in this example, but we are just using it as an illustration. Below is the code to that.

ALTER TABLE dbo.Box ADD CONSTRAINT
	PK_Box PRIMARY KEY CLUSTERED 
	(Player)