Identities Overview
From SQLServerPedia
|
See Also: Main_Page - Code Management - Identities The column property known as an identity is a monotonically increasing counter whose value is automatically placed in the column. The end-user need not provide the value and developers do not have to write any special code to create the values. When assigning identities to a table, the seed value is the integer indicating the starting value of the first identity inserted into the table (usually 1). The increment value is an integer indicating the amount of increase when every new row is inserted into the table. The default for both seed and increment is 1. But if you skip one, you must skip both when defining an identity column. CREATE TABLE agency (agency_id INT NOT NULL IDENTITY(1,1) PRIMARY KEY NONCLUSTERED, agency_name CHAR(50) NOT NULL, phone_number VARCHAR(23) NOT NULL) You may use the ALTER TABLE statement to only ADD or DROP identity columns from the table: ALTER TABLE agency ADD (counter_column INT IDENTITY(100,10)) This statement adds a column, column_counter, to the agency table. The first record insterted with a value for column_counter will start at 100 and every subsequent row will increase by 10. IDENTITY_INSERTIDENTITY_INSERT allows you to insert specific values into an IDENTITY column. Basically, it temporarily turns off the IDENTITY constraint for the table. This can be useful for filling in rows that were deleted by mistake. Note: Remember that the identity property of a column is in place for a reason. When you use IDENTITY_INSERT, you’re circumventing that restriction. Don’t use this lightly! CREATE TABLE table1 (id_col int identity(1,1), [name] varchar(100))
INSERT INTO table1 VALUES ('TV')
INSERT INTO table1 VALUES ('Movies')
INSERT INTO table1 VALUES ('Books')
INSERT INTO table1 VALUES ('Radio')
DELETE FROM table1 where [name] = 'Books'
SELECT * FROM table1
SET IDENTITY_INSERT table1 ON INSERT table1 (id_col, [name]) VALUES (3,'Magazines') SELECT * FROM table1
You might notice that the insert statement here contains the column list (id_col, [name]). This is required when inserting to a table when IDENTITY_INSERT is ON.
IDENTITY_INSERT is session specific; if you turn IDENTITY_INSERT ON (for example) in a Query Analyzer window, and try to insert an explicit value to the identity column in another window, you will get the error " Cannot insert explicit value for identity column in table 'table1' when IDENTITY_INSERT is set to OFF." Note that only one table in a session can have IDENTITY_INSERT ON. And, if you insert a value larger than the biggest current identity value, SQL will pick it up from there automagically (after IDENTITY_INSERT is turned OFF): INSERT table1 (id_col, [name]) VALUES (45,'Books')
SET IDENTITY_INSERT table1 OFF
INSERT table1 ([name]) VALUES ('MP3 Player')
SELECT * FROM table1
Finally, here's a quick script to find gaps in the sequence of identity values for a column: SELECT A.id_col, B.id_col from table1 A LEFT OUTER JOIN table1 B ON A.Id_col + 1 = B.id_col
We can see that there are missing steps after identity values 4 and 46 (as 46 is the last item in the list). Be sure to change the + 1 to your increment value if you're not using an increment value of 1 in the identity column. Author CreditsJennifer McCown contributed to this article. Jen McCown is a SQL Server developer and DBA with over 10 years experience. She writes book and product reviews for ITBookworm.com, makes training videos and blogs for MidnightDBA.com, and maintains both websites. Her online presences include: |