The DBLink feature of OceanBase Database enables cross-database access, allowing you to access remote databases from the local database. After a DBLink is created, you can use it to access remote database objects, including tables and views.
Limitations
Currently, the DBLink read feature in the MySQL mode of OceanBase Database only supports reading data between different MySQL tenants.
In MySQL mode, you can access data of the following types through DBLinks:
Numeric data types:
TINYINT,SMALLINT,MEDIUMINT,INT,BIGINT,FLOAT,DOUBLE, andDECIMALDatetime data types:
DATE,TIME,YEAR,DATETIME, andTIMESTAMPString data types:
CHAR,VARCHAR,TINYBLOB,TINYTEXT,BLOB,TEXT,MEDIUMBLOB,LONGBLOB, andLONGTEXTOther data types:
ENUMandSETNotice
Due to limitations of OBClient, when you use a DBLink to read table data,
ENUMandSETcolumns in the remote table are treated asVARCHARcolumns.
For more information about the data types, see Overview of data types.
Prerequisites
You have created a DBLink. For more information about how to create a DBLink, see Create a DBLink.
Query the data of a table in a remote database
The SQL syntax is as follows:
SELECT select_expr_list FROM table_name@dblink_name WHERE where_conditions;
where
select_expr_listspecifies the expressions or column names to be queried. Separate the column names with commas (,). An asterisk (*) indicates all columns.table_name@dblink_namespecifies the tables to be queried in the remote database. Separate the tables with commas (,). By default, the tables in the database specified when you created the DBLink are accessed. If you want to access a table in another database, you can specify the table in the SQL statement in the format ofdatabase_name.table_name@dblink_name.where_conditionsspecifies the filter condition, which is optional. Only the data that meets the condition is returned.
Examples
To use a DBLink for data query, perform the following steps:
Create a DBLink for connecting to a remote OceanBase database in MySQL mode. The DBLink name is
my_link, the remote tenant ismysql001, and the default database to be accessed istest.CREATE DATABASE LINK my_link CONNECT TO root@mysql001 DATABASE test IDENTIFIED BY '******' HOST '10.10.10.1:2881';Query data in the remote database through the DBLink created in Step 1.
Query the data of the
test_tbl1table in the remote databasetestthroughmy_link.SELECT * FROM test_tbl1@my_link WHERE id = 1;The result is as follows:
+------+------+ | id | name | +------+------+ | 1 | A1 | +------+------+ 1 row in setRead the
test_tbl1table in the remote databasetestthroughmy_linkand join the table with thetbl1table in the local database.SELECT a.id, b.name FROM tbl1 a, test_tbl1@my_link b WHERE a.id = b.id;The result is as follows:
+------+------+ | id | name | +------+------+ | 1 | A1 | | 2 | A2 | +------+------+ 2 rows in setRead the data of the
studenttable in the remote databasestudent_dbthroughmy_link. The database specified when you created the DBLink istest.SELECT * FROM student_db.student@my_link LIMIT 3;The result is as follows:
+------+---------+--------+------+-------+-----------------+ | id | name | gender | age | score | enrollment_date | +------+---------+--------+------+-------+-----------------+ | 1 | Emma | 0 | 20 | 85 | 2021-09-01 | | 2 | William | 1 | 21 | 90.5 | 2021-09-02 | | 3 | Olivia | 0 | 19 | 95.5 | 2021-09-03 | +------+---------+--------+------+-------+-----------------+ 3 rows in set
References
For more information about data types, see Overview of data types.
For more information about how to create a DBLink, see Create a DBLink.
For more information about how to use a DBLink, see Use a DBLink to access data in a remote database.
For more information about how to use the
SELECTstatement, see SELECT statement.