# File /Users/nshb/svn/svn.inimit.com/railsmanual.com/www/trunk/src/rails-1.2.6/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb, line 186 def add_index(table_name, column_name, options = {}) column_names = Array(column_name) index_name = index_name(table_name, :column => column_names) if Hash === options # legacy support, since this param was a string index_type = options[:unique] ? "UNIQUE" : "" index_name = options[:name] || index_name else index_type = options end quoted_column_names = column_names.map { |e| quote_column_name(e) }.join(", ") execute "CREATE #{index_type} INDEX #{quote_column_name(index_name)} ON #{table_name} (#{quoted_column_names})" end
Adds a new index to the table. column_name can be a single Symbol, or an Array of Symbols.
The index will be named after the table and the first column names, unless you pass +:name+ as an option.
When creating an index on multiple columns, the first column is used as a name for the index. For example, when you specify an index on two columns [+:first+, +:last+], the DBMS creates an index for both columns as well as an index for the first colum +:first+. Using just the first name for this index makes sense, because you will never have to create a singular index with this name.
Examples
Creating a simple index
generates
Creating a unique index
generates
Creating a named index
generates