Junior — Middle
What are the ways to create an index in a database?
sobes.tech AI
Answer from AI
Indexes in a database can be created in several ways, depending on the DBMS and requirements:
- Using SQL query — the most common method. For example, in PostgreSQL or MySQL:
CREATE INDEX idx_column ON table_name(column_name);
- During table creation — you can specify an index directly in the table definition:
CREATE TABLE table_name (
id SERIAL PRIMARY KEY,
column_name VARCHAR(255),
INDEX idx_column (column_name)
);
- Unique indexes — created to ensure the uniqueness of values:
CREATE UNIQUE INDEX idx_unique_column ON table_name(column_name);
-
Indexes using special types — such as full-text, spatial, or partial indexes.
-
Via ORM or migrations — if an ORM (like GORM for Go) is used, indexes can be declared in models or migrations.
Thus, the choice of method depends on the specific task and the technology used.