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>
如果表已经存在,则你可以用 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>
谢谢LS的达人,请问只用一句SQL语句怎么实现其功能?
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>
alter table MYTABLE modify 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>
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>
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>