# Table Operations

> Part of the NocoDB documentation (Workflows > Scripts > Examples > Generic). Index of all pages: https://nocodb.com/llms.txt. Any docs page is available as Markdown by adding `.md` to its URL.

URL: https://nocodb.com/docs/workflows/scripts/examples/demo/table-operations
Last updated: 2026-10-03

A NocoDB script that checks if a table exists, creates a table and lists all tables in the base.

This script shows how to do these table operations in NocoDB:

* Check if a table exists.
* Create a new table if it does not exist.
* List all tables in the current base.

For details on each table operation and its configuration options, refer to the [NocoDB meta API documentation](https://nocodb.com/apis/v3/meta#tag/Tables).

```javascript
// NocoDB Script: Table Operations
// Demonstrates how to check for table existence, create a table, and list all tables in the base.

const tableName = 'Inventory';

// ------------------------------
// Check if table exists
// ------------------------------
let table = base.getTable(tableName);

if (!table) {
  output.text(`🔍 Table "${tableName}" not found. Creating it now...`);

  try {
    // ------------------------------
    // CREATE TABLE
    // ------------------------------
    table = await base.createTableAsync(tableName, [
      { name: 'Item ID', type: "SingleLineText" },
      { name: 'Item Name', type: "SingleLineText" },
      { name: 'Quantity', type: "Number" },
      {
        name: 'Category',
        type: "SingleSelect",
        options: {
          choices: [
            { title: 'Electronics', color: '#FFC107' },
            { title: 'Clothing', color: '#007BFF' },
            { title: 'Home & Garden', color: '#28A745' },
            { title: 'Books', color: '#6F42C1' },
            { title: 'Sports', color: '#DC3545' }
          ]
        }
      }
    ]);

    output.text(`✅ Table "${tableName}" created successfully!`);
  } catch (error) {
    output.text(`❌ Error creating table: ${error.message}`);
  }
} else {
  output.text(`ℹ️ Table "${tableName}" already exists.`);
}

// ------------------------------
// READ: List all tables in base
// ------------------------------
output.markdown('### 📋 Tables in this base');

const tableSummary = base.tables.map(t => ({
  'Table Name': t.name,
  'Table ID': t.id,
  'Fields': t.fields.length,
  'Views': t.views.length
}));

output.table(tableSummary);

```

---

## Related pages

- [Fields: Text-Based](https://nocodb.com/docs/workflows/scripts/examples/demo/field-text-based.md): A NocoDB script that creates and manages text-based fields.
- [Fields: Numerical](https://nocodb.com/docs/workflows/scripts/examples/demo/field-number-based.md): A NocoDB script that creates numerical fields for quantities, percentages, currency and more.
- [Fields: Date & Time](https://nocodb.com/docs/workflows/scripts/examples/demo/field-date-time.md): A NocoDB script that creates Date, Time, DateTime and Duration fields.
- [Fields: Select](https://nocodb.com/docs/workflows/scripts/examples/demo/field-select-based.md): A NocoDB script that creates single select and multi select fields to categorize data.
- [Fields: System](https://nocodb.com/docs/workflows/scripts/examples/demo/field-system.md): A NocoDB script that creates system fields to capture audit information automatically.
- [Fields: User & Collaborator](https://nocodb.com/docs/workflows/scripts/examples/demo/field-user.md): A NocoDB script that creates a User field to assign or reference users in records.
- [Fields: Virtual & Formula](https://nocodb.com/docs/workflows/scripts/examples/demo/field-virtual.md): A NocoDB script that adds calculated or derived data with virtual fields.
- [Fields: Miscellaneous](https://nocodb.com/docs/workflows/scripts/examples/demo/field-miscellaneous.md): A NocoDB script that creates fields for attachments, checkboxes, ratings and structured data.
