Chiave primaria, chiave esterna e chiave univoca usando il pool SQL dedicato in Azure Synapse Analytics

Informazioni sui vincoli di tabella nel pool SQL dedicato, tra cui chiave primaria, chiave esterna e chiave univoca.

Vincoli di tabella

Il pool SQL dedicato supporta questi vincoli di tabella:

  • PRIMARY KEY è supportato solo se vengono usati sia NONCLUSTERED che NOT ENFORCED.
  • Il vincolo UNIQUE è supportato solo quando viene usato NOT ENFORCED.

Per la sintassi, selezionare ALTER TABLE e CREATE TABLE.

Il vincolo FOREIGN KEY non è supportato nel pool SQL dedicato.

Osservazioni:

La chiave primaria e/o la chiave univoca consente al motore del pool SQL dedicato di generare un piano di esecuzione ottimale per una query. Tutti i valori di una colonna chiave primaria o di una colonna vincolo univoco devono essere univoci.

Importante

Dopo aver creato una tabella con chiave primaria o vincolo univoco nel pool SQL dedicato, gli utenti devono assicurarsi che tutti i valori in tali colonne siano univoci. Una violazione di questo principio può far sì che la query restituisca risultati imprecisi.

Questo esempio mostra come una query può restituire risultati imprecisi se la chiave primaria o la colonna del vincolo univoco include valori duplicati.

 -- Create table t1
CREATE TABLE t1 (a1 INT NOT NULL, b1 INT) WITH (DISTRIBUTION = ROUND_ROBIN)

-- Insert values to table t1 with duplicate values in column a1.
INSERT INTO t1 VALUES (1, 100)
INSERT INTO t1 VALUES (1, 1000)
INSERT INTO t1 VALUES (2, 200)
INSERT INTO t1 VALUES (3, 300)
INSERT INTO t1 VALUES (4, 400)

-- Run this query.  No primary key or unique constraint.  4 rows returned. Correct result.
SELECT a1, COUNT(*) AS total FROM t1 GROUP BY a1

/*
a1          total
----------- -----------
1           2
2           1
3           1
4           1

(4 rows affected)
*/

-- Add unique constraint
ALTER TABLE t1 ADD CONSTRAINT unique_t1_a1 unique (a1) NOT ENFORCED

-- Re-run this query.  5 rows returned.  Incorrect result.
SELECT a1, count(*) AS total FROM t1 GROUP BY a1

/*
a1          total
----------- -----------
2           1
4           1
1           1
3           1
1           1

(5 rows affected)
*/

-- Drop unique constraint.
ALTER TABLE t1 DROP CONSTRAINT unique_t1_a1

-- Add primary key constraint
ALTER TABLE t1 add CONSTRAINT PK_t1_a1 PRIMARY KEY NONCLUSTERED (a1) NOT ENFORCED

-- Re-run this query.  5 rows returned.  Incorrect result.
SELECT a1, COUNT(*) AS total FROM t1 GROUP BY a1

/*
a1          total
----------- -----------
2           1
4           1
1           1
3           1
1           1

(5 rows affected)
*/

-- Manually fix the duplicate values in a1
UPDATE t1 SET a1 = 0 WHERE b1 = 1000

-- Verify no duplicate values in column a1 
SELECT * FROM t1

/*
a1          b1
----------- -----------
2           200
3           300
4           400
0           1000
1           100

(5 rows affected)
*/

-- Add unique constraint
ALTER TABLE t1 add CONSTRAINT unique_t1_a1 UNIQUE (a1) NOT ENFORCED  

-- Re-run this query.  5 rows returned.  Correct result.
SELECT a1, COUNT(*) as total FROM t1 GROUP BY a1

/*
a1          total
----------- -----------
2           1
3           1
4           1
0           1
1           1

(5 rows affected)
*/

-- Drop unique constraint.
ALTER TABLE t1 DROP CONSTRAINT unique_t1_a1

-- Add primary key contraint
ALTER TABLE t1 ADD CONSTRAINT PK_t1_a1 PRIMARY KEY NONCLUSTERED (a1) NOT ENFORCED

-- Re-run this query.  5 rows returned.  Correct result.
SELECT a1, COUNT(*) AS total FROM t1 GROUP BY a1

/*
a1          total
----------- -----------
2           1
3           1
4           1
0           1
1           1

(5 rows affected)
*/

Esempi

Creare una tabella del pool SQL dedicata con una chiave primaria:

CREATE TABLE mytable (c1 INT PRIMARY KEY NONCLUSTERED NOT ENFORCED, c2 INT);

Creare una tabella del pool SQL dedicata con un vincolo univoco:

CREATE TABLE t6 (c1 INT UNIQUE NOT ENFORCED, c2 INT);

Passaggi successivi

Dopo avere creato le tabelle per il pool SQL dedicato, il passaggio successivo consiste nel caricare i dati nella tabella. Per un'esercitazione sul caricamento, vedere Caricamento di dati in un pool SQL dedicato.