Generate and manage vector embeddings

MySQL | PostgreSQL | SQL Server

This page describes how to generate and store vector embeddings. For an overview, see Vector embedding storage.

Before you begin

You need to have a Cloud SQL instance with the vector database flags enabled.

Generate vector embeddings based on row data

You can generate a vector embedding for a given row's data by using a text embedding API such as Vertex AI or OpenAI. You can use any text embedding API with Cloud SQL vector embeddings. However, you must use the same text embedding API for query string vector generation. You can't combine different APIs for source data and query vectorization.

For example, you can generate a vector embedding from Vertex AI:

fromvertexai.language_modelsimport TextEmbeddingModel
deftext_embedding() -> list:
"""Text embedding with a Large Language Model."""
 model = TextEmbeddingModel.from_pretrained("text-embedding-004")
 embeddings = model.get_embeddings(["What is life?"])
 for embedding in embeddings:
 vector = embedding.values
 print(f"Length of Embedding Vector: {len(vector)}")
 return vector
if __name__ == "__main__":
 text_embedding()

Store vector embeddings

This section provides example statements for storing vector embeddings in Cloud SQL.

Create a new table with a vector embedding column

Use the CREATE TABLE statement with a column that uses the VECTOR data type.

Use the following syntax to create the table:

CREATETABLETABLE_NAME(
idINTEGER
PRIMARYKEY
AUTO_INCREMENT,
titleVARCHAR(60),
EMBEDDING_COLUMN_NAME
VECTOR(VECTOR_DIMENSIONS)
USINGVARBINARY);

Replace the following parameters:

  • TABLE_NAME: the name of the table you where you want to store the embeddings.
  • EMBEDDING_COLUMN_NAME: the name of column that stores the embedding.
  • VECTOR_DIMENSIONS: the number of dimensions to use for the embedding.

In the following example, the embedding column has a vector with three dimensions. The data stored in this column has the VARBINARY data type.

CREATETABLEbooks(
idINTEGERPRIMARYKEYAUTO_INCREMENT,titleVARCHAR(60),embeddingVECTOR(3)USINGVARBINARY);

Add a vector embedding column to an existing table

Use the ALTER TABLE statement to add a vector embedding column to an existing table. The column must use the VECTOR data type to hold the embedding.

In the following example, an embedding column that has a vector with three dimensions is inserted into the table. The data stored in this column has the VARBINARY data type.

ALTERTABLEbooks
ADDCOLUMNembedding
VECTOR(3)
USINGVARBINARY;

Insert a vector embedding

Use INSERT with the string_to_vector function to insert a vector embedding values into a table.

In the following example, a vector with three dimensions is inserted into the embedding column.

INSERTINTObooks
(
title,
embedding)
VALUES(('book title',string_to_vector('[1,2,3]')));

Insert multiple vector embeddings

Use INSERT with the string_to_vector function to insert a comma-separated list of vector embeddings.

In the following statement, two embeddings, each containing a vector with three dimensions and is inserted into the embedding column.

INSERTINTObooks
(
title,
embedding)
VALUES
(
(
'book title',
string_to_vector('[1,2,3]')),
('book title',string_to_vector('[4,5,6]')));

Upsert a vector embedding

Use an INSERT or UPDATE operation on a table with the string_to_vector function to add a vector embedding column, using the following syntax.

In the following statement, an upsert is used to insert or update the embedding column with an embedding that contains a vector with three dimensions.

INSERTINTObooks
(
id,
title,
embedding)
VALUES
(
(
1,
'book title',
string_to_vector('[1,2,3]')))
ONDUPLICATEKEYUPDATEembedding=string_to_vector('[1,2,3]');

Update a vector embedding

Use UPDATE with the string_to_vector function to update a vector embedding.

In the following statement, UPDATE is used to update the embedding column with a vector with three dimensions.

UPDATEbooks
SETembedding=string_to_vector('[7,8,9]')
WHEREid=1;

Retrieve vector embeddings

To retrieve vector embeddings, use the Cloud SQL vector_to_string function along with the name of the embedding.

In the following statement, the embedding column is retrieved to view.

SELECTvector_to_string(embedding)FROMbooksWHEREid=1;

Delete a vector embedding

Use DELETE with the string_to_vector function to remove a vector embedding from a table. If there's a vector index, you must first delete it. For more information, see Drop a vector index.

In the following statement, DELETE is used to delete the value in the embedding column.

DELETEFROMbooks
WHEREembedding=string_to_vector('[1,2,3]');

What's next

Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025年12月10日 UTC.