0

I'm not sure which indexes postgresql AUTOMATICALLY creates; I think it will create one on Book(ibsn), because its the primary key, and also Book(title), because its a candidate key... but I'm not sure if postgres will automatically create a combined index on all key attributes Book(ibsn, title). Also, would it create any other indexes automatically?

CREATE TABLE Book (
 isbn       INTEGER CONSTRAINT B_ISBN CHECK (ISBN BETWEEN 1 AND 2000),
 title      VARCHAR(200) CONSTRAINT B_TITLE NOT NULL UNIQUE,
 author     VARCHAR(50) CONSTRAINT B_AUTH NOT NULL,
 cost       FLOAT DEFAULT 0.00,
 lent_date  DATE,
 returnDate DATE,
 times_lent INTEGER,
 sectionID  SMALLINT,
 CONSTRAINT BOOK_PRIME PRIMARY KEY (isbn),
 CONSTRAINT BOOK_SECT FOREIGN KEY (sectionID) REFERENCES Section(sectionID) ON DELETE CASCADE
  );

1 Answer 1

1

Postgres will automatically create indexes only for:

  • primary keys: Adding a primary key will automatically create a unique B-tree index on the column or group of columns listed in the primary key
  • unique constraints: Adding a unique constraint will automatically create a unique B-tree index on the column or group of columns listed in the constraint

In your case, Postgres creates one unique index on the column isbn and one unique index on the column unique because you declared each column individually to be unique, not the combination of both.

No other indexes will be created automatically.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.