Sunday , December 22 2024

How To Create a Table in SQL Server

 

We will create a table in SQL Server by using two methods in this article.

First Method (With SSMS):

Right-click on the corresponding database as follows to create the table using SSMS. Then click “New and Table …” on the Tables tab.

We enter the column names that we want to have in the table as shown in the Columns section below. In the Data type section, we determine the data types of the columns. In the Allow Nulls section, we determine whether the corresponding column can receive a null value.

You can find detailed information about SQL Server data types in the article “SQL Server Data Types”.

You can save the table with Ctrl + s keys.

Or you can get the script of the operations you have done to create the table by clicking on Table Designer and Generate Change Script as follows.

Second Method (With TSQL):

We can create a table with the help of the following script.

You can add some features to the table when creating a table. I have listed some of these features below.

  • In Table, you can set some columns as primary key or foreing key. You can find detailed information in the article “What is Primary Key and Foreign Key “.
  • In the Table, you can set the identitiy property to some columns so that the value of the columns will automatically increase. You can find detailed information in the article “Identitiy Insert“.
  • You can set a default value for some columns in the Table. You can find detailed information in the article “How To Create Default Constraint On SQL Server“.
  • In the Table, you can set some columns to accept a certain range of values. You can find detailed information in the article “How To Create CHECK CONSTRAINT“.

Loading

About dbtut

We are a team with over 10 years of database management and BI experience. Our Expertises: Oracle, SQL Server, PostgreSQL, MySQL, MongoDB, Elasticsearch, Kibana, Grafana.

Leave a Reply

Your email address will not be published. Required fields are marked *