Basic Insert
Insert a single row into a table:Bulk Insert
Insert multiple rows in a single query:Bulk inserts are more efficient than multiple single inserts as they execute in a single database round-trip.
Insert with Returning
Get the inserted row(s) back from the database:On Conflict Do Nothing
Ignore inserts that would violate constraints:On Conflict Do Update (Upsert)
Update existing rows when conflicts occur:Insert from Select
Insert data from another query:The selected columns must match the target table’s column structure.
Insert with SQL Expressions
Use SQL expressions in insert values:Insert with Default Values
Omit columns with default values:PostgreSQL: Override System Values
Insert into generated columns:Type Safety
Drizzle provides complete type safety for inserts:With Clause (CTE)
Use Common Table Expressions with inserts:Batch Inserts
For very large datasets, use batch processing:Insert Examples by Database
- PostgreSQL
- MySQL
- SQLite
Performance Tips
1
Use Bulk Inserts
Insert multiple rows in a single query instead of multiple queries
2
Batch Large Datasets
Split very large datasets (10k+ rows) into batches of 1000-5000 rows
3
Use Transactions
Wrap multiple inserts in a transaction for consistency and performance
4
Avoid Returning When Unnecessary
Only use
.returning() when you need the inserted data backNext Steps
Update Queries
Learn how to update existing data
Select Queries
Query and filter your data
Transactions
Use transactions for data consistency