mysql not null default / default
not null default 说明不能是NULL, 并设置默认值
default 设置默认值 , 但值也可能是NULL
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | mysql> create table test (id int , name varchar (10) default 'a' , addr varchar (10) not null default 'b' ); Query OK, 0 rows affected (0.04 sec) mysql> desc test; + -------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | + -------+-------------+------+-----+---------+-------+ | id | int (11) | YES | | NULL | | | name | varchar (10) | YES | | a | | | addr | varchar (10) | NO | | b | | + -------+-------------+------+-----+---------+-------+ 3 rows in set (0.00 sec) mysql> insert into test id values (1); ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'id values(1)' at line 1 mysql> insert into test id values (1); ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'id values(1)' at line 1 mysql> insert into test(id) values (1); Query OK, 1 row affected (0.00 sec) mysql> insert into test(id) values (1); Query OK, 1 row affected (0.00 sec) mysql> insert into test(id) values (1); Query OK, 1 row affected (0.00 sec) mysql> select * from test; + ------+------+------+ | id | name | addr | + ------+------+------+ | 1 | a | b | | 1 | a | b | | 1 | a | b | + ------+------+------+ 3 rows in set (0.00 sec) mysql> insert into test(id, name ) values (1, null ); Query OK, 1 row affected (0.00 sec) mysql> select * from test; + ------+------+------+ | id | name | addr | + ------+------+------+ | 1 | a | b | | 1 | a | b | | 1 | a | b | | 1 | NULL | b | + ------+------+------+ 4 rows in set (0.00 sec) mysql> insert into test(id,addr) values (1, null ); ERROR 1048 (23000): Column 'addr' cannot be null mysql> |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步