create table if not exists myDATABASE.MYTABLE(id int)这里只说明了一个int型的名字为ID的列,我想说明这个ID是不可为空的,也就是必填的怎么写SQL语句里追加呢?
还有这个ID的备注说明,比如我要说明这个ID是用户的ID,就是一个文字的说明

解决方案 »

  1.   

    mysql> create table if not exists MYTABLE(id int not null);
    Query OK, 0 rows affected (0.09 sec)mysql> desc MYTABLE;
    +-------+---------+------+-----+---------+-------+
    | Field | Type    | Null | Key | Default | Extra |
    +-------+---------+------+-----+---------+-------+
    | id    | int(11) | NO   |     | NULL    |       |
    +-------+---------+------+-----+---------+-------+
    1 row in set (0.06 sec)mysql>
      

  2.   

    如果表已经存在,则你可以用 alter table MYTABLE modify id int not null; 来修改。
    mysql> create table if not exists MYTABLE(id int);
    Query OK, 0 rows affected (0.08 sec)mysql> desc MYTABLE;
    +-------+---------+------+-----+---------+-------+
    | Field | Type    | Null | Key | Default | Extra |
    +-------+---------+------+-----+---------+-------+
    | id    | int(11) | YES  |     | NULL    |       |
    +-------+---------+------+-----+---------+-------+
    1 row in set (0.02 sec)mysql> alter table MYTABLE modify id int not null;
    Query OK, 0 rows affected (0.14 sec)
    Records: 0  Duplicates: 0  Warnings: 0mysql> desc MYTABLE;
    +-------+---------+------+-----+---------+-------+
    | Field | Type    | Null | Key | Default | Extra |
    +-------+---------+------+-----+---------+-------+
    | id    | int(11) | NO   |     | NULL    |       |
    +-------+---------+------+-----+---------+-------+
    1 row in set (0.01 sec)mysql>
      

  3.   

    谢谢LS的达人,请问只用一句SQL语句怎么实现其功能?
      

  4.   


    mysql> create table if not exists MYTABLE(id int comment 'asdfasdf');
    Query OK, 0 rows affected (0.08 sec)mysql> show full columns from MYTABLE;
    +-------+---------+-----------+------+-----+---------+-------+---------------------------------+----------+
    | Field | Type    | Collation | Null | Key | Default | Extra | Privileges                      | Comment  |
    +-------+---------+-----------+------+-----+---------+-------+---------------------------------+----------+
    | id    | int(11) | NULL      | YES  |     | NULL    |       | select,insert,update,references | asdfasdf |
    +-------+---------+-----------+------+-----+---------+-------+---------------------------------+----------+
    1 row in set (0.01 sec)mysql>
      

  5.   

    alter table MYTABLE modify id int not null; 本来就是一句啊。