Kunci primer, kunci asing, dan kunci unik di Gudang di Microsoft Fabric

Berlaku untuk: Gudang di Microsoft Fabric

Pelajari tentang batasan tabel di Gudang di Microsoft Fabric, termasuk kunci utama, kunci asing, dan kunci unik.

Penting

Untuk menambahkan atau menghapus kunci primer, kunci asing, atau batasan unik, gunakan ALTER TABLE.

Penting

Microsoft Fabric sedang dalam pratinjau.

Batasan tabel

Gudang di Microsoft Fabric mendukung batasan tabel ini:

  • PRIMARI KEY hanya didukung ketika kedua NONCLUSTERED dan NOT ENFORCED digunakan.
  • Batasan UNIK hanya didukung ketika NONCLUSTERED dan NOT ENFORCED digunakan.
  • KUNCI ASING hanya didukung ketika NOT ENFORCED digunakan.

Untuk sintaks, centang UBAH TABEL.

Keterangan

Memiliki kunci primer, kunci asing, dan/atau kunci unik memungkinkan Gudang di Microsoft Fabric menghasilkan rencana eksekusi yang optimal untuk kueri.

Penting

Setelah membuat tabel dengan kunci primer atau batasan unik di Gudang di Microsoft Fabric, pengguna perlu memastikan semua nilai dalam kolom tersebut unik. Pelanggaran yang dapat menyebabkan kueri mengembalikan hasil yang tidak akurat. Kunci asing tidak diberlakukan.

Contoh ini memperlihatkan bagaimana kueri dapat mengembalikan hasil yang tidak akurat jika kunci primer atau kolom batasan unik yang menyertakan nilai duplikat.

 -- Create table t1
CREATE TABLE t1 (a1 INT NOT NULL, b1 INT) 

-- 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 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)
*/

-- 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 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)
*/

-- 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)
*/

Contoh

Buat Gudang di tabel Microsoft Fabric dengan kunci primer:

CREATE TABLE PrimaryKeyTable (c1 INT NOT NULL, c2 INT);

ALTER TABLE PrimaryKeyTable ADD CONSTRAINT PK_PrimaryKeyTable PRIMARY KEY NONCLUSTERED (c1) NOT ENFORCED;

Buat Gudang di tabel Microsoft Fabric dengan batasan unik:

CREATE TABLE UniqueConstraintTable (c1 INT NOT NULL, c2 INT);

ALTER TABLE UniqueConstraintTable ADD CONSTRAINT UK_UniqueConstraintTablec1 UNIQUE NONCLUSTERED (c1) NOT ENFORCED;

Buat Gudang di tabel Microsoft Fabric dengan kunci asing:

CREATE TABLE ForeignKeyReferenceTable (c1 INT NOT NULL);

ALTER TABLE ForeignKeyReferenceTable ADD CONSTRAINT PK_ForeignKeyReferenceTable PRIMARY KEY NONCLUSTERED (c1) NOT ENFORCED;

CREATE TABLE ForeignKeyTable (c1 INT NOT NULL, c2 INT);

ALTER TABLE ForeignKeyTable ADD CONSTRAINT FK_ForeignKeyTablec1 FOREIGN KEY (c1) REFERENCES ForeignKeyReferenceTable (c1) NOT ENFORCED;

Langkah berikutnya