Skip to content

Commit ba882aa

Browse files
committed
updates
1 parent de9e768 commit ba882aa

File tree

2 files changed

+72
-82
lines changed

2 files changed

+72
-82
lines changed

docs/en/sql-reference/10-sql-commands/00-ddl/01-table/92-attach-table.md

Lines changed: 14 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -55,89 +55,28 @@ CONNECTION = ( <connection_parameters> )
5555

5656
- `CONNECTION` specifies the connection parameters required for establishing a link to the object storage where the source table's data is stored. The connection parameters vary for different storage services based on their specific requirements and authentication mechanisms. For more information, see [Connection Parameters](../../../00-sql-reference/51-connect-parameters.md).
5757

58-
## Examples
58+
## Tutorials
5959

60-
This example illustrates how to link a new table in Databend Cloud with an existing table in Databend, which stores data within an Amazon S3 bucket named "databend-toronto".
60+
- [Linking Tables with ATTACH TABLE](/tutorials/databend-cloud/link-tables)
6161

62-
#### Step 1. Creating Table in Databend
62+
## Examples
6363

64-
Create a table named "population" and insert some sample data:
64+
This example creates an attached table, which includes all columns from a source table stored in AWS S3:
6565

66-
```sql title='Databend:'
67-
CREATE TABLE population (
68-
city VARCHAR(50),
69-
population INT
66+
```sql
67+
ATTACH TABLE population_all_columns 's3://databend-doc/1/16/' CONNECTION = (
68+
REGION='us-east-2',
69+
AWS_KEY_ID = '<your_aws_key_id>',
70+
AWS_SECRET_KEY = '<your_aws_secret_key>'
7071
);
71-
72-
INSERT INTO population (city, population) VALUES
73-
('Toronto', 2731571),
74-
('Montreal', 1704694),
75-
('Vancouver', 631486);
76-
```
77-
78-
#### Step 2. Obtaining Database ID and Table ID
79-
80-
Use the [FUSE_SNAPSHOT](../../../20-sql-functions/16-system-functions/fuse_snapshot.md) function to obtain the database ID and table ID. The result below indicates that the database ID is **1**, and the table ID is **556**:
81-
82-
```sql title='Databend:'
83-
SELECT * FROM FUSE_SNAPSHOT('default', 'population');
84-
85-
┌──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
86-
│ snapshot_id │ snapshot_location │ format_version │ previous_snapshot_id │ segment_count │ block_count │ row_count │ bytes_uncompressed │ bytes_compressed │ index_size │ timestamp
87-
├──────────────────────────────────┼───────────────────────────────────────────────────┼────────────────┼──────────────────────┼───────────────┼─────────────┼───────────┼────────────────────┼──────────────────┼────────────┼────────────────────────────┤
88-
│ f252dd43d1aa44898a04827808342daf │ 1/556/_ss/f252dd43d1aa44898a04827808342daf_v4.mpk4NULL113704485312023-11-01 02:35:47.325319
89-
└──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
9072
```
9173

92-
When you access the bucket page on Amazon S3, you'll observe that the data is organized within the path `databend-toronto` > `1` > `556`, like this:
93-
94-
![Alt text](/img/sql/attach-table-2.png)
95-
96-
#### Step 3. Linking Table in Databend Cloud
97-
98-
Sign in to Databend Cloud and run the following command in a worksheet to link a table named "population_readonly":
74+
This example creates an attached table, which includes only selected columns (`city` and `population`) from a source table stored in AWS S3:
9975

100-
```sql title='Databend Cloud:'
101-
ATTACH TABLE population_readonly 's3://databend-toronto/1/556/' CONNECTION = (
76+
```sql
77+
ATTACH TABLE population_only (city, population) 's3://databend-doc/1/16/' CONNECTION = (
78+
REGION='us-east-2',
10279
AWS_KEY_ID = '<your_aws_key_id>',
10380
AWS_SECRET_KEY = '<your_aws_secret_key>'
10481
);
105-
```
106-
107-
To verify the success of the link, run the following query in Databend Cloud:
108-
109-
```sql title='Databend Cloud:'
110-
SELECT * FROM population_readonly;
111-
112-
-- Expected result:
113-
┌────────────────────────────────────┐
114-
│ city │ population │
115-
├──────────────────┼─────────────────┤
116-
│ Toronto │ 2731571
117-
│ Montreal │ 1704694
118-
│ Vancouver │ 631486
119-
└────────────────────────────────────┘
120-
```
121-
122-
You're all set! If you update the source table in Databend, you can observe the same changes reflected in the target table on Databend Cloud. For example, if you change the population of Toronto to 2,371,571 in the source table:
123-
124-
```sql title='Databend:'
125-
UPDATE population
126-
SET population = 2371571
127-
WHERE city = 'Toronto';
128-
```
129-
130-
You can see that the updates are synced to the attached table in Databend Cloud:
131-
132-
```sql title='Databend Cloud:'
133-
SELECT * FROM population_readonly;
134-
135-
-- Expected result:
136-
┌────────────────────────────────────┐
137-
│ city │ population │
138-
├──────────────────┼─────────────────┤
139-
│ Toronto │ 2371571
140-
│ Montreal │ 1704694
141-
│ Vancouver │ 631486
142-
└────────────────────────────────────┘
143-
```
82+
```

docs/en/tutorials/databend-cloud/link-tables.md

Lines changed: 58 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
title: Linking Tables with ATTACH TABLE
33
---
44

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.
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](/sql/sql-commands/ddl/table/attach-table) command.
66

77
## Before You Start
88

@@ -23,8 +23,8 @@ docker run \
2323
-e QUERY_STORAGE_TYPE=s3 \
2424
-e AWS_S3_ENDPOINT="https://s3.us-east-2.amazonaws.com" \
2525
-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
26+
-e AWS_ACCESS_KEY_ID=<your-aws-access-key-id> \
27+
-e AWS_SECRET_ACCESS_KEY=<your-aws-secrect-access-key> \
2828
datafuselabs/databend:v1.2.699-nightly
2929
```
3030

@@ -61,7 +61,7 @@ SELECT snapshot_location FROM FUSE_SNAPSHOT('default', 'population');
6161

6262
2. Execute the following statements to create two attached tables:
6363
- 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).
64+
- The second table, `population_only`, includes only the selected columns (`city` & `population`).
6565

