Skip to content

Commit 05a130b

Browse files
committed
Introducing query JSON and foreign keys in Query databases chapter.
1 parent ec24c8d commit 05a130b

File tree

1 file changed

+48
-18
lines changed

1 file changed

+48
-18
lines changed

src/content/docs/d1/best-practices/query-d1.mdx

Lines changed: 48 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ There are a number of ways you can interact with a D1 database:
1212
3. Using [D1 REST API](/api/operations/cloudflare-d1-create-database).
1313
4. Using [D1 Wrangler commands](/d1/wrangler-commands/).
1414

15+
:::note
16+
D1 is compatible with most SQLite's SQL convention since it leverages SQLite's query engine.
17+
:::
18+
1519
## Query D1 with Workers Binding API
1620

1721
Workers Binding API primarily interacts with the data plane, and allows you to query your D1 database from your Worker.
@@ -47,29 +51,55 @@ Refer to [Workers Binding API](/d1/worker-api/) for more information.
4751

4852
## Query D1 with SQL API
4953

50-
D1 is compatible with most SQLite's SQL convention since it leverages SQLite's query engine.
54+
D1 understands SQLite semantics, which allows you to query a database using SQL statements via Workers BindingAPI or REST API (including Wrangler commands). Refer to [D1 SQL API](/d1/sql-api/sql-statements/) to learn more about supported SQL statements.
5155

52-
```sh
53-
npx wrangler d1 execute prod-d1-tutorial --local --command="SELECT * FROM Customers"
56+
### Use foreign key relationships
57+
58+
When using SQL with D1, you may wish to define and and enforce foreign key constraints across tables in a database. Foreign key constraints allow you to enforce relationships across tables, or prevent you from deleting rows that reference rows in other tables. An example of a foreign key relationship is shown below.
59+
60+
```sql
61+
CREATE TABLE users (
62+
user_id INTEGER PRIMARY KEY,
63+
email_address TEXT,
64+
name TEXT,
65+
metadata TEXT
66+
)
67+
68+
CREATE TABLE orders (
69+
order_id INTEGER PRIMARY KEY,
70+
status INTEGER,
71+
item_desc TEXT,
72+
shipped_date INTEGER,
73+
user_who_ordered INTEGER,
74+
FOREIGN KEY(user_who_ordered) REFERENCES users(user_id)
75+
)
5476
```
5577

56-
```sh output
57-
🌀 Mapping SQL input into an array of statements
58-
🌀 Executing on local database production-db-backend (database-id) from .wrangler/state/v3/d1:
59-
┌────────────┬─────────────────────┬───────────────────┐
60-
│ CustomerId │ CompanyName │ ContactName │
61-
├────────────┼─────────────────────┼───────────────────┤
62-
│ 1 │ Alfreds Futterkiste │ Maria Anders │
63-
├────────────┼─────────────────────┼───────────────────┤
64-
│ 4 │ Around the Horn │ Thomas Hardy │
65-
├────────────┼─────────────────────┼───────────────────┤
66-
│ 11 │ Bs Beverages │ Victoria Ashworth │
67-
├────────────┼─────────────────────┼───────────────────┤
68-
│ 13 │ Bs Beverages │ Random Name │
69-
└────────────┴─────────────────────┴───────────────────┘
78+
Refer to [Define foreign keys](/d1/sql-api/foreign-keys/) for more information.
79+
80+
### Query JSON
81+
82+
D1 allows you to query and parse JSON data stored within a database. For example, you can extract a value inside a JSON object.
83+
84+
Given the following JSON object (`type:blob`) in a column named `sensor_reading`, you can extract values from it directly.
85+
86+
```json
87+
{
88+
"measurement": {
89+
"temp_f": "77.4",
90+
"aqi": [21, 42, 58],
91+
"o3": [18, 500],
92+
"wind_mph": "13",
93+
"location": "US-NY"
94+
}
95+
}
96+
```
97+
```sql
98+
-- Extract the temperature value
99+
SELECT json_extract(sensor_reading, '$.measurement.temp_f')-- returns "77.4" as TEXT
70100
```
71101

72-
Refer to [D1 SQL API](/d1/sql-api/sql-statements/) to learn more about supported SQL queries.
102+
Refer to [Query JSON](/d1/sql-api/query-json/) to learn more about querying JSON objects.
73103

74104
## Query D1 with REST API
75105

0 commit comments

Comments
 (0)