1 Create table

Tables are database objects that contain all the information in a database. In tables, data is logically organized in a row and column format like a spreadsheet. For example, let’s say that you want to create a table called Cities. This table will have four (04) columns:

  • code
  • city_name
  • country_name
  • official_language

Notice, that next to the column names there is the data types of each column. Then, we're gonna add some values to our table.

Copy
<xsql-script>
    <body> 
        <nativesql>
            !DROP TABLE m_tb_cities; 
            CREATE TABLE m_tb_cities (
                code                char(4)         not null,
                city_name           varchar(60)     not null,
                country_name        varchar(60)     not null,
                official_language   varchar(60)     not null
            );
            ALTER TABLE m_tb_cities LOCK MODE (ROW);

            INSERT INTO m_tb_cities VALUES ('0001',  'Lima',            'Perú',          'Spanish');
            INSERT INTO m_tb_cities VALUES ('0002',  'Bogota',          'Colombia',      'Spanish');  
            INSERT INTO m_tb_cities VALUES ('0003',  'Montreal',        'Canadá',        'French');  
            INSERT INTO m_tb_cities VALUES ('0004',  'Río de Janeiro',  'Brasil',        'Portuguese');   
        </nativesql> 
        
        <print.sql head='true'>
            <select>
                <columns>*</columns>
                <from table='m_tb_cities' />
            </select>
        </print.sql>  
        
        <nativesql>
            !DROP TABLE m_tb_cities;         
        </nativesql> 
    </body>
</xsql-script>
+----+----------------------------------------+----------------------------------------+----------------------------------------+
|code|city_name                               |country_name                            |official_language                       |
+----+----------------------------------------+----------------------------------------+----------------------------------------+
|0001|Lima                                    |Perú                                    |Spanish                                 |
|0002|Bogota                                  |Colombia                                |Spanish                                 |
|0003|Montreal                                |Canadá                                  |French                                 |
|0004|Río de Janeiro                          |Brazil                                  |Portuguese                               |
+----+----------------------------------------+----------------------------------------+----------------------------------------+
Copy
Ax.db.execute(`DROP TABLE IF EXISTS m_table_cities;`); 
Ax.db.execute(`
    CREATE TABLE m_table_cities (
            code                char(4)      not null,
            city_name           varchar(60)  not null,
            country_name        varchar(60)  not null,
            official_language   varchar(60)  not null
    );           
`); 
Ax.db.execute(`ALTER TABLE m_table_cities LOCK MODE (ROW);`); 

Ax.db.execute(`INSERT INTO m_table_cities  VALUES ('0001', 'Lima',           'Perú',     'Spanish'   )`);   
Ax.db.execute(`INSERT INTO m_table_cities  VALUES ('0002', 'Bogota',         'Colombia', 'Spanish'   )`);   
Ax.db.execute(`INSERT INTO m_table_cities  VALUES ('0003', 'Montreal',       'Canadá',   'French'    )`);   
Ax.db.execute(`INSERT INTO m_table_cities  VALUES ('0004', 'Río de Janeiro', 'Brasil',   'Portuguese')`);   

console.log(Ax.db.executeQuery("SELECT * FROM m_table_cities").toMemory());

Ax.db.execute(`DROP TABLE IF EXISTS m_table_cities;`);
+--------------+----------------------------------------+----------------------------------------+----------------------------------------+
|m_table_cities|m_table_cities                          |m_table_cities                          |m_table_cities                          |
|code          |city_name                               |country_name                            |official_language                       |
|char(4)       |varchar(60)                             |varchar(60)                             |varchar(60)                             |
+--------------+----------------------------------------+----------------------------------------+----------------------------------------+
|0001          |Lima                                    |Perú                                    |Spanish                                 |
|0002          |Bogota                                  |Colombia                                |Spanish                                 |
|0003          |Montreal                                |Canadá                                  |French                                  |
|0004          |Río de Janeiro                          |Brazil                                  |Portuguese                              |
+--------------+----------------------------------------+----------------------------------------+---------------------------------------+

1.1 Temporary Table

Temporary tables are stored in tempdb. You can created a explicit temporary table, named automatically, like this:

Copy
<xsql-script>
    <body>
        <table name='@TMP_systb' temp='yes'>
            <column name='tabname' type='varchar(20)'  required='y' />
            <column name='rowsize' type='integer'      required='y' />
        </table>

        <set name='m_tabid'>2</set>

        <insert table='@TMP_systb' columns='tabname, rowsize'>
            <select>
                <columns>
                    tabname, rowsize
                </columns>
                <from table='systables' alias='s' />
                <where>
                    s.tabid = <m_tabid />
                </where>
            </select>
        </insert>

        <print.sql head='true'>
            <select>
                <columns>*</columns>
                <from table='@TMP_systb' />
            </select>
        </print.sql>
    </body>
