当前位置:网站首页>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

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

 Insert picture description here

Official website

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 .

  1. 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 .
  2. Force case changing fields Oracle stay SQL The statement must also be quoted to indicate ,MySQL It seems that you can ignore case .
  3. 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 .
  4. 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 .
  5. 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,...), Again COMMENT ON Table TName IS ' notes '
  • PostgreSQL : First Create Table TName (Field1,Field2,...), Again COMMENT ON Table TName IS ' notes ', ditto
  • MySQL : First Create Table TName (Field1,Field2,...), Again ALTER 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 with Alter 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, Again select 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 , occasionally select 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 join local-infile=1
// Client side Settings :
Property.setProperty("allowLoadLocalInfile","true");
Connection conn = DriverManager.getConnection(jdbcUrl, Property);
......

Compared with :
 Data import speed comparison

(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 or EXCEPT, 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(+) Equivalent select 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 ……️

原网站

版权声明
本文为[Ruo Miaoshen]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/187/202207060920282123.html