> For the complete documentation index, see [llms.txt](https://mariadb.com/docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://mariadb.com/docs/server/reference/sql-structure/vectors/create-table-with-vectors.md).

# CREATE TABLE with Vectors

<table data-view="cards"><thead><tr><th align="center"></th><th align="center"></th><th align="center"></th><th data-hidden data-card-cover data-type="files"></th></tr></thead><tbody><tr><td align="center"><strong>WEBINAR</strong></td><td align="center">The Next Generation of MariaDB: Powered by Vector Search</td><td align="center"><a href="https://go.mariadb.com/GLBL-WBN-2025-01-30-WhatsnewinMariaDB-ES.html?utm_source=onpagepromo&#x26;utm_medium=kb&#x26;utm_campaign=webinar-platform-vector"><strong>Watch Now</strong></a></td><td><a href="/files/57TC3V6duqnJsPBeRWiq">/files/57TC3V6duqnJsPBeRWiq</a></td></tr></tbody></table>

{% hint style="info" %}
[Vectors](/docs/server/reference/sql-structure/vectors.md) are available from [MariaDB 11.7](/docs/release-notes/community-server/old-releases/11.7/what-is-mariadb-117.md).
{% endhint %}

MariaDB has a dedicated [VECTOR(N)](/docs/server/reference/sql-structure/vectors/vector.md) data type with a built-in data validation. `N` is the number of dimensions that all vector values in the column have.

{% hint style="info" %}

* Vector indexes are dimensionality-specific.
* All vectors inserted into an indexed column must match the index's target dimensionality.
* Inserting vectors with different dimensionalities will result in an error.
  {% endhint %}

Consider the following table:

```sql
CREATE TABLE embeddings (
        doc_id BIGINT UNSIGNED PRIMARY KEY,
        embedding VECTOR(1536)
);
```

To have a fast vector search, you have to index the vector column, creating a `VECTOR` index:

```sql
CREATE TABLE embeddings (
        doc_id BIGINT UNSIGNED PRIMARY KEY,
        embedding VECTOR(1536) NOT NULL,
        VECTOR INDEX (embedding)
);
```

{% hint style="warning" %}
Note that there can be only one vector index in the table, and the indexed vector column must be `NOT NULL`.
{% endhint %}

There are two options that can be used to configure the vector index:

* `M` — Larger values mean slower `SELECT` and `INSERT` statements, larger index size and higher memory consumption, but more accurate results. The valid range is from `3` to `200`.
* `DISTANCE` — Distance function to build the vector index for. Searches using a different distance function will not be able to use a vector index. Valid values are `cosine` and `euclidean` (the default).

```sql
CREATE TABLE embeddings (
        doc_id BIGINT UNSIGNED PRIMARY KEY,
        embedding VECTOR(1536) NOT NULL,
        VECTOR INDEX (embedding) M=8 DISTANCE=cosine
);
```

{% hint style="info" %}
Declare `DISTANCE` explicitly. The default is `euclidean`, and a query using a different distance function than the one the index was built for cannot use the index, it falls back to a full table scan. Match the distance function to the metric your embedding model recommends.
{% endhint %}

<sub>*This page is licensed: CC BY-SA / Gnu FDL*</sub>

{% @marketo/form formId="4316" %}
