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.
1 2 3 4 5 |
CREATE TABLE dbo.MyTable ( ID int NOT NULL IDENTITY (1, 1), Name varchar(50) NOT NULL ) ON [PRIMARY] |
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“.