正文
包含postgresqlifendif的词条
小程序:扫一扫查出行
【扫一扫了解最新限行尾号】
复制小程序
【扫一扫了解最新限行尾号】
复制小程序
postgresql 怎么判断一个字符串是否是数字
--1.判断字符串是否是数字
CREATE OR REPLACE FUNCTION isnumeric(txtStr VARCHAR) RETURNS BOOLEAN
AS
$$
BEGIN
RETURN txtStr ~ '^([0-9]+[.]?[0-9]*|[.][0-9]+)$';
END;
$$
LANGUAGE 'plpgsql';
--2.判断传入的字符串是否是日期格式
CREATE OR REPLACE FUNCTION isDate(dateStr VARCHAR) RETURNS BOOLEAN
AS
$$
BEGIN
IF (dateStr IS NULL) THEN
RETURN FALSE;
END IF;
PERFORM dateStr::timestamp;
RETURN TRUE;
EXCEPTION
WHEN others THEN
RETURN FALSE;
END;
$$
LANGUAGE 'plpgsql';
postgreSQL怎样创建一个序列号/自动递增的字段
在oracle中sequence就是所谓的序列号,每次取的时候它会自动增加,一般用在需要按序列号排序的地方。 1、Create Sequence 你首先要有CREATE SEQUENCE或者CREATE ANY SEQUENCE权限, CREATE SEQUENCE emp_sequence INCREMENT BY 1 -- 每次加几个 START WITH 1 -- 从1开始计数 NOMAXVALUE -- 不设置最大值 NOCYCLE -- 一直累加,不循环 CACHE 10; 一旦定义了emp_sequence,你就可以用CURRVAL,NEXTVAL CURRVAL=返回 sequence的当前值 NEXTVAL=增加sequence的值,然后返回 sequence 值 比如: emp_sequence.CURRVAL emp_sequence.NEXTVAL 可以使用sequence的地方: - 不包含子查询、snapshot、VIEW的 SELECT 语句 - INSERT语句的子查询中 - NSERT语句的VALUES中 - UPDATE 的 SET中 可以看如下例子: INSERT INTO emp VALUES (empseq.nextval, 'LEWIS', 'CLERK',7902, SYSDATE, 1200, NULL, 20); SELECT empseq.currval FROM DUAL; 但是要注意的是: - 第一次NEXTVAL返回的是初始值;随后的NEXTVAL会自动增加你定义的INCREMENT BY值,然后返回增加后的值。CURRVAL 总是返回当前SEQUENCE的值,但是在第一次NEXTVAL初始化之后才能使用CURRVAL,否则会出错。一次NEXTVAL会增加一次SEQUENCE的值,所以如果你在同一个语句里面使用多个NEXTVAL,其值就是不一样的。明白? - 如果指定CACHE值,ORACLE就可以预先在内存里面放置一些sequence,这样存取的快些。cache里面的取完后,oracle自动再取一组到cache。 使用cache或许会跳号, 比如数据库突然不正常down掉(shutdown abort),cache中的sequence就会丢失. 所以可以在create sequence的时候用nocache防止这种情况。 2、Alter Sequence 你或者是该sequence的owner,或者有ALTER ANY SEQUENCE 权限才能改动sequence. 可以alter除start至以外的所有sequence参数.如果想要改变start值,必须 drop sequence 再 re-create . Alter sequence 的例子 ALTER SEQUENCE emp_sequence INCREMENT BY 10 MAXVALUE 10000 CYCLE -- 到10000后从头开始 NOCACHE ; 影响Sequence的初始化参数: SEQUENCE_CACHE_ENTRIES =设置能同时被cache的sequence数目。 可以很简单的Drop Sequence DROP SEQUENCE order_seq; 示例Sequence: CREATE SEQUENCE SCOTT.DMIFPOSTID START WITH 261 INCREMENT BY 1 NOMINVALUE NOMAXVALUE NOCYCLE CACHE 20 NOORDER 3、如何使用 第一种方法:一般来说需要新建一个触发器(TRIGGER),使得在插入数据之前先运行Sequence生成自增号。示例Trigger -- Create table create table TEST ( SEQ INTEGER not null, NAME VARCHAR2(20), PWD VARCHAR2(20) ) tablespace USERS pctfree 10 initrans 1 maxtrans 255 storage ( initial 64K minextents 1 maxextents unlimited ); -- Create/Recreate primary, unique and foreign key constraints alter table TEST add constraint PK_TEST primary key (SEQ) using index tablespace USERS pctfree 10 initrans 2 maxtrans 255 storage ( initial 64K minextents 1 maxextents unlimited ); create or replace trigger TI_TEST before insert on test for each row declare -- local variables here begin SELECT SEQ_TEST.NEXTVAL INTO :NEW.SEQ FROM DUAL; end TI_TEST; 插入语句 insert into test values('aa','aa'); 第二种方法:可以在插入数据时直接调用。 insert into table(id,name) values(seq_name.nextval,'名字');
postgreSQL触发器
PostgreSQL 提供按行与按语句触发的触发器。按行触发的触发器函数为触发语句影响的每一行执行一次;按语句触发的触发器函数为每条触发语句执行一次,而不管影响的行数。特别是,一个影响零行的语句将仍然导致按语句触发的触发器执行。这两种类型的触发器有时候分别叫做行级触发器和语句级触发器。触发器还通常分成 before 触发器和 after 触发器。语句级别的"before"触发器通常在语句开始做任何事情之前触发,而语句级别的"after"触发器在语句结束时触发。行级别的"before"触发器在对特定行进行操作之前触发,而行级别的"after"触发器在语句结束的时候触发(但是在任何语句级别的"after"触发器之前)。
一个触发器是一种声明,告诉数据库应该在执行特定的操作的时候执行特定的函数。触发器可以定义在一个 INSERT, UPDATE, DELETE 命令之前或者之后执行,要么是对每行执行一次,要么是对每条 SQL 语句执行一次。如果发生触发器事件,那么将在合适的时刻调用触发器函数以处理该事件。触发器函数必须在创建触发器之前,作为一个没有参数并且返回 trigger 类型的函数定义。触发器函数通过特殊的 TriggerData 结构接收其输入,而不是用普通的函数参数方式.
注意:
一.按语句触发的触发器应该总是返回 NULL.
二.如果必要,按行触发的触发器函数可以给调用它的执行者返回一行数据(一个类型为 HeapTuple 的数值),那些在操作之前触发的触发器有以下选择
1. 它可以返回 NULL 以忽略对当前行的操作。这就指示执行器不要执行调用该触
发器的行级别操作(对特定行的插入或者更改)。
2.只用于 INSERT 和 UPDATE 行触发器:返回的行将成为被插入的行或者是成为
将要更新的行。这样就允许触发器函数修改将要被插入或者更新的行。
一个无意导致任何这类行为的在操作之前触发的行级触发器必须仔细返回那个被当作新行传进来的行。也就是说,对于 INSERT 和 UPDATE 触发器而言,是 NEW 行,对于 DELETE触发器而言,是 OLD 行。
三. 对于在操作之后触发的行级触发器,其返回值会被忽略,因此可以回NULL。
下面通过具体的例子来说明在postgresql中触发器的建立和使用(老规矩先写代码然后讲解)
#include postgres.h
#include executor/spi.h
#include funcapi.h
#include commands/trigger.h
#include fmgr.h
extern Datum pg_trigf(PG_FUNCTION_ARGS);
#ifdef PG_MODULE_MAGIC
PG_MODULE_MAGIC;
#endif
PG_FUNCTION_INFO_V1(pg_trigf);
Datum
pg_trigf(PG_FUNCTION_ARGS)
{
TriggerData *trigdata = (TriggerData *)fcinfo-context;
HeapTuple rettuple = NULL;
int ret;
int proc; /* to store the value of SPI_processed (actual row number)*/
/* to be sure this function will be called by trigger */
if (!(CALLED_AS_TRIGGER(fcinfo))) {
elog(ERROR, "trigf: not called by triggermanager");
}
/* should be fired by statement */
if (TRIGGER_FIRED_FOR_ROW(trigdata-tg_event)) {
elog(ERROR, "cannot process row events");
}
/* should be fired before event */
if (TRIGGER_FIRED_AFTER(trigdata-tg_event)) {
elog(ERROR, "must be fired before event");
}
/* connect spi manager */
if ((ret = SPI_connect()) 0) {
elog(INFO, "SPI_connect failed: SPI_connectreturned: %d", ret);
return PointerGetDatum(rettuple);
}
/* check the permanent table name(perm_user) existsor not*/
ret = SPI_exec("SELECT tablename FROM pg_tables WHERE tablename LIKE'perm!_user' ESCAPE '!';", 1);
proc = SPI_processed;
if (ret != SPI_OK_SELECT) {
elog(INFO, "SPI_exec execute error: user table.");
SPI_finish();
return PointerGetDatum(rettuple);
}
/* create the permanent table(perm_user) if it does not exist */
if (proc 1) {
/* create permanent table: perm_user */
ret = SPI_exec("CREATE TABLE perm_user ASSELECT * FROM tbl_user;", 0);
if (ret != SPI_OK_SELINTO ) {
elog(INFO, "SPI_execexecute error: fail to create perm_user");
SPI_finish();
returnPointerGetDatum(rettuple);
}
/* set attribute to perm_user */
ret = SPI_exec("ALTER TABLE perm_user ADD PRIMARYKEY (user_name);", 0);
if (ret != SPI_OK_SELINTO) {
elog(INFO, "SPI_execexecute error: fail to add primary key to perm_user");
SPI_finish();
returnPointerGetDatum(rettuple);
}
/* set attribute to perm_user*/
ret = SPI_exec("ALTER TABLE perm_user ALTERuser_passwd SET NOT NULL;", 0);
if (ret != SPI_OK_SELINTO) {
elog(INFO, "SPI_execexecute error: fail to set attribute to password.");
SPI_finish();
returnPointerGetDatum(rettuple);
}
}
.....
/* check the permanent table name(perm_member) exists or not */
ret = SPI_exec("SELECT tablename FROM pg_tables WHERE tablename LIKE'perm!_member' ESCAPE '!';", 1);
proc = SPI_processed;
if (ret != SPI_OK_SELECT) {
elog(INFO, "SPI_exec execute errortbl_member");
SPI_finish();
return PointerGetDatum(rettuple);
}
/* create the permanent table(perm_member) if it does not exist */
if (proc 1) {
/* create permanent table: perm_member */
ret = SPI_exec("CREATE TABLE perm_member ASSELECT * FROM tbl_member;", 0);
if (ret != SPI_OK_SELINTO) {
elog(INFO, "SPI_execexecute error");
SPI_finish();
returnPointerGetDatum(rettuple);
}
/* set attribute to perm_member */
ret = SPI_exec("ALTER TABLE perm_member ADDCONSTRAINT user_fk FOREIGN KEY (user_name) REFERENCES perm_user(user_name) ONDELETE CASCADE ON UPDATE CASCADE;", 0);
if (ret != SPI_OK_UTILITY) {
elog(INFO, "SPI_execexecute error: fail to set attribute to user_name.");
SPI_finish();
returnPointerGetDatum(rettuple);
}
/* set attribute to perm_member */
ret = SPI_exec("ALTER TABLE perm_member ADDCONSTRAINT group_fk FOREIGN KEY (grp_name) REFERENCES perm_group(grp_name) ONDELETE CASCADE ON UPDATE CASCADE;", 0);
if (ret != SPI_OK_UTILITY) {
elog(INFO, "SPI_execexecute error: fail to set attribute to grp_name.");
SPI_finish();
return PointerGetDatum(rettuple);
}
/* add primary key to perm_member */
ret = SPI_exec("ALTER TABLE perm_member ADDPRIMARY KEY (user_name, grp_name);", 0);
if (ret != SPI_OK_UTILITY) {
elog(INFO, "SPI_execexecute error: fail to add primary key to perm_member.");
SPI_finish();
returnPointerGetDatum(rettuple);
}
}
/*close connect with SPI manager */
SPI_finish();
/* return back must be NULL*/
return PointerGetDatum(rettuple);
}
这个函数写法与postgresql服务端函数的写法很相似, 但是不完全相同.具体需要注意的地方是:
1. 需要多添加头文件:#include commands/trigger.h
2. 这个函数的返回值一定是trigger类型的.
3. 函数的开始最好确认这个函数是供触发器调用的并且明确一下自己要写的触发器的类型是什么,然后做一下判断,以免别的语句也触发的触发器.
二. 接下来的事情是编译:
gcc -fpic -c trigger.c-I/usr/local/postgreSQL/include/postgresql/server
gcc -shared -o trigger.so trigger.o
如果不明白可以参考手册(说句题外话,手册的作用实在是太大了,在手册中也提供了一例子).
三. 在数据库中创建函数和触发器:
/* create a trigger used to write memory and configmemory */
CREATE OR REPLACE FUNCTION pg_trigf() RETURNS trigger
AS 'filename'
LANGUAGE C IMMUTABLESTRICT;
CREATE TRIGGER tbuser BEFORE INSERT OR UPDATE OR DELETE
ON tbl_user FOR EACH STATEMENT
EXECUTE PROCEDURE pg_trigf();
CREATE TRIGGER tbgroup BEFORE INSERT OR UPDATE OR DELETE
创建的触发器是语句触发器,这个和手册上的不一样, 手册上的是行触发器.
然后在数据库中使用SQL语句就可以看到触发器的效果了.
postgresql中使用if else语句
1、首先在postgresql数据库中复制表的时候,必须先建立表结构,然后通过insert into语句来实现。
2、如果在复制数据之前没有建立相应的表结构,如下图中的错误信息。
3、在查询数据的时候也可以把两个表联合起来进行查询,通过union关键字来进行查询,如下图。
4、如果在使用union进行查询的进修,两条结果一样人被合并成一条。
5、如果想显示联合查询中相同的语句,可以使用union all来进行查询。
如何实现在PostgreSQL中不重复插入
主键的话自然不会重复, 其他的的话可以这样 if not exists(select * from 表 where 字段值 in 字段) begin insert into 表 (字段) values (字段值) where 字段值 is not in 字段 end
postgresqlifendif的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于、postgresqlifendif的信息别忘了在本站进行查找喔。