postgreSQL设置自增长字段并插入值


以postgreSQL为例(与mysql等完全不同),新建表_category,设置自增长字id。

create table _category(
id serial not null,
name varchar(10),
description varchar(200),
primary key(id)
);

这时postgreSQL会为你创建table以及对应的sequence:

sequence默认从一开始并以一为增量。

如果要自定义,如下:

create sequence seq_test   
     minvalue 100000  
     maxvalue 600000  
     start with 101000  
     increment by 1  
     catch 20  
     cycle  
     order;

 

向表中插入值:

insert into _category(name,description)values('yu','bihao');

不可以这样:

 insert into _category(id,name,description)values(_category_id_seq.nextval,'yu','bihao'); 

会报出错误: 对于表"_category_id_seq",丢失FROM子句项。。。

 

之后添加对其他数据库的同样操作例子。

 

 

参考:http://blog.csdn.net/qiyuexuelang/article/details/9531891

     http://rainbow702.iteye.com/blog/1550310

 

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM