Lesson 4: Create a database table

In this lesson, you create a simple table called Names, which contains two columns that have the following properties:

Column name Data type Allow null? Default Primary key?
ID UUID No None Yes
Name Varchar(254) No None No
  1. Add a DataAccess method to create the table.
    private void createDatabaseSchema()
    {
        try{
            _conn.schemaCreateBegin();
            ColumnSchema column_schema;
            TableSchema table_schema = _conn.createTable("Names");
            column_schema = table_schema.createColumn( "ID", Domain.UUID );
            column_schema.setDefault( ColumnSchema.COLUMN_DEFAULT_UNIQUE_ID);
            table_schema.createColumn( "Name", Domain.VARCHAR, 254 );
            IndexSchema index_schema =
                    table_schema.createPrimaryIndex("prime_keys");
            index_schema.addColumn("ID", IndexSchema.ASCENDING);           
            _conn.schemaCreateComplete();
        }
        catch( ULjException uex1){
            System.out.println( "ULjException: " + uex1.toString() );
        }
        catch( Exception ex1){
            System.out.println( "Exception: " + ex1.toString() );
        }
    }

    If the table already exists, an exception is thrown.

  2. Call the DataAccess.getDataAccess method.

    Add the following code before the method returns:

    _da.createDatabaseSchema();
    return _da;
  3. Run the application again in the simulator.
Altering the table schema

When you alter the table schema, for example, by adding a table definition, you must keep the following information in mind: