Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add unique index. SQLite3

Tags:

I need to add a unique field index to an existing table. I made this row:

ALTER TABLE auth_user ADD UNIQUE INDEX (email);

The table and the field are already exist. The error is:

Query Error: near "UNIQUE": syntax error Unable to execute statement

What am I missed? Did it have any specific requirements for SQLite3?

like image 480
I159 Avatar asked Oct 20 '11 13:10

I159


People also ask

How do you create a unique index?

Right-click the table on which you want to create a unique index and select Design. On the Table Designer menu, select Indexes/Keys. In the Indexes/Keys dialog box, click Add. Select the new index in the Selected Primary/Unique Key or Index text box.

How do I create a unique field in SQLite?

Introduction to SQLite UNIQUE constraint To define a UNIQUE constraint, you use the UNIQUE keyword followed by one or more columns. You can define a UNIQUE constraint at the column or the table level. Only at the table level, you can define a UNIQUE constraint across multiple columns.

Does unique constraint CREATE INDEX SQLite?

Yes, they are the same. A unique constraint in a table definition is exactly and precisely the same as an explicit unique index, save for what is different: an explicit UNIQUE index may contain expressions, a UNIQUE constraint in a table may only contain bare columns.

Does SQLite have indexing?

A table may have multiple indexes. Whenever you create an index, SQLite creates a B-tree structure to hold the index data. The index contains data from the columns that you specify in the index and the corresponding rowid value. This helps SQLite quickly locate the row based on the values of the indexed columns.


1 Answers

CREATE UNIQUE INDEX IF NOT EXISTS MyUniqueIndexName ON auth_user (email)

Also, read the official manual:

http://www.sqlite.org/lang_createindex.html

like image 169
Adriano Carneiro Avatar answered Feb 02 '23 14:02

Adriano Carneiro