语法兼容性介绍
LightDB支持部分oracle特性和mysql特性扩展,这些特性和LightDB特性是冲突的。因此提供了语法兼容性开关。22.3版本不允许不同数据库设置不同的语法兼容类型,22.4版本放开了限制。下面以22.4为例介绍语法兼容类型。
创建数据库语法
语法结构如下:
create database dbname [with lightdb_syntax_compatible_type [oracle|mysql|off]]
22.4版 create database语句增加了选项lightdb_syntax_compatible_type用于指明创建的数据库的语法兼容性的。以下举例说明
创建oracle语法兼容的数据库:
create database test_oracle with lightdb_syntax_compatible_type oracle;
创建mysql语法兼容的数据库:
create database test_mysql with lightdb_syntax_compatible_type mysql;
创建兼容模式关闭的数据库,此时不包含oracle,mysql不兼容特性。
create database test_off with lightdb_syntax_compatible_type off;
创建默认兼容模式数据库,默认数据库模式有guc参数lightdb_syntax_compatible决定:
test@postgres=> show lightdb_syntax_compatible_type;
lightdb_syntax_compatible_type
--------------------------------
Oracle
(1 row)
-- 此时语法兼容类型为oracle
lightdb@postgres=# create database test_none;
CREATE DATABASE
如何查看当前数据库的语法兼容类型
为方便查看语法兼容类型,LightDB提供了guc参数方便查看当前数据库语法兼容类型。可以通过以下sql查询语法兼容类型:
show lightdb_dblevel_syntax_compatible_type;
连续上面的例子,查看不同数据库的兼容类型的示例如下:(例如在ltsql中执行):
lightdb@test_oracle=# \c test_oracle
You are now connected to database "test_oracle" as user "lightdb".
lightdb@test_oracle=# show lightdb_dblevel_syntax_compatible_type;
lightdb_dblevel_syntax_compatible_type
----------------------------------------
Oracle
(1 row)
lightdb@test_oracle=# \c test_mysql;
You are now connected to database "test_mysql" as user "lightdb".
lightdb@test_mysql=# show lightdb_dblevel_syntax_compatible_type;
lightdb_dblevel_syntax_compatible_type
----------------------------------------
MySql
(1 row)
lightdb@test_mysql=# \c test_off;
You are now connected to database "test_off" as user "lightdb".
lightdb@test_off=# show lightdb_dblevel_syntax_compatible_type;
lightdb_dblevel_syntax_compatible_type
----------------------------------------
off
(1 row)
lightdb@postgres=# \c test_none;
You are now connected to database "test_none" as user "lightdb".
lightdb@test_none=# show lightdb_dblevel_syntax_compatible_type;
lightdb_syntax_compatible_type
--------------------------------
Oracle
(1 row)
search_path的变化
22.4之前的版本需要根据数据库兼容类型设置search_path,22.4提供search_path支持变量lt_syntax_compatible_type。
- 当语法兼容类型为oracle时(即: oracle模式),lt_syntax_compatible_type的值是oracle;
- 当语法兼容类型的值是mysql时(即:mysql模式),值是mysql;
- 当语法兼容类型的值是off时,lt_syntax_compatible_type失效。
实际生效的search_path可以通过以下sql查询:
select current_schemas(true);
示例演示如下:
lightdb@test_oracle=# show search_path;
search_path
-----------------------------------------------------------
"$user", public, "$lt_syntax_compatible_type", lt_catalog
(1 row)
lightdb@test_oracle=# select current_schemas(false);
current_schemas
----------------------------
{public,oracle,lt_catalog}
(1 row)