MySQL和Oracle时区设置比较

WBOY
풀어 주다: 2016-06-07 14:59:17
원래의
1163명이 탐색했습니다.

MYSQL:注意时区会影响TIMESTAMP的取值,默认为系统时区为TIME_ZONE=SYSTEM, 动态可以修改set global time_zone =

MYSQL:
注意时区会影响TIMESTAMP的取值,默认为系统时区为TIME_ZONE=SYSTEM,
 动态可以修改
set global  time_zone = '+8:00';

然后
my.cnf加上,永久修改
default-time_zone = '+8:00'

 The current time zone. This variable is used to initialize the time zone for each client that
 connects. By default, the initial value of this is 'SYSTEM'(which means, “use the value of
 system_time_zone”).
也就是说每个链接都会使用这个参数作为他的默认时区,而TIMESTMAP是根据客户端的时区不同而不同,所以如果如果这个参数设置有误会导致TIMESTAMP时间出现问题

MYSQL的测试:
mysql> select now();
 +---------------------+
 | now()              |
 +---------------------+
 | 2015-06-12 12:10:13 |
 +---------------------+
 1 row in set (0.00 sec)


 mysql> select sysdate();
 +---------------------+
 | sysdate()          |
 +---------------------+
 | 2015-06-12 12:10:18 |
 +---------------------+
 1 row in set (0.00 sec)


 mysql> select current_timestamp from dual;
 +---------------------+
 | current_timestamp  |
 +---------------------+
 | 2015-06-12 12:10:46 |
 +---------------------+
 1 row in set (0.00 sec)


 mysql> set time_zone='+00:00';
 Query OK, 0 rows affected (0.00 sec)


 mysql> select sysdate();
 +---------------------+
 | sysdate()          |
 +---------------------+
 | 2015-06-12 04:11:01 |
 +---------------------+
 1 row in set (0.00 sec)


 mysql> select now();
 +---------------------+
 | now()              |
 +---------------------+
 | 2015-06-12 04:11:04 |
 +---------------------+
 1 row in set (0.00 sec)


 mysql> select current_timestamp from dual;
 +---------------------+
 | current_timestamp  |
 +---------------------+
 | 2015-06-12 04:11:06 |
 +---------------------+
 1 row in set (0.01 sec)

可见MYSQL的NOW(),SYSDATE(),current_timestamp 均跟着客户端时区走的。

Oracle:
另外说一下ORACLE的时区问题,ORACLE时区分为
dbtimezone和sessiontimezone
其中DBTIMEZONE只和TIMESTAMP WITH LOCAL TIME ZONE有关,在TIMESTAMP WITH LOCAL TIME ZONE类型存入数据库中,实际上是转换为DBTIMEZONE的时间,取出的时候
 自动加上客户端的SESSIONTIMEZONE的偏移量,文档如下:

TimeStamp with Local Time Zone (TSLTZ) data stores internally the time converted to/from the database timezone (see point 3) from the timezone specified at insert/select time. 

Note that the data stored in the database is normalized to the database time zone, and the time zone offset is not stored as part of the column data, the current DBTIMZONE is used. When users retrieve the data, Oracle Database returns it in the users' local session time zone from the current DBTIMEZONE.

而其他的时间类型和DBTIMEZONE无关,这也是为什么有了TIMESTAMP WITCH LOCAL TIME ZONE修改DBTIMEZONE不行的原因,因为如果修改了DBTIMEZONE会导致时间错误。
 实际上MYSQL的TIMESTAMP类型和ORACLE的TIMESTAMP WITCH LOCAL TIME ZONE类型都是根据客户端的时间来进行返回时间,但是MYSQL可以简单的设置
 time_zone参数来改变所有连接的时区,这样返回的时间能够正确。
 在说明一下ORACLE的TIMESTAMP和MYSQL的TIMESTAMP完全不同,
ORACLE的TIMESTAMP是为了精确到秒后6位,
 而MYSQL的TIMESTAMP是为了更少的存储单元(DATETIME为4字节,TIMESTAMP为1个字节)但是范围为1970的某时的开始到2037年,而且会根据客户端的时区判断返回值

 而sessiontimezone,则影响着客户端的时区,TIMESTAMP WITCH LOCAL TIME ZONE也会跟着这个时区进行改变,其他数据类型如DATE,TIMESTAMP等不会受到影响
 可以再ALTER SESSION中设置也可以设置环境变量TZ=
如:
ALTER SESSION SET TIME_ZONE = '-05:00';
或者
export TZ='Asia/Shanghai';

做个简单的实验
SQL> desc testtim;
  Name                                      Null?    Type
  ----------------------------------------- -------- ----------------------------
 DATE1                                              TIMESTAMP(6)
  DATE2                                              TIMESTAMP(6) WITH TIME ZONE
  DATE3                                              TIMESTAMP(6) WITH LOCAL TIME ZONE
 SQL> select * from testtim;

원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
최신 이슈
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!