Condividi tramite


Proprietà IDENTITY (SQL Server Compact)

Crea una colonna Identity in una tabella. Questa proprietà viene utilizzata con le istruzioni CREATE TABLE e ALTER TABLE.

Sintassi

IDENTITY [ (seed,increment) ]

Argomenti

  • seed
    Valore utilizzato per la prima riga caricata nella tabella.

  • increment
    Valore incrementale aggiunto al valore Identity della riga caricata in precedenza.

    Nota

    È necessario specificare sia il valore di inizializzazione (seed) che di incremento (increment) oppure nessuno dei due valori. In questo secondo caso, il valore predefinito è (1,1).

Osservazioni

In Microsoft SQL Server Compact 3.5 la proprietà IDENTITY può essere creata solo in una colonna con tipo di dati integer o bigint. In una tabella è consentita una sola colonna IDENTITY.

Esempi

Descrizione

Negli esempi seguenti viene illustrato come creare una tabella in cui la prima colonna è una colonna IDENTITY e viene indicato come inserire ed eliminare i valori nella tabella.

Codice

-- Create the Tool table.
CREATE TABLE Tool(
   ID INT IDENTITY NOT NULL PRIMARY KEY, 
   Name VARCHAR(40) NOT NULL
)
-- Insert values into the Tool table.
INSERT INTO Tool(Name) VALUES ('Screwdriver')
INSERT INTO Tool(Name) VALUES ('Hammer')
INSERT INTO Tool(Name) VALUES ('Saw')
INSERT INTO Tool(Name) VALUES ('Shovel')
-- Create a gap in the identity values.
DELETE Tool 
WHERE Name = 'Saw'
-- Select the records and check results.
SELECT * 
FROM Tool
-- Insert an explicit ID value of 3.
-- Query returns an error.
INSERT INTO Tool (ID, Name) 
VALUES (3, 'Garden shovel')

-- SET IDENTITY_INSERT to ON.
SET IDENTITY_INSERT Tool ON

-- Insert an explicit ID value of 3.
INSERT INTO Tool (ID, Name) 
VALUES (3, 'Garden shovel')

-- Select the records and check results.
SELECT * 
FROM Tool

-- Drop Tool table.
DROP TABLE Tool

Vedere anche

Altre risorse

ALTER TABLE (SQL Server Compact)

CREATE TABLE (SQL Server Compact)