盘点一下PostgreSQL和MySQL的区别点

上期有说,数据环境切换,由MySQL换成PostgreSQL。
那么为了大家以后少踩坑,简单总结了下我曾踩过的坑和需要注意的点。
首先PostgreSQL有一个模式的概念。
一、格式区别:

和Oracle一样,PostgreSQL也是严格区分大小写。

二、符号区别:

和Oracle一样PostgreSQL中," " 双引号是区分库名,关键字等,而MySQL则是反单引号(tab键上方的键),pg查询时字符类型的字段必须使用单引号,而MySQL带有优化器(不是必须)。

三、自增区别:

MySQL中使用auto_increment ,在需要的列指定自增,而pg中需要设置自增序列。

(1)使用SQL语句
①创建表时

CREATE table infisa_template_config(id serial );

②表已存在

<--设置序列从1开始,自增1-->
CREATE SEQUENCE user_id_seq START WITH 1  
INCREMENT BY 1  NO MINVALUE  NO MAXVALUE  CACHE 1;
<--设置序列-->
ALTER table user ALTER column id SET DEFAULT nextval('user_id_seq');

(2)使用可视化工具
连接navicat15(或者使用DBeaver)
在这里插入图片描述
点击序列
在这里插入图片描述
可以通过可视化界面新建序列
在这里插入图片描述
注:同步数据的话,推荐通过sql方式新建序列。

四、函数区别:
(1)时间转化

①时间转字符串:

MySQL: date_format(a.tag_create_date,‘%Y-%m-%d %H:%i:%s’)

PostgreSQL: to_char(a.tag_create_date,‘yyyy-mm-dd HH:MM:SS’)

②字符串转时间:

MySQL: date_format(a.tag_create_date,‘%Y-%m-%d %H:%i:%s’)

PostgreSQL: to_date(a.tag_create_date,‘yyyy-mm-dd HH:MM:SS’)

(2) IFNULL()函数

MySQL: IFNULL(a.idm,‘’)

PostgreSQL: COLESCE(a.id,‘’)

(3) sysdate()函数

MySQL: SELECT sysdate()

PostgreSQL: SELECT now()

(4) find_in_set()函数(允许在逗号分隔的字符串列表中查找指定字符串的位置)

MySQL: SELECT t.dept_id FROM sys_dept t WHERE find_in_set(‘100’, ancestors)

PostgreSQL: SELECT t.dept_id FROM sys_dept t WHERE ‘100’ = ANY (string_to_array(ancestors, ‘,’))

(5)group_concat()函数

MySQL: select a.name,group_concat(distinct city)from user_city a group by a.name;

PostgreSQL: select a.name,array_to_string(array_agg(distinct a.city),‘,’)from user_city a group by a.name;

(6)LIMIT

MySQL: select id,name from hospital.ods_user_basic limit 10,2;

PostgreSQL: select id,name from hospital.ods_user_basic limit 10 offset 2;


版权声明:本文为weixin_47162914原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。