更新时间:2024-12-04 GMT+08:00
分享

SQL兼容性概述

GaussDB数据库兼容绝大多数MySQL语法,但存在部分差异。本章节介绍GaussDB数据库当前支持的MySQL语法。

  • 部分关键字在MySQL可以做标识符但在M-Compatibility不可以或存在限制,如表1所示。
    表1 受限标识符列表

    关键字类型

    关键字

    约束

    保留(可以是类型或函数)

    COLLATION、COMPACT

    除函数和变量,不可以作为其他数据库标识符。

    非保留(不能是类型或函数)

    BIT、BOOLEAN、COALESCE、DATE、NATIONAL、NCHAR、NONE、NUMBER、TEXT、TIME、TIMESTAMP、TIMESTAMPDIFF

    不可以作为函数或变量的标识符。

    保留

    ANY、ARRAY、BUCKETS、DO、END、LESS、MODIFY、OFFSET、ONLY、RETURNING、SOME、USER

    不可以作为任意数据库标识符。

  • GaussDB优化器与MySQL的优化器存在差异,由于优化器生成的执行计划的差异,可能导致GaussDB行为与MySQL行为的不一致,不影响业务数据结果。

    例如在下述场景中,GaussDB在计算col1以及使用col1进行where比较时,均会调用cast函数,因此会产生两条WARNING记录;MySQL在计算col1时会调用cast函数,在进行where比较时,直接使用计算好的值直接进行比较,因此只产生一条WARNING记录。

    -- GaussDB行为:
    m_db=# SELECT * FROM (SELECT cast('abc' AS decimal) AS col1) t1 WHERE col1=0;
    WARNING:  Truncated incorrect DECIMAL value: 'abc'
    WARNING:  Truncated incorrect DECIMAL value: 'abc'
    CONTEXT:  referenced column: col1
     col1 
    ------
        0
    (1 row)
    
    m_db=# EXPLAIN VERBOSE SELECT * FROM (SELECT cast('abc' AS decimal) AS col1) t1 WHERE col1=0;
    WARNING:  Truncated incorrect DECIMAL value: 'abc'
    WARNING:  Truncated incorrect DECIMAL value: 'abc'
    CONTEXT:  referenced column: col1
                    QUERY PLAN                
    ------------------------------------------
     Result  (cost=0.00..0.01 rows=1 width=0)
       Output: 0::decimal
    (2 rows)
    
    -- MySQL行为:
    mysql>  SELECT * FROM (SELECT cast('abc' AS decimal) AS col1) t1 WHERE col1=0;
    +------+
    | col1 |
    +------+
    |    0 |
    +------+
    1 row in set, 1 warning (0.00 sec)
    
    mysql> SHOW warnings;
    +---------+------+------------------------------------------+
    | Level   | Code | Message                                  |
    +---------+------+------------------------------------------+
    | Warning | 1292 | Truncated incorrect DECIMAL value: 'abc' |
    +---------+------+------------------------------------------+
    1 row in set (0.00 sec)
    
    mysql> EXPLAIN SELECT * FROM (SELECT cast('abc' AS decimal) AS col1) t1 WHERE col1=0;
    +----+-------------+------------+------------+--------+---------------+------+---------+------+------+----------+----------------+
    | id | select_type | table      | partitions | type   | possible_keys | key  | key_len | ref  | rows | filtered | Extra          |
    +----+-------------+------------+------------+--------+---------------+------+---------+------+------+----------+----------------+
    |  1 | PRIMARY     | <derived2> | NULL       | system | NULL          | NULL | NULL    | NULL |    1 |   100.00 | NULL           |
    |  2 | DERIVED     | NULL       | NULL       | NULL   | NULL          | NULL | NULL    | NULL | NULL |     NULL | No tables used |
    +----+-------------+------------+------------+--------+---------------+------+---------+------+------+----------+----------------+
    2 rows in set, 2 warnings (0.01 sec)
    
    mysql> SHOW warnings;
    +---------+------+---------------------------------------------------------------+
    | Level   | Code | Message                                                       |
    +---------+------+---------------------------------------------------------------+
    | Warning | 1292 | Truncated incorrect DECIMAL value: 'abc'                      |
    | Note    | 1003 | /* select#1 */ select '0' AS `col1` from dual where ('0' = 0) |
    +---------+------+---------------------------------------------------------------+
    2 rows in set (0.00 sec)

相关文档