首页 > 数据库技术 > 详细

PostgreSQL tips

时间:2015-03-24 19:03:17      阅读:309      评论:0      收藏:0      [点我收藏+]

 

tip 1

在sql中我们可以设置一个列自增长identity(1,1),但在postgresql中却没有这个关键字定义。但postgresql也有实现相关功能,那就是只需要将该列数据类型标记为serial,就可以实现sql中的自增长功能

   

smallserial 2 bytes small autoincrementing integer 1 to 32767
serial 4 bytes autoincrementing integer 1 to 2147483647
bigserial 8 bytes large autoincrementing integer 1 to 9223372036854775807

tips 2

 对于postgresql中的部分系统表,有个唯一表示Row identifier 行标识符,(hidden attribute; must be explicitly selected),是一个隐藏的字段,只有显示调用才能显示,比如select oid,* from pg_class

 tips 3  

postgresql中case when 语法和sql 差不多。

SELECT 
CASE WHEN atttypid=23 THEN true
ELSE false
END as hehe, * from pg_attribute 
where attrelid=‘90330‘

tip 4

postgressql中的自增长用serial存储,但是在数据库中是以默认值的形式实现的,用nextvalue函数,可以在pg_attrdef表中查出值

tip 5

postgresql字符串拼接函数,字符串拼接语法

string || string text String concatenation ‘Post‘ || ‘greSQL‘ PostgreSQL

tip 6

 对于数组可以用unnest函数化为多行然后left join,例如select * from pg_index对于系统的索引表中 indkey字段,对于复合主键就需要化为多行然后关联

unnest(anyarray) setof anyelement expand an array to a set of rows unnest(ARRAY[1,2])
1
2
(2 rows)

tip7    sql中添加表的时候对于以下敏感字可以加上方括号,而在postgresql中,对于敏感字需要加上双引号如

create table "Table" ( 
Id serial not null ,
PointCode varchar(32) 
);

tip8

PostgreSQL中像numeric这样的包含长度和精度的数据类型被存储在系统表pg_attribute里面,但是并没有单独存放这个字段的值,而是通过一个函数转化为一个整数存放在里面atttypmod,这样当我们直接到系统表取值的时候就不得不将其重新转化为精度和长度。转换方法如下。

SELECT
  CASE atttypid
         WHEN 21 /*int2*/ THEN 16
         WHEN 23 /*int4*/ THEN 32
         WHEN 20 /*int8*/ THEN 64
         WHEN 1700 /*numeric*/ THEN
              CASE WHEN atttypmod = -1
                   THEN null
                   ELSE ((atttypmod - 4) >> 16) & 65535     -- calculate the precision
                   END
         WHEN 700 /*float4*/ THEN 24 /*FLT_MANT_DIG*/
         WHEN 701 /*float8*/ THEN 53 /*DBL_MANT_DIG*/
         ELSE null
  END   AS numeric_precision,
  CASE 
    WHEN atttypid IN (21, 23, 20) THEN 0
    WHEN atttypid IN (1700) THEN            
        CASE 
            WHEN atttypmod = -1 THEN null       
            ELSE (atttypmod - 4) & 65535            -- calculate the scale  
        END
       ELSE null
  END AS numeric_scale,
  *
FROM 
    pg_attribute ;

tip9  

postgresql c#操作类库  Npgsql ( .NET 连接到 PostgreSQL 数据库的驱动程序) 

  NpgSql连接字符串:

 

//远程连接模式
Server=127.0.0.1;Port=5432;Database=myDataBase;User Id=myUsername;
Password=myPassword;
PostgreSQL
//本地连接模式
Server=127.0.0.1;Port=5432;Database=myDataBase;Integrated Security=true;

 另:https://connectionstrings.com/下有几乎所有连接数据库的连接字符串例子

参考  stackoverflow

参考:http://blog.163.com/digoal@126/blog/static/163877040201371763839672/

参考:https://connectionstrings.com/postgresql/

本文地址:http://www.cnblogs.com/santian/p/4362679.html

博客地址:http://www.cnblogs.com/santian/

转载请以超链接形式标明文章原始出处。

PostgreSQL tips

原文:http://www.cnblogs.com/santian/p/4362679.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!