6666
```sql
6767
-- Create an attached table with all columns from the source
@@ -72,7 +72,7 @@ ATTACH TABLE population_all_columns 's3://databend-doc/1/16/' CONNECTION = (
7272
);
7373

7474
-- 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 = (
75+
ATTACH TABLE population_only (city, population) 's3://databend-doc/1/16/' CONNECTION = (
7676
REGION='us-east-2',
7777
AWS_KEY_ID = '<your_aws_key_id>',
7878
AWS_SECRET_KEY = '<your_aws_secret_key>'
@@ -94,7 +94,7 @@ SELECT * FROM population_all_columns;
9494
│ Vancouver │ British Columbia │ 631486
9595
└───────────────────────────────────────────────────────┘
9696

97-
SELECT * FROM population_withoutprovince;
97+
SELECT * FROM population_only;
9898

9999
┌────────────────────────────────────┐
100100
│ city │ population │
@@ -113,4 +113,55 @@ SET population = 2371571
113113
WHERE city = 'Toronto';
114114
```
115115

116-
After executing the update, you can query the attached table again to see the updated data:
116+
After executing the update, you can query both attached tables to verify that the changes are reflected:
117+
118+
```sql
119+
-- Check the updated population in the attached table with all columns
120+
SELECT population FROM population_all_columns WHERE city = 'Toronto';
121+
122+
-- Check the updated population in the attached table with only the population column
123+
SELECT population FROM population_only WHERE city = 'Toronto';
124+
```
125+
126+
Expected output for both queries above:
127+
128+
```sql
129+
┌─────────────────┐
130+
│ population │
131+
├─────────────────┤
132+
2371571
133+
└─────────────────┘
134+
```
135+
136+
3. If you drop the `province` column from the source table, it will no longer be available in the attached table for queries.
137+
138+
```sql
139+
ALTER TABLE population DROP province;
140+
```
141+
142+
After dropping the column, any queries referencing it will result in an error. However, the remaining columns can still be queried successfully.
143+
144+
For example, attempting to query the dropped `province` column will fail:
145+
146+
```sql
147+
SELECT province FROM population_all_columns;
148+
error: APIError: QueryFailed: [1065]error:
149+
--> SQL:1:8
150+
|
151+
1 | SELECT province FROM population_all_columns
152+
| ^^^^^^^^ column province doesn't exist
153+
```
154+
155+
However, you can still retrieve the `city` and `population` columns:
156+
157+
```sql
158+
SELECT city, population FROM population_all_columns;
159+
160+
┌────────────────────────────────────┐
161+
│ city │ population │
162+
├──────────────────┼─────────────────┤
163+
│ Toronto │ 2371571 │
164+
│ Montreal │ 1704694 │
165+
│ Vancouver │ 631486 │
166+
└────────────────────────────────────┘
167+
```

0 commit comments

Comments
 (0)