</xsql-script>
+--------------------+----------+
|tabname             |rowsize   |
+--------------------+----------+
|syscolumns          |157       |
+--------------------+----------+
Copy
function __getTempTable (pStrTmpTable, pBoolDropTable) {
    var mTmpTable = Ax.db.getTempTableName(`${pStrTmpTable}`);
    if (pBoolDropTable) {
       Ax.db.execute(`DROP TABLE IF EXISTS ${mTmpTable}`);
    }
    return mTmpTable;
 } 

var mTmpTbTest     = __getTempTable('TmpTbTest', true);

Ax.db.execute(`
    CREATE TEMP TABLE IF NOT EXISTS ${mTmpTbTest} (
        code      integer, 
        continent varchar(15)
    )
    WITH NO LOG;
`);

Ax.db.execute(`INSERT INTO ${mTmpTbTest} VALUES (0,"EUROPA")`);

 
console.log(Ax.db.executeQuery(`SELECT * FROM ${mTmpTbTest}`).toMemory());
+---------------+---------------+
|TmpTbTest_00537|TmpTbTest_00537|
|code           |continent      |
|int            |varchar(15)    |
+---------------+---------------+
|0              |EUROPA         |
+---------------+---------------+

Remenber when you leaving the script, temporary tables created will be automatically removed.

2 table.copy

You can copy the content of a table to another.

Copy
<xsql-script>
    <body>
        <nativesql>
            DROP TABLE IF EXISTS m_tb_cities;
            CREATE TABLE m_tb_cities (
                code                char(4)         not null,
                city_name           varchar(60)     not null,
                country_name        varchar(60)     not null,
                official_language   varchar(60)     not null
            );

            INSERT INTO m_tb_cities VALUES ('0001',  'Lima',            'Perú',     'Spanish'   );
            INSERT INTO m_tb_cities VALUES ('0002',  'Bogota',          'Colombia', 'Spanish'   );  
            INSERT INTO m_tb_cities VALUES ('0003',  'Montreal',        'Canadá',   'French'    );  
            INSERT INTO m_tb_cities VALUES ('0004',  'Río de Janeiro',  'Brasil',   'Portuguese');

            DROP TABLE IF EXISTS m_tb_cities_copy;
            CREATE TABLE m_tb_cities_copy (
                code                char(4)         not null,
                city_name           varchar(60)     not null,
                country_name        varchar(60)     not null,
                official_language   varchar(60)     not null
            );
        </nativesql>

        <set name='nrows'>
            <table.copy src="m_tb_cities" dst="m_tb_cities_copy" maxrows="3"/>
        </set>

        <println><nrows/> rows have been copied.</println> 

        <print.sql head='true'>
            <select>
                <columns>*</columns>
                <from table='m_tb_cities_copy' />
            </select>
        </print.sql>  

        <nativesql>
            DROP TABLE IF EXISTS m_tb_cities;         
            DROP TABLE IF EXISTS m_tb_cities_copy;         
        </nativesql> 
    </body>
</xsql-script>
2 rows have been copied.
+----+----------------------------------------+----------------------------------------+----------------------------------------+
|code|city_name                               |country_name                            |official_language                       |
+----+----------------------------------------+----------------------------------------+----------------------------------------+
|0001|Lima                                    |Perú                                    |Spanish                                 |
|0002|Bogota                                  |Colombia                                |Spanish                                 |
+----+----------------------------------------+----------------------------------------+----------------------------------------+
Copy
Ax.db.execute(`
        DROP TABLE IF EXISTS m_table_cities;
        CREATE TABLE m_table_cities (
            code                char(4)      not null,
            city_name           varchar(60)  not null,
            country_name        varchar(60)  not null,
            official_language   varchar(60)  not null
        ); 

        DROP TABLE IF EXISTS m_table_cities_copy;
        CREATE TABLE m_table_cities_copy (
            code                char(4)         not null,
            city_name           varchar(60)     not null,
            country_name        varchar(60)     not null,
            official_language   varchar(60)     not null
        );  

    `); 

Ax.db.execute(`INSERT INTO m_table_cities  VALUES ('0001', 'Lima',           'Perú',     'Spanish'   )`);   
Ax.db.execute(`INSERT INTO m_table_cities  VALUES ('0002', 'Bogota',         'Colombia', 'Spanish'   )`);   
Ax.db.execute(`INSERT INTO m_table_cities  VALUES ('0003', 'Montreal',       'Canadá',   'French'    )`);   
Ax.db.execute(`INSERT INTO m_table_cities  VALUES ('0004', 'Río de Janeiro', 'Brasil',   'Portuguese')`);   

Ax.db.execute(`INSERT INTO m_table_cities_copy
               SELECT code,
                      city_name,
                      country_name,
                      official_language
                FROM  m_table_cities
               WHERE  code <= '0002'
            `);

