【mysql】深入探索mysql中的各种约束条件

慈云数据 6个月前 (05-30) 技术支持 37 0

在这里插入图片描述

✨✨ 欢迎大家来到景天科技苑✨✨

🎈🎈 养成好习惯,先赞后看哦~🎈🎈

🏆 作者简介:景天科技苑

🏆《头衔》:大厂架构师,华为云开发者社区专家博主,阿里云开发者社区专家博主,CSDN全栈领域优质创作者,掘金优秀博主,51CTO博客专家等。

🏆《博客》:Python全栈,前后端开发小程序开发人工智能,js逆向,App逆向,网络系统安全,数据分析,Django,fastapi,flask等框架,linux,shell脚本等实操经验,网站搭建数据库等分享。

所属的专栏:MySQL数据库入门,进阶应用实战必备

景天的主页:景天科技苑

文章目录

  • mysql约束
    • unsigned 无符号
    • not null : 不为空
    • default : 默认值
    • unique: 唯一约束
    • primary key: 主键
    • auto_increment: 自增加一
    • zerofill : 零填充 (配合int使用,不够5位拿0来填充)

      mysql约束

      在mysql中对编辑的数据进行类型的限制,不满足约束条件的报错

      unsigned   :    无符号
      not null   :       不为空
      default    :       默认值
      unique     :      唯一值,加入唯一索引
      (索引相当于字典目录,索引的提出是为了加快速度,一味地乱加索引不会提高查询效率)
      primary key:    主键
      auto_increment: 自增加一 ,必须设置了主键才能设置该参数
      zerofill   :    零填充
      foreign key:    外键
      

      约束在写sql时,放在数据类型的后面,如下,放在int的后面

      字段名 类型 约束

      unsigned 无符号

      create table t3(id int unsigned);
      insert into t3 values(-1); error
      insert into t3 values(4000000000); success
      

      设置无符号位约束,插入负值就报错

      在这里插入图片描述

      not null : 不为空

      create table t4(id int not null , name varchar(11));
      insert into t4 values(1,"张宇");
      insert into t4 values(null,"张宇"); error
      insert into t4(name) values("李四"); error
      

      设置不为空约束,插入空就报错

      在这里插入图片描述

      NULL值是处于0和1之间的某个值,他也表示一个值,只不过这个值是NULL值,而不是0。

      在进行计算的时候,1与NULL则结果为NULL。而0与NULL则结果为0。

      1或NULL则结果为1,0或NULL则结果为NULL;可见NULL值是介于0和1之间的值。

      另外非NULL既不是1也不是0,还是NULL

      default : 默认值

      create table t5(id int not null  , name varchar(11) default "沈思雨" );
      insert into t5 values(1,null);
      insert into t5(id) values(2);
      

      设置了默认值后,插入时填入值,就是设置的值,非全列插入时,不写该字段的值,就用默认值

      在这里插入图片描述

      create table t5_2(id int not null  default "1111" , name varchar(11) default "沈思雨" );
      insert into t5_2 values(); # 在values里面不写值,默认使用默认值;
      

      在这里插入图片描述

      unique: 唯一约束

      加入唯一索引(索引的提出是为了加快速度,一味地乱加索引不会提高查询效率,索引是有一个文件来存索引)

      唯一 可为null 标记成: UNI

      create table t6(id int unique , name char(10) default "赵万里" );
      insert into t6(id) values(1);
      insert into t6(id) values(1); error
      insert into t6(id) values(null);
      insert into t6(id) values(null); # id变成了多个null
      

      如果要删除null的字段,可以用 where 字段 is null 来删

      唯一性约束,可以有多个null值,不违背唯一性约束

      在这里插入图片描述

      primary key: 主键

      [ 唯一 + 不为null ] PRI 标记数据的唯一特征

      一个表中,只能设置一个字段为一个主键,unique唯一约束可以设置多个

      创建主键

      create table t7(id int primary key , name varchar(10) default "赵沈阳");
      insert into t7(id) values(1);
      insert into t7(id) values(1); error 
      insert into t7(id) values(null); error
      

      设了主键,该字段不能重复,不能为空

      在这里插入图片描述

      unique + not null => PRI

      create table t8(id int unique not null ,  name varchar(10) default "赵沈阳" );
      

      设置了唯一性约束,且不为null,功能就跟primary key一样了

      在这里插入图片描述

      如果没有设置primary key,设置了unique not null ,默认把unique +not null 设置的字段设为主键

      在这里插入图片描述

      primary key / unique + not null => 优先把primary key 作为主键;

      create table t9(id1 int unique not null ,  id2 int primary key );
      

      同时设置了unique +not null 和 primary key 。优先把primary key 作为主键

      在这里插入图片描述

      一个表只能设置单个字段为一个主键;

      create table t10(id1 int  primary key  ,  id2 int primary key ); error
      

      在这里插入图片描述

      auto_increment: 自增加一

      一般配合 主键或者unique 使用

      create table t11(id int primary key auto_increment , name varchar(255) default "敬文栋");
      insert into t11 values(1,"张三");
      insert into t11 values(null,"李四");
      insert into t11(id) values(null);
      # 使用默认值或者自增插入数据
      insert into t11 values();
      

      在这里插入图片描述

      删除数据,这是删除所有数据

      delete from t11;

      删除数据 + 重置id

      truncate table t11;

      主键自增,可以用0,null,default占位

      在这里插入图片描述

      在这里插入图片描述

      删除一条数据后,如果再添加不想主键从下一个开始,需要在添加之前,复位主键

      删除数据后,执行下面的sql

      如果是中途删除,先查看一下目前的auto_increment

      show create table student;
      | student | CREATE TABLE `student` (
        `id` int NOT NULL AUTO_INCREMENT,
        `name` varchar(20) NOT NULL,
        `age` int NOT NULL,
        `birthday` date DEFAULT NULL,
        `is_del` tinyint DEFAULT '0',
        `height` decimal(3,2) DEFAULT NULL,
        `cls_id` varchar(6) NOT NULL,
        PRIMARY KEY (`id`),
        KEY `fk_class` (`cls_id`),
        CONSTRAINT `fk_class` FOREIGN KEY (`cls_id`) REFERENCES `class` (`id`)
      ) ENGINE=InnoDB AUTO_INCREMENT=20 DEFAULT CHARSET=utf8 |
      

      AUTO_INCREMENT=几 下次插入时就从几开始递增

      ALTER TABLE (表名) AUTO_INCREMENT = 1;

      在这里插入图片描述

      zerofill : 零填充 (配合int使用,不够5位拿0来填充)

      create table t12(id int(5) zerofill);
      insert into t12 values(1234567);
      

      位数超了之后,按写入的数据直接插入

      在这里插入图片描述

      insert into t12 values(12);
      

      位数不足,前面补0

      在这里插入图片描述

微信扫一扫加客服

微信扫一扫加客服

点击启动AI问答
Draggable Icon