Languages
[Edit]
EN

PostgreSQL - add index to existing table using CREATE INDEX

0 points
Created by:
Amir-Hashempur
607

In this article, we would like to show you how to add index to an existing table using CREATE INDEX statement in PostgreSQL.

Quick solution:

Practical example

Edit

To show how the CREATE INDEX statement works, we will use the following table:

PostgreSQL - example data used to add index to existing table
PostgreSQL - example data used to add index to existing table

Note:

At the end of this article you can find database preparation SQL queries.

Example 1 - one column index

Edit

In this example, we will create an index for the salary column in users table.

Query:

Result:

1. Using query

PostgreSQL - add index to existing table - result using query
PostgreSQL - add index to existing table - result using query

2. Using HeidiSQL

PostgreSQL - add index to existing table - result using HeidiSQL
PostgreSQL - add index to existing table - result using HeidiSQL

Example 2 - two column index

Edit

In this example, we will create an index on the name and surname columns in users table.

Query:

Result:

1. Using query

PostgreSQL - add index to existing table - result
PostgreSQL - add index to existing table - result using query

2. Using HeidiSQL

PostgreSQL - add index to existing table - result
PostgreSQL - add index to existing table - result using HeidiSQL

Database preparation

Edit

create_tables.sql file:

insert_data.sql file:

1
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

PostgreSQL - indexes

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join