目录

数据库造神计划第十天-数据库约束1

数据库造神计划第十天—数据库约束(1)

https://i-blog.csdnimg.cn/direct/7d591b544f874d8a8495f2f641c7d7a5.jpeg

🔥个人主页:

🎬作者简介:Java研发方向学习者

📖个人专栏: **************************************************************************************************************************** 、《************************************************************ ****************************************************************

⭐️人生格言:没有人生来就会编程,但我生来倔强!!!


https://i-blog.csdnimg.cn/direct/7bc50643513c4841ab37a6a5e97eb4b5.gif


一、什么是数据库约束

        数据库约束是指对数据库表中的数据所施加的规则或条件,用于确保数据的准确性和可靠性。这些约束可以是基于数据类型、值范围、唯⼀性、非空等规则,以确保数据的正确性和相容性。

二、约束类型

https://i-blog.csdnimg.cn/direct/69fee962fdb4450c8aa59e36d90ff68c.png

1、not null空约束

定义表时某列不允许为null时,可以为列添加非空约束。(相当于注册账号时的必填项)

1.1创建⼀个学生表

        学生名为null时,这条记录是不完整的

drop table if exists student;
create table student(
    id bigint,
    name varchar(20)
);

# 插⼊数据
insert into student values (1, null);

# 查询
select * from student;

https://i-blog.csdnimg.cn/direct/c8954b0568404de394363019d9fc33f8.png

1.2此时需要约束学生名的列不能为null

drop table if exists student;

# 为所有列添加⾮空约束
create table student (
    id bigint,
    name varchar(20) NOT NULL
);

# 由于name列有⾮空约束,插⼊NULL值时报错
insert into student values (1, null);

https://i-blog.csdnimg.cn/direct/089ffc00e7c442adbf836a3ac2670e9b.png

# 正常值可以成功插⼊
insert into student values (1, '张三');

select * from student;

https://i-blog.csdnimg.cn/direct/f673b1e97e54416e8bbbf568bc44eac6.png

1.3查看表结构

        NULL列为NO表示值不允许为NULL,YES表示值可以为NULL

desc student;

https://i-blog.csdnimg.cn/direct/dd44cd202eef46b8a2d972b070ae4c79.png

2、default默认值约束

default约束用于向列中插⼊默认值,如果没有为列设置值,那么会将默认值设置到该列

2.1重构学生表

        新增年龄列

drop table student;

# 创建学⽣表,加⼊年龄列
create table student (
    id bigint,
    name varchar(20) not null,
    age int
);

        插入⼀条记录,没有设置默认约束时,不指定年龄的值时列为NULL

insert into student(id, name) values (1, '张三');

select * from student;

https://i-blog.csdnimg.cn/direct/a81604b2ac194bd994550b998c29fbc2.png

2.2重构学生表

        为年龄的列加入默认约束

drop table student;

# 为年龄列加⼊默认约束
create table student (
    id bigint,
    name varchar(20) not null,
    age int default 18
);

        插入⼀条记录,不指定年龄的值时列使用了默认值

insert into student(id, name) values (1, '张三');

select * from student;

https://i-blog.csdnimg.cn/direct/d8a22b78daf8473e857d670d36ba30ee.png

2.3查看表结构

        年龄列的默认值为18

desc student;

https://i-blog.csdnimg.cn/direct/4a43a075d0b24712b96b831de9b6650a.png

        当手动明确指年龄列为NULL时列值为NULL

insert into student(id, name, age) values (2, '李四', NULL);

https://i-blog.csdnimg.cn/direct/97d2e79706fc4f26a5b3391aa72c8bd0.png

3、unique唯一约束

指定了唯⼀约束的列,该列的值在所有记录中不能重复,如⼀个⼈的身份证号,学生的学号等

3.1重构学生表

        新增学号列

drop table student;

# 学号列设置唯⼀约束
create table student (
    id bigint,
    name varchar(20) not null,
    age int DEFAULT 18,
    sno varchar(10)
);

        不设置唯⼀约束时,学号可以重复

insert into student(id, name, sno) values (1, '张三', '100001');

insert into student(id, name, sno) values (2, '李四', '100001');

select * from student;

https://i-blog.csdnimg.cn/direct/47d26cfe6b0748c38508fb910f883c35.png

3.2重构学生表

        为学号列设置唯⼀约束

drop table student;

create table student (
    id bigint,
    name varchar(20) not null,
    age int DEFAULT 18,
    sno varchar(10) UNIQUE# 唯⼀约束
);

        插入重复的学号时报错,唯⼀约束生效

insert into student(id, name, sno) values (1, '张三', '100001');

https://i-blog.csdnimg.cn/direct/29ef86790cbd49e58ffa7299ea2caf7d.png

insert into student(id, name, sno) values (2, ' 李四 ', '100001');

https://i-blog.csdnimg.cn/direct/11d55989d4da4f18b62f380f61d99494.png

select * from student;

https://i-blog.csdnimg.cn/direct/e72830d57b384c9e9e74f6db1588be01.png

3.3查看表结构

        Key列显示UNI表示唯⼀约束

 desc student;

https://i-blog.csdnimg.cn/direct/7d4dbc683bba418cba002b4002d8a872.png

#注:

distinct本来数据库服务器存储的内容中,已经有重复的了。展示给用户的时候,展示的是去重的结果

unique 是存的数据就不能重复(重复的数据存不下去),查询的结果自然也是不重复的

         由于内容较多,会分为多篇讲解,预知后续内容,请看后续博客!!!