Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add unique constraint to existing table in Sqlite?

Tags:

sqlite

I have an existing table in Sqlite. How do I add a unique constraint to it?

like image 628
user1660882 Avatar asked Mar 19 '13 11:03

user1660882


1 Answers

You can't add a constraint to existing table in SQLite,(In SQL there is a option for do that). You need to re-create the table with necessary contraints.

There is only a few option available for alter table command in sqlite. Please check the image:

Alter table

Also check Sqlite org reference.


EDIT

However you can add unique index for your table to achieve the same effect. So you can use the following query to achieve that:

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.

Reference : SQLite Constraints

like image 200
Midhun MP Avatar answered Oct 10 '22 01:10

Midhun MP