sqlite的autoincrement是一个关键字,用于表中的字段值自动递增。我们可以在创建表时在特定的列表名称上使用autoincrement关键字实现该字段值的自动增加。关键字 autoincrement只能用于整型(integer)字段。
语法
create table table_name( column1 integer autoincrement, column2 datatype,阿 column3 datatype, ..... columnn datatype, );
实例
假设要创建的company表如下所示:
sqlite> create table company( id integer primary key autoincrement, name text not null, age int not null, address char(50), salary real );
现在,向company表插入以下记录:
insert into company (name,age,address,salary) values ( 'paul', 32, 'california', 20000.00 ); insert into company (name,age,address,salary) values ('allen', 25, 'texas', 15000.00 ); insert into company (name,age,address,salary) values ('teddy', 23, 'norway', 20000.00 ); insert into company (name,age,address,salary) values ( 'mark', 25, 'rich-mond ', 65000.00 ); insert into company (name,age,address,salary) values ( 'david', 27, 'texas', 85000.00 ); insert into company (name,age,address,salary) values ( 'kim', 22, 'south-hall', 45000.00 ); insert into company (name,age,address,salary) values ( 'james', 24, 'houston', 10000.00 );
这将向company表插入7个元组,此时company表的记录如下:
id name age address salary ---------- ---------- ---------- ---------- ---------- 1 paul 32 california 20000.0 2 allen 25 texas 15000.0 3 teddy 23 norway 20000.0 4 mark 25 rich-mond 65000.0 5 david 27 texas 85000.0 6 kim 22 south-hall 45000.0 7 james 24 houston 10000.0