# SQL Database

import Tag from '~/components/webkit/Tag.vue'
import DocButton from '~/components/webkit/DocButton.vue';




Azion **SQL Database** is an edge-native SQL solution designed for serverless applications. It's fully ACID-compliant and utilizes SQLite's SQL dialect to provide a familiar development environment, facilitating fast integration. Azion SQL Database implements a Main/Replicas architecture to enable ultra-low latency querying at the network edge, enhancing performance for distributed applications. This way, the main instance is replicated and stored across all nodes within the Azion Global Infrastructure to guarantee the availability and fault tolerance.

With SQL Database, you can perform different tasks, including:

- Store real-time telemetry data, ideal for IoT.
- Manage inventory of distributed e-commerce/retail distribution centers.
- Store and analyze access logs, quickly identifying and mitigating security attacks.
- Store and manage access data.

There are two ways to build a database through SQL Database: using the SQL Database REST API or writing an [function]. The REST API allows you to create, read, update, delete, and list databases. An function allows you to retrieve data from a database.

### Implementation

| Scope | Resource |
| ----- | -------- |
| Manage databases with REST API | [How to create an SQL Database database](/en/documentation/products/guides/manage-sql-database/) |
| Create queries with REST API | [How to create and query data on SQL Database](/en/documentation/products/guides/create-tables-sql-database/) |
| Retrieve data from a database | [How to retrieve data from a database with SQL Database and Functions](/en/documentation/products/guides/edge-sql/retrieve-data-with-functions/) |
| Get to know SQL Database Vector Search | [SQL Database Vector Search reference](/en/documentation/products/store/sql-database/vector-search/) |
| Import data to SQL Database | [How to import data guide ](/en/documentation/products/guides/import-data-sql-database/) |

---

## Business rules

Writing databases, through REST API or an function, is only possible in the [main instance](#main-instance). All [replicas](#replicas) are read-only.

:::caution[warning]
Once you delete a main instance, this action can't be undone. You'll no longer be able to write or read on that database.
:::

During the creation of a database, especially your first one, there's a propagation time for it to be replicated on the edge nodes. Your `POST` request response will exhibit a `creating` state, and you can run `GET` requests to check once it's created.

You can also use a JavaScript function to query and analyze if the snapshots have been merged into replicas.

:::note
If you receive a `creation_failed` state, try creating the database again or contact the [support team](/en/documentation/services/support/#service-channels) to check.
:::

---

## Data resilience

With a distributed architecture running across Azion's edge nodes, SQL Database guarantees data is kept safe and available. Your data is created in a main instance and propagated through replicas across Azion's edge nodes.

---

## Databases

A database is a set of structured tables that allow to write and read data in SQL language. Organized in columns and rows, users are able to add data logically, with columns representing specific information in the vertical axis, while rows represent individual entry data, related to a column, in the horizontal axis.

[Creating a database](/en/documentation/products/guides/manage-sql-database/) is the first step to use SQL Database and add data:

```bash
curl --location 'https://api.azion.com/v4/edge_sql/databases' \
--header 'Authorization: Token [TOKEN VALUE]' \
--header 'Content-Type: application/json' \
--data '{
   "name": "my-database"
}'
```

### Query

A query is how you structure your table or a search on rows on a previously created database. It's possible to query based on relations and constraints.

For example, to create a table via cURL:

```bash
curl --location 'https://api.azion.com/v4/edge_sql/databases/{id_database}/query' \
--header 'Authorization: Token [TOKEN VALUE]' \
--header 'Content-Type: application/json' \
--data '{
    "statements": [
        "CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT NOT NULL);"
    ]
}'
```

To query data on the already created table:

```bash
curl --location 'https://api.azion.com/v4/edge_sql/databases/{id_database}/query' \
--header 'Authorization: Token [TOKEN VALUE]' \
--header 'Content-Type: application/json' \
--data '{
    "statements": [
        "SELECT * FROM users;"
    ]
}'
```

---

## Vector Search 

[Vector Search](/en/documentation/products/store/sql-database/vector-search/) is an **Azion SQL Database** feature that enables customers to implement semantic search engines. While traditional search models aim to find exact matches, such as keyword matches, vector search models use specialized algorithms to identify similar items based on their mathematical representations, or vector embeddings.

This feature natively integrates with [AI Inference](/en/documentation/products/ai/ai-inference/) to power RAG implementations without additional infrastructure, combining SQL and vector capabilities in a single database. The system supports **LangChain Vector Store** integration for document storage and **LangChain Retriever** for advanced hybrid search combining vector and full-text search capabilities.

---

## Interacting with SQL Database via Terminal

You can interact with SQL Database directly from your terminal using [the edgesql-shell project](https://github.com/aziontech/edgesql-shell). This tool provides a command-line interface for executing SQL commands against your SQL Database databases.

Check the documentation below for details on how to install and use EdgeSQL Shell.

<DocButton href="/en/documentation/products/store/sql/install-edge-sql-shell/" label="Go to SQL Database Shell installation guide" kind="secondary" size="medium" /> 
<DocButton href="/en/documentation/products/store/sql/sql-database-shell-commands/" label="Go to SQL Database Shell commands reference" kind="secondary" size="medium" />



---

## Instances

### Main instance

The primary storage instance, it receives all *write* commands and synchronizes the received data with the replicas. It also allows *read* actions.

All [databases](#databases) are created and all [queries](#query) are written on the main instance. After the automatic process handled by Azion, your data is processed on edge locations, and snapshots are created and merged into replicas.

:::caution[warning]
It's recommended to always create a backup of your [main instance](#main-instance) to guarantee your data is safe. As there's a propagation time which differs for the replicas, it's best to backup the main rather than the replicas.
:::

### Replicas

Replicas are *read-only* copies of the main instance. Replicas are updated within different propagation time, so they'll eventually be in sync in the main instance, but it doesn't occur instantly.

The replication process occurs automatically through Azion back-end systems, in which snapshots are created and then merged in each replica.

They handle data storage, processing, and management in proximity to the devices reading the data, helping with data distribution, availability, and low latency.

---

## Supported query languages

As Azion **SQL Database** is based on SQLite, it supports the same SQL languages SQLite does.

[See SQL languages understood](https://www.sqlite.org/lang.html) to create queries.

---

## Limits

:::tip
**Increase limits** <br></br>
You can request to increase the limits based on your plan. Contact the [technical support team](/en/documentation/services/support/) to request it.
:::

These are the **default limits**:

| Scope | Limit | 
| ----- | ----- | 
| Maximum number of columns per table | 100 |
| Maximum SQL query duration | 30 seconds |

These are the **default limits** for each Support service:

| Scope | Developer | Business | Enterprise | Mission-Critical |
| ----- | --------- | -------- | ---------- | ---------------- |
| Databases | 10 | 50 | 200 | 200 |
| Maximum database size | 200 MB | 500 MB | 2 GB | 2 GB |
| Maximum storage per account | 5 GB | 50 GB | 300 GB | 300 GB |

To view the databases associated with your acount visit this [guide](/en/documentation/products/store/sql/list-databases/).

---