当前位置:网站首页>Business system compatible database oracle/postgresql (opengauss) /mysql Trivia
Business system compatible database oracle/postgresql (opengauss) /mysql Trivia
2022-07-06 16:38:00 【Ruo Miaoshen】
List of articles
- ( One ) Continue to move the database
- ( Two ) Database differences
- (2.1)Oracle/PostgreSQL/MySQL Basic differences
- (2.2) Create table statement, etc (DDL)
- (2.3) Delete table statement
- (2.4) Sequence
- (2.5) Check the user object
- (2.6) Table data import
- (2.7) Common functions
- (2.8) Dataset operations (DML)
- (2.9) Join syntax of table ( Left connection )
- (2.10) UPDATE statement ( special )
- (2.11) Get database information
I remember before 《 The business system starts from Oracle Migrate to openGauss Simple records of the database 》:)
But it's not over yet ……
( One ) Continue to move the database
It's endless ……
PostgreSQL -> Huawei openGauss
MySQL -> ZTE GoldenDB
……
(1.1) About GoldenDB
Mature and stable , Commercial leading financial level distributed database .
The product is as solid as a rock , Leading commercial market , Lead the formulation of industry standards , Build a new industrial ecology
According to the information given by ZTE , It is completely compatible in development MySQL Of , even JDBC Packages and drivers are MySQL.
And it seems not open source , Nor is it free software , So it can only be regarded as MySQL got .
(1.2) About openGauss Compatibility
Anyone who PostgreSQL Things that are openGauss All compatible .
and openGauss Extended some syntax similar Oracle Of , In turn, PostgreSQL No use .
So in order to unify and simplify , All use PostgreSQL Standards for .
( Two ) Database differences
(2.1)Oracle/PostgreSQL/MySQL Basic differences
In order to unify the code, whoever is rigorous will follow whoever .
There are many differences , I don't know how to list them more appropriately .
- Default object name Oracle Capitalize and PostgreSQL/MySQL A lowercase letter . You can use quotation marks ( Even the quotation marks are different ) Forced small / Capitalization . Under normal circumstances, do not add quotation marks .
- Force case changing fields Oracle stay SQL The statement must also be quoted to indicate ,MySQL It seems that you can ignore case .
- For fields with different types Oracle You can directly compare
CharField1 = NumField2
( Seems to be openGauss It's also ), and PostgreSQL/MySQL The data type should be consistent . - The binding value in the program is also the same as SQL In the same ,Oracle Can mix String and Int use , and PostgreSQL/MySQL The data type should be consistent .
- Many sentences are slightly different , Don't list them one by one , such as Oracle Of
CONSTRAINT
The word , There are other databases everywhere ……
(2.2) Create table statement, etc (DDL)
The main difference is the data type , such as varchar Is there any 2,number,numberic,decimal……
This part is relatively simple , Just modify the sentence , Or use Navicat
direct Data Transfer
.
PS: Find out Navicat
It's convenient to synchronize table structure and data across databases ( But I don't want to change the original article ), You can first use the mutual import table structure , Then export from the corresponding library DDL sentence , Avoid changing yourself one by one ,yeah!
(2.2.1) Common data types
- Oracle :
VARCHAR2
,NUMBER
,DATE
,…… - PostgreSQL :
varchar
,numeric
,timestamp
,…… - MySQL :
varchar
,numeric
,datetime
,……
(2.2.2) Notes to table
The writing is different , But the difference is small :
- Oracle : First
Create Table TName (Field1,Field2,...)
, AgainCOMMENT ON Table TName IS ' notes '
- PostgreSQL : First
Create Table TName (Field1,Field2,...)
, AgainCOMMENT ON Table TName IS ' notes '
, ditto - MySQL : First
Create Table TName (Field1,Field2,...)
, AgainALTER Table TName COMMENT = ' notes '
(2.2.3) Field comments
Field comments are different , It involves some code logic :
- Oracle : First
Create Table TName (Field1,Field2,...)
Recycle comments for each field :COMMENT ON COLUMN TName.Field1 IS ' notes '
- PostgreSQL : First
Create Table TName (Field1,Field2,...)
Recycle comments for each field :COMMENT ON COLUMN TName.Field1 IS ' notes '
, ditto - MySQL : direct
Create Table TName (Field1 COMMENT ' notes 1‘,Field2 COMMENT ' notes 2‘,...)
In fact, you can also create tables first and then annotate fields , But withAlter Table
sentence , And a little complicated ( Write everything down ), Therefore, it is relatively simple to annotate the code at one time .
(2.3) Delete table statement
Under normal circumstances , need CASCADE Delete other objects associated with the table ( Indexes , constraint ).
about Oracle Our data sheet is too big for us to flash back , So add PURGE.
and MySQL Of drop … if exists Interesting as it is , But try to unify or not .
- Oracle :
DROP TABLE TName CASCADE CONSTRAINTS PURGE'
- PostgreSQL :
DROP TABLE TName CASCADE
- MySQL :
DROP TABLE TName CASCADE
(2.4) Sequence
It is not recommended to make MySQL Realize similar sequence functions , Because the target database may have permission problems .
Because the sequence takes out the value first and then uses , The autoincrement field is Insert Then get , So the logic of some codes involved is different .
(2.4.1) establish
- Oracle : Use sequence objects
CREATE SEQUENCE SEQXXX START WITH 1000 MAXVALUE 99999999 MINVALUE 1000
- PostgreSQL : Use sequence objects
CREATE SEQUENCE SEQXXX START WITH 1000 MAXVALUE 99999999 MINVALUE 1000
, Basically the same as above - MySQL : Set table field self increment
Create Table TName (Field1 int NOT NULL AUTO_INCREMENT ,Field2,...)
,
(2.4.2) Use
- Oracle :
Select SEQXXX.nextval from dual
- PostgreSQL :
select nextval('SEQXXX')
- MySQL : First
Insert into TName
Record , Auto increment field value assignment 0 or Null, Againselect last_insert_id()
Select the value after self increment .
We need to pay attention to : Insert statement and select from value-added statement need to be carried out in the same connection .
Even in the same connection , Due to the development language / The drive is different , occasionallyselect last_insert_id()
Will also choose 0 It's worth it ( Well, I met ).
(2.5) Check the user object
(2.5.1) surface
Due to database differences , The statement here is not very accurate , Change it according to the actual situation .
Is it a choice (User/Owner) User's table , still (Schema) Table of schemes ?
- Oracle :
select TABLE_NAME from user_tables where TABLE_NAME like ?
—— Users are solutions - PostgreSQL :
select tablename from pg_tables where schemaname = ? and upper(tablename) like ?
—— User owned solutions , Can also be combined with MySQL equally , as follows : - MySQL :
select TABLE_NAME from information_schema.tables where TABLE_SCHEMA=? and upper(TABLE_NAME) like ?
—— Users and Libraries ( programme )
(2.5.2) Field
- Oracle :
select * from USER_TAB_COLUMNS where TABLE_NAME = ? and COLUMN_NAME = ?
- PostgreSQL :
select * from information_schema.columns where TABLE_SCHEMA= ? and upper(TABLE_NAME) = ? and upper(COLUMN_NAME) = ?
, Uh, no pg_columns. - MySQL :
select * from information_schema.columns where TABLE_SCHEMA= ? and upper(TABLE_NAME) = ? and upper(COLUMN_NAME) = ?
, ditto .
(2.6) Table data import
except insert outside , A large number of data import databases have their own methods .
This place here openGauss Acting strange , It's too slow .
- Oracle : Use SQL Loader, That is to say
sqlldr...
command , This command file and related information files are required under the client , Skip here . - PostgreSQL : Java Use org.postgresql.copy.CopyManager,
COPY ... FROM STDIN...
, There is no time to study other languages . - MySQL : perform SQL sentence :
LOAD DATA LOCAL INFILE ... INTO TABLE ...
, Both client and server are required to allow this Load How to file ( Understand specific safety risks by yourself ).
Otherwise, the report will be wrong :Loading local data is disabled; this must be enabled on both the client and server sides
Need server settings (Windows Next is ini):my.ini
file[mysqld]
Lower joinlocal-infile=1
// Client side Settings :
Property.setProperty("allowLoadLocalInfile","true");
Connection conn = DriverManager.getConnection(jdbcUrl, Property);
......
Compared with :
(2.7) Common functions
(2.7.1) Date to string
- Oracle :
To_Char(DATE,'YYYY-MM-DD')
- PostgreSQL :
To_Char(DATE,'YYYY-MM-DD')
, ditto - MySQL :
date_format(DATE,'%Y-%m-%d')
It is not recommended to make MySQL The implementation is similar to To_Char function , Because the target database may have permission problems .
attach : Not recommended MySQL Custom function :
CREATE FUNCTION to_char(d datetime, format varchar(40)) RETURNS varchar(40)
DETERMINISTIC
begin
declare str varchar(40) DEFAULT '';
set str = replace(format, 'YYYY', '%Y');
set str = replace(str, 'yyyy', '%Y');
set str = replace(str, 'MM', '%m');
set str = replace(str, 'mm', '%m');
set str = replace(str, 'DD', '%d');
set str = replace(str, 'dd', '%d');
set str = replace(str, 'HH24', '%H');
set str = replace(str, 'hh24', '%H');
set str = replace(str, 'HH', '%h');
set str = replace(str, 'hh', '%h');
set str = replace(str, 'MI', '%i');
set str = replace(str, 'mi', '%i');
set str = replace(str, 'SS', '%s');
set str = replace(str, 'ss', '%s');
return date_format(d, str);
end
(2.7.2) Date calculation
Add or subtract months as an example :
- Oracle :
select add_months(sysdate,xxx) from dual
- PostgreSQL :
select now()::timestamp + 'yyy month'
- MySQL :
select date_add(now(),interval zzz month)
(2.7.3) String to date
Be careful date and timestamp stay Oracle The difference between .
- Oracle :
select to_date('2022-06-01 23:45:59','yyyy-mm-dd hh24:mi:ss') from dual
- PostgreSQL :
select to_timestamp('2022-06-01 23:45:59','yyyy-mm-dd hh24:mi:ss')
- MySQL :
select str_to_date('2022-06-01 23:45:59','%Y-%m-%d %T')
(2.7.4) String splicing
- Oracle :
Field1 || '_' || Field2 || '_' ||...
, It can also be used.CONCAT()
- PostgreSQL :
Field1 || '_' || Field2 || '_' ||...
, It can also be used.CONCAT()
, ditto - MySQL :
CONCAT(Field1,'_',Field2,'_',...)
(2.7.5) Substring function
️️️ ️ hole ️️️
Everyone supports substr, The problem is substr It's from 1 Start counting Index, but Oracle from 0 from 1 Are all the same .
So if it was written like substr(field1,0,x) This kind of sentence ……
that Oracle It's okay , Other databases cannot get the corresponding results ( The mistakes are different ).
- Oracle :
substr(name,0,2)
=substr(name,1,2)
: obtain 2 Characters . - PostgreSQL :
substr(name,0,2)
=substr(name,1,1)
: obtain 1 Characters . - MySQL :
substr(name,0,2)
: obtain 0 Characters ( Can't get ).
(2.7.6) Return non null function
It is also not recommended to implement custom functions nvl name ( Prefer in the program SQL Add variables ).
- Oracle :
nvl(field,-1)
- PostgreSQL :
coalesce(field,-1)
- MySQL :
ifnull(field,-1)
(2.7.7) length (Blob type )
- Oracle :
Length(Blobfield)
- PostgreSQL :
pg_column_size(Blobfield)
- MySQL :
Length(Blobfield)
(2.8) Dataset operations (DML)
Fortunately 【 Additions and deletions 】 It's the same , After all, are SQL, Set operation :
First union( Filter repeat ), union all( Do not filter duplicates ) It also means the same in different databases , however ……
- Oracle :
select * from T1 MINUS select * from T2
- PostgreSQL :
select * from T1 EXCEPT select * from T2
- MySQL : No,
MINUS
orEXCEPT
, Use other SQL Instead of ( What is other ? Uh :join, not in).
If you use not in Be careful about the efficiency problems caused by the amount of data ( Probably originally 1 Seconds of operation becomes dozens of minutes ).
(2.9) Join syntax of table ( Left connection )
It was used Oracle Of (+) All methods need to be changed into left join, Ah ……
If you don't use it at the beginning (+) Of Oracle Grammar is easy .
as for inner join and where The difference between , Find out for yourself . The result is the same , The size of the intermediate temporary set is different .
- Oracle :
select x from T1,T2 where T1.a=T2.a(+)
Equivalentselect X from T1 left join T2 on T1.a=T2.a
- PostgreSQL :
select X from T1 left join T2 on T1.a=T2.a
- MySQL :
select X from T1 left join T2 on T1.a=T2.a
(2.10) UPDATE statement ( special )
Something like this SQL( Simplified , So it seems completely meaningless ):
update tablename
set name = 'XXXXXX'
where id=0
and id IN
(
select id from tablename
);
- Oracle : Successful implementation ️
- PostgreSQL : Successful implementation ️
- MySQL : Execution failure ️
You can't specify target table 'XXXXX' for update in FROM clause
Need to nest another layer , You can perform :
update tablename
set name = 'XXXXXX'
where id=0
and id IN
(
select id from
(select id from tablename) tn
);
(2.11) Get database information
(2.11.1) essential information ( edition )
- Oracle :
select * from v$version
- PostgreSQL :
select version()
- MySQL :
select version()
+show variables like '%storage_engine%'
(2.11.1) Database character set ( code )
- Oracle :
select value from nls_database_parameters where Parameter='NLS_CHARACTERSET'
- PostgreSQL :
show server_encoding
- MySQL :
show variables like '%character_set%'
…… At the end of the temporary article ……️
边栏推荐
- < li> dot style list style type
- Codeforces Round #800 (Div. 2)AC
- Li Kou: the 81st biweekly match
- Market trend report, technological innovation and market forecast of China's double sided flexible printed circuit board (FPC)
- 使用jq实现全选 反选 和全不选-冯浩的博客
- Spark独立集群Worker和Executor的概念
- 第6章 DataNode
- Chapter 6 datanode
- Kubernetes cluster deployment
- Installation and configuration of MariaDB
猜你喜欢
Hbuilder X格式化快捷键设置
FLV格式详解
拉取分支失败,fatal: ‘origin/xxx‘ is not a commit and a branch ‘xxx‘ cannot be created from it
MariaDB的安装与配置
300th weekly match - leetcode
(lightoj - 1369) answering queries (thinking)
Solve the problem that intel12 generation core CPU single thread only runs on small cores
第6章 DataNode
图像处理一百题(11-20)
QT有关QCobobox控件的样式设置(圆角、下拉框,向上展开、可编辑、内部布局等)
随机推荐
Specify the format time, and fill in zero before the month and days
Raspberry pie 4b64 bit system installation miniconda (it took a few days to finally solve it)
日期加1天
Is the sanic asynchronous framework really so strong? Find truth in practice
解决Intel12代酷睿CPU【小核载满,大核围观】的问题(WIN11)
解决Intel12代酷睿CPU单线程调度问题(二)
QT实现窗口置顶、置顶状态切换、多窗口置顶优先关系
第2章 HFDS的Shell操作
Market trend report, technical innovation and market forecast of double-sided foam tape in China
使用jq实现全选 反选 和全不选-冯浩的博客
Codeforces Global Round 19
Spark的RDD(弹性分布式数据集)返回大结果集
Codeforces Round #803 (Div. 2)A~C
Chapter 6 rebalance details
QT有关QCobobox控件的样式设置(圆角、下拉框,向上展开、可编辑、内部布局等)
提交Spark应用的若干问题记录(sparklauncher with cluster deploy mode)
Solve the problem of intel12 generation core CPU [small core full, large core onlookers] (win11)
Kubernetes cluster deployment
第五章 Yarn资源调度器
Codeforces Round #799 (Div. 4)A~H