Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

CHECK constraint matching beginning of value

We are using sqlite3 for a database and ensuring values inserted into a column matching a specific string would be useful in our case.

Example:

CREATE TABLE people ("firstname" TEXT),
CHECK(LIKE('ca%',"firstname"));

The error we see is that sqlite3 gives this error:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Parse error: near "CHECK": syntax error
  CREATE TABLE people ("firstname" TEXT), CHECK(LIKE('ca%',"firstname"));
                            error here ---^

We want this be ok:

INSERT INTO people VALUES ('Callie');

but this be not ok:

INSERT INTO people VALUES ('Erik');

Is a CHECK with this possible in sqlite3? there is LIKE but everybody seem to only mention SELECT statements.

>Solution :

This is the correct syntax for the constraint:

CREATE TABLE people (
  firstname TEXT,
  CHECK (firstname LIKE 'ca%')
);

See the demo.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading