Skip to content
Home » D1Admin Documentation » Creating Indexes, Views and Triggers

Creating Indexes, Views and Triggers

We can create Views, Indexes and Triggers for existing Cloudflare D1 database tables by reaching at the bottom of the D1Admin web interface at the Structure view tab. This tutorial will illustrate the necessary steps needed for each of these database objects.

Creating a View

A database View is a read only view over data obtained through a SELECT statement or simply put a view acts like an alias of a SELECT statement (in most cases a complicated one). Your views will be listed before the tables in the left sidebar navigation pane of D1Admin:

Cloudflare D1 views

We can create new view(s) from the bottom of main Structure tab interface (we can go there by clicking on the Database link from the left sidebar):

Cloudflare D1 view SELECT statement

What we need is the view name and a valid SELECT statement that will populate the view, as shown in the picture above. After the view has been created we can browse its data, just like an ordinary table and perform the Export operations against it.

Indexes in D1

We can create a Cloudflare D1 table Index by selecting the destination table that should be indexed and selecting is Structure tab. At the bottom of the page (after the list with all the table columns) we can find the create Index action box. As you can see from the picture below, we should decide the number of columns that will be indexed upfront:

create D1 table Index

Then we should enter the Index name, whether it should allow duplicate values (is it UNIQUE) , and the columns that construct the index and their order:

create cloudflare D1 index columns

A properly configured index can speed up the data retrieval of our applications, but this is something that we should be consider carefully, because indexes will also slower the data insertion process. So keep in mind that indexes are not a bullet proof solution and must be created with caution and extra time to measure their impact on the speed of the application(s).

Creating a Triggers

We can create trigger(s) that execute upon CRUD actions over D1 tables, by selecting the desired table and clicking on its Structure view tab.

Then we should fill in the details for the trigger.

creating cloudflare D1 trigger

Creating triggers, requires good knowledge of the SQL language and although we can create them with the D1Admin user interface, this cannot substitute the SQL code needed for the trigger body. Just like indexes, triggers are a powerful tool and should be used with caution.

What really D1Admin helps with is, that we have all the table triggers with their code in the same Structure tab for a given table:

Summary

This tutorial was a short introduction for the D1Admin user interface toolkit and its elements that allow the creation of views, indexes and triggers for the Cloudflare D1 database.

If you still haven’t tested D1Admin, we encourage you to do so, by following the Setup Steps.