SQL Server 2022 : Automatically Assign Record Identities

We know that  social security number, employee ID or passport number are all unique numbers. 

SQL Server has the capability to let  the database engine automatically assign new unique identifies when new records are added. Let's do it.

First, select the table 'Customers' from my SQL server 2022's database 'Red30Tech', then right click the table name and select Design.





Add a new column named 'CustomerID', set Data Type as 'int', scroll down to the Column Properties, update (Is Identity) from Identity Specification as 'Yes', set Identity Seed as '1000' which means the very first row loaded into the table starts from 1000. Set Identity Increment as '1' which means it will increase 1 from 1000 for the next row.


Now, drag the little triangle icon on the left of CustomerID to the top, click the Primary key icon, this column will be assigned as column with unique value for each row. 


Save the design window and hover over to the table name again, right click and press 'Select Top 1000 Rows'.


Then we can see the SQL sever created the unique CustomerID automatically for each row.


Let's go back to Object Explorer, click the dbo.Customers table name, then we can see the key icon is next to CustomerID,  PK_Customers and PK_Customers(Clustered)  from Columns folder, Keys folder and Indexes folders respectively.  


So, Identity Specification gives us a chance to customize the column with unique value automatically.

Notes: I use the sample database from Microsoft SQL Server 2022 Essential Training With Adam Wilbert


Comments

Popular posts from this blog

Schema and Security Control in SQL server

The statistics of competition results in Excel

Washington State's Gasoline Price Hit the historical High - PowerBI Dashboard