最新消息: 新版网站上线了!!!

MYSQL表的添加与删除字段

  • MYSQL表的添加与删除字段
    +---------+-----------------------+------+-----+---------+-------+
    | Field   | Type                  | Null | Key | Default | Extra |
    +---------+-----------------------+------+-----+---------+-------+
    | tagname | char(20)              | NO   | PRI | NULL    |       |
    | closed  | tinyint(1)            | NO   | MUL | 0       |       |
    | total   | mediumint(8) unsigned | NO   | MUL | NULL    |       |
    +---------+-----------------------+------+-----+---------+-------+
    添加一列自增的ID,如何添加?


    alter table cdb_tags add id int(6) default 0 not null auto_increment,add primary key(id);
    ERROR 1067 (42000): Invalid default value for 'id'

    alter table cdb_tags add id int(6) auto_increment,add primary key(id);
    ERROR 1068 (42000): Multiple primary key defined
    我们可以看出tagname是主键,所以不能再添加主键了。

    -------定义为unique key就可以了----
    alter table cdb_tags add id int(6) auto_increment,add unique key(id);
    Query OK, 8 rows affected (0.05 sec)
    Records: 8  Duplicates: 0  Warnings: 0
    -----------------------------------
    mysql> describe cdb_tags;
    +---------+-----------------------+------+-----+---------+----------------+
    | Field   | Type                  | Null | Key | Default | Extra          |
    +---------+-----------------------+------+-----+---------+----------------+
    | tagname | char(20)              | NO   | PRI | NULL    |                |
    | closed  | tinyint(1)            | NO   | MUL | 0       |                |
    | total   | mediumint(8) unsigned | NO   | MUL | NULL    |                |
    | id      | int(6)                | NO   | UNI | NULL    | auto_increment |
    +---------+-----------------------+------+-----+---------+----------------+
    4 rows in set (0.01 sec)

     

    ------删除id字段OK----------------
    alter table cdb_tags drop id;
    Query OK, 8 rows affected (0.08 sec)
    Records: 8  Duplicates: 0  Warnings: 0
    ----------------------------------

.....

转载请注明:谷谷点程序 » MYSQL表的添加与删除字段