Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement a unique index on two columns in rails

I have a table and I'm trying to add a unique index on two columns. Those columns are also indexed. So my question is if I just can remove the indexes who were just for one column or if I have to use all three indexes:

add_index "subscriptions", ["user_id"] add_index "subscriptions", ["content_id"] add_index "subscriptions", ["user_id"], ["content_id"], :unique => true 
like image 479
Markus Avatar asked Nov 08 '10 12:11

Markus


People also ask

What is unique index with multiple columns?

A unique index ensures the index key columns do not contain any duplicate values. A unique index may consist of one or many columns. If a unique index has one column, the values in this column will be unique. In case the unique index has multiple columns, the combination of values in these columns is unique.

Can we use same index on two columns?

Introduction to MySQL composite indexA composite index is an index on multiple columns. MySQL allows you to create a composite index that consists of up to 16 columns. A composite index is also known as a multiple-column index.

Are unique columns indexed?

When you specify UNIQUE KEY , the column is indexed. So it has no difference in performance with other indexed column (e.g. PRIMARY KEY) of same type.

Can a table have two unique index?

Benefits of a Unique Index Provided that the data in each column is unique, you can create both a unique clustered index and multiple unique nonclustered indexes on the same table. Unique indexes ensure the data integrity of the defined columns.


1 Answers

add_index :subscriptions, [:user_id, :content_id], unique: true 
like image 111
shingara Avatar answered Oct 03 '22 12:10

shingara