MySql 怎样修改某字段的属性?使其不自动增长??

解决方案 »

  1.   

    alter table tx modify id int not null ;就可以了如下。mysql> create table tx(
        ->  id int not null primary key auto_increment,
        ->  c1 int
        -> );
    Query OK, 0 rows affected (0.13 sec)mysql>
    mysql> insert into tx values (null,11);
    Query OK, 1 row affected (0.01 sec)mysql> insert into tx values (null,22);
    Query OK, 1 row affected (0.05 sec)mysql> select * from tx;
    +----+------+
    | id | c1   |
    +----+------+
    |  1 |   11 |
    |  2 |   22 |
    +----+------+
    2 rows in set (0.00 sec)mysql> alter table tx modify id int not null ;
    Query OK, 2 rows affected (0.42 sec)
    Records: 2  Duplicates: 0  Warnings: 0mysql> show create table tx \G
    *************************** 1. row ***************************
           Table: tx
    Create Table: CREATE TABLE `tx` (
      `id` int(11) NOT NULL,
      `c1` int(11) DEFAULT NULL,
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=latin1
    1 row in set (0.00 sec)mysql> select * from tx;
    +----+------+
    | id | c1   |
    +----+------+
    |  1 |   11 |
    |  2 |   22 |
    +----+------+
    2 rows in set (0.00 sec)mysql> insert into tx values (null,33);
    ERROR 1048 (23000): Column 'id' cannot be null
    mysql> insert into tx values (3,33);
    Query OK, 1 row affected (0.06 sec)mysql> select * from tx;
    +----+------+
    | id | c1   |
    +----+------+
    |  1 |   11 |
    |  2 |   22 |
    |  3 |   33 |
    +----+------+
    3 rows in set (0.00 sec)
    
      

  2.   

    mysql> show create table a;
    +-------+-----------------------------------------------------------------------
    --------------------------------------------------------------------------------
    ------------------------------------------------+
    | Table | Create Table                                                |
    +-------+-----------------------------------------------------------------------
    --------------------------------------------------------------------------------
    ------------------------------------------------+
    | a     | CREATE TABLE `a` (
      `id` int(255) NOT NULL AUTO_INCREMENT,
      `titile` varchar(255) NOT NULL,
      `num` int(255) NOT NULL,
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=latin1 |
    +-------+-----------------------------------------------------------------------
    --------------------------------------------------------------------------------
    ------------------------------------------------+
    1 row in set (0.03 sec)mysql> select * from a;
    +----+--------+-----+
    | id | titile | num |
    +----+--------+-----+
    |  1 | aa     |   4 |
    |  2 | pp     |   2 |
    |  3 | ii     |   2 |
    +----+--------+-----+
    3 rows in set (0.02 sec)mysql> alter table a modify column id int not null;
    Query OK, 3 rows affected (0.19 sec)
    Records: 3  Duplicates: 0  Warnings: 0mysql> select * from a;
    +----+--------+-----+
    | id | titile | num |
    +----+--------+-----+
    |  1 | aa     |   4 |
    |  2 | pp     |   2 |
    |  3 | ii     |   2 |
    +----+--------+-----+
    3 rows in set (0.02 sec)mysql> insert into a(titile,num) values('aaa',23);
    Query OK, 1 row affected, 1 warning (0.05 sec)mysql> select * from a;
    +----+--------+-----+
    | id | titile | num |
    +----+--------+-----+
    |  0 | aaa    |  23 |
    |  1 | aa     |   4 |
    |  2 | pp     |   2 |
    |  3 | ii     |   2 |
    +----+--------+-----+
    4 rows in set (0.00 sec)