This article provides a quick guide for migrating application systems based on MySQL databases to GBase databases (GBase 8c). For detailed information about specific aspects of both databases, readers can refer to the MySQL official documentation (https://dev.mysql.com/doc/) and the GBase 8c user manual. Due to the extensive content involved in basic mapping of MySQL data types and other aspects of the migration process, this will not be covered in detail in this article. If interested, please leave a comment, and we can discuss it next time.
1. Creating a Database
In both MySQL and GBase 8c, the CREATE DATABASE
statement is used to create a database. The specific syntax differences are as follows:
Operation | MySQL SQL Statement | GBase 8c SQL Statement |
---|---|---|
CREATE DATABASE | CREATE DATABASE example CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; |
CREATE DATABASE example OWNER gbase ENCODING ‘UTF8’ LC_COLLATE 'en_US.UTF-8' LC_CTYPE 'en_US.UTF-8'; |
Considerations for Migrating SQL Statements for Creating Databases:
(1) In both MySQL and GBase 8c, you can specify the character set and collation rules when creating a database.
Unlike MySQL, in GBase 8c, the ENCODING
keyword is used to specify the character set, and the LC_COLLATE
and LC_CTYPE
keywords are used to specify collation rules:
LC_COLLATE
: This parameter affects the sorting order of strings (e.g., when using ORDER BY, as well as the order of indexes on text columns).LC_CTYPE
: This parameter affects character classification, such as uppercase, lowercase, and digits.
(2) When creating a database in GBase 8c, you can also specify unique additional attributes. Common attributes include:
OWNER
: This parameter specifies the owner of the database. If not specified, the owner defaults to the current user.CONNECTION LIMIT
: This parameter specifies the number of concurrent connections the database can accept. System administrators are not subject to this limit.
(3) Database Structure
In MySQL, database and schema are synonymous, and databases can reference each other. In GBase 8c, database and schema are distinct objects. A single database can contain multiple schemas, and databases cannot reference each other, but schemas within the same database can.
2. Using the Database
Comparison of various SQL statements for operating the database:
Operation | MySQL SQL Statement | GBase 8c SQL Statement | GBase 8c gsql Tool |
---|---|---|---|
View Databases |
SHOW DATABASES; or SHOW DATABASE example;
|
SELECT * FROM pg_database; |
\l or \l+
|
Switch Database | USE example; |
Reconnect to switch, this function does not use SQL to switch | \c example |
Delete Database | DROP DATABASE example; |
DROP DATABASE example; |
None |
3. Creating Tables
Both MySQL and GBase 8c support creating tables using the CREATE TABLE statement. The specific syntax differences are as follows:
Operation | MySQL SQL Statement | GBase 8c SQL Statement |
---|---|---|
Creating Tables using CREATE TABLE |
CREATE TABLE `my_table` (`id` int NOT NULL AUTO_INCREMENT COMMENT 'id',`user_id` int NOT NULL COMMENT 'User id',`name` varchar(50) DEFAULT NULL COMMENT 'Name',`address` varchar(50) DEFAULT NULL COMMENT 'Address',`password` varchar(20) DEFAULT 'passwd' COMMENT 'Password',PRIMARY KEY ( `id` )) ENGINE=InnoDB DEFAULT CHARSET=utf8; |
CREATE TABLE "my_table" ( "id" SERIAL NOT NULL, "user_id" int NOT NULL, "name" varchar(50), "address" varchar(50), "passwd" varchar(20) DEFAULT 'password', CONSTRAINT "my_table_pkey" PRIMARY KEY ("id") ); COMMENT ON COLUMN "my_table"."id" IS 'id'; COMMENT ON COLUMN "my_table"."user_id" IS 'User id'; COMMENT ON COLUMN "my_table"."name" IS 'Name'; COMMENT ON COLUMN "my_table"."address" IS 'Address'; COMMENT ON COLUMN "my_table"."passwd" IS 'Password'; |
Creating Tables using CREATE TABLE ... LIKE |
create table `my_table_like` like `my_table` ; |
create table my_table_like (like my_table); |
Creating Tables using CREATE TABLE ... AS |
create table `my_table_as` as select * from `my_table` ; |
create table my_table_as as select * from my_table ; |
When migrating SQL statements for creating tables, the following syntax changes are required:
(1) Naming Rules and Case Sensitivity
In MySQL, database, table, and field names are enclosed in backticks (``) for marking. This is not allowed in GBase 8c; instead, GBase 8c uses either double quotes or no marks at all.
In GBase 8c, if table and field names are not enclosed in double quotes, they are automatically converted to lowercase when the table is created. If you need to specify uppercase names, you must enclose the names in double quotes.
(2) Storage Engine Related Changes
When migrating to GBase 8c, you need to remove storage engine-related clauses such as ENGINE and TYPE from MySQL statements.
GBase 8c does not support setting character sets at the table level, so CHARSET clauses in MySQL statements should be removed when migrating to GBase 8c.
(3) CREATE TABLE LIKE\AS
GBase 8c also supports the CREATE TABLE LIKE\AS syntax, but the usage of the LIKE clause differs from MySQL. In GBase 8c, the LIKE clause must be enclosed in "()", and it does not automatically copy the COMMENT annotations from the original table columns.
4. View-Related Statements
Both MySQL and GBase 8c support views, and the basic creation method is similar. However, it is important to note that in GBase 8c, under the default rule, directly modifying data in a view is not supported.
Operation | MySQL SQL Statement | GBase 8c SQL Statement |
---|---|---|
Creating a View | CREATE VIEW v_my_table AS SELECT * FROM my_table; |
CREATE VIEW v_my_table AS SELECT * FROM my_table; |
Modifying Data Through a View | INSERT INTO v_my_table(user_id, name, address) VALUES(2222, 'bbb', 'xxxx'); |
Supported, but requires adjusting the default RULE |
Dropping a View | DROP VIEW v_my_table; |
DROP VIEW v_my_table; |
5. Index-Related Statements
Both MySQL and GBase 8c support indexing functionality, but there are slight differences in the creation and deletion operations. The basic syntax differences are as follows:
Operation | MySQL SQL Statement | GBase 8c SQL Statement |
---|---|---|
Creating Index |
CREATE INDEX i_user_id USING BTREE ON my_table (user_id); or CREATE INDEX i_user_id ON my_table (user_id) USING BTREE;
|
CREATE INDEX i_user_id ON my_table USING BTREE (user_id); |
Dropping Index | DROP INDEX i_user_id ON my_table; |
DROP INDEX i_user_id; |
Attention Points for Migrating Index Creation and Deletion Statements:
(1) Position of USING index_type
In MySQL, the USING index_type clause can appear either before or after the table_name(col_name) clause, as shown:
... USING index_type table_name(col_name) ...
OR
... table_name(col_name) USING index_type ...
However, in GBase 8c, the USING index_type clause must be placed in the middle of the table_name(col_name) clause:
... table_name USING index_type (col_name) ...
(2) DROP INDEX ON table
In GBase 8c, when deleting an index object, you do not need to specify the ON table clause. This clause should be removed during migration.
(3) Other Properties
GBase 8c does not support FULLTEXT and SPATIAL properties when creating index objects. These properties need to be removed during migration.