如果列恰好是MySQL类型,如何修改列?

时间:2015-12-09 14:33:39

标签: mysql alter-table

我有一个名为foregroundNight的表,它有一个sidebar_items列,类型为Type。我想将此列更改为enum('image', 'html')类型。我试过这个:

enum('image', 'html', 'structure')

它给了我

的错误
alter table sidebar_items modify Type Type enum('image', 'html', 'structure');

我也尝试过使用`Type`。我可以进行此查询吗?

或者,我可以通过以下方式解决问题:

  • 创建一个与alter table sidebar_items modify Type Type enum('image', 'html', 'structure') Error Code: 1064. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Type enum('image', 'html', 'structure')' at line 1
  • 结构相同的temp
  • 将记录从sidebar_items迁移到sidebar_items
  • 放弃temp
  • 使用sidebar_items
  • 的新类型重新创建sidebar_items
  • 将记录从Type表格迁移到temp
  • 删除sidebar_items

但是,我很想知道是否有更简单的解决方案,可能只有一个temp命令。

1 个答案:

答案 0 :(得分:4)

正确的语法是:

<?php

/*
 * DataTables example server-side processing script.
 *
 * Please note that this script is intentionally extremely simply to show how
 * server-side processing can be implemented, and probably shouldn't be used as
 * the basis for a large complex system. It is suitable for simple use cases as
 * for learning.
 *
 * See http://datatables.net/usage/server-side for full details on the server-
 * side processing requirements of DataTables.
 *
 * @license MIT - http://datatables.net/license_mit
 */

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * Easy set variables
 */

// DB table to use
$table = 'datatables_demo';

// Table's primary key
$primaryKey = 'id';

// Array of database columns which should be read and sent back to DataTables.
// The `db` parameter represents the column name in the database, while the `dt`
// parameter represents the DataTables column identifier. In this case simple
// indexes
$columns = array(
    array( 'db' => 'first_name', 'dt' => 0 ),
    array( 'db' => 'last_name',  'dt' => 1 ),
    array( 'db' => 'position',   'dt' => 2 ),
    array( 'db' => 'office',     'dt' => 3 ),
    array(
        'db'        => 'start_date',
        'dt'        => 4,
        'formatter' => function( $d, $row ) {
            return date( 'jS M y', strtotime($d));
        }
    ),
    array(
        'db'        => 'salary',
        'dt'        => 5,
        'formatter' => function( $d, $row ) {
            return '$'.number_format($d);
        }
    )
);

// SQL server connection information
$sql_details = array(
    'user' => '',
    'pass' => '',
    'db'   => '',
    'host' => ''
);


/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * If you just want to use the basic configuration for DataTables with PHP
 * server-side, there is no need to edit below this line.
 */

require( 'ssp.class.php' );

echo json_encode(
    SSP::simple( $_GET, $sql_details, $table, $primaryKey, $columns )
);

使用ALTER TABLE ... MODIFY命令,您无需指定列名称两次,但您需要ALTER TABLE sidebar_items MODIFY `Type` ENUM('image', 'html', 'structure');

  

您可以使用CHANGE old_col_name new_col_name重命名列   column_definition子句。为此,请指定旧列和新列   列和当前列的定义。例如,   要将INTEGER列从a重命名为b,您可以执行以下操作:

     

ALTER TABLE ... CHANGE

     

要更改列的类型但不更改名称,仍然会更改CHANGE语法   需要旧的和新的列名称,即使它们是相同的。对于   例如:

     

ALTER TABLE t1 CHANGE a b INTEGER;

     

您也可以使用MODIFY更改列的类型而不重命名:

     

ALTER TABLE t1 CHANGE b b BIGINT NOT NULL;

     

MODIFY是ALTER TABLE for Oracle兼容性的扩展。