altertable Back | Forward | Home

'Language Reference'

User Functions

Database Functions

Data Manipulation Functions

Table Functions
  • showtables

  • createtable

  • droptable

  • altertable

  • describe

  • table_count

  • empty_cache

  • table_exists

  • last_insert_id


  • Error Handling Functions

    << Last Section ( droptable ) altertable Next Section ( describe ) >>
    Usage void altertable ( array ('table' => $table, 'action' => $action, 'name' => $column, 'values' => $values [, 'after' => $afterColumn [, 'db' => $db]]) )
    Purpose To alter a txtSQL-table's column definitions
    Availability txtSQL >= 2.2.2 RC2

    This function will alter a txtSQL-$table's column defintions. It will only work with the specified column, which is $column. The $action can be either

  • insert- Inserts a new column, $column, and if specified, after the column $afterColumn

  • modify- Modifies an existing $column

  • drop- Drops an existing $column

  • rename col- Renames a $column. Expects array('name' => $newcolname) in the $values

  • rename table- Renames a $table. Expects array('name' => $newTableName) in the $values

  • addkey- Sets $column as the primary key ( must be integer and auto_increment ). Expects array('name' => $colName) in the $values

  • dropkey- Does opposite of 'addkey'

  • The $values element is an array containing information about the column, it must be in the following format

         array( [$colType => $value]... ) unless otherwise noted above


    Important- To achieve the same results as this function before txtSQL 2.2.2 RC2, use the execute() function
    Note- For more information on data types, see the data types section, or for more information the column types, see the column types section
    Note- If $tablename does not already exists, txtSQL will issue an error
    Note- If no $db is specified and no database is already selected, txtSQL will issue an error

    Example 23: altertable() Inserting a column Copy to Clipboard
    <?php
    $sql
    ->altertable(array('db'     => 'testDB',
                           
    'table'  => 'testTable',
                           
    'name'   => 'id',
                           
    'action' => 'insert'
                           'values'
    => array(
                                   
    'auto_increment' => 1,
                                   
    'type' => 'int'
                           
    )));
    // Inserts a column named 'id' into table 'testTable' which is in database 'testDB'. The column is auto_increment, and of type int
    }

    ?>

    Example 24: altertable() Modifying a column Copy to Clipboard
    <?php
    $sql
    ->altertable(array('db'     => 'testDB',
                           
    'table'  => 'testTable',
                           
    'name'   => 'id',
                           
    'action' => 'modify'
                           'values'
    => array(
                                   
    'auto_increment' => 0,
                                   
    'type' => 'string'
                            
    )));
    // Modifies a column named 'id' into table 'testTable' which is in database 'testDB'. The column is no longer auto_increment, and is of type string now
    ?>

    Example 25: altertable() Dropping a column Copy to Clipboard
    <?php
    $sql
    ->altertable(array('db'     => 'testDB',
                           
    'table'  => 'testTable',
                           
    'name'   => 'id',
                           
    'action' => 'drop'
                           
    ));
    // Drops a column named 'id' from table 'testTable' which is in
    // database 'testDB'
    ?>

    User-Contributed Comments for:
    altertable()
    Faraz Ali <SaiyanM at hotmail dot com>
    July 30, 2004, 11:17 pm
    If you want to insert a new column, but at the begginning, specify $afterCol as 'primary' like so:
    <?php
    $sql
    ->altertable(array('after' => 'primary'...));
    ?>

    that way, it shows up as the first column