console.log(Ax.db.executeQuery(`SELECT * FROM m_table_cities_copy`));

Ax.db.execute(`DROP TABLE IF EXISTS m_table_cities;`);
Ax.db.execute(`DROP TABLE IF EXISTS m_table_cities_copy;`);
+-------------------+----------------------------------------+----------------------------------------+----------------------------------------+
|m_table_cities_copy|m_table_cities_copy                     |m_table_cities_copy                     |m_table_cities_copy                     |
|code               |city_name                               |country_name                            |official_language                       |
|char(4)            |varchar(60)                             |varchar(60)                             |varchar(60)                             |
+-------------------+----------------------------------------+----------------------------------------+----------------------------------------+
|0001               |Lima                                    |Perú                                    |Spanish                                 |
|0002               |Bogota                                  |Colombia                                |Spanish                                 |
+-------------------+----------------------------------------+----------------------------------------+----------------------------------------+

3 table.getColumnIndex

Return true or false in case the column will be a column index.

Copy
<xsql-script>
    <body>
        <println>
           Result : <table.getColumnLength table='systables' column='tabname' />
        </println>
    </body>
</xsql-script>
1
Copy
var refresh = false;

Ax.db.execute(`
    DROP TABLE IF EXISTS m_tbContinent;
    CREATE TABLE m_tbContinent (
        code            char(4)      not null,
        continent_name  varchar(50)  not null,
        population      integer      not null
    );       
`);  
    
var md = Ax.db.getMetaData().getTableMetaData("m_tbContinent", refresh);
    for  (var columnName of md.getColumnNames()) {
        console.log(`Is ${columnName} a column index? : ${md.isColumnIndex(columnName)}`);     
    }

Ax.db.execute(`DROP TABLE IF EXISTS m_tbContinent`);
Is code a column index? : false
Is continent_name a column index? : false
Is population a column index? : false

4 table.getColumnLength

The LENGTH functions return the length of char.

Copy
<xsql-script>
    <body> 
        <nativesql>
            DROP TABLE IF EXISTS m_tbContinent;
            CREATE TABLE m_tbContinent (
                code            char(4)         not null,
                continent_name  varchar(50)     not null,
                population      integer         not null
            );

            ALTER TABLE m_tbContinent LOCK MODE (ROW);
        </nativesql> 

        <println>
            Length of continent_name : <table.getColumnLength table='m_tbContinent' column='continent_name' />
        </println>

        <nativesql>
            !DROP TABLE m_tbContinent;         
        </nativesql> 
    </body>
</xsql-script>
Length of continent_name : 50
Copy
var refresh = false;

Ax.db.execute(`
    DROP TABLE IF EXISTS m_tbContinent;
    CREATE TABLE m_tbContinent (
        code            char(4)      not null,
        continent_name  varchar(50)  not null,
        population      integer      not null
    );       
`);  
    
var md = Ax.db.getMetaData().getTableMetaData("m_tbContinent", refresh);
    for  (var columnName of md.getColumnNames()) {
        if (columnName == 'continent_name'){
            console.log(`Length of ${columnName} : ${md.getColumnSize(columnName)}`);
        }
    }

Ax.db.execute(`DROP TABLE IF EXISTS m_tbContinent`);
Length of continent_name : 50

5 table.getColumnNullable

Returns a boolean indicating if the columnn allows nulls. In case the does not exist, an exception is issued.

Copy
<xsql-script>
    <body> 
        <nativesql>
            DROP TABLE IF EXISTS m_tbContinent;
            CREATE TABLE m_tbContinent (
                code            char(4)         not null,
                continent_name  varchar(50)     not null,
                population      integer         not null
            );

            ALTER TABLE m_tbContinent LOCK MODE (ROW);
        </nativesql> 

        <println>
            Column continent_name allow nulls : <table.getColumnNullable  table='m_tbContinent' column='continent_name' />
        </println>

        <nativesql>
            DROP TABLE IF EXISTS m_tbContinent;         
        </nativesql> 
    </body>
</xsql-script>
Column continent_name allow nulls : false
Copy
var refresh = false;

Ax.db.execute(`
    DROP TABLE IF EXISTS m_tbContinent;
    CREATE TABLE m_tbContinent (
        code            char(4)      not null,
        continent_name  varchar(50)  not null,
        population      integer      not null
    );       
`);  
    
var md = Ax.db.getMetaData().getTableMetaData("m_tbContinent", refresh);
    for  (var columnName of md.getColumnNames()) {
        if (columnName == 'continent_name'){
            console.log(`Column ${columnName} allow nulls : ${md.isColumnNullable(columnName)}`);
        }
    }

Ax.db.execute(`DROP TABLE IF EXISTS m_tbContinent`);
Column continent_name allow nulls : false

6 table.getColumnType

