SQLite auto-increment non-primary key field

前端 未结 4 1279
醉话见心
醉话见心 2020-12-01 10:56

Is it possible to have a non-primary key to be auto-incremented with every insertion?

For example, I want to have a log, where every log entry has a primary key (for

4条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-01 11:28

    SQLite creates a unique row id (rowid) automatically. This field is usually left out when you use "select * ...", but you can fetch this id by using "select rowid,* ...". Be aware that according to the SQLite documentation, they discourage the use of autoincrement.

    create table myTable ( code text, description text );
    insert into myTable values ( 'X', 'some descr.' );
    select rowid, * from myTable;
    

    :: Result will be; 1|X|some descr.

    If you use this id as a foreign key, you can export rowid - AND import the correct value in order to keep data integrity;

    insert into myTable values( rowid, code text, description text ) values
    ( 1894, 'X', 'some descr.' );
    

提交回复
热议问题