简体   繁体   中英

Does the unique constraint in Postgres create an index?

Let's say that I have a table that looks like the following:

CREATE TABLE products (
   product_no integer UNIQUE NOT NULL,
   name text,
   price numeric
);

Then if I insert values into the table, will postgres (or any similar DBMS) actually check through each row of the table or is an index automatically created? If there is no index, this becomes expensive really fast [O(N!)].

Yes and no. Postgres checks that the product_no has not already been inserted. It does so by checking an index , not the individual rows.

The unique constraint is implemented using an index, so checking for duplication is O(log n) not O(n).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM