最近有位同事问了一个,和EDB日期类型相关的问题,看起来很有意思,但实际和EDB原理有一些关系,使用客户端工具,创建新表,字段名称a,类型选择的是date,
保存完成,此时类型显示的则是timestamp,长度6,并不是之前的date,
开始以为是客户端的问题,使用SQL语句,
create table a(
a date;
);
执行完成,发现类型就是timestamp,不是语句定义的date,难道有类型限制?
经过雪哥指点,从官方手册和官网上,找到了些许答案,
When DATE appears as the data type of a column in the commands, it is translated to TIMESTAMP(0) at the time the table definition is stored in the database if the configuration parameter edb_redwood_date is set to TRUE. Thus, a time component will also be stored in the column along with the date. This is consistent with Oracle’s DATE data type.
When DATE appears as the data type of a column in the data definition language (DDL) commands, CREATE TABLE or ALTER TABLE, it is translated to TIMESTAMP(0) at the time the table definition is stored in the database. Thus, a time component will also be stored in the column along with the date.
If edb_redwood_date is set to FALSE the column’s data type in a CREATE TABLE or ALTER TABLE command remains as a native PostgreSQL DATE data type and is stored as such in the database. The PostgreSQL DATE data type stores only the date without a time component in the column.
Regardless of the setting of edb_redwood_date, when DATE appears as a data type in any other context such as the data type of a variable in an SPL declaration section, or the data type of a formal parameter in an SPL procedure or SPL function, or the return type of an SPL function, it is always internally translated to a TIMESTAMP(0) and thus, can handle a time component if present.
TIMESTAMP accepts an optional precision value p which specifies the number of fractional digits retained in the seconds field. The allowed range of p is from 0 to 6 with the default being 6.
When TIMESTAMP values are stored as double precision floating-point numbers (currently the default), the effective limit of precision may be less than 6. TIMESTAMP values are stored as seconds before or after midnight 2000-01-01. Microsecond precision is achieved for dates within a few years of 2000-01-01, but the precision degrades for dates further away.
When TIMESTAMP values are stored as eight-byte integers (a compile-time option), microsecond precision is available over the full range of values. However eight-byte integer timestamps have a more limited range of dates than shown above: from 4713 BC up to 294276 AD. TIMESTAMP (p) WITH TIME ZONE is similar to TIMESTAMP (p), but includes the time zone as well.
EDB中的日期类型,
介绍的比较清楚,简单来讲,就是有一个参数,edb_redwood_date控制DATE类型是否含有时间,如果需要和Oracle兼容,则设置为TRUE,
edb_redwood_date – Controls whether or not a time component is stored in DATE columns. For Oracle compatible behavior, set edb_redwood_date to TRUE.
参数默认值为false,可以session级别修改,
看下当前库中,该参数值设置为on,
设置参数值为off,
执行建表语句,
create table a(
a date;
);
此时类型就是date,不是timestamp,
之前我们说过,《》,EDB是语法上最兼容Oracle的数据库,但其核心还是PG,因此为了兼容,就可能要支持两种用法,相应地就要有参数可以控制,相当于开关操作。PG中的DATE类型仅含有日期,不含有时间,这和Oracle中的DATE,有些不同,于是出现了edb_redwood_date参数,可以控制和Oracle的兼容性,之所以此处值为true不是false,推测有可能因为安装的时候,选择兼容Oracle,因此参数会以兼容Oracle为准。
如果您觉得本文有帮助,欢迎关注转发:bisal的个人杂货铺,