All Products
Search
Document Center

Automatically create tables based on RDS tables

Last Updated: Jul 25, 2019

Prerequisites

To facilitate the demonstration, complete the following steps to prepare the test data in ApsaraDB for RDS (RDS).

  1. Create an ApsaraDB RDS for MySQL (MySQL) instance.

  2. Set a whitelist.

  3. Create accounts and databases.

  4. Connect to the instance.

  5. Create a table and insert the test data to the table.

    Create a table named person.

    1. create table person (
    2. id int,
    3. name varchar(1023),
    4. age int
    5. );

    Insert the test data to the person table.

    1. insert into person
    2. values
    3. (1, 'james', 10),
    4. (2, 'bond', 20),
    5. (3, 'jack', 30),
    6. (4, 'lucy', 40);
  6. Create a MySQL schema.

    All tables in the example use the following schema:

    1. CREATE SCHEMA hello_mysql_vpc_rds WITH DBPROPERTIES (
    2. CATALOG = 'mysql',
    3. LOCATION = 'jdbc:mysql://rm-2zer0vg58mfofake.mysql.rds.aliyuncs.com:3306/rds_mysql_dbname',
    4. USER = 'rds_mysqldb_username',
    5. PASSWORD = 'rds_mysqldb_password',
    6. INSTANCE_ID = 'rds_mysql_instance_id',
    7. VPC_ID = 'rds_mysqldb_vpcid'
    8. );

Considerations

Before you create a MySQL, SQL Server, or PostgreSQL schema in DLA, you must add the CIDR block 100.104.0.0/16 to the whitelist of your RDS instance.

The RDS instance is in a VPC, and by default DLA cannot access resources in the VPC. To enable DLA to access the RDS instance, you need to use the reverse VPC technology, that is, add the CIDR block 100.104.0.0/16 to the whitelist of your RDS instance.

Permission statement: When you use the method described in this topic to create a MySQL schema, you agree that we can use the reverse VPC technology to read and write data from and to RDS.

Example 1: Create a table by using CREATE TABLE LIKE MAPPING

Create a table:

  1. create external table person like mapping('person');
  2. +-------------+-----------+-----------------+
  3. desc person;
  4. +-------------+-----------+-----------------+
  5. | Field | Type | Collation |
  6. +-------------+-----------+-----------------+
  7. | id | int | utf8_general_ci |
  8. | age | int | utf8_general_ci |
  9. | name | varchar | utf8_general_ci |

Query table data:

  1. SELECT * FROM hello_mysql_vpc_rds.person
  2. +------+------+----+
  3. | id | name | age |
  4. +-----+------+-----+
  5. | 1| james| 10|
  6. | 2| bond | 20|
  7. | 3| jack | 30|
  8. | 4| lucy | 40|

Example 2: Copy a table by using CREATE TABLE LIKE

Traditional databases allow you to create new tables or collections by copying existing ones. DLA provides CREATE TABLE LIKE, which can achieve the same purpose.

Syntax:

  1. create external table_name2 like table_name_1;

Examples:

In the preceding example, you can run CREATE TABLE LIKE to copy the person table as a new table named person_2, which has the same structure and data as the person table.

  1. create external table person_2 like person;
  2. +------+-------------------+---+------+
  3. SELECT * FROM hello_mysql_vpc_rds.person_2
  4. +------+------+----+
  5. | id | name | age |
  6. +-----+------+-----+
  7. | 1| james| 10|
  8. | 2| bond | 20|
  9. | 3| jack | 30|
  10. | 4| lucy | 40|