How to Create Index in SQL
Ads by Google
What is Index in SQL?
An index is a special data structure in SQL for tables. We use Indexes for SQL operations like SELECT query, DELETE, UPDATE. Indexes help to fast these operations and manipulate from the table. Indexes are just like a column or columns in the table.
How to Create Index in SQL?
There are many queries are available in SQL to use or create an index in your table. The basic syntax for creating an index in SQL is given below:
CREATE INDEX index_name
ON table_name ( column1 , column2 , . . . ) ;
Above is the simple syntax for the index in a table. But in the above index, there is no restriction on duplicate values.
Below is a query for unique value in a table. Means every record of this table will unique according to its primary key.
CREATE UNIQUE INDEX index_name
ON table_name ( column1 , column2 , . . . ) ;
Example of Index in SQL
A simple example of Index in SQL is given below:
CREATE INDEX websites_idx
ON websites ( site_name , server ) ;
For unique Index:
CREATE UNIQUE INDEX websites_idx
ON websites ( site_name ) ;
How to Create Index in SQL Server (2012, 2014 and 2017)
A complete query for creating a simple index system in your SQL database is given below:
CREATE [ UNIQUE ] [ CLUSTERED | NONCLUSTERED ] INDEX index_name
ON
ON <object> ( column [ ASC | DESC ] [ ,…n ] )
[ INCLUDE ( column_name [ ,…n ] ) ]
[ WHERE <filter_predicate> ]
[ WITH ( <relational_index_option> [ ,…n ] ) ]
[ ON { partition_scheme_name ( column_name )
| filegroup_name
| default
}
]
[ FILESTREAM_ON { filestream_filegroup_name | partition_scheme_name | ‘ NULL ‘ } ]
sss[;]
< object > :: = { database_name.schema_name.table_or_view_name | schema_name.table_or_view_name | table_or_view_name }
<relational_index_option> ::= {
PAD_INDEX = { ON | OFF }
| FILLFACTOR = fillfactor
| SORT_IN_TEMPDB = { ON | OFF }
| IGNORE_DUP_KEY = { ON | OFF }
| STATISTICS_NORECOMPUTE = { ON | OFF }
| STATISTICS_INCREMENTAL = { ON | OFF }
| DROP_EXISTING = { ON | OFF }
| ONLINE = { ON | OFF }
| RESUMABLE = {ON | OF }
| MAX_DURATION = <time> [MINUTES]
| ALLOW_ROW_LOCKS = { ON | OFF }
| ALLOW_PAGE_LOCKS = { ON | OFF }
| OPTIMIZE_FOR_SEQUENTIAL_KEY = { ON | OFF}
| MAXDOP = max_degree_of_parallelism
| DATA_COMPRESSION = { NONE | ROW | PAGE}
[ ON PARTITIONS ( { <partition_number_expression> | <range> }
[ , …n ] ) ]
}
<filter_predicate> ::= <conjunct> [ AND <conjunct> ]
<conjunct> ::= <disjunct> | <comparison>
<disjunct> ::= column_name IN (constant ,…n)
<comparison> ::= column_name <comparison_op> constant
<comparison_op> ::= { IS | IS NOT | = | <> | != | > | >= | !> | < | <= | !< }
<range> ::= <partition_number_expression> TO <partition_number_expression>
How do you create an index?
Why do we create index in SQL?
How do I create a full text index in SQL Server?
Ads by Google