66

Hopefully, I can get answers for each database server.

For an outline of how indexing works check out: How does database indexing work?

Community
  • 1
  • 1
Xenph Yan
  • 83,019
  • 16
  • 48
  • 55
  • 1
    This seems like a question that would be well answered by the docs for the database server you might be using. For Oracle: [http://download.oracle.com/docs/cd/B28359_01/server.111/b28310/indexes003.htm](Oracle) – WW. Oct 03 '08 at 12:45
  • I didn't get the correct page when I clicked the above link for Oracle. Posting the correct link: http://docs.oracle.com/cd/B28359_01/server.111/b28310/indexes003.htm – Saurabh Patil Jul 08 '13 at 03:19
  • 7
    One of SO's stated goals is to show up first in a google search. Often times, a SO answer is better than the docs themselves. Would it not be valuable to set up an answer here as a community wiki to collate all of the answers? – Ethan Reesor Jun 05 '14 at 20:35

9 Answers9

70

The following is SQL92 standard so should be supported by the majority of RDMBS that use SQL:

CREATE INDEX [index name] ON [table name] ( [column name] )
John Downey
  • 13,854
  • 5
  • 37
  • 33
  • 5
    In most systems, this can be used to have multiple columns as well, just by adding a comma separated list of column names instead of the single column. – David Manheim Jun 15 '12 at 17:53
7

Sql Server 2005 gives you the ability to specify a covering index. This is an index that includes data from other columns at the leaf level, so you don't have to go back to the table to get columns that aren't included in the index keys.

create nonclustered index my_idx on my_table (my_col1 asc, my_col2 asc) include (my_col3);

This is invaluable for a query that has my_col3 in the select list, and my_col1 and my_col2 in the where clause.

Eray Balkanli
  • 7,752
  • 11
  • 48
  • 82
Eric Z Beard
  • 37,669
  • 27
  • 100
  • 145
4

In SQL Server, you can do the following: (MSDN Link to full list of options.)

CREATE [ UNIQUE ] [ CLUSTERED | NONCLUSTERED ] INDEX index_name 
    ON <object> ( column [ ASC | DESC ] [ ,...n ] ) 
    [ INCLUDE ( column_name [ ,...n ] ) ]
    [ WHERE <filter_predicate> ]

(ignoring some more advanced options...)

The name of each Index must be unique database wide.

All indexes can have multiple columns, and each column can be ordered in whatever order you want.

Clustered indexes are unique - one per table. They can't have INCLUDEd columns.

Nonclustered indexes are not unique, and can have up to 999 per table. They can have included columns, and where clauses.

David Manheim
  • 2,553
  • 2
  • 27
  • 42
4

For python pytables, indexes don't have names and they are bound to single columns:

tables.columns.column_name.createIndex()
tdc
  • 8,219
  • 11
  • 41
  • 63
2

To create indexes following stuff can be used:

  1. Creates an index on a table. Duplicate values are allowed: CREATE INDEX index_name ON table_name (column_name)

  2. Creates a unique index on a table. Duplicate values are not allowed: CREATE UNIQUE INDEX index_name ON table_name (column_name)

  3. Clustered Index: CREATE CLUSTERED INDEX CL_ID ON SALES(ID);

  4. Non-clustered index:
    CREATE NONCLUSTERED INDEX NONCI_PC ON SALES(ProductCode);

Refer: http://www.codeproject.com/Articles/190263/Indexes-in-MS-SQL-Server for details.

Eray Balkanli
  • 7,752
  • 11
  • 48
  • 82
Sharvari
  • 41
  • 1
1
  1. CREATE INDEX name_index ON Employee (Employee_Name)

  2. On a multi column: CREATE INDEX name_index ON Employee (Employee_Name, Employee_Age)

Eray Balkanli
  • 7,752
  • 11
  • 48
  • 82
Looking_for_answers
  • 343
  • 1
  • 6
  • 20
0

Since most of the answers are given for SQL databases, I am writing this for NOSQL databases, specifically for MongoDB.

Below is the syntax to create an index in the MongoDB using mongo shell.

db.collection.createIndex( <key and index type specification>, <options> )

example - db.collection.createIndex( { name: -1 } )

In the above example an single key descending index is created on the name field.

Keep in mind MongoDB indexes uses B-tree data structure.

There are multiple types of indexes we can create in mongodb, for more information refer to below link - https://docs.mongodb.com/manual/indexes/

mdeora
  • 4,152
  • 2
  • 19
  • 29
0

An index is not always needed for all the databases. For eg: Kognitio aka WX2 engine doesn't offer a syntax for indexing as the database engine takes care of it implicitly. Data goes on via round-robin partitioning and Kognitio WX2 gets data on and off disk in the simplest possible way.

Srini V
  • 11,045
  • 14
  • 66
  • 89
0

We can use following syntax to create index.

CREATE INDEX <index_name> ON <table_name>(<column_name>)

If we do not want duplicate value to be allowed then we can add UNIQUE while creating index as follow

CREATE UNIQUE INDEX <index_name> ON <table_name>(<column_name>)

We can create index on multiple column by giving multiple column name separated by ','