When recently encountered in a data table area right questions, troubleshoot general idea is to look at the table field type, time zone, and ways of looking at the insertion time to see. Oracle official documentation "Database Globalization Support Guide" There is a very detailed description, inductive learning about.
First, the time type
Oracle in time type divided into two categories - Datetime and the Data Types Interval , this article focuses on the first category Datetime.
Datetime can be divided into four categories , which are associated with the time zone after two categories:
- DATE
- TIMESTAMP
- TIMESTAMP WITH TIME ZONE
- TIMESTAMP WITH LOCAL TIME ZONE
1. DATE
Storage date + time, accurate to the second, and the time zone area information is not stored. Determined by the output format and language NLS_DATE_LANGUAGE NLS_DATE_FORMAT and two initialization parameters. If you do not specify a query when these two parameters is not typecast, will by default output format.
SQL> select sysdate from dual;
SYSDATE
-------------------
2014-02-12 01:12:18
2. TIMESTAMP
DATE type of expansion, the storage date + time, accurate to the second decimal 0 to 9 (default 6), and the time zone is not stored area information. By the output format and language NLS_TIMESTAMP_FORMAT and NLS_DATE_LANGUAGE decision two initialization parameters. If you do not specify a query when these two parameters is not typecast, will by default output format.
SQL> select localtimestamp from dual;
LOCALTIMESTAMP
---------------------------------------------------------------------------
12-FEB-14 01.14.12.945256 AM
SQL> alter session set nls_timestamp_format='YYYY-MM-DD HH24:MI:SSXFF';
Session altered.
SQL> select localtimestamp from dual;
LOCALTIMESTAMP
---------------------------------------------------------------------------
2014-02-12 01:28:31.652888
3. TIMESTAMP WITH TIME ZONE
TIMESTAMP type of expansion, the storage date + time, accurate to the second decimal 0 to 9 (default 6), when the memory area (or the time zone and area) information . This type of data with the current client session timezone when saved to the database, no matter what time zone view this data, the data will not change any time zone.
create table t1 (id number,time timestamp with time zone); --创建t1表,其中time 列的数据类型是timestamp with time zone
Table created.
select sessiontimezone from dual; --当前客户端的session timezone 是 -8:00
SESSIONTIMEZONE
---------------------------------------------------------------------------
-08:00
insert into t1 values(1,timestamp '2014-02-12 02:00:00'); --向t1表中插入一条数据
1 row created.
select * from t1; --查看t1表,其中time列带时区显示,并且时区为数据被插入时的session timezone
ID TIME
---------- ---------------------------------------------------------------------------
1 2014-02-12 02:00:00.000000 -08:00
alter session set time_zone='-6:00'; --修改当前客户端的session timezone为 -6:00
Session altered.
select * from t1; --再次查看t1表,其中time列数据无变化
ID TIME
---------- ---------------------------------------------------------------------------
1 2014-02-12 02:00:00.000000 -08:00
4. TIMESTAMP WITH LOCAL TIME ZONE
Another type TIMESTAMP extended, storage date + time, accurate to the second decimal 0 to 9 (default 6), the time zone information is not stored, but the time the client after input into the database based conversion database timezone (this is the meaning of database tmiezone setting where, as a TIMESTAMP WITH LOCAL TIME ZONE type of computing scale). When a user queries this type of data, Oracle will then converted to data of the user session time zone back to the user.
A time zone Client -> time zone database database tmiezone set - time zone> Client B
create table t2(id number,time timestamp with local time zone); -- 创建t2表,其中time列为TIMESTAMP WITH LOCAL TIME ZONE
Table created.
insert into t2 values(1,timestamp '2014-02-12 02:10:00 -8:00'); --在t2表插入数据指定时区为-8:00,实际在保存到数据库时转化为基于database timezone的时间保存
1 row created.
select sessiontimezone from dual; --当前客户端的session timezone 为 -6:00
SESSIONTIMEZONE
---------------------------------------------------------------------------
-06:00
select * from t2; --查看时oracle将数据转换成当前客户端session timezone的时间
ID TIME
---------- ---------------------------------------------------------------------------
1 2014-02-12 04:10:00.000000
The choice of the type of time
- DATE: Time required accuracy is not high, do not need to save the time zone / Area Information
- TIMESTAMP: high temporal precision required, no need to save the time zone / Area Information
- TIMESTAMP WITH TIME ZONE: Time zone / region information needs to be saved. Such as the need accurate records of the time and place of each transaction (time zone) to see if it happened in a few local
- TIMESTAMP WITH LOCAL TIME ZONE: do not care about location operation takes place, is only interested in operating in your current location when the occurrence of a few areas. For example, there is a TV broadcasting in Japan time ten o'clock, but in fact, I only care about a few Chinese can catch live together in time, for me, the most convenient is a search database directly told me it began broadcasting in China nine times.
Second, the time zone
In fact, according to an earlier already know, distinguish between the two when Oracle - time zone and time zone database session
1. Database time zone
As TIMESTAMP WITH LOCAL TIME ZONE type of computing scales.
Query methods
SELECT dbtimezone FROM DUAL;
Setting method
- You can specify a SET TIME_ZONE clause in the CREATE DATABASE.
CREATE DATABASE db01
...
SET TIME_ZONE='Europe/London';
-- 或者
CREATE DATABASE db01
...
SET TIME_ZONE='-05:00';
- The latter can also be modified (DB restart to take effect)
ALTER DATABASE SET TIME_ZONE='Europe/London';
--或者
ALTER DATABASE SET TIME_ZONE='-05:00';
2. The session time zone
Sql when the current session's time zone, the default is the server operating system's time zone.
Query methods
SELECT sessiontimezone FROM DUAL;
Setting method
- You can set the operating system environment variables ORA_SDTZ
setenv ORA_SDTZ 'OS_TZ' #默认
setenv ORA_SDTZ 'DB_TZ'
setenv ORA_SDTZ 'Europe/London'
setenv ORA_SDTZ '-05:00'
- You can also use the sql command set
ALTER SESSION SET TIME_ZONE=local; -- 相当于os
ALTER SESSION SET TIME_ZONE=dbtimezone;
ALTER SESSION SET TIME_ZONE='Asia/Hong_Kong';
ALTER SESSION SET TIME_ZONE='+10:00';
Third, the time correlation function
Datetime function operable date (DATE), timestamp (TIMESTAMP, TIMESTAMP WITH TIME ZONE, TIMESTAMP WITH LOCAL TIME ZONE) and interval (INTERVAL DAY TO SECOND, INTERVAL YEAR TO MONTH) value types.
1. Datetime Functions Designed for the DATE Data Type
2. Additional Datetime Functions
3. Time Zone Conversion Functions
参考