MySQL和Oracle时区设置比较
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;
上一篇: C#中添加窗口的步骤图文详解
下一篇: Oracle 调度程序学习
推荐阅读
-
【SQL】Oracle和Mysql的分页、重复数据查询(limit、rownum、rowid)
-
MySQL设置global变量和session变量的两种方法详解
-
MySQL和SQLServer的比较
-
MySQL两种表存储结构MyISAM和InnoDB的性能比较测试
-
Oracle8i和Microsoft SQL Server比较
-
Oracle数据远程连接的四种设置方法和注意事项
-
Oracle10个分区和Mysql分区区别详解
-
MySQL设置global变量和session变量
-
Oracle的RBO和CBO详细介绍和优化模式设置方法
-
Oracle In和exists not in和not exists的比较分析