0

I'm cleaning up some unused indexes on some tables. Before removing them I'm looking up to see how they were created. Some of them looks like they were added when the table was created by adding a constraint somename unique int the sql script. It makes me think there are multiple ways to create an index. Does creating a table like this create an index automatically?

(
    id serial not null,
    name text not null,
    other_name text not null,
    constraint constraint_or_index
        unique (name, other_name)
);

1 Answer 1

2

Quote from the manual

Adding a unique constraint will automatically create a unique B-tree index on the column or group of columns listed in the constraint

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

5 Comments

Thank you. Do you think they are safe to remove if they aren't being used?
@Jonathan: only you can tell. If you don't need the unique constraint any more then you can remove it.
Hmm... If I drop the automatically created index would this also remove the constraint? I don't think I want to do that. I'll have to go through these indexes and see which of them are created via unique constraint and not remove them. Thanks again for your help.
@Jonathan: yes, you can't have a unique constraint without a unique index
Thanks again! This kept me from doing something rash, lol. :)

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.