Elye Elye - 1 month ago 8
SQL Question

How to support ADD CONSTRAINTS in SQLite?

I'm thinking of ADD CONSTRAINT to my db table as per example from http://www.w3schools.com/sql/sql_unique.asp

ALTER TABLE Persons ADD CONSTRAINT uc_PersonID UNIQUE (P_Id,LastName)


However, discover from https://www.sqlite.org/omitted.html, ADD CONSTRAINT is not supported by SQLite. What's the option I have (fyi, I'm developing in Android, and only have SQLite).

Answer

Instead of a unique constraint, just add a unique index:

CREATE UNIQUE INDEX unq_persons_pid_lastname ON PERSONS(P_Id, LastName);

It is curious that you would have a table called Persons and that P_Id would not already be unique in the table (by being a primary key).

Comments