How can I make a primary key auto-increment in SQL Server?

In SQL Server, you can use the IDENTITY property to make the primary key automatically increment.

When creating a table, you can use IDENTIY(1,1) after the primary key column to designate the primary key’s auto-increment property. Here, 1 represents the starting value and 1 represents the increment value.

For example, the following is an example of creating a table named “Customers” and making the “CustomerID” column the primary key with auto-increment.

Make a table called Customers with columns for CustomerID, CustomerName, ContactName, and Address. The CustomerID column will be the primary key and automatically increment by 1 for each new entry.

In the above example, the CustomerID column is designated as the primary key and set to auto-increment using the IDENTIFY(1,1) property. This means that the value of CustomerID will automatically increase each time a row of data is inserted.

Please note that the IDENTITY property can only be applied to columns of integer data type.

Leave a Reply 0

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


广告
Closing in 10 seconds
bannerAds