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

DOCSLANGCREATE


See also ➞ ALTER DATABASE ➞ Manage Tables

Create empty table:

// (a): SQL syntax
const savepoint = await client.query(
    `CREATE TABLE database_1.table_1 ()`,
    { desc: 'Create description' }
);
// (b): Function-based syntax
const savepoint = 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 an IF NOT EXISTS check:

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

Create with columns:

// (a): SQL syntax
const savepoint = 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 savepoint = 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' });
Clone this wiki locally