How to add default value in SQLite?

前端 未结 1 2004
北恋
北恋 2021-02-03 17:51

I had a table modified to add status column to it in this fashion

ALTER TABLE ITEM ADD COLUMN STATUS VARCHAR DEFAULT \'N\';

However SQLite does

相关标签:
1条回答
  • 2021-02-03 18:33

    Looks good to me. Here are the Docs.

    sqlite> create table t1 (id INTEGER PRIMARY KEY, name TEXT, created DATE);
    sqlite> .table
    t1
    sqlite> .dump
    PRAGMA foreign_keys=OFF;
    BEGIN TRANSACTION;
    CREATE TABLE t1 (id INTEGER PRIMARY KEY, name TEXT, created DATE);
    COMMIT;
    
    sqlite> alter table t1 add column status varchar default 'N';
    sqlite> .dump
    PRAGMA foreign_keys=OFF;
    BEGIN TRANSACTION;
    CREATE TABLE t1 (id INTEGER PRIMARY KEY, name TEXT, created DATE, status varchar default 'N');
    COMMIT;
    
    sqlite> insert into t1 (name) values ("test");
    sqlite> select * from t1;
    1|test||N
    

    Dump your schema and verify that your table structure is there after calling ALTER TABLE but before the INSERT. If it's in a transaction, make sure to COMMIT the transaction before the insert.

    $ sqlite3 test.db ".dump"
    
    0 讨论(0)
提交回复
热议问题