How to add a unique constraint to an already created table in sqlite ios?

I made the database and the table already through the sqlite database browser now, since I allowed to add the selected friends names from facebook for the user, but I do not want the second time they were added, the same names should not be added to the database , therefore, how to add restrictions to sqlite

I tried to check the sqlite database browser, but nothing changed there.

I tried lita for it, but there is an option in lita to make it non-zero and unique, but I can’t click this checkbox, I don’t know why

Help plz

+8
ios sqlite
source share
3 answers

I assume that you are using SQLiteManager from FireFox, please re-create the table, it usually does not allow you to change restrictions when you have already created the table.

Edited See Image below

enter image description here

+1
source share

You cannot add a constraint to an existing table in SQLite , (SQL has an option for this). You need to recreate the table with the necessary restrictions.

Sqlite has only a few parameters for the alter table command. Please check the image:

Alter table

Also check out the Sqlite org link .


EDIT

However, you can add a unique index to your table to achieve the same effect. To do this, you can use the following query:

CREATE UNIQUE INDEX your_unique_index ON your_table(column_name); 

In most cases, UNIQUE and PRIMARY KEY constraints are implemented by creating a unique index in the database.

Link: SQLite Constraints

+18
source share

You can add a constraint by creating a unique index:

 CREATE UNIQUE INDEX ux_friend_name ON friend(name); 

where ux_friend_name is the unique name of the index, friend(name) is the table and its columns, which will be affected by this restriction.

You can read it here .

+8
source share

All Articles