d b wrote:
>   I would like to add check constraint for existing database. Is it
> possible?

SQLite has no built-in function (such as ALTER TABLE) to do this.

However, if you want to do a change that does not affect how the table's
data is stored in the database file, then you can change the table
definition in the system table.

First, get the current table definition:

  > select sql from sqlite_master where type='table' and name='emp';
  CREATE TABLE emp(id integer primary key autoincrement,
  fullname,fathername,mothername text)

Then, use "PRAGMA writable_schema" to allow changes, and change that
record:

  > PRAGMA writable_schema = on;
  > update sqlite_master set sql = 'CREATE TABLE ... CHECK ...' where 
type='table' and name='emp';
  > PRAGMA writable_schema = off;

Note: if you do this for a change that _does_ affect the table's record
format, the database will become corrupted.

It might be safer to copy the data into a new table:

  > create table emp2(... CHECK ...);
  > insert into emp2 select * from emp;
  > drop table emp;
  > alter table emp2 rename to emp;


Regards,
Clemens
_______________________________________________
sqlite-users mailing list
sqlite-users@sqlite.org
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users

Reply via email to