4.mysql表的约束

Mysql表的约束

mysql为上面要有约束?

约束本质是MySQL通过限制用户操作的方式,来维护数据本身的权限的一套方案

本质上MySQL是一套数据存储解决方案,处理解决基本的数据存储功能之外,还要尽可能保证数据的安全性,减少数据误操作

mysql如何约束?

约束也是一套整体的约束方案

约束的体现,不仅仅可以体现在数据库层面,在我们进行用户业务逻辑编码的时候,我们其实也可以添加约束(判断),不满足就不让往数据库插入

数据库层面上,编码层面上也是可以提现的

空属性
null(默认的)not null(不为空)
数据库默认字段为空,但是实际开发中,尽可能保证字段不为空,因为数据为空不能参与运算

查看空字段

select null;

image-20220418155214678

案例

//设置名称和班级不为空
create table if not existts mycalss
(
    class_name varchar(20) not null,
    class_room varchar(20) not null
);

image-20220418155647440

此时我们要是有一个不进行设置则会报错,只有两个都不为空的时候才不会报错

image-20220418155738959
在这里插入图片描述

默认值default
某种数据会经常性的出现某一个具体的值,可以在一开始就指定好,在开始时使用默认值

例:

create table t1(age tinyint unsigned default 0);
//age一栏默认设置为0

[(img-2hTFcF04-1650333078958)](https://img-blog.csdnimg.cn/img_convert/9a9266027cd7dfafa754f3b31ccdba2b.png)]

上面当我们在次插入要是不指定age的值那么就会自动填充为0

not null和default可以同时存在,此时会自动添加default设定的默认值

建议not null和default只设置一个

有默认值就设置default

没有默认值只是不想让它为空就设置not null

列描述comment

comment,没有实际意义,用来描述字段,相当于注释

create table t16(name varchar(20)not null comment '名字',
                age tinyint unsigned default 18 comment '用户年龄');
zerofill

设定属性的宽度,如果宽度小于设定的宽度,自动填充0,需要注意的是,这只是显示的结果,只是一种格式化输出

create table if not exists t10(
	a int(10) int unsigned default,
	b int(10) int unsigned default
);
alter table t10 change a int(10) unsigned zerofill; 

img-mA0yD1Si-1650333078959)

[(img-PiY5kUtI-1650333078959)](https://img-blog.csdnimg.cn/img_convert/e0b53a331086c189d10433ec3c770393.png)]

image-20220419092047408

主键primary key

主键:primary key用来唯一的约束该字段的数据,不能重复,不能为空,一张表中只能用一个主键

案例

create table t18(id int unsigned primary key comment '学生的学号,作为主键',name varchar(10) not null comment '学生姓名')engine=InnoDB default charset=utf8;

image-20220418161149949

insert into t18 values(1,'孙悟空');
insert into t18 values(1,'猪八戒');
//这时候主键冲突,出错

//要是设置了default,则第一次可以插入,后面就不行了

image-20220418161321127
在这里插入图片描述

设置指定列是主键之后,默认不能为空,如果设置了default,默认插入的时候是可以出现空的,但是不推荐(只能使用一次)

主键的删除
alter table t18 drop primary key;

image-20220419092150492

添加主键
alter table t18 add priamry key(id);

image-20220419092228215

复合主键

创建表的时候,在所有字段之后,使用primary key(主键字段列表)来创建主键,

create table t19(ip varchar(30) comment 'IP',
                 port int unsigned comment '断开'
                priamry key(ip,port));

只有当两个都相同才会产生主键冲突

img-QXBDumUA-1650333078963

自增长auto_increment
create table if not exists t19(
    id int unsigned auto_increment comment '自增长字段',
    name varchar(10) NOT NULL comment '用户名'
);

可以自己插一个值,然后他会从前面中取最大值然后进行增长

一般在设置主键的时候,建议设置成与业务无关的数据字段来充当主键,

自增长属性!

注意这个关键字必须用在主键或者下面讲到的唯一键之后

ERROR 1075 (42000): Incorrect table definition; there can be only one auto column and it must be defined as a key
唯一键unique

表中很多字段需要唯一性,但是主键只有一个。

例如学生存在大量唯一性的信息:学号,身份证号,电话

不是主键具有唯一性,而是唯一性的数据被选择成为了主键

假设用学号作为主键,那么其它唯一性属性怎么办?万一上层用户插入大量重复值,但是主键符合要求

唯一间目的不在于类似主键一般,保证数据的绝对唯一性,目的在于,其它非主键字段,如果也想要有一定的唯一性,就可以设置改变列的唯一性约束

唯一性约束的本质在于让我们的数据更安全,完整性更高

唯一键修饰的列可以为null,null不做唯一性比较,唯一键+not null与主键功能类似

create table user(
	id int unsigned primary key auto_increment comment '主键',
    vip_id int unsigned comment 'vip账号',
    name varchar(10) not null comment 'vip用户姓名'
);

添加唯一键

alter table user add unique(vip_id);
alter table user add telphone varchar(11) unique;
外键 freign key… references ….

一般我们不会只有一只表,例如在刚入学的时候要进行分班,那么学生表里面有一个班级id,这个班级id就必须是在班级表中出现过的,借助外键,我们就可以完成这一限制,建立表与表之间的联系

外键存在的意义是建立表和表之间的关系

外键和外键约束是不同的概念

create table if not exists classes (
	id int unsigned primary key comment '班级id标识信息',
    name varchar(20) not null comment '这个班级的名字',
    
)
create table if not exists std(
stu_id int unsigned primary key,
    stu_name varchar(20) not null
    class_id int not null 
    ,foreign key(class_id) references classes(id)
)
  • 40
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 45
    评论
评论 45
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

 落禅

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值