D1 is compatible with most SQLite's SQL convention since it leverages SQLite's query engine. You can use SQL commands to query D1.
There are a number of ways you can interact with a D1 database:
Using D1 Workers Binding API in your code.
Using D1 REST API .
Using D1 Wrangler commands .
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 to learn more about supported SQL statements.
Use foreign key relationships
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.
user_id INTEGER PRIMARY KEY ,
order_id INTEGER PRIMARY KEY ,
user_who_ordered INTEGER ,
FOREIGN KEY (user_who_ordered) REFERENCES users(user_id)
Refer to Define foreign keys for more information.
D1 allows you to query and parse JSON data stored within a database. For example, you can extract a value inside a JSON object.
Given the following JSON object (type:blob
) in a column named sensor_reading
, you can extract values from it directly.
-- Extract the temperature value
SELECT json_extract(sensor_reading, '$.measurement.temp_f' ) -- returns "77.4" as TEXT
Refer to Query JSON to learn more about querying JSON objects.
Query D1 with Workers Binding API
Workers Binding API primarily interacts with the data plane, and allows you to query your D1 database from your Worker.
This requires you to:
Bind your D1 database to your Worker.
Prepare a statement.
Run the statement.
async fetch ( request , env ) {
const { pathname } = new URL ( request . url ) ;
const companyName1 = `Bs Beverages` ;
const companyName2 = `Around the Horn` ;
const stmt = env . DB . prepare ( `SELECT * FROM Customers WHERE CompanyName = ?` ) ;
if ( pathname === `/RUN` ) {
const returnValue = await stmt . bind ( companyName1 ) . run () ;
return Response . json ( returnValue ) ;
`Welcome to the D1 API Playground!
\n Change the URL to test the various methods inside your index.js file.` ,
Refer to Workers Binding API for more information.
REST API primarily interacts with the control plane, and allows you to create/manage your D1 database.
Refer to D1 REST API for D1 REST API documentation.
Query D1 with Wrangler commands
You can use Wrangler commands to query a D1 database. Note that Wrangler commands use REST APIs to perform its operations.
npx wrangler d1 execute prod-d1-tutorial --command="SELECT * FROM Customers"
๐ Mapping SQL input into an array of statements
๐ Executing on local database production-db-backend (<DATABASE_ID>) from .wrangler/state/v3/d1:
โโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโ
โ CustomerId โ CompanyName โ ContactName โ
โโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโค
โ 1 โ Alfreds Futterkiste โ Maria Anders โ
โโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโค
โ 4 โ Around the Horn โ Thomas Hardy โ
โโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโค
โ 11 โ Bs Beverages โ Victoria Ashworth โ
โโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโค
โ 13 โ Bs Beverages โ Random Name โ
โโโโโโโโโโโโโโดโโโโโโโโโโโโโโโโโโโโโโดโโโโโโโโโโโโโโโโโโโโ
Thank you for helping improve Cloudflare's documentation!