MySQL -- 约束

 

    一、什么是约束

约束(constraint),也称为数据库约束,是为了保证数据的完整性、一致性、有效性的规则。

数据库约束可以限制无效的数据入到数据表中,提供了数据库层面的“安检”。

MySQL -- 约束

 

    二、默认约束

    1、作用:在插入记录时,如果不给该字段赋值,则使用默认值
                2、格式
                    字段名 数据类型 default 值,
                    create table t1(id int(3) zerofill,name char(20),sex enum("F","M","Secret") default "Secret",age tinyint unsigned)default charset=utf8;

MySQL -- 约束

 

    三、非空约束

                1、作用:不允许将该字段设置为NULL
                2、格式:字段名 数据类型 not null
                    create table t2(id int, name varchar(15) not null default "单挑王")default charset=utf8;

MySQL -- 约束

 

 

 

 

 

相关文章:

  • 2021-11-06
  • 2021-10-19
  • 2022-12-23
  • 2021-06-07
  • 2022-12-23
  • 2021-12-06
猜你喜欢
  • 2021-04-24
  • 2021-04-19
  • 2022-03-04
  • 2022-02-15
  • 2022-12-23
相关资源
相似解决方案