zoukankan      html  css  js  c++  java
  • mysql not null default / default

    not null default 说明不能是NULL, 并设置默认值

    default 设置默认值 , 但值也可能是NULL

    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>
    
  • 相关阅读:
    WiFi QC 自动测试:ixChariot API初探
    WiFi QC 自动测试:Qt控制无线路由器
    树莓派实现只有一个按键的播放器(2)
    树莓派变身路由器
    树莓派实现一个下载机
    对于AP中为什么有4个WEP KEY的分析
    备份U盘分区表,未雨绸缪
    根文件系统理解
    kernel(一)编译体验
    kernel(二)源码浅析
  • 原文地址:https://www.cnblogs.com/perl6/p/7057616.html
Copyright © 2011-2022 走看看