System Migration from MySQL to ORACLE Implementation

1. Database script migration

 

1. Data structure synchronization

Open Navicat Premium, click the menu [Tools] - [Data Transfer], and select the corresponding source library and target library in the pop-up dialog box.

For the convenience of use, in the [Advanced] tab of the TAB page, check the [Convert Object Name], and select [Capitalization]. After the setting is complete, click the [Start] button to synchronize the data script.

Note: During the data transmission process, there may be exceptions due to the irregular design of the MySQL database. Please correct the data structure corresponding to the source database in time. The maximum length of identifiers in Oracle is 30 characters, and identifiers include but are not limited to table names, field names, view names, sequence names, and primary keys.

 

2. Table structure adjustment

During Navicat Premium data synchronization, the column length is not set precisely (retains the default length). Therefore, it is necessary to export the Oracle database script and set the column length accurately after the data structure synchronization is completed.

Note: When there is data in the Oracle database table, the table structure cannot be directly modified, so it is recommended to export the database script and modify it in the script. In addition, the SQL exported by Navicat Premium contains prefixes and double quotes, please remove them after exporting.

 

3. Add sequence

The auto-increment of the primary key of the Oracle table is realized by the sequence, so it is necessary to add the sequence to the auto-increment primary key of the table. The sequence creation example is as follows:

CREATE SEQUENCE SEQ_ACCOUNT_INFO INCREMENT BY 1 START WITH 1 minvalue 1 NOMAXVALUE NOCACHE;

 

 

2. System configuration adjustment

1. Add the Oracle database driver to the pom file

<dependency>
				<groupId>com.oracle</groupId>
				<artifactId>ojdbc14</artifactId>
				<version>10.2.0.4.0</version>
				<scope>system</scope>
				<systemPath>${project_basedir}/lib/ojdbc14-10.2.0.4.0.jar</systemPath>
</dependency>

 

2. Adjust the data source

jdbc.properties configuration adjustment

db.driverClassName=oracle.jdbc.driver.OracleDriver
db.url=jdbc:oracle:thin:@10.10.1.107:1521:orcl
db.username=root
db.password=root
db.mapper=classpath:mybatis/oracle/*Mapper.xml
validationQuery=SELECT 'x' FROM DUAL

 

3. Java background adjustment

 

1. In the Mapper file, the auto-incrementing primary key is adjusted to Oracle's sequence method

    The original MySQL method is as follows:

<selectKey resultType="java.lang.Long" keyProperty="id" order="BEFORE" >
SELECT LAST_INSERT_ID()
</selectKey>

     Oracle's sequence method is as follows:

<selectKey resultType="java.lang.Long" keyProperty="id" order="BEFORE" >
SELECT SEQ_ACCOUNT_INFO.NEXTVAL FROM DUAL
</selectKey>

 

2. Date (time) type adjustment

 

    date formatting

The MySQL database formats the date into a string through the DATE_FORMAT(date,format) function, where the available identifiers for format are: %Y (year), %m (month), %H (hour), %i (minute), %S (seconds), etc. The commonly used formats are '%Y-%m-%d', '%H:%i:%S', '%Y%m', etc.

The Oracle database uses the TO_CHAR(date,format) function to format the date, such as formatting the current date: TO_CHAR(SYSDATE,'yyyy-mm-dd HH:mi:ss').

In addition, MySQL is a weakly typed data type. When converting a string to a date (as well as other types), type conversion is not required as long as it conforms to the date format, but Oracle must perform type conversion. Oracle provides the TO_DATE(date_str,'yyyy-mm-dd hh24:mi:ss') function to convert strings to date types.

 

    function to get current date

MySQL commonly used functions to get the current date (time) are: NOW(), SYSDATE(), CURDATE(), CURTIME(), etc. Oracle uses SYSDATE.

 

    Calculate time difference

The MySQL database uses the TIMESTAMPDIFF(interval, datetime_expr1, datetime_expr2) function to calculate the time difference, where the interval can be YEAR (year), QUARTER (quarter), MONTH (month), WEEK (week), DAY (day), HOUR (hour), MINUTE (minutes), SECOND (seconds), FRAC_SECOND (milliseconds), such as calculating the number of days from the May Day Labor Day in 2017:

SELECT TIMESTAMPDIFF(day,'2017-05-01',now());

 MySQL also has many functions for calculating time difference, such as DATEDIFF(date1, date2) and so on.

 The Oracle database directly calculates the time difference by subtracting two dates, such as calculating the number of days from the May Day Labor Day in 2017:

SELECT FLOOR(SYSDATE-TO_DATE('2017-5-1','yyyy-mm-dd')) FROM DUAL ;

 Calculate the hours since May 1, 2017:

SELECT FLOOR(TO_NUMBER(SYSDATE-TO_DATE('2017-5-1 00:00:00','yyyy-mm-dd hh24:mi:ss'))*24) FROM DUAL ;

 

3. Fuzzy query

    MySQL use

concat('%',#{keywords},'%')

     Used in Oracle

'%'||#{keywords}||'%'

 

4. Frequently Asked Questions

1. Remove duplicate columns to prevent an error in oracle paging query: ORA-00918: column not clearly defined.

 

2. ORA-00942: There is no error in the table or view, it may be the database table name is wrong, or there is no permission or the account does not exist, etc.

 

3. MySQL strings support single and double quotes, but Oracle only supports single quotes.

 

4. When performing grouping statistics in Oracle, all non-aggregated columns must be placed in the group by clause. If sorting is also involved, the sorted column must also be in the group by clause. Before MySQL 5.7, there was no mandatory constraint on group statistics, and after 5.7, it followed the same standard as Oracle.

 

5. You cannot use column aliases in Oracle's GROUP BY clause.

 

6. Aliases in Oracle cannot add single quotation marks.

 

7. The batch insert and update of MySQL need to be added to the batch insert and update of Oracle

BEGIN
statement
; END ;

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326270669&siteId=291194637