当前位置:网站首页>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
CONSTRAINTThe 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 Tablesentence , 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 TNameRecord , 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.inifile[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,
MINUSorEXCEPT, 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 ……️
边栏推荐
猜你喜欢

第2章 HFDS的Shell操作

业务系统兼容数据库Oracle/PostgreSQL(openGauss)/MySQL的琐事

Raspberry pie 4b64 bit system installation miniconda (it took a few days to finally solve it)

去掉input聚焦时的边框

MariaDB的安装与配置

QT实现窗口渐变消失QPropertyAnimation+进度条

QT implementation fillet window

Flag framework configures loguru logstore

Chapter 6 rebalance details

Kubernetes cluster deployment
随机推荐
Acwing: Game 58 of the week
Raspberry pie 4b64 bit system installation miniconda (it took a few days to finally solve it)
Educational Codeforces Round 130 (Rated for Div. 2)A~C
原生js实现全选和反选的功能 --冯浩的博客
Spark的RDD(弹性分布式数据集)返回大结果集
Kubernetes集群部署
FLV格式详解
顺丰科技智慧物流校园技术挑战赛(无t4)
第5章 NameNode和SecondaryNameNode
ffmpeg命令行使用
VMware Tools和open-vm-tools的安装与使用:解决虚拟机不全屏和无法传输文件的问题
两个礼拜速成软考中级软件设计师经验
Chapter 2 shell operation of hfds
Codeforces Round #800 (Div. 2)AC
Codeforces Round #798 (Div. 2)A~D
Market trend report, technological innovation and market forecast of China's double sided flexible printed circuit board (FPC)
Codeforces Round #801 (Div. 2)A~C
Codeforces Round #802(Div. 2)A~D
Oneforall installation and use
MariaDB的安装与配置