What Are the Differences Between Unique Constraints and Unique Indexes?

Example: Create a composite index for two columns, which is not required to be a unique index.

1
2
CREATE TABLE t (n1 number,n2 number,n3 number,PRIMARY KEY (n3));
CREATE INDEX t_idx ON t(n1,n2);

GaussDB (DWS) supports multiple unique indexes for a table.

1
2
CREATE UNIQUE INDEX u_index ON t(n3);
CREATE UNIQUE INDEX u_index1 ON t(n3);

You can use the index t_idx created in the example above to create a unique constraint t_uk, which is unique only on column n1. A unique constraint is stricter than a unique index.

1
ALTER TABLE t ADD CONSTRAINT t_uk UNIQUE USING INDEX u_index;