IDENTITY(속성)(Transact-SQL)

테이블에 ID 열을 만듭니다. 이 속성은 CREATE TABLE 및 ALTER TABLE Transact-SQL 문에 사용됩니다.

[!참고] IDENTITY 속성은 열의 행 ID 속성을 표시하는 SQL-DMO Identity 속성과 다릅니다.

항목 링크 아이콘Transact-SQL 구문 표기 규칙

구문

IDENTITY [ (seed , increment) ]

인수

seed

테이블에 로드되는 첫 번째 행에 사용하는 값입니다.

increment

로드된 이전 행의 ID 값에 추가되는 증가값입니다.

초기값과 증가값을 모두 지정하거나 모두 지정하지 않아야 합니다. 모두 지정하지 않은 경우 기본값 (1,1)이 사용됩니다.

주의

자주 삭제되는 테이블에 ID 열이 있는 경우 ID 값 사이에 간격이 생길 수 있습니다. 이 점이 우려되는 경우에는 IDENTITY 속성을 사용하지 마십시오. 간격이 발생하지 않도록 하거나 기존 간격을 채우려면 SET IDENTITY_INSERT를 ON으로 설정하고 명시적으로 값을 입력하기 전에 기존 ID 값을 평가하십시오.

제거된 ID 값을 다시 사용하는 경우에는 예 B의 예제 코드를 사용하여 다음에 사용할 수 있는 ID 값을 찾으십시오. tablename, column_typeMAX(column_type) - 1 대신 테이블 이름, ID 열 데이터 형식, 해당 데이터 형식에 대해 허용되는 최대 숫자 값 -1을 사용하십시오.

DBCC CHECKIDENT를 사용하여 현재 ID 값을 검사하고 이 값을 ID 열의 최대 값과 비교합니다.

ID 열이 있는 테이블이 복제를 위해 게시된 경우 해당 ID 열은 사용되는 복제 유형에 적합한 방식으로 관리해야 합니다. 자세한 내용은 ID 열 복제를 참조하십시오.

1. CREATE TABLE에 IDENTITY 속성 사용

다음 예에서는 자동으로 ID를 증가시키는 IDENTITY 속성을 사용하여 새 테이블을 만듭니다.

USE AdventureWorks
IF OBJECT_ID ('dbo.new_employees', 'U') IS NOT NULL
   DROP TABLE new_employees
GO
CREATE TABLE new_employees
(
 id_num int IDENTITY(1,1),
 fname varchar (20),
 minit char(1),
 lname varchar(30)
)

INSERT new_employees
   (fname, minit, lname)
VALUES
   ('Karin', 'F', 'Josephs')

INSERT new_employees
   (fname, minit, lname)
VALUES
   ('Pirkko', 'O', 'Koskitalo')

2. 일반 구문을 사용하여 ID 값 간의 간격 찾기

다음 예에서는 데이터가 제거되었을 때 ID 값에서 간격을 찾기 위한 일반 구문을 보여 줍니다.

[!참고] 다음 Transact-SQL 스크립트의 첫 번째 부분은 설명 목적으로만 제공되는 것입니다. 다음과 같은 주석으로 시작하는 Transact-SQL 스크립트를 실행할 수 있습니다. -- Create the img table.

-- Here is the generic syntax for finding identity value gaps in data.
-- The illustrative example starts here.
SET IDENTITY_INSERT tablename ON
DECLARE @minidentval column_type
DECLARE @maxidentval column_type
DECLARE @nextidentval column_type
SELECT @minidentval = MIN($IDENTITY), @maxidentval = MAX($IDENTITY)
    FROM tablename
IF @minidentval = IDENT_SEED('tablename')
   SELECT @nextidentval = MIN($IDENTITY) + IDENT_INCR('tablename')
   FROM tablename t1
   WHERE $IDENTITY BETWEEN IDENT_SEED('tablename') AND 
      @maxidentval AND
      NOT EXISTS (SELECT * FROM tablename t2
         WHERE t2.$IDENTITY = t1.$IDENTITY + 
            IDENT_INCR('tablename'))
ELSE
   SELECT @nextidentval = IDENT_SEED('tablename')
SET IDENTITY_INSERT tablename OFF
-- Here is an example to find gaps in the actual data.
-- The table is called img and has two columns: the first column 
-- called id_num, which is an increasing identification number, and the 
-- second column called company_name.
-- This is the end of the illustration example.

-- Create the img table.
-- If the img table already exists, drop it.
-- Create the img table.
IF OBJECT_ID ('dbo.img', 'U') IS NOT NULL
   DROP TABLE img
GO
CREATE TABLE img (id_num int IDENTITY(1,1), company_name sysname)
INSERT img(company_name) VALUES ('New Moon Books')
INSERT img(company_name) VALUES ('Lucerne Publishing')
-- SET IDENTITY_INSERT ON and use in img table.
SET IDENTITY_INSERT img ON

DECLARE @minidentval smallint
DECLARE @nextidentval smallint
SELECT @minidentval = MIN($IDENTITY) FROM img
 IF @minidentval = IDENT_SEED('img')
    SELECT @nextidentval = MIN($IDENTITY) + IDENT_INCR('img')
    FROM img t1
    WHERE $IDENTITY BETWEEN IDENT_SEED('img') AND 32766 AND
      NOT    EXISTS (SELECT * FROM img t2
          WHERE t2.$IDENTITY = t1.$IDENTITY + IDENT_INCR('img'))
 ELSE
    SELECT @nextidentval = IDENT_SEED('img')
SET IDENTITY_INSERT img OFF

참고 항목

참조

ALTER TABLE(Transact-SQL)
CREATE TABLE(Transact-SQL)
DBCC CHECKIDENT(Transact-SQL)
IDENT_INCR(Transact-SQL)
@@IDENTITY(Transact-SQL)
IDENTITY(함수)(Transact-SQL)
IDENT_SEED(Transact-SQL)
SELECT(Transact-SQL)
SET IDENTITY_INSERT(Transact-SQL)

관련 자료

ID 열 복제

도움말 및 정보

SQL Server 2005 지원 받기