CREATE SEQUENCE person_seq;
create table person (
id INT4 DEFAULT nextval('person_seq') PRIMARY KEY,
name text
);
1、获取序列的下一个值 select nextval('person_seq')
ps: oracle 获取序列的下一个值 select 序列名称.nextval from dual;
2、获取序列当前值 select currval('person_seq')
3、把所建的person表赋予某个角色 alter table person owner to 角色
把所建序列赋予某个角色 ALTER SEQUENCE 序列名称
OWNER TO 角色;
4、把所建的序列赋予到表中的某列上 alter sequence person_seq owend by person.id 二、数据库中某个字段的null值不能与其他值的比较
比如:某个表的flag字段置为null,查询时只能通过is not null 或者是is null,不能写成flag !=1,has_messaged字段值为null,通过SELECT *FROM 表名 where has_messaged !=1,查询has_messaged不为1的记录是错误的。 三、数据库中某个字段全为数字的字符串,且需要比较大小
1、数据库中某个字段全为数字的字符串,且需要比较大小的话,需要转换成整型来进行比较
如:cast(要转的字符串 as int) as 别名 2、查询数据库的连接数 select count(*) from pg_stat_activity
四、存储过程
1、使用存储过程往表里插入数据
create or replace function dataInsert(integer) returns integer as
$$
declare
count alias for $1;
begin
while count>0 loop
insert into 表名(?,?,?....,?) values (?,?,?...,?);
count := count-1;
end loop;
return count;
end;
$$
LANGUAGE plpgsql;
select dataInsert(2);