Returns the column's type number

Copy
<xsql-script>
    <body> 
        <nativesql>
            DROP TABLE IF EXISTS m_tbContinent; 
            CREATE TABLE m_tbContinent (
                code            char(4)         not null,
                continent_name  varchar(50)     not null,
                population      integer         not null
            );

            ALTER TABLE m_tbContinent LOCK MODE (ROW);
        </nativesql> 

        <println>
           Column type number (continent_name) : <table.getColumnType table='m_tbContinent' column='continent_name' />
        </println>

        <nativesql>
            DROP TABLE m_tbContinent;         
        </nativesql> 
    </body>
</xsql-script>
Column type number (continent_name) : 12
Copy
var refresh = false;

Ax.db.execute(`
    DROP TABLE IF EXISTS m_tbContinent;
    CREATE TABLE m_tbContinent (
        code            char(4)      not null,
        continent_name  varchar(50)  not null,
        population      integer      not null
    );       
`);  
    
var md = Ax.db.getMetaData().getTableMetaData("m_tbContinent", refresh);
    for  (var columnName of md.getColumnNames()) {
        if (columnName == 'continent_name'){
            console.log(`Column type number (${columnName}) : ${md.getColumnType(columnName)}`);
        }
    }

Ax.db.execute(`DROP TABLE IF EXISTS m_tbContinent`);
Column type number (continent_name) : 12

7 table.getTableType

Returns the type of table. In case you're using XSQL the typical types are: table, view, system table, global temporary, between others. However in server- side script we have a function for each type that allow us to know what kind of table is, returning us true or false.

Copy
<xsql-script>
    <body>
        <println>
            Table type: <table.getTableType table='systables' />
        </println>
    </body>
</xsql-script>
Table type: SYSTEM TABLE
Copy
var refresh = false;

Ax.db.execute(`
    DROP TABLE IF EXISTS m_tbContinent;
    CREATE TABLE m_tbContinent (
        code            char(4)      not null,
        continent_name  varchar(50)  not null,
        population      integer      not null
    );       
`); 

var md = Ax.db.getMetaData().getTableMetaData("m_tbContinent", refresh);

console.log("Is a synonym? : " + md.isSynonym());

console.log("Is a table? : " + md.isTable());

console.log("Is a temporal table? : " + md.isTemp());

console.log("Is a View? : " + md.isView());

Ax.db.execute(`DROP TABLE IF EXISTS m_tbContinent`);
Is a synonym? : false
Is a table? : true
Is a temporal table? : false
Is a View? : false

8 table.lock

The lock element limits access to a database table in two ways: "Exclusive" or "Share" mode. A table in shared mode gives other processes read access to the table but denies write access, meanwhile in exclusive mode denies other processes both read and write access to the table.

Copy
<xsql-script>
    <body> 
        <nativesql>
            DROP TABLE IF EXISTS m_tbContinent;
            CREATE TABLE m_tbContinent (
                code            char(4)         not null,
                continent_name  varchar(50)     not null,
                population      integer         not null
            );

            INSERT INTO m_tbContinent VALUES ('0001',  'Europe',  10 );
            INSERT INTO m_tbContinent VALUES ('0002',  'America', 20); 
        </nativesql> 

        <table.lock table='m_tbContinent' mode='share' /> 

        <print.sql head='true'>
            <select>
                <columns>*</columns>
                <from table='m_tbContinent' />
            </select>
        </print.sql>

        <nativesql>
            DROP TABLE m_tbContinent;         
        </nativesql> 
    </body>
</xsql-script>
+----+----------------------------------------+----------+
|code|continent_name                          |population|
+----+----------------------------------------+----------+
|0001|Europe                                  |30        |
|0002|America                                 |20        |
+----+----------------------------------------+----------+
Copy
Ax.db.execute(`
    DROP TABLE IF EXISTS m_tbContinent;
    CREATE TABLE m_tbContinent (
        code            char(4)      not null,
        continent_name  varchar(30)  not null,
        population      integer      not null
    );       
`); 

Ax.db.lockTable("m_tbContinent", "share");

Ax.db.execute(`INSERT INTO m_tbContinent  VALUES ('0001', 'Europe',  30)`);   
Ax.db.execute(`INSERT INTO m_tbContinent  VALUES ('0002', 'America', 20)`);   

console.log( Ax.db.executeQuery(`SELECT * FROM m_tbContinent`));

Ax.db.execute(`DROP TABLE IF EXISTS m_tbContinent;`);
+-------------+------------------------------+-------------+
|m_tbContinent|m_tbContinent                 |m_tbContinent|
|code         |continent_name                |population   |
|char(4)      |varchar(30)                   |int          |
+-------------+------------------------------+-------------+
|0001         |Europe                        |30           |
|0002         |America                       |20           |
+-------------+------------------------------+-------------+