Altering the Table to Add an Identity Column : Identity « Sequence Indentity « SQL Server / T-SQL Tutorial






7> CREATE TABLE Dupes(
8>   ID  int       NOT NULL,
9>   Txt char (10) NOT NULL
10> )
11> GO
1> INSERT Dupes (ID, Txt) VALUES (1, 'x')
2> INSERT Dupes (ID, Txt) VALUES (1, 'a')
3> INSERT Dupes (ID, Txt) VALUES (1, 'x')
4> INSERT Dupes (ID, Txt) VALUES (1, 'x')
5> INSERT Dupes (ID, Txt) VALUES (2, 'b')
6> INSERT Dupes (ID, Txt) VALUES (2, 'x')
7> INSERT Dupes (ID, Txt) VALUES (2, 'b')
8> INSERT Dupes (ID, Txt) VALUES (3, 'c')
9> GO

(1 rows affected)

(1 rows affected)

(1 rows affected)

(1 rows affected)

(1 rows affected)

(1 rows affected)

(1 rows affected)

(1 rows affected)
1>
2>
3> ALTER TABLE Dupes
4> ADD
5>   Ident int NOT NULL IDENTITY (1, 1)
6> GO
1>
2>
3> --Deleting Duplicates with a Correlated Subquery
4> DELETE D1
5> FROM
6>     Dupes AS D1
7> WHERE
8>     D1.Ident >
9> (
10>   SELECT
11>     MIN (D2.Ident)
12>   FROM
13>       Dupes AS D2
14>   WHERE
15>       D2.ID  = D1.ID
16>     AND
17>       D2.Txt = D1.Txt
18> )
19>
20>
21> drop table dupes;
22> GO

(3 rows affected)
1>








15.1.Identity
15.1.1.Using the IDENTITY Property and the IDENTITY() Function
15.1.2.Syntax for Using the IDENTITY() Function
15.1.3.Create table with IDENTITY column
15.1.4.Generating IDENTITY values
15.1.5.Cannot insert explicit value for identity column in table 'T' when IDENTITY_INSERT is set to OFF.
15.1.6.IDENTITY/NOT NULL/PRIMARY KEY
15.1.7.IDENTITY(100, 20)
15.1.8.Identity column
15.1.9.SELECT Statement and IDENTITY Property
15.1.10.Identity: A numerical value automatically assigned to a row any time a new row is added.
15.1.11.Supported data types for the IDENTITY property are TinyInt, SmallInt Int, BigInt, Decimal, and Numeric.
15.1.12.A table could also be created so that the seed value was negative and the increment was positive:
15.1.13.The IDENTITY property must be temporarily turned off when inserting a specific value.
15.1.14.SQL Server will choose the highest number as its current seed for a positive increment value or the lowest for a negative increment value.
15.1.15.Saving the @@IDENTITY Value in a Variable
15.1.16.SQL Server provides the @@IDENTITY global variable to hold that value.
15.1.17.Altering the Table to Add an Identity Column
15.1.18.Check @@IDENTITY value
15.1.19.Query a view with four table join
15.1.20.Retrieving the Maximum Value of key_col from MyTable
15.1.21.Query that Returns Result Row Numbers, Starting with 1 and Incrementing by 3
15.1.22.Storing the Results in a Temporary Table Using the IDENTITY() Function