Database Guide


Creating and accessing tables

The following examples illustrate how to create a table named PEOPLE, which stores the name, address, and phone number of a customer. This example contains four blocks of code:

These examples create the table as follows:

  1. The sqlDef expression is a portion of an SQL statement that defines the columns for the PEOPLE table.
  2. The createTableNamed: method creates the table called PEOPLE using the column definition values in sqlDef.

Since each database manager implements data types that the others do not, or they have different names for the same things, you may find other exceptions if you use data types other than CHAR, VARCHAR, OR INT.

For IBM and ODBC database accesss, substitute table and column names and column data types in the sqlDef temporary variable definition. Evaluate the code that creates the table using the Execute command.

Note:Execute only one of the blocks of code for creating the table.

Evaluate the code for displaying table columns using the Display command.

The columnNames method returns an ordered collection containing all column names as follows:

OrderedCollection('NAME' 'STREET' 'CITY' 'STATE' 'ZIPCODE' 'PHONE')
"Establish a connection"
(AbtDatabaseConnectionSpec forDbmClass: AbtIbmCliDatabaseManager
     databaseName: 'SAMPLE') connectUsingAlias: 'SampleConSpec'
           logonSpec: (AbtDatabaseLogonSpec new 
             id: 'userid';  
             password: 'password';  
             server: '').
 
"Create a table in any database"
| sqlDef table connection |
sqlDef := '(NAME varchar(30) NOT NULL,',
' STREET varchar(20) null,',
' CITY varchar(20) null,',
' STATE   varchar(2) null,',
' ZIPCODE int null,',
' PHONE  varchar(13) null)'.
connection := AbtDbmSystem activeDatabaseConnectionWithAlias: 'SampleConSpec'.
table := connection createTableNamed: 'PEOPLE' definition: sqlDef.
 
"Return column names"
| connection |
connection := AbtDbmSystem activeDabataseConnectionWithAlias: 'SampleConSpec'.
(connection openTableNamed: 'PEOPLE')
           columnNames.


[ Top of Page | Previous Page | Next Page | Table of Contents | Index ]