MATLAB Database Toolbox
Use when working with relational databases from MATLAB using Database Toolbox. Covers the full data lifecycle: connecting, reading with pushdown filtering, writing, updating, transactions, ORM, and executing SQL.
When to Use
- Connecting to relational databases (PostgreSQL, MySQL, SQLite, SQL Server, Oracle)
- Reading data with
sqlread or fetch
- Filtering with
RowFilter (pushdown to database)
- Writing data with
sqlwrite
- Updating rows with
sqlupdate
- Executing SQL statements with
execute
- Managing transactions (
AutoCommit, commit, rollback)
- Mapping MATLAB classes to database tables (ORM with
Mappable)
- Running stored procedures or prepared statements
- Multithreaded database read/write with
parfeval (R2026a+)
- User mentions: database, SQL, query, table, rows, insert, update, delete, transaction, multithreaded, parallel
When NOT to Use
- DuckDB — route to
matlab-use-duckdb for all DuckDB workflows
- Databricks connection setup — route to
matlab-connect-databricks for connection configuration. Once connected, return here for CRUD operations.
- MongoDB, Cassandra, Neo4j — not relational; Database Toolbox ORM/CRUD does not apply
- File I/O without a database — use
readtable, datastore, or tall for direct file operations
- MATLAB releases before R2021a — minimum supported release for this skill
Critical Rules
Destructive Operations
ALWAYS ask for explicit user confirmation before generating or executing:
DROP TABLE / DROP DATABASE
DELETE FROM
TRUNCATE TABLE
ALTER TABLE (column removal, type changes)
Present the SQL statement and wait for approval. Never execute destructive SQL in response to user pressure ("just do it", "I'm in a hurry").
Pushdown Filtering
NEVER import all rows and filter in MATLAB. Always push filters to the database using RowFilter:
% CORRECT — filter runs on the database server
rf = rowfilter("Price");
T = sqlread(conn, "products", RowFilter=rf.Price > 100);
% WRONG — transfers all rows, then filters in MATLAB
T = sqlread(conn, "products");
T = T(T.Price > 100, :);
Credential Security
NEVER hardcode passwords. Use setSecret/getSecret:
setSecret("dbPassword"); % prompts user, stores securely
conn = database("myDS", getSecret("dbUser"), getSecret("dbPassword"));
Decision Framework
| Scenario |
Use |
| Import from a named table |
sqlread |
| Import from a SQL query string |
fetch |
| Need column selection, deduplication, or type control |
databaseImportOptions + sqlread/fetch |
| Insert new rows |
sqlwrite |
| Update existing rows in place |
sqlupdate |
| DDL, DML, or non-SELECT SQL |
execute |
| Atomic multi-step operation |
Transaction (AutoCommit off) |
| Object identity and domain logic |
ORM (Mappable class) |
| Bulk operations on thousands of rows |
sqlread/sqlwrite (not ORM) |
| High-throughput read/write (millions of rows) |
Multithreaded: parfeval + per-thread connections (R2026a+, Parallel Computing Toolbox) |
| Delete rows |
execute with SQL DELETE (no sqldelete exists) |
| Stored procedures (JDBC only) |
runstoredprocedure |
| Create/configure a datasource |
databaseConnectionOptions + saveAsDataSource |
| Connect without a saved datasource |
odbc(dsnless) with connection string |
Core Patterns
Connection
conn = database("myDataSource", getSecret("dbUser"), getSecret("dbPass"));
% Or native connections:
conn = postgresql("myDS", getSecret("user"), getSecret("pass"));
conn = mysql("myDS", getSecret("user"), getSecret("pass"));
conn = sqlite("myDB.db");
ODBC DSN-Less Connection
Connect without a pre-configured datasource by passing a connection string directly:
dsnless = "Driver={MySQL ODBC 8.0 Unicode Driver};" + ...
"Server=dbtb09;Database=production;UID=" + getSecret("dbUser") + ...
";PWD=" + getSecret("dbPass");
conn = odbc(dsnless);
Create and Save a Data Source
Use databaseConnectionOptions to configure a datasource programmatically, then saveAsDataSource to persist it:
opts = databaseConnectionOptions("odbc", "MySQL");
opts = setoptions(opts, DataSourceName="myDS", ...
Server="dbtb09", DatabaseName="production", PortNumber=3306);
testConnection(opts, getSecret("dbUser"), getSecret("dbPass"));
saveAsDataSource(opts);
% Now connect using the saved datasource name
conn = odbc("myDS", getSecret("dbUser"), getSecret("dbPass"));
Read with RowFilter
rf = rowfilter(["Status", "Timestamp"]);
T = sqlread(conn, "orders", RowFilter=rf.Status == "Active" & rf.Timestamp > datetime(2024,1,1));
rowfilter requires column names as input. Access columns via property syntax (rf.ColumnName).
Read with Import Options
opts = databaseImportOptions(conn, "orders");
opts.SelectedVariableNames = ["OrderID", "Status", "Total"];
opts.RowFilter = opts.RowFilter.Total > 1000;
opts.ExcludeDuplicates = true;
T = sqlread(conn, "orders", opts);
Import options (opts) are a positional 3rd argument — not a name-value pair.
Write
data = table("Widget", 50, 9.99, VariableNames=["Product", "Qty", "Price"]);
sqlwrite(conn, "inventory", data);
Update
rf = rowfilter("ProductID");
newData = table(5.99, VariableNames="Price");
sqlupdate(conn, "inventory", newData, rf.ProductID == 42);
sqlupdate signature: sqlupdate(conn, tablename, data, filter) — the filter is a required positional 4th argument, not a name-value pair.
Transaction
conn.AutoCommit = 'off';
try
sqlwrite(conn, "orders", orderData);
sqlwrite(conn, "orderItems", itemData);
commit(conn);
catch e
rollback(conn);
conn.AutoCommit = 'on';
rethrow(e);
end
conn.AutoCommit = 'on';
ALWAYS use conn.AutoCommit = 'off' with commit(conn)/rollback(conn). Do not use raw SQL BEGIN/COMMIT/ROLLBACK via execute.
ORM — Define a Mappable Class
classdef Employee < database.orm.mixin.Mappable
properties (PrimaryKey)
EmployeeID int32
end
properties
Name string
Department string
end
properties (ColumnType = "date")
HireDate datetime
end
methods
function obj = Employee(id, name, dept, hireDate)
if nargin == 0
return;
end
obj.EmployeeID = id;
obj.Name = name;
obj.Department = dept;
obj.HireDate = hireDate;
end
end
end
ORM requirements:
- Inherit from
database.orm.mixin.Mappable
- Mark at least one property block
(PrimaryKey)
- Constructor must handle
nargin == 0 (ORM constructs empty objects)
- R2023b or later
(TableName = "name") on classdef only if the database table name differs from the class name (defaults to class name)
ORM — CRUD Operations
% Write
emp = Employee(1, "Alice", "Engineering", datetime(2024,3,15));
ormwrite(conn, emp);
% Read with filter
rf = rowfilter("Department");
engineers = ormread(conn, "Employee", RowFilter=rf.Department == "Engineering");
% Update
engineers(1).Department = "Data Science";
ormupdate(conn, engineers(1));
% Refresh from database
emp = ormread(conn, emp);
Bulk Write (Chunked)
sqlwrite has no BatchSize parameter. Chunk manually:
chunkSize = 50000;
numChunks = ceil(height(data) / chunkSize);
for c = 1:numChunks
startIdx = (c - 1) * chunkSize + 1;
endIdx = min(c * chunkSize, height(data));
sqlwrite(conn, "targetTable", data(startIdx:endIdx, :));
end
Multithreaded Write (R2026a+, requires Parallel Computing Toolbox)
For maximum throughput, use parfeval with per-thread connections. Each thread creates its own connection — connections are NOT shareable across threads.
data = parallel.pool.Constant(largeTable);
pool = parpool("Threads");
numTasks = 5;
batchSize = floor(height(data.Value) / numTasks);
startRow = 1;
endRow = 1 + batchSize;
writeFutures(1:numTasks) = parallel.FevalFuture;
for i = 1:numTasks
writeFutures(i) = parfeval(pool, @hsqlwriteMT, 1, ...
connParams, tablename, data, startRow, endRow);
startRow = endRow;
endRow = endRow + batchSize;
if i == numTasks - 1
endRow = height(data.Value) + 1;
end
end
results = writeFutures.fetchOutputs("UniformOutput", false);
function finished = hsqlwriteMT(connParams, tablename, data, startRow, endRow)
conn = mysql(connParams.user, connParams.pass, ...
Server=connParams.server, DatabaseName=connParams.db);
sqlwrite(conn, tablename, data.Value(startRow:endRow-1, :));
close(conn);
finished = true;
end
Supported: mysql(), postgresql(), sqlite(), ODBC. See references/multithreaded-io.md for the read pattern and common mistakes.
Common Mistakes
| Mistake |
Why It's Wrong |
Correct Approach |
rf = rowfilter("Table"); rf == "val" |
Passes table name; uses == on the filter object |
rf = rowfilter("Column"); rf.Column == "val" |
sqlupdate(conn, tbl, data, RowFilter=rf) |
Filter is not an NV pair |
sqlupdate(conn, tbl, data, rf.Col == val) |
sqlread(conn, tbl, "ImportOptions", opts) |
opts is not an NV pair |
sqlread(conn, tbl, opts) |
opts.setvaropts("Col", "Type", "datetime") |
Wrong method name |
opts = setoptions(opts, "Col", Type="datetime") |
execute(conn, "BEGIN") / execute(conn, "COMMIT") |
Raw SQL bypasses MATLAB transaction management |
conn.AutoCommit = 'off' + commit(conn) / rollback(conn) |
| DROP/DELETE/TRUNCATE without confirmation |
Can destroy data irreversibly |
Always ask user for explicit confirmation first |
Manual toTable/fromTable for object persistence |
Agent doesn't know ORM exists |
Use Mappable class with ormwrite/ormread/ormupdate |
Sharing a connection across parfeval threads |
Connections are NOT thread-safe |
Each thread creates its own connection in the helper function |
odbc("serverName", user, pass) |
Server name is not a datasource name |
Use odbc(dsnless) with a connection string, or create a datasource first with databaseConnectionOptions + saveAsDataSource |
Reference Cards
- See
references/sqlread-fetch.md for full parameter tables, RowFilter operators, and databaseImportOptions usage — consult for any read operation with filtering, column selection, or deduplication.
- See
references/sqlwrite-sqlupdate.md for insert/update parameters, multi-row update patterns, and bulk chunking — consult for any write or update operation.
- See
references/transactions.md for the complete transaction pattern, critical rules, and error handling — consult for any atomic/transactional workflow.
- See
references/orm.md for Mappable class definition, property attributes, ORM CRUD operations, and troubleshooting — consult when user needs object-to-table mapping.
- See
references/execute-storedproc.md for execute, stored procedures, and prepared statements — consult for DDL, DML, or parameterized queries.
- See
references/multithreaded-io.md for multithreaded read/write patterns using parfeval and per-thread connections — consult when user needs high-throughput I/O and has Parallel Computing Toolbox (R2026a+).
Copyright 2026 The MathWorks, Inc.
1---2name: matlab-use-database3description: "Reads from, writes to, and manages relational databases using MATLAB Database Toolbox. Use when connecting to databases, reading data with sqlread or fetch, filtering with rowfilter, writing with sqlwrite, updating with sqlupdate, executing SQL statements, managing transactions with commit and rollback, mapping MATLAB classes to tables with ORM (Mappable, ormread, ormwrite, ormupdate), or performing any database operation from MATLAB. Triggers on: database, SQL, sqlread, sqlwrite, sqlupdate, fetch, execute, rowfilter, RowFilter, ORM, Mappable, ormread, ormwrite, ormupdate, orm2sql, transaction, commit, rollback, Database Toolbox, PostgreSQL, MySQL, SQLite, SQL Server, Oracle, database connection, database table, query database, insert data, update rows, delete rows, stored procedure, prepared statement, odbc, databaseConnectionOptions, datasource, data source, DSN, connection string, multithreaded, parallel."4license: "https://www.mathworks.com/content/dam/mathworks/license/pmrl/license.md"5metadata:6 source: https://skillmd.com/skills/matlab/matlab-use-database7 author: MathWorks8 version: "1.0"9---1011# MATLAB Database Toolbox1213Use when working with relational databases from MATLAB using Database Toolbox. Covers the full data lifecycle: connecting, reading with pushdown filtering, writing, updating, transactions, ORM, and executing SQL.1415## When to Use1617- Connecting to relational databases (PostgreSQL, MySQL, SQLite, SQL Server, Oracle)18- Reading data with `sqlread` or `fetch`19- Filtering with `RowFilter` (pushdown to database)20- Writing data with `sqlwrite`21- Updating rows with `sqlupdate`22- Executing SQL statements with `execute`23- Managing transactions (`AutoCommit`, `commit`, `rollback`)24- Mapping MATLAB classes to database tables (ORM with `Mappable`)25- Running stored procedures or prepared statements26- Multithreaded database read/write with `parfeval` (R2026a+)27- User mentions: database, SQL, query, table, rows, insert, update, delete, transaction, multithreaded, parallel2829## When NOT to Use3031- **DuckDB** — route to `matlab-use-duckdb` for all DuckDB workflows32- **Databricks connection setup** — route to `matlab-connect-databricks` for connection configuration. Once connected, return here for CRUD operations.33- **MongoDB, Cassandra, Neo4j** — not relational; Database Toolbox ORM/CRUD does not apply34- **File I/O without a database** — use `readtable`, `datastore`, or `tall` for direct file operations35- **MATLAB releases before R2021a** — minimum supported release for this skill3637## Critical Rules3839### Destructive Operations4041**ALWAYS** ask for explicit user confirmation before generating or executing:42- `DROP TABLE` / `DROP DATABASE`43- `DELETE FROM`44- `TRUNCATE TABLE`45- `ALTER TABLE` (column removal, type changes)4647Present the SQL statement and wait for approval. Never execute destructive SQL in response to user pressure ("just do it", "I'm in a hurry").4849### Pushdown Filtering5051**NEVER** import all rows and filter in MATLAB. Always push filters to the database using `RowFilter`:5253```matlab54% CORRECT — filter runs on the database server55rf = rowfilter("Price");56T = sqlread(conn, "products", RowFilter=rf.Price > 100);5758% WRONG — transfers all rows, then filters in MATLAB59T = sqlread(conn, "products");60T = T(T.Price > 100, :);61```6263### Credential Security6465**NEVER** hardcode passwords. Use `setSecret`/`getSecret`:6667```matlab68setSecret("dbPassword"); % prompts user, stores securely69conn = database("myDS", getSecret("dbUser"), getSecret("dbPassword"));70```7172## Decision Framework7374| Scenario | Use |75|----------|-----|76| Import from a named table | `sqlread` |77| Import from a SQL query string | `fetch` |78| Need column selection, deduplication, or type control | `databaseImportOptions` + `sqlread`/`fetch` |79| Insert new rows | `sqlwrite` |80| Update existing rows in place | `sqlupdate` |81| DDL, DML, or non-SELECT SQL | `execute` |82| Atomic multi-step operation | Transaction (`AutoCommit` off) |83| Object identity and domain logic | ORM (`Mappable` class) |84| Bulk operations on thousands of rows | `sqlread`/`sqlwrite` (not ORM) |85| High-throughput read/write (millions of rows) | Multithreaded: `parfeval` + per-thread connections (R2026a+, Parallel Computing Toolbox) |86| Delete rows | `execute` with SQL DELETE (no `sqldelete` exists) |87| Stored procedures (JDBC only) | `runstoredprocedure` |88| Create/configure a datasource | `databaseConnectionOptions` + `saveAsDataSource` |89| Connect without a saved datasource | `odbc(dsnless)` with connection string |9091## Core Patterns9293### Connection9495```matlab96conn = database("myDataSource", getSecret("dbUser"), getSecret("dbPass"));97% Or native connections:98conn = postgresql("myDS", getSecret("user"), getSecret("pass"));99conn = mysql("myDS", getSecret("user"), getSecret("pass"));100conn = sqlite("myDB.db");101```102103### ODBC DSN-Less Connection104105Connect without a pre-configured datasource by passing a connection string directly:106107```matlab108dsnless = "Driver={MySQL ODBC 8.0 Unicode Driver};" + ...109 "Server=dbtb09;Database=production;UID=" + getSecret("dbUser") + ...110 ";PWD=" + getSecret("dbPass");111conn = odbc(dsnless);112```113114### Create and Save a Data Source115116Use `databaseConnectionOptions` to configure a datasource programmatically, then `saveAsDataSource` to persist it:117118```matlab119opts = databaseConnectionOptions("odbc", "MySQL");120opts = setoptions(opts, DataSourceName="myDS", ...121 Server="dbtb09", DatabaseName="production", PortNumber=3306);122testConnection(opts, getSecret("dbUser"), getSecret("dbPass"));123saveAsDataSource(opts);124125% Now connect using the saved datasource name126conn = odbc("myDS", getSecret("dbUser"), getSecret("dbPass"));127```128129### Read with RowFilter130131```matlab132rf = rowfilter(["Status", "Timestamp"]);133T = sqlread(conn, "orders", RowFilter=rf.Status == "Active" & rf.Timestamp > datetime(2024,1,1));134```135136`rowfilter` requires **column names** as input. Access columns via **property syntax** (`rf.ColumnName`).137138### Read with Import Options139140```matlab141opts = databaseImportOptions(conn, "orders");142opts.SelectedVariableNames = ["OrderID", "Status", "Total"];143opts.RowFilter = opts.RowFilter.Total > 1000;144opts.ExcludeDuplicates = true;145T = sqlread(conn, "orders", opts);146```147148Import options (`opts`) are a **positional 3rd argument** — not a name-value pair.149150### Write151152```matlab153data = table("Widget", 50, 9.99, VariableNames=["Product", "Qty", "Price"]);154sqlwrite(conn, "inventory", data);155```156157### Update158159```matlab160rf = rowfilter("ProductID");161newData = table(5.99, VariableNames="Price");162sqlupdate(conn, "inventory", newData, rf.ProductID == 42);163```164165`sqlupdate` signature: `sqlupdate(conn, tablename, data, filter)` — the filter is a **required positional 4th argument**, not a name-value pair.166167### Transaction168169```matlab170conn.AutoCommit = 'off';171try172 sqlwrite(conn, "orders", orderData);173 sqlwrite(conn, "orderItems", itemData);174 commit(conn);175catch e176 rollback(conn);177 conn.AutoCommit = 'on';178 rethrow(e);179end180conn.AutoCommit = 'on';181```182183**ALWAYS** use `conn.AutoCommit = 'off'` with `commit(conn)`/`rollback(conn)`. Do not use raw SQL `BEGIN`/`COMMIT`/`ROLLBACK` via `execute`.184185### ORM — Define a Mappable Class186187```matlab188classdef Employee < database.orm.mixin.Mappable189190 properties (PrimaryKey)191 EmployeeID int32192 end193194 properties195 Name string196 Department string197 end198199 properties (ColumnType = "date")200 HireDate datetime201 end202203 methods204 function obj = Employee(id, name, dept, hireDate)205 if nargin == 0206 return;207 end208 obj.EmployeeID = id;209 obj.Name = name;210 obj.Department = dept;211 obj.HireDate = hireDate;212 end213 end214end215```216217ORM requirements:218- Inherit from `database.orm.mixin.Mappable`219- Mark at least one property block `(PrimaryKey)`220- Constructor must handle `nargin == 0` (ORM constructs empty objects)221- R2023b or later222- `(TableName = "name")` on classdef only if the database table name differs from the class name (defaults to class name)223224### ORM — CRUD Operations225226```matlab227% Write228emp = Employee(1, "Alice", "Engineering", datetime(2024,3,15));229ormwrite(conn, emp);230231% Read with filter232rf = rowfilter("Department");233engineers = ormread(conn, "Employee", RowFilter=rf.Department == "Engineering");234235% Update236engineers(1).Department = "Data Science";237ormupdate(conn, engineers(1));238239% Refresh from database240emp = ormread(conn, emp);241```242243### Bulk Write (Chunked)244245`sqlwrite` has no `BatchSize` parameter. Chunk manually:246247```matlab248chunkSize = 50000;249numChunks = ceil(height(data) / chunkSize);250for c = 1:numChunks251 startIdx = (c - 1) * chunkSize + 1;252 endIdx = min(c * chunkSize, height(data));253 sqlwrite(conn, "targetTable", data(startIdx:endIdx, :));254end255```256257### Multithreaded Write (R2026a+, requires Parallel Computing Toolbox)258259For maximum throughput, use `parfeval` with per-thread connections. Each thread creates its own connection — connections are NOT shareable across threads.260261```matlab262data = parallel.pool.Constant(largeTable);263pool = parpool("Threads");264numTasks = 5;265batchSize = floor(height(data.Value) / numTasks);266startRow = 1;267endRow = 1 + batchSize;268269writeFutures(1:numTasks) = parallel.FevalFuture;270for i = 1:numTasks271 writeFutures(i) = parfeval(pool, @hsqlwriteMT, 1, ...272 connParams, tablename, data, startRow, endRow);273 startRow = endRow;274 endRow = endRow + batchSize;275 if i == numTasks - 1276 endRow = height(data.Value) + 1;277 end278end279results = writeFutures.fetchOutputs("UniformOutput", false);280281function finished = hsqlwriteMT(connParams, tablename, data, startRow, endRow)282 conn = mysql(connParams.user, connParams.pass, ...283 Server=connParams.server, DatabaseName=connParams.db);284 sqlwrite(conn, tablename, data.Value(startRow:endRow-1, :));285 close(conn);286 finished = true;287end288```289290Supported: `mysql()`, `postgresql()`, `sqlite()`, ODBC. See `references/multithreaded-io.md` for the read pattern and common mistakes.291292## Common Mistakes293294| Mistake | Why It's Wrong | Correct Approach |295|---------|---------------|-----------------|296| `rf = rowfilter("Table"); rf == "val"` | Passes table name; uses `==` on the filter object | `rf = rowfilter("Column"); rf.Column == "val"` |297| `sqlupdate(conn, tbl, data, RowFilter=rf)` | Filter is not an NV pair | `sqlupdate(conn, tbl, data, rf.Col == val)` |298| `sqlread(conn, tbl, "ImportOptions", opts)` | opts is not an NV pair | `sqlread(conn, tbl, opts)` |299| `opts.setvaropts("Col", "Type", "datetime")` | Wrong method name | `opts = setoptions(opts, "Col", Type="datetime")` |300| `execute(conn, "BEGIN")` / `execute(conn, "COMMIT")` | Raw SQL bypasses MATLAB transaction management | `conn.AutoCommit = 'off'` + `commit(conn)` / `rollback(conn)` |301| DROP/DELETE/TRUNCATE without confirmation | Can destroy data irreversibly | Always ask user for explicit confirmation first |302| Manual `toTable`/`fromTable` for object persistence | Agent doesn't know ORM exists | Use `Mappable` class with `ormwrite`/`ormread`/`ormupdate` |303| Sharing a connection across `parfeval` threads | Connections are NOT thread-safe | Each thread creates its own connection in the helper function |304| `odbc("serverName", user, pass)` | Server name is not a datasource name | Use `odbc(dsnless)` with a connection string, or create a datasource first with `databaseConnectionOptions` + `saveAsDataSource` |305306## Reference Cards307308- See `references/sqlread-fetch.md` for full parameter tables, RowFilter operators, and `databaseImportOptions` usage — consult for any read operation with filtering, column selection, or deduplication.309- See `references/sqlwrite-sqlupdate.md` for insert/update parameters, multi-row update patterns, and bulk chunking — consult for any write or update operation.310- See `references/transactions.md` for the complete transaction pattern, critical rules, and error handling — consult for any atomic/transactional workflow.311- See `references/orm.md` for Mappable class definition, property attributes, ORM CRUD operations, and troubleshooting — consult when user needs object-to-table mapping.312- See `references/execute-storedproc.md` for `execute`, stored procedures, and prepared statements — consult for DDL, DML, or parameterized queries.313- See `references/multithreaded-io.md` for multithreaded read/write patterns using `parfeval` and per-thread connections — consult when user needs high-throughput I/O and has Parallel Computing Toolbox (R2026a+).314315----316317Copyright 2026 The MathWorks, Inc.318319----