PostgreSQL:可以在CREATE TABLE定义中创建索引吗? [英] PostgreSQL: Can you create an index in the CREATE TABLE definition?

查看:1777
本文介绍了PostgreSQL:可以在CREATE TABLE定义中创建索引吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在创建时向表中的某些列添加索引。有没有办法将它们添加到CREATE TABLE定义中,还是我必须在之后再添加另一个查询?

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);


推荐答案

似乎没有任何指定方法 CREATE TABLE 语法的索引。但是,PostgreSQL确实会默认为唯一约束和主键创建索引,如此说明

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会自动为每个唯一约束和主键约束创建索引

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.

这篇关于PostgreSQL:可以在CREATE TABLE定义中创建索引吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