{"id":144,"date":"2019-07-09T04:15:56","date_gmt":"2019-07-08T22:45:56","guid":{"rendered":"https:\/\/techieshouts.com\/?p=144"},"modified":"2022-08-09T19:08:38","modified_gmt":"2022-08-09T13:38:38","slug":"create-table-in-sql-server","status":"publish","type":"post","link":"https:\/\/techieshouts.com\/home\/create-table-in-sql-server\/","title":{"rendered":"CREATE TABLE in MS SQL Server"},"content":{"rendered":"\n<p>In Microsoft SQL Server, there are different ways in which we can create the table. The table can be created with or without constraints.<\/p>\n\n\n\n<div class=\"wp-block-ub-table-of-contents ub_table-of-contents\" data-showtext=\"show\" data-hidetext=\"hide\"><div class=\"ub_table-of-contents-header\"><div class=\"ub_table-of-contents-title\">Table of contents<\/div><\/div><div style=\"display:block\" class=\"ub_table-of-contents-container ub_table-of-contents-1-column\"><ul><li><a href=\"#0-create-table-with-primary-key\">Create table with Primary Key<\/a><\/li><li><a href=\"#1-table-with-identity-column\">Table with Identity Column<\/a><\/li><li><a href=\"#2-create-table-with-foreign-key\">Create table with Foreign Key<\/a><\/li><li><a href=\"#3-table-with-default-constraint\">Table with Default constraint<\/a><\/li><li><a href=\"#4-table-with-check-constraint\">Table with Check constraint<\/a><\/li><\/ul><\/div><\/div>\n\n\n\n<p>Basic Syntax for creating a table is,<\/p>\n\n\n\n<pre data-mode=\"sql\" data-theme=\"dreamweaver\" data-fontsize=\"14\" data-lines=\"Infinity\" class=\"wp-block-simple-code-block-ace\">CREATE TABLE databaseName.tableName(  \n   columnName1 datatype [NULL | NOT NULL], \n   columnName2 datatype [NULL | NOT NULL] \n);<\/pre>\n\n\n\n<p>databaseName &#8211; Name of the DB where the table will be created<\/p>\n\n\n\n<p>tableName \u2013 Name of the table.&nbsp;<\/p>\n\n\n\n<p>columnName \u2013 Name of the column or field.&nbsp;<\/p>\n\n\n\n<p>datatype \u2013 Type of the data to be stored in the column.&nbsp;<\/p>\n\n\n\n<p>The column can be either null or non-null. But it is not mandatory to specify the nullability. It is purely optional to define the nullability of the column. If it is not specified, it is considered as NULL by default.&nbsp;Even though it is not mandatory, it is always a good practice to specify that.<\/p>\n\n\n\n<p>Now that we have seen the basic syntax of create statement, we will see the different types of it with examples.<\/p>\n\n\n\n<h2 id=\"0-create-table-with-primary-key\">Create table with Primary Key<\/h2>\n\n\n\n<p>Syntax to create a table with&nbsp;<strong>primary key constraint<\/strong><\/p>\n\n\n\n<pre data-mode=\"sql\" data-theme=\"dreamweaver\" data-fontsize=\"14\" data-lines=\"Infinity\" class=\"wp-block-simple-code-block-ace\">CREATE TABLE dbo.tblCountry \n( \n\tID INT NOT NULL, \n\tcountryName VARCHAR(50) NOT NULL, \n\tcountryCode CHAR(3) NOT NULL, \n\tCONSTRAINT PK_tblCountry PRIMARY KEY (ID) \n);<\/pre>\n\n\n\n<p>PK_tblCountry &#8211; Name of the primary key of the tblCountry table.<\/p>\n\n\n\n<h2 id=\"1-table-with-identity-column\">Table with Identity Column<\/h2>\n\n\n\n<p>Syntax to create with&nbsp;an<strong> identity column<\/strong><\/p>\n\n\n\n<pre data-mode=\"sql\" data-theme=\"dreamweaver\" data-fontsize=\"14\" data-lines=\"Infinity\" class=\"wp-block-simple-code-block-ace\">CREATE TABLE dbo.tblCountry \n( \n\tID INT NOT NULL IDENTITY(1,1), \n\tcountryName VARCHAR(50) NOT NULL, \n\tcountryCode CHAR(3) NOT NULL, \n\tCONSTRAINT PK_tblCountry PRIMARY KEY (ID)\n);<\/pre>\n\n\n\n<p>IDENTITY(1,1)&nbsp;&#8211; the First parameter is the seed value and the second parameter is the increment value.<\/p>\n\n\n\n<h2 id=\"2-create-table-with-foreign-key\">Create table with Foreign Key<\/h2>\n\n\n\n<p>Syntax to create with&nbsp;<strong>foreign key constraint<\/strong>&nbsp;<\/p>\n\n\n\n<pre data-mode=\"sql\" data-theme=\"dreamweaver\" data-fontsize=\"14\" data-lines=\"Infinity\" class=\"wp-block-simple-code-block-ace\">CREATE TABLE dbo.tblPlayer \n( \n\tplayerID INT NOT NULL IDENTITY(1,1), \n\tfirstName VARCHAR(100) NOT NULL, \n\tage INT NOT NULL, \n\tcountryID INT NOT NULL, \n\tcreatedDate DATETIME NOT NULL, \n\tCONSTRAINT PK_tblPlayer PRIMARY KEY (playerID), \n\tCONSTRAINT FK_tblPlayer_tblCountry_countryID FOREIGN KEY([countryID]) REFERENCES [dbo].[tblCountry] ([ID]) \n);<\/pre>\n\n\n\n<p>FK_tblPlayer_tblCountry_CountryID &#8211; Name of the foreign key constraint.&nbsp; <\/p>\n\n\n\n<h2 id=\"3-table-with-default-constraint\">Table with Default constraint<\/h2>\n\n\n\n<p> Syntax to create with&nbsp;<strong>default constraint<\/strong><\/p>\n\n\n\n<pre data-mode=\"sql\" data-theme=\"dreamweaver\" data-fontsize=\"14\" data-lines=\"Infinity\" class=\"wp-block-simple-code-block-ace\">CREATE TABLE dbo.tblPlayer \n( \n\tplayerID INT NOT NULL IDENTITY(1,1), \n\tfirstName VARCHAR(100) NOT NULL, \n\tage INT NOT NULL, \n\tcountryID INT NOT NULL, \n\tcreatedDate DATETIME NOT NULL CONSTRAINT DF_tblPlayer_createdDate DEFAULT GETDATE(), \n\tCONSTRAINT PK_tblPlayer PRIMARY KEY (playerID), \n\tCONSTRAINT FK_tblPlayer_tblCountry_countryID FOREIGN KEY([countryID]) REFERENCES [dbo].[tblCountry] ([ID]) \n);<\/pre>\n\n\n\n<p>DF_tblPlayer_CreatedDate &#8211; Name of the default constraint.&nbsp; <\/p>\n\n\n\n<h2 id=\"4-table-with-check-constraint\">Table with Check constraint<\/h2>\n\n\n\n<p>Syntax to create  with&nbsp;a <strong>check constraint<\/strong><\/p>\n\n\n\n<pre data-mode=\"sql\" data-theme=\"dreamweaver\" data-fontsize=\"14\" data-lines=\"Infinity\" class=\"wp-block-simple-code-block-ace\">CREATE TABLE dbo.tblPlayer \n( \n\tplayerID INT NOT NULL IDENTITY(1,1), \n\tfirstName VARCHAR(100) NOT NULL, \n\tage INT NOT NULL CONSTRAINT CK_tblPlayer_age CHECK (age >= 15), \n\tcountryID INT NOT NULL, \n\tcreatedDate DATETIME NOT NULL CONSTRAINT DF_tblPlayer_createdDate DEFAULT GETDATE(), \n\tCONSTRAINT PK_tblPlayer PRIMARY KEY (playerID), \n\tCONSTRAINT FK_tblPlayer_tblCountry_countryID FOREIGN KEY([countryID]) REFERENCES [dbo].[tblCountry] ([ID]) \n);<\/pre>\n\n\n\n<p>CK_tblPlayer_Age &#8211; Name of the check constraint.&nbsp; <\/p>\n\n\n\n<p>In the above examples, the constraints were created with meaningful names. Please refer to the below screenshot.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"https:\/\/techieshouts.com\/wp-content\/uploads\/2019\/07\/image-2.png\" target=\"_blank\" rel=\"noreferrer noopener\"><img loading=\"lazy\" width=\"1024\" height=\"123\" src=\"https:\/\/techieshouts.com\/wp-content\/uploads\/2019\/07\/image-2-1024x123.png\" alt=\"Create table result to show check constraint\" class=\"wp-image-186\" srcset=\"https:\/\/techieshouts.com\/wp-content\/uploads\/2019\/07\/image-2.png 1024w, https:\/\/techieshouts.com\/wp-content\/uploads\/2019\/07\/image-2-300x36.png 300w, https:\/\/techieshouts.com\/wp-content\/uploads\/2019\/07\/image-2-768x92.png 768w, https:\/\/techieshouts.com\/wp-content\/uploads\/2019\/07\/image-2-660x79.png 660w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure>\n\n\n\n<p>Syntax to create with SQL server-generated constraint names. It same as the above technique but the difference is that the name of the constraint will be chosen by SQL Server itself.<\/p>\n\n\n\n<pre data-mode=\"sql\" data-theme=\"dreamweaver\" data-fontsize=\"14\" data-lines=\"Infinity\" class=\"wp-block-simple-code-block-ace\">CREATE TABLE dbo.tblPlayer \n( \n\tplayerID INT NOT NULL IDENTITY(1,1), \n\tfirstName VARCHAR(100) NOT NULL, \n\tage INT NOT NULL CHECK (age >= 15), \n\tcountryID INT NOT NULL, \n\tcreatedDate DATETIME NOT NULL DEFAULT GETDATE(), \n\tPRIMARY KEY (playerID), \n\tFOREIGN KEY([countryID]) REFERENCES [dbo].[tblCountry] ([ID]) \n);<\/pre>\n\n\n\n<p>Check the constraint names generated by SQL server in the below table<\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"https:\/\/techieshouts.com\/wp-content\/uploads\/2019\/07\/image-3.png\" target=\"_blank\" rel=\"noreferrer noopener\"><img loading=\"lazy\" width=\"1024\" height=\"121\" src=\"https:\/\/techieshouts.com\/wp-content\/uploads\/2019\/07\/image-3-1024x121.png\" alt=\"Image to show automatically created check constraints\" class=\"wp-image-187\" srcset=\"https:\/\/techieshouts.com\/wp-content\/uploads\/2019\/07\/image-3.png 1024w, https:\/\/techieshouts.com\/wp-content\/uploads\/2019\/07\/image-3-300x35.png 300w, https:\/\/techieshouts.com\/wp-content\/uploads\/2019\/07\/image-3-768x91.png 768w, https:\/\/techieshouts.com\/wp-content\/uploads\/2019\/07\/image-3-660x78.png 660w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure>\n\n\n\n\n\n<p>Also, check &#8220;<a href=\"https:\/\/techieshouts.com\/creating-table-with-data\/\">CREATE TABLE using SELECT in MS SQL Server<\/a>&#8220;<\/p>\n\n\n\n<p>Reference &#8211;  <a href=\"https:\/\/docs.microsoft.com\/en-us\/sql\/t-sql\/statements\/create-table-transact-sql?view=sql-server-2017\" target=\"_blank\" rel=\"noopener noreferrer\">Microsoft SQL Server 2017<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In Microsoft SQL Server, there are different ways in which we can create the table. The table can be created with or without constraints. Basic Syntax for creating a table is, databaseName &#8211; Name of the DB where the table will be created tableName \u2013 Name of the table.&nbsp; columnName \u2013 Name of the column\u2026 <span class=\"read-more\"><a href=\"https:\/\/techieshouts.com\/home\/create-table-in-sql-server\/\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[10],"tags":[47,42,39,40],"_links":{"self":[{"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/posts\/144"}],"collection":[{"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/comments?post=144"}],"version-history":[{"count":31,"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/posts\/144\/revisions"}],"predecessor-version":[{"id":597,"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/posts\/144\/revisions\/597"}],"wp:attachment":[{"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/media?parent=144"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/categories?post=144"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/tags?post=144"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}