zoukankan      html  css  js  c++  java
  • mysql --非空约束

    ---非空约束
    ---修饰的字段不能为空NULL
    
    mysql> create table user9(
        -> id int,
        -> name varchar(20) not null
        -> );
    Query OK, 0 rows affected (0.01 sec)
    
    mysql> desc user9;
    +-------+-------------+------+-----+---------+-------+
    | Field | Type        | Null | Key | Default | Extra |
    +-------+-------------+------+-----+---------+-------+
    | id    | int         | YES  |     | NULL    |       |
    | name  | varchar(20) | NO   |     | NULL    |       |
    +-------+-------------+------+-----+---------+-------+
    2 rows in set (0.00 sec)
    
    mysql> 
    
    mysql> insert into user9 (id) values (1);
    ERROR 1364 (HY000): Field 'name' doesn't have a default value
    mysql> 
    
    mysql> insert into user9 (id) values (1);
    ERROR 1364 (HY000): Field 'name' doesn't have a default value
    mysql> insert into user9 values(1,"zhangsan");
    Query OK, 1 row affected (0.00 sec)
    
    mysql> desc user9;
    +-------+-------------+------+-----+---------+-------+
    | Field | Type        | Null | Key | Default | Extra |
    +-------+-------------+------+-----+---------+-------+
    | id    | int         | YES  |     | NULL    |       |
    | name  | varchar(20) | NO   |     | NULL    |       |
    +-------+-------------+------+-----+---------+-------+
    2 rows in set (0.00 sec)
    
    mysql> select * from user9;
    +------+----------+
    | id   | name     |
    +------+----------+
    |    1 | zhangsan |
    +------+----------+
    1 row in set (0.00 sec)
    
    mysql> 
    
    
    ---再举个例子:
    
    mysql> create table user66(
        -> id int not null,
        -> name varchar(20)
        -> );
    Query OK, 0 rows affected (0.01 sec)
    
    mysql> desc user66;
    +-------+-------------+------+-----+---------+-------+
    | Field | Type        | Null | Key | Default | Extra |
    +-------+-------------+------+-----+---------+-------+
    | id    | int         | NO   |     | NULL    |       |
    | name  | varchar(20) | YES  |     | NULL    |       |
    +-------+-------------+------+-----+---------+-------+
    2 rows in set (0.01 sec)
    mysql> insert into user66(name) values("zhangsan");
    ERROR 1364 (HY000): Field 'id' doesn't have a default value
    mysql>
    
    mysql> insert into user66(id) values("9");
    Query OK, 1 row affected (0.00 sec)
    
    mysql> select * from user66;
    +----+------+
    | id | name |
    +----+------+
    |  9 | NULL |
    +----+------+
    1 row in set (0.00 sec)
    
    mysql> 
  • 相关阅读:
    排序算法
    各种容器
    avl树
    zhenya moves from parents
    maven 相关
    Spring Cloud 子项目介绍
    WebStorm 中 dva 项目用 start 命令需要不断重启项目问题
    git常用命令
    SQL 的各种 join 用法
    程序员成长过程
  • 原文地址:https://www.cnblogs.com/clairedandan/p/13276574.html
Copyright © 2011-2022 走看看