0% found this document useful (0 votes)
4 views

SQL Index

The SQL CREATE INDEX statement is used to create indexes on tables to speed up data retrieval, with the option to create unique indexes that do not allow duplicate values. The syntax varies across different databases, and indexes can be removed using the DROP INDEX command without affecting the underlying data. It is recommended to create indexes only on frequently searched columns due to the additional time required for updates.

Uploaded by

monika.deshmukh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

SQL Index

The SQL CREATE INDEX statement is used to create indexes on tables to speed up data retrieval, with the option to create unique indexes that do not allow duplicate values. The syntax varies across different databases, and indexes can be removed using the DROP INDEX command without affecting the underlying data. It is recommended to create indexes only on frequently searched columns due to the additional time required for updates.

Uploaded by

monika.deshmukh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

SQL CREATE INDEX Statement

SQL CREATE INDEX Statement


The CREATE INDEX statement is used to create indexes in tables.

Indexes are used to retrieve data from the database more quickly than otherwise. The users
cannot see the indexes, they are just used to speed up searches/queries.

Note: Updating a table with indexes takes more time than updating a table without (because
the indexes also need an update). So, only create indexes on columns that will be frequently
searched against.

CREATE INDEX Syntax

Creates an index on a table. Duplicate values are allowed:

CREATE INDEX index_name


ON table_name (column1, column2, ...);

CREATE UNIQUE INDEX Syntax

Creates a unique index on a table. Duplicate values are not allowed:

CREATE UNIQUE INDEX index_name


ON table_name (column1, column2, ...);

Note: The syntax for creating indexes varies among different databases. Therefore: Check the
syntax for creating indexes in your database.

SQL CREATE INDEX


In SQL, the INDEX constraint in a column makes it faster to retrieve data when querying that
column.

Example
-- create table
CREATE TABLE Colleges (
college_id INT PRIMARY KEY,
college_code VARCHAR(20) NOT NULL,
college_name VARCHAR(50)
);
-- create index
CREATE INDEX college_index
ON Colleges(college_code);
Run Code

Here, the SQL command creates an index named college_index on the Colleges table using
the college_code column.

SQL CREATE INDEX Syntax


The syntax of the SQL CREATE INDEX statement is:

CREATE INDEX index_name


ON table_name (column_name1, column_name2, ...);

Here,

 index_name is the name given to the index


 table_name is the name of the table on which the index is to be created
 column_name1, column_name2, ... are the names of the columns to be included in
the index

CREATE UNIQUE INDEX for Unique Values


If you want to create indexes for unique values in a column, we use the CREATE UNIQUE
INDEX constraint. For example,

-- create table
CREATE TABLE Colleges (
college_id INT PRIMARY KEY,
college_code VARCHAR(20) NOT NULL,
college_name VARCHAR(50)
);

-- create unique index


CREATE UNIQUE INDEX college_index
ON Colleges(college_code);
Run Code

Here, the SQL command creates a unique index named college_index on the Colleges table
using the college_code column.

Note: Although the index is created for only unique values, the original data in the table
remains unaltered.
Remove Index From Tables
To remove INDEX from a table, we can use the DROP INDEX command. For example,

SQL Server

DROP INDEX Colleges.college_index;

PostgreSQL, Oracle

DROP INDEX college_index;

MySQL

ALTER TABLE Colleges


DROP INDEX college_index;

Here, the SQL command removes an index named college_index from the Colleges table.

Note: Deleting an index in SQL means only the index is deleted. The data in the original
table remains unaltered.

You might also like