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?
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.
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.
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.
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.
CREATE UNIQUE INDEX IF NOT EXISTS MyUniqueIndexName ON auth_user (email)
Also, read the official manual:
http://www.sqlite.org/lang_createindex.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With