datasquirel-docs/pages/database-reference/querying-data/insert/index.mdx

83 lines
2.0 KiB
Plaintext

---
title: INSERT | Datasquirel docs
description: Add new rows to a table using INSERT statements in Datasquirel
page_title: INSERT
page_description: Add new rows to your tables using SQL INSERT statements.
---
## Overview
`INSERT INTO` adds one or more new rows to a table. Each row must provide values for all `NOT NULL` columns that do not have a default value.
## Basic Syntax
```sql
INSERT INTO table_name (column1, column2, column3)
VALUES (value1, value2, value3);
```
## Examples
### Insert a Single Row
```sql
INSERT INTO users (name, email, is_active)
VALUES ('Alice', 'alice@example.com', 1);
```
### Insert Multiple Rows
```sql
INSERT INTO tags (name)
VALUES ('javascript'), ('typescript'), ('sql');
```
### Insert with Default Values
If a column has a DEFAULT value defined, you can omit it:
```sql
INSERT INTO posts (title, content)
VALUES ('My First Post', 'Hello world!');
-- created_at and updated_at will use their DEFAULT values
```
## AUTO_INCREMENT
Every Datasquirel table has an `id` column with `AUTO_INCREMENT`. You never need to provide the `id` value on insert — the database assigns the next available integer automatically.
After an INSERT, the new row's `id` is returned as the `insertId` in the API response.
## Using INSERT via the API
The [CRUD POST endpoint](/docs/api-reference/crud/post) handles inserts through a simple object:
```javascript
const result = await datasquirel.crud.insert({
dbName: "my_database",
tableName: "users",
body: {
name: "Alice",
email: "alice@example.com",
is_active: 1,
},
apiKey: process.env.DATASQUIREL_API_KEY,
});
console.log(result.payload); // The new row's id
```
For batch inserts via the API, include a `batchData` array:
```javascript
const result = await datasquirel.crud.insert({
dbName: "my_database",
tableName: "tags",
body: { batchData: [
{ name: "javascript" },
{ name: "typescript" },
]},
apiKey: process.env.DATASQUIREL_API_KEY,
});
```