Copyright © Curt Hill Index Creation SQL
Copyright © Curt Hill Introduction We have already seen that the two basic types of indices are based on B+Trees and Hashing We generally get one index by default on the primary key Now we see how to get a clustered index that is not based primary key
Copyright © Curt Hill SQL There is a Create Index command that allows the construction of an index The index may also be added to a Create Table or an Alter Table This allows the index to allow duplicates or be unique
Copyright © Curt Hill Create Syntax Create [unique] Index name on table (f1[,f2…]) Legend: Reserved, optional, item names Unique may be applied to any candidate key –Not the default Cannot be used on a primary key Other options may be applied by different servers
Copyright © Curt Hill Create Table Syntax An index may be a clause on a Create Table or Alter Table Attached as a clause to a Create Table Index [name] (f1[,f2…]) With an Alter use the Add clause
Copyright © Curt Hill Examples Create unique index on students (name) Create table students ( naid int, name char(20), address char(20), primary key (naid) index (name)) Alter table students add index (name)
Specific Oracle, MySQL and SQL Server have additional options These can specify the type of the index –Among other things –These options determine if a hash or Btree We will next look at the MySQL –Oracle and SQL Server are more complicated Copyright © Curt Hill
MySQL syntax Copyright © Curt Hill Create statement: CREATE [UNIQUE | FULLTEXT | SPATIAL] INDEX index-name [index_type] ON tbl_name (col_name1, … col_name2) [index_type] col_name: col_name [ (length) ] [ ASC | DESC] index_type: [ USING {BTREE | HASH} ]