Creating tables dynamically and total tables limit

How do I create a table in a database dynamically on the fly on the client side or inside worker?

Also what is the hard limit on the number of tables per database?

D1 is just SQLite under the hood. They do have a few things tuned/some patches, but generally it’s all the same. You just send a query with a create table statement: CREATE TABLE

I don’t believe it’s overridden, so should be the normal SQlite 2 billion: sql - Maximum of tables a SQLite database file supports - Stack Overflow
Of course you’d probably hit the maximum amount of storage (500 mb free, 10gb paid) per database long before.

1 Like

I was using DROP TABLE copied from schema.sql file. I have used nosql databases before but this is first sql database that I am learning.

I am not able to create table from a worker.

Thank you so much for the help.

now*

In create table query how do I specify database name?

CREATE TABLE IF NOT EXISTS demoDb.members (

I have tried database_name from wrangle.toml which is in camel case but it throws error

Error: D1_ERROR: unknown database demoDb at offset 30

Even trying it in lowercase or snake and kebab case its throwing error.

You don’t specify the database name, just the table name.
CREATE TABLE IF NOT EXISTS members (…
(if you really wanted to specify a database name, the default/hardcoded one is main in sqlite’s context)

It’s already being created in that specific D1 Database because of the binding.
Should check out a get starting with sqlite guide/video/etc:

It’s mostly all the same, at least commands wise and such. D1 has a few extra security limitations in terms of things like max command length/max amount of tables you can join, etc, but all the commands are the same.

1 Like

Thanks a lot Chaika

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.