PostgreSQL: Can you create an index in the CREATE TABLE definition?

陌路散爱 提交于 2019-11-30 11:15:54

问题


I want to add indexes to some of the columns in a table on creation. Is there are way to add them to the CREATE TABLE definition or do I have to add them afterward with another query?

CREATE INDEX reply_user_id ON reply USING btree (user_id);

回答1:


There doesn't seem to be any way of specifying an index in the CREATE TABLE syntax. PostgreSQL does however create an index for unique constraints and primary keys by default, as described in this note:

PostgreSQL automatically creates an index for each unique constraint and primary key constraint to enforce uniqueness.

Other than that, if you want a non-unique index, you will need to create it yourself in a separate CREATE INDEX query.




回答2:


No.

However, you can create unique indexes in the create, but that's because they are classed as constraints. You can't create a "general" index.



来源:https://stackoverflow.com/questions/6239657/postgresql-can-you-create-an-index-in-the-create-table-definition

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!