---
title: "BLOB and CLOB types"
description: "Store large binary and text objects in CoraDB with BLOB and CLOB: external LOB files, LOB columns, reading and writing LOB values."
url: "https://www.coradb.ai/manual/sql/datatype_lob.html"
markdown_url: "https://www.coradb.ai/manual/markdown/sql/datatype_lob.md"
product: "CoraDB"
version: "1.0"
keywords: "coradb blob, clob, lob, large object, clob_to_char, char_to_clob, bit_to_blob, lob-base-path"
---

# BLOB and CLOB types

## BLOB/CLOB data types

**BLOB** (binary large object) and **CLOB** (character large object) store large values such as documents and images. CoraDB keeps LOB data *outside* the database volumes, in files on the database server's file system, and stores only a reference to the file, the *LOB locator*, in the row.

| Type | Holds | SQL value form | Maximum size |
|---|---|---|---|
| **BLOB** | Binary data | Bit string | The largest file the file system allows |
| **CLOB** | Character data | Character string | The largest file the file system allows |

- LOB files are named `{table_name}_{unique_name}`, where *unique_name* is generated by the server, so the LOB files of many tables can share one directory.
- The files are created, replaced and deleted by CoraDB together with the rows that reference them. Do not change them with other tools; CoraDB cannot keep them consistent with the database if you do.
- Converting a LOB value to a bit or character string (and the reverse) is limited to 1 GB.

### Creating LOB columns

**BLOB** and **CLOB** columns are created with **CREATE TABLE** or added with **ALTER TABLE**, like any other column. The following table stores a manual and an image for products of the e-commerce sample database:

```sql
CREATE TABLE product_doc (
    product_id INT PRIMARY KEY,
    manual     CLOB,
    image      BLOB
);
```

LOB columns have these restrictions:

| Not allowed on a LOB column | Error |
|---|---|
| An index, or a **PRIMARY KEY**, **FOREIGN KEY** or **UNIQUE** constraint | `Cannot define index on domain "clob".` |
| **NOT NULL** | `Set NOT NULL on blob typed column is not allowed.` |
| A **DEFAULT** other than **NULL**, or **SHARED** | `Set DEFAULT or SHARED on blob typed column is not allowed.` |
| Use as an element of a collection type | |

### Storing and updating LOB values

A string or bit string assigned to a LOB column is converted automatically. The functions [CHAR_TO_CLOB](https://www.coradb.ai/manual/sql/function/lob_fn.html#char_to_clob) and [BIT_TO_BLOB](https://www.coradb.ai/manual/sql/function/lob_fn.html#bit_to_blob) make the conversion explicit.

```sql
INSERT INTO product_doc VALUES (1618, CHAR_TO_CLOB('Gobi HeatTec Tee by Diadora. Breathable mesh.'), BIT_TO_BLOB(X'89504E47'));
INSERT INTO product_doc VALUES (1619, CHAR_TO_CLOB('Hero Hoodie. Fleece lining, kangaroo pocket.'), NULL);
INSERT INTO product_doc VALUES (1620, 'Plain string into CLOB', X'FFD8');

-- copy a LOB value from another row
INSERT INTO product_doc (product_id, manual) SELECT 1621, manual FROM product_doc WHERE product_id = 1619;

UPDATE product_doc SET manual = NULL WHERE product_id = 1621;
UPDATE product_doc SET image = (SELECT image FROM product_doc WHERE product_id = 1618) WHERE product_id = 1619;
DELETE FROM product_doc WHERE product_id = 1620;
```

What happens to the files:

- **INSERT** of a non-NULL value creates a new file and stores its locator in the row. Copying a LOB value from another row creates a copy of the file.
- **UPDATE** to a non-NULL value deletes the old file, if any, and creates a new one. **UPDATE** to **NULL** deletes the old file and stores **NULL**.
- **DELETE** of a row deletes its LOB files, also when the row is deleted by a cascading foreign key.
- **DROP TABLE** and **ALTER TABLE … DROP COLUMN** remove only the locators; the LOB files themselves are *not* deleted.

To load a file that already exists on the server, use [CLOB_FROM_FILE](https://www.coradb.ai/manual/sql/function/lob_fn.html#clob_from_file) or [BLOB_FROM_FILE](https://www.coradb.ai/manual/sql/function/lob_fn.html#blob_from_file).

### Reading LOB values

Convert a LOB value to a string or bit string with [CLOB_TO_CHAR](https://www.coradb.ai/manual/sql/function/lob_fn.html#clob_to_char), [BLOB_TO_BIT](https://www.coradb.ai/manual/sql/function/lob_fn.html#blob_to_bit) or [CAST](https://www.coradb.ai/manual/sql/function/typecast_fn.html#cast) to work with its contents. [CLOB_LENGTH](https://www.coradb.ai/manual/sql/function/lob_fn.html#clob_length) and [BLOB_LENGTH](https://www.coradb.ai/manual/sql/function/lob_fn.html#blob_length) return the size without reading the data.

```sql
SELECT product_id,
       CLOB_TO_CHAR(manual) AS manual_text,
       CLOB_LENGTH(manual) AS chars,
       BLOB_LENGTH(image) AS bytes,
       BLOB_TO_BIT(image) AS bits,
       image IS NULL AS no_image
FROM product_doc
ORDER BY product_id;
```

| product_id | manual_text | chars | bytes | bits | no_image |
|---|---|---|---|---|---|
| 1618 | Gobi HeatTec Tee by Diadora. Breathable mesh. | 45 | 4 | 0x89504e47 | 0 |
| 1619 | Hero Hoodie. Fleece lining, kangaroo pocket. | 44 | NULL | NULL | 1 |
| 1620 | Plain string into CLOB | 22 | 2 | 0xffd8 | 0 |
| 1621 | Hero Hoodie. Fleece lining, kangaroo pocket. | 44 | NULL | NULL | 1 |

(Values right after the four **INSERT** statements above; bit strings as drivers return them, in hexadecimal.)

- Drivers return the data of a LOB column. corasql shows the locator (a `file:` path) instead; use **CLOB_TO_CHAR** to see the text.
- **LIKE** works on a **CLOB** column directly. Other string functions need **CLOB_TO_CHAR** first.
- A LOB column cannot be used in **ORDER BY** or **GROUP BY** (`product_doc.manual can not be an ORDER BY column`), nor with comparison operators, **IN** or **NOT IN** (`'=' operator is not defined on types clob and char`). Convert it first.
- **IS NULL** checks whether the column has a locator. A **NULL** LOB value has no file.
- If a LOB file is removed outside CoraDB, the locator points to nothing and the LOB functions return **NULL** for it.

```sql
SELECT product_id FROM product_doc WHERE manual LIKE '%Hoodie%';   -- 1619, 1621
SELECT product_id, SUBSTRING(CLOB_TO_CHAR(manual), 1, 8) AS head
FROM product_doc
ORDER BY CLOB_TO_CHAR(manual);
```

### Functions and operators for LOB

[CAST](https://www.coradb.ai/manual/sql/function/typecast_fn.html#cast) converts between LOB types and bit or character strings:

```sql
CAST (bit_or_char_value AS { BLOB | CLOB })
CAST (clob_value AS { CHAR | VARCHAR })
CAST (blob_value AS { BIT | BIT VARYING })
```

The LOB functions are described in [LOB functions](https://www.coradb.ai/manual/sql/function/lob_fn.html):

| Function | Purpose |
|---|---|
| [CLOB_TO_CHAR](https://www.coradb.ai/manual/sql/function/lob_fn.html#clob_to_char) | **CLOB** to character string |
| [BLOB_TO_BIT](https://www.coradb.ai/manual/sql/function/lob_fn.html#blob_to_bit) | **BLOB** to bit string |
| [CHAR_TO_CLOB](https://www.coradb.ai/manual/sql/function/lob_fn.html#char_to_clob) | Character string to **CLOB** |
| [BIT_TO_BLOB](https://www.coradb.ai/manual/sql/function/lob_fn.html#bit_to_blob) | Bit string to **BLOB** |
| [CHAR_TO_BLOB](https://www.coradb.ai/manual/sql/function/lob_fn.html#char_to_blob) | Character string to **BLOB** (its bytes) |
| [CLOB_FROM_FILE](https://www.coradb.ai/manual/sql/function/lob_fn.html#clob_from_file) | **CLOB** from a file on the server |
| [BLOB_FROM_FILE](https://www.coradb.ai/manual/sql/function/lob_fn.html#blob_from_file) | **BLOB** from a file on the server |
| [CLOB_LENGTH](https://www.coradb.ai/manual/sql/function/lob_fn.html#clob_length) | Length of a **CLOB** in characters |
| [BLOB_LENGTH](https://www.coradb.ai/manual/sql/function/lob_fn.html#blob_length) | Length of a **BLOB** in bytes |


### LOB storage

LOB files are stored on the local file system of the database server, in the *LOB base path* of the database:

- By default, the `lob` directory under the directory of the database volumes.
- Another directory if you give one with `--lob-base-path` (`-B`) when you create the database; CoraDB creates the directory if needed. See [createdb](https://www.coradb.ai/manual/admin/admin_utils.html#createdb).

```bash
# LOB files in <volume directory>/lob
coradb createdb image_db en_US

# LOB files in /data/lob
coradb createdb --lob-base-path="file:/data/lob" image_db en_US
```

`coradb spacedb` shows the LOB base path in its last line (`LOB space description file:/data/lob`).

To move new LOB files to another directory, change the `lob-base-path` column of the database in `databases.txt` and restart the database server. LOB files already stored in the old directory remain readable.

- Backup and restore cover the locators but **not** the LOB files. Back up the LOB directory separately, at the same point in time as the database.
- [copydb](https://www.coradb.ai/manual/admin/admin_utils.html#copydb) copies LOB files only when asked to (`--copy-lob-path`, or `-B` for a new path); otherwise update `databases.txt` for the copy yourself.
- If the LOB base path registered in `databases.txt` is removed, the server and the utilities cannot work with the database.

### Transactions and recovery

Changes to LOB values are transactional: **COMMIT** and **ROLLBACK** keep each locator and its file consistent.

```sql
-- with autocommit off
INSERT INTO product_doc VALUES (2001, CHAR_TO_CLOB('Version 1'), NULL);
COMMIT;
UPDATE product_doc SET manual = CHAR_TO_CLOB('Version 2') WHERE product_id = 2001;
ROLLBACK;
SELECT CLOB_TO_CHAR(manual) FROM product_doc WHERE product_id = 2001;   -- 'Version 1'
```

Keep in mind that the files themselves are outside the log:

- If the server stops in the middle of a transaction that changed a LOB value, recovery rolls back the locator, which can then point to a file that no longer holds the old data. Reading it raises an error.
- A driver reads LOB data when a row is fetched, not when the query runs. If another transaction updates the LOB value in between, the fetch can fail because the locator no longer matches its file.
- After a point-in-time restore, locators and LOB files can be out of step for the same reason.
