|
| 1 | +--- |
| 2 | +title: Linking Tables with ATTACH TABLE |
| 3 | +--- |
| 4 | + |
| 5 | +In this tutorial, we'll walk you through how to link a table in Databend Cloud with an existing Databend table stored in an S3 bucket using the ATTACH TABLE command. |
| 6 | + |
| 7 | +## Before You Start |
| 8 | + |
| 9 | +Before you start, ensure you have the following prerequisites in place: |
| 10 | + |
| 11 | +- [Docker](https://www.docker.com/) is installed on your local machine, as it will be used to launch a self-hosted Databend. |
| 12 | +- An AWS S3 bucket used as storage for your self-hosted Databend. [Learn how to create an S3 bucket](https://docs.aws.amazon.com/AmazonS3/latest/userguide/create-bucket-overview.html). |
| 13 | +- AWS Access Key ID and Secret Access Key with sufficient permissions for accessing your S3 bucket. [Manage your AWS credentials](https://docs.aws.amazon.com/general/latest/gr/aws-sec-cred-types.html#access-keys-and-secret-access-keys). |
| 14 | +- BendSQL is installed on your local machine. See [Installing BendSQL](/guides/sql-clients/bendsql/#installing-bendsql) for instructions on how to install BendSQL using various package managers. |
| 15 | + |
| 16 | +## Step 1: Launch Databend in Docker |
| 17 | + |
| 18 | +1. Start a Databend container on your local machine. The command below launches a Databend container with S3 as the storage backend, using the `databend-doc` bucket, along with the specified S3 endpoint and authentication credentials. |
| 19 | + |
| 20 | +```bash |
| 21 | +docker run \ |
| 22 | + -p 8000:8000 \ |
| 23 | + -e QUERY_STORAGE_TYPE=s3 \ |
| 24 | + -e AWS_S3_ENDPOINT="https://s3.us-east-2.amazonaws.com" \ |
| 25 | + -e AWS_S3_BUCKET=databend-doc\ |
| 26 | + -e AWS_ACCESS_KEY_ID=<your-aws-access-key-id> \ # Replace with your AWS access key ID |
| 27 | + -e AWS_SECRET_ACCESS_KEY=<your-aws-secrect-access-key> \ # Replace with your AWS secret access key |
| 28 | + datafuselabs/databend:v1.2.699-nightly |
| 29 | +``` |
| 30 | + |
| 31 | +2. Create a table named `population` to store city, province, and population data, and insert sample records as follows: |
| 32 | + |
| 33 | +```sql |
| 34 | +CREATE TABLE population ( |
| 35 | + city VARCHAR(50), |
| 36 | + province VARCHAR(50), |
| 37 | + population INT |
| 38 | +); |
| 39 | + |
| 40 | +INSERT INTO population (city, province, population) VALUES |
| 41 | + ('Toronto', 'Ontario', 2731571), |
| 42 | + ('Montreal', 'Quebec', 1704694), |
| 43 | + ('Vancouver', 'British Columbia', 631486); |
| 44 | +``` |
| 45 | + |
| 46 | +3. Run the following statement to retrieve the table's location in S3. As indicated in the result below, the S3 URI for the table is `s3://databend-doc/1/16/` for this tutorial. |
| 47 | + |
| 48 | +```sql |
| 49 | +SELECT snapshot_location FROM FUSE_SNAPSHOT('default', 'population'); |
| 50 | + |
| 51 | +┌──────────────────────────────────────────────────┐ |
| 52 | +│ snapshot_location │ |
| 53 | +├──────────────────────────────────────────────────┤ |
| 54 | +│ 1/16/_ss/513c5100aa0243fe863b4cc2df0e3046_v4.mpk │ |
| 55 | +└──────────────────────────────────────────────────┘ |
| 56 | +``` |
| 57 | + |
| 58 | +## Step 2: Set Up Attached Tables in Databend Cloud |
| 59 | + |
| 60 | +1. Connect to Databend Cloud using BendSQL. If you're unfamiliar with BendSQL, refer to this tutorial: [Connecting to Databend Cloud using BendSQL](../connect/connect-to-databendcloud-bendsql.md). |
| 61 | + |
| 62 | +2. Execute the following statements to create two attached tables: |
| 63 | + - The first table, `population_all_columns`, includes all columns from the source data. |
| 64 | + - The second table, `population_withoutprovince`, includes only the selected columns (city & population). |
| 65 | + |
| 66 | +```sql |
| 67 | +-- Create an attached table with all columns from the source |
| 68 | +ATTACH TABLE population_all_columns 's3://databend-doc/1/16/' CONNECTION = ( |
| 69 | + REGION='us-east-2', |
| 70 | + AWS_KEY_ID = '<your_aws_key_id>', |
| 71 | + AWS_SECRET_KEY = '<your_aws_secret_key>' |
| 72 | +); |
| 73 | + |
| 74 | +-- Create an attached table with selected columns (city & population) from the source |
| 75 | +ATTACH TABLE population_withoutprovince (city, population) 's3://databend-doc/1/16/' CONNECTION = ( |
| 76 | + REGION='us-east-2', |
| 77 | + AWS_KEY_ID = '<your_aws_key_id>', |
| 78 | + AWS_SECRET_KEY = '<your_aws_secret_key>' |
| 79 | +); |
| 80 | +``` |
| 81 | + |
| 82 | +## Step 3: Verify Attached Tables |
| 83 | + |
| 84 | +1. Query the two attached tables to verify their contents: |
| 85 | + |
| 86 | +```sql |
| 87 | +SELECT * FROM population_all_columns; |
| 88 | + |
| 89 | +┌───────────────────────────────────────────────────────┐ |
| 90 | +│ city │ province │ population │ |
| 91 | +├──────────────────┼──────────────────┼─────────────────┤ |
| 92 | +│ Toronto │ Ontario │ 2731571 │ |
| 93 | +│ Montreal │ Quebec │ 1704694 │ |
| 94 | +│ Vancouver │ British Columbia │ 631486 │ |
| 95 | +└───────────────────────────────────────────────────────┘ |
| 96 | + |
| 97 | +SELECT * FROM population_withoutprovince; |
| 98 | + |
| 99 | +┌────────────────────────────────────┐ |
| 100 | +│ city │ population │ |
| 101 | +├──────────────────┼─────────────────┤ |
| 102 | +│ Toronto │ 2731571 │ |
| 103 | +│ Montreal │ 1704694 │ |
| 104 | +│ Vancouver │ 631486 │ |
| 105 | +└────────────────────────────────────┘ |
| 106 | +``` |
| 107 | + |
| 108 | +2. If you update the source table in Databend, you can observe the same changes reflected in the attached table on Databend Cloud. For example, if you change the population of Toronto to 2,371,571 in the source table: |
| 109 | + |
| 110 | +```sql |
| 111 | +UPDATE population |
| 112 | +SET population = 2371571 |
| 113 | +WHERE city = 'Toronto'; |
| 114 | +``` |
| 115 | + |
| 116 | + After executing the update, you can query the attached table again to see the updated data: |
0 commit comments