Skip to content
Oxford Harrison edited this page Nov 15, 2024 · 14 revisions

DOCSLANGCREATE


See APIS ➞ client.query(), database.createTable()

See related ➞ ALTER DATABASE ➞ Manage Tables

Create empty table:

// (a): SQL syntax
await client.query(
    `CREATE TABLE database_1.table_1 ()`,
    { desc: 'Create description' }
);
// (b): Function-based syntax
const table = await client.database('database_1').createTable(
    { name: `table_1`, columns: [] },
    { desc: 'Create description' }
);

Note

While the default function-based syntax may read "create table", you can imply the "view" kind by setting options.kind === 'view':

client.createTable(..., { desc: 'Create description', kind: 'view' });

Create with columns:

// (a): SQL syntax
await client.query(
    `CREATE TABLE database_1.table_1 (
        col_1 int PRIMARY KEY,
        col_2 varchar
    )`,
    { desc: 'Create description' }
);
// (b): Function-based syntax
const table = await client.database('database_1').createTable({
    name: 'table_1',
    columns: [
        { name: 'col_1', type: 'int', primaryKey: true },
        { name: 'col_2', type: 'varchar' }
    ]
}, { desc: 'Create description' });

The IF NOT EXISTS clause

Create with an EXISTS check:

// (a): SQL syntax
await client.query(
    `CREATE TABLE IF NOT EXISTS database_1.table_1 ()`,
    { desc: 'Create description' }
);
// (b): Function-based syntax
const table = await client.database('database_1').createTable(
    { name: `table_1`, columns: [] },
    { desc: 'Create description', ifNotExists: true }
);

The RETURNING clause

Return the resulting table schema:

// (a): SQL syntax
const schema = await client.query(
    `CREATE TABLE database_1.table_1 ()
    RETURNING SCHEMA`,
    { desc: 'Create description' }
);
// (b): Function-based syntax
const schema = await client.database('database_1').createTable(
    'table_1',
    { desc: 'Create description', returning: 'schema' }
);

See related ➞ table.schema()

Return the associated savepoint instance:

// (a): SQL syntax
const savepoint = await client.query(
    `CREATE TABLE database_1.table_1 ()
    RETURNING SAVEPOINT`,
    { desc: 'Create description' }
);
// (b): Function-based syntax
const savepoint = await client.database('database_1').createTable(
    'table_1',
    { desc: 'Create description', returning: 'savepoint' }
);

See related ➞ database.savepoint()

Clone this wiki locally