中国建设银行江西分行网站首页,展架设计制作图片,wordpress信息登记,金华网站建设yw126文章目录 第七章 数据完整性7.1 完整性约束7.2 实体完整性7.2.1 唯一约束7.2.2 主键约束7.2.3 自增约束 7.3 域完整性7.3.1 非空约束7.3.2 默认值约束7.3.3 检查约束 7.4 引用完整性 第七章 数据完整性
7.1 完整性约束
我们已经知道了如何创建数据库、如何创建表、如何在表中… 文章目录 第七章 数据完整性7.1 完整性约束7.2 实体完整性7.2.1 唯一约束7.2.2 主键约束7.2.3 自增约束 7.3 域完整性7.3.1 非空约束7.3.2 默认值约束7.3.3 检查约束 7.4 引用完整性 第七章 数据完整性
7.1 完整性约束
我们已经知道了如何创建数据库、如何创建表、如何在表中进行数据的插入操作。但是在实际工作中我们需要考虑插入到表中的数据是否是合乎逻辑的。因此有时候我们需要在插入数据的时候对数据进行校验以保证插入表中的数据没有逻辑的错误。这就是数据的完整性。而为了保证数据的完整性我们往往需要借助一些约束来实现。 MySQL数据完整性非常重要它能保障MySQL库-表中的数据质量会影响后续大数据的数据仓库等项目的实施所以大家一定要看得懂(重视)。
数据的完整性约束可以分为三类: 实体完整性、域完整性和引用完整性。
7.2 实体完整性
我们将表中的一行数据称为一个“实体”而所谓的实体完整性约束就是保证这一行数据的唯一的不会出现重复的行数据。
为了达到这样的目的实体完整性约束有三种: 唯一约束、主键约束、自增约束。
7.2.1 唯一约束
关键字: unique
作用: 顾名思义就是修饰某一个字段这个字段的值在一张表内不能重复 单个键的唯一约束 建表的同时添加 -- 创建表
create table student (id int unique, -- 修饰为unique表示id字段的值不允许出现重复的情况sname varchar(50)
);
-- 或者
create table student (id int,sname varchar(50),unique(id)
);-- 表创建完成后在进行数据插入的时候如果插入了重复的id则第二次插入数据的时候会有错误提示无法插入数据保证这一行数据的唯一
insert into student values (1, zhangsan);
insert into student values (2, lisi);建表之后追加 -- 创建表
create table student (id int,sname varchar(50)
);-- 追加id为唯一约束
alter table student add unique (id);联合唯一约束 描述 所谓的“联合唯一约束”指的是可以将多个字段绑定在一起进行非空的约束。只有当这些字段的值都相同的情况下才会视为重复的数据不允许继续插入。建表的时候添加 create table student (id int,sname varchar(50),unique(id, sname)
);建表之后追加 -- 创建表
create table student (id int,sname varchar(50)
);-- 追加id为唯一约束
alter table student add unique (id, sname);测试 insert into student values (1, a); -- 第一次插入数据表中没有重复的行可以添加
insert into student values (1, b); -- 虽然表中已经有id为1的行但是sname并不重复依然可以添加
insert into student values (2, b); -- 同理表中虽然已经有sname为b的行但是id并不重复依然可以添加
insert into student values (2, b); -- id和sname的值都与已经存在的某一行数据重复因此添加失败移除约束 alter table student drop index sid;7.2.2 主键约束
关键字: primary key
其实为了保证数据的唯一性主键才是工作中最常用的约束方式。一般情况下会将代表这一行数据的唯一标识的字段设置为主键。在进行查询操作的时候通常情况下也是会使用主键进行行数据的区分。
主键的值不允许出现重复也不允许出现NULL的情况。
添加主键有几种方式: 在建表的时候添加主键只添加一个主键 create table student (id int primary key,sname varchar(50)
)在建表的时候添加主键同时添加多个主键 create table sc (sid int,cid int,score int,primary key(sid, cid) -- 如果有多个主键需要添加不能在字段的后面直接写primary key。需要通过这样的方式来添加。
)在建表完成后追加主键 create table course (cid int,cname varchar(50)
)alter table course add primary key (cid);删除表中的主键:
alter table course drop primary key;7.2.3 自增约束
顾名思义自增约束表示对某一个字段的值进行自动的增长。只能用在整型数据类型的主键约束上。表示在进行行数据的赋值的时候如果不设置主键的值这个值会自动的在之前的值的基础上1。
-- 建表
create table student(sid int primary key auto_increment,sname varchar(50)
)-- 插入数据
insert into student (sname) values (zhangsan); -- 没有设置主键sid的值则此时的sid自动增长为1
insert into student values (10, lisi); -- 此时已经设置了sid的值则这行数据的sid就是10
insert into student (sname) values (wangwu); -- 没有设置主键sid的值将会在目前最大的id基础上1也就是11注意事项:
在表内会有一个变量来记录当前的表最大的主键已经到达多少了。需要进行主键自增的时候从这个记录了最大的主键的基础上进行自增而不是简单的从上一行的基础上自增。
insert into student values (100, wangwu);
insert into student values (50, zhaoliu);
insert into student (sname) values (tianqi); -- 当前的表中记录最大的主键值是100因此这一行数据主键会自增到101注意事项:
在删除表中的数据的时候如果使用delete from进行删除则无法删除表中记录的最大值。
insert into student values (200, xiaoming);
delete from student;
insert into student (sname) values (xiaohei); -- 此时的自增结果依然是201注意事项:
在删除表的时候除了可以使用delete from进行删除也可以使用truncate进行删除的操作。前面也说过这两种操作的区别其中truncate是将现在的表进行drop然后再按照之前的表结构(字段的名字、类型等信息)创建出来一张跟这个表结构完全相同的表出来。那么问题来了delete在删除数据的时候只是删除数据并不会对表结构进行修改也就是说表中记录的最大的主键信息是会保留下来的。但是truncate是drop原来的表然后创建一张新的表。那么之前的记录的最大主键值的信息也就不存在了。
insert into student values (200, xiaoming);
delete from student;
insert into student (sname) values (xiaohei); -- 自增的sid值是201
truncate student;
insert into student (sname) values (xiaolan); -- 从头自增的sid值是17.3 域完整性
域完整性约束指的是对一个单元格内的数据进行约束对某一个单元格内的数据进行约束。
7.3.1 非空约束
关键字: not null
顾名思义就是对某一个字段进行修饰修饰这一个字段的值不能是NULL。
create table student (sid int primary key,sname varchar(30) not null
)-- 建表之后追加
alter table student change sname sname varchar(30) not null;
-- 建表之后移除
alter table student change sname sname varchar(30);-- 正常的数据插入
insert into student values (10, Jerry);-- 错误的写法因为sname已经修饰为not null了
insert into student values (10, NULL);-- 错误的写法因为非空的字段的值没有设置
insert into student (sid) values (1);7.3.2 默认值约束
关键字: default
修饰某一个字段在这个字段进行赋值的时候没有赋值的情况下这个字段将会拥有一个默认的初始值
create table student (sid int primary key,sname varchar(20) default unkonwn
)-- 正确的使用
insert into student (sid) values (100); -- 没有设置sname的值此时会采用默认的值
insert into student values (101, default); -- 直接使用关键字default表示使用默认值7.3.3 检查约束
检查约束类似于Java中的枚举约束这个字段的值只能够在有限的几个值内进行选择。
关键字: check
create table teacher( tid int, tname varchar(20), tgender char(1) check(tgender in(f,m)) -- 约束这个字段的值只能是m或者f
);
insert into temp_5 values (1001,zs,a);
select * from teacher;关键字: enum
create table temp_6( tid int, tname varchar(20), tgender enum(f,m) -- 枚举的写法
); 7.4 引用完整性
主要就是外键约束(foreign key)
字段A的值依赖于字段B的值。这两个字段可以在同一张表中也可以在不同的表中。字段A所在的表称之为从表(副表)字段B所在的表称之为主表(父表)。字段A的值也可以为null. 字段B必须为主键约束。
依赖的字段B必须是目标表的主键
-- 1. 在建表的时候添加外键
create table tableName(tid int primary key auto_increment, tname varchar(20), tmgr int,constraint constraintName Foreign key(tmgr) references tableName(tid)
);-- 2. 在建表之后追加
alter table tableName1 add constraint constraintName foreign key(colName1) references tableName2(colName2);-- 3. 撤销外键
ALTER TABLE score DROP FOREIGN KEY score_ibfk_1;EXT: 在添加主键的时候可以同时设置删除和更新时的约束: RESTRICT: 默认的约束不允许修改、删除被引用的字段的值CASCADE: 级联修改在修改、删除被引用的字段的时候引用方的一行数据也随之修改和删除SET NULL: 空值处理在修改、删除被引用的字段的时候引用方的引用字段的值会设置为NULLNO ACTION: 不处理在修改、删除被引用的字段的时候引用方的引用字段不作任何改变 语法: alter table tableName1 add constraint constraintName foreign key(colName1) references tableName2(colName2) on delete restrict on update restrict;
文章转载自: http://www.morning.wqbfd.cn.gov.cn.wqbfd.cn http://www.morning.cgtfl.cn.gov.cn.cgtfl.cn http://www.morning.shsh1688.com.gov.cn.shsh1688.com http://www.morning.thlr.cn.gov.cn.thlr.cn http://www.morning.mqfhy.cn.gov.cn.mqfhy.cn http://www.morning.ymrq.cn.gov.cn.ymrq.cn http://www.morning.tdcql.cn.gov.cn.tdcql.cn http://www.morning.jzkqg.cn.gov.cn.jzkqg.cn http://www.morning.hnhkz.cn.gov.cn.hnhkz.cn http://www.morning.mjbnp.cn.gov.cn.mjbnp.cn http://www.morning.fmrwl.cn.gov.cn.fmrwl.cn http://www.morning.qnbck.cn.gov.cn.qnbck.cn http://www.morning.kqnwy.cn.gov.cn.kqnwy.cn http://www.morning.geledi.com.gov.cn.geledi.com http://www.morning.fgrkc.cn.gov.cn.fgrkc.cn http://www.morning.ftsmg.com.gov.cn.ftsmg.com http://www.morning.bwqcx.cn.gov.cn.bwqcx.cn http://www.morning.yrrnx.cn.gov.cn.yrrnx.cn http://www.morning.kdjtt.cn.gov.cn.kdjtt.cn http://www.morning.thwhn.cn.gov.cn.thwhn.cn http://www.morning.tknqr.cn.gov.cn.tknqr.cn http://www.morning.rjhts.cn.gov.cn.rjhts.cn http://www.morning.prjns.cn.gov.cn.prjns.cn http://www.morning.qsbcg.cn.gov.cn.qsbcg.cn http://www.morning.gfkb.cn.gov.cn.gfkb.cn http://www.morning.skcmt.cn.gov.cn.skcmt.cn http://www.morning.nchlk.cn.gov.cn.nchlk.cn http://www.morning.wrtbx.cn.gov.cn.wrtbx.cn http://www.morning.fbzdn.cn.gov.cn.fbzdn.cn http://www.morning.jtfcd.cn.gov.cn.jtfcd.cn http://www.morning.hhrpy.cn.gov.cn.hhrpy.cn http://www.morning.tllhz.cn.gov.cn.tllhz.cn http://www.morning.kspfq.cn.gov.cn.kspfq.cn http://www.morning.rnjgh.cn.gov.cn.rnjgh.cn http://www.morning.shxrn.cn.gov.cn.shxrn.cn http://www.morning.fnxzk.cn.gov.cn.fnxzk.cn http://www.morning.mtgnd.cn.gov.cn.mtgnd.cn http://www.morning.knmp.cn.gov.cn.knmp.cn http://www.morning.dkfb.cn.gov.cn.dkfb.cn http://www.morning.nqrdx.cn.gov.cn.nqrdx.cn http://www.morning.jbtzx.cn.gov.cn.jbtzx.cn http://www.morning.srkqs.cn.gov.cn.srkqs.cn http://www.morning.dmthy.cn.gov.cn.dmthy.cn http://www.morning.jjhng.cn.gov.cn.jjhng.cn http://www.morning.chfxz.cn.gov.cn.chfxz.cn http://www.morning.rbzd.cn.gov.cn.rbzd.cn http://www.morning.zgqysw.cn.gov.cn.zgqysw.cn http://www.morning.lnbyk.cn.gov.cn.lnbyk.cn http://www.morning.sxfnf.cn.gov.cn.sxfnf.cn http://www.morning.mlfgx.cn.gov.cn.mlfgx.cn http://www.morning.gchqy.cn.gov.cn.gchqy.cn http://www.morning.sqyjh.cn.gov.cn.sqyjh.cn http://www.morning.bpmnj.cn.gov.cn.bpmnj.cn http://www.morning.qqtzn.cn.gov.cn.qqtzn.cn http://www.morning.lkbyj.cn.gov.cn.lkbyj.cn http://www.morning.syynx.cn.gov.cn.syynx.cn http://www.morning.xflwq.cn.gov.cn.xflwq.cn http://www.morning.knlbg.cn.gov.cn.knlbg.cn http://www.morning.qtltg.cn.gov.cn.qtltg.cn http://www.morning.wgbmj.cn.gov.cn.wgbmj.cn http://www.morning.pmmrb.cn.gov.cn.pmmrb.cn http://www.morning.jtsdk.cn.gov.cn.jtsdk.cn http://www.morning.ryfqj.cn.gov.cn.ryfqj.cn http://www.morning.zlff.cn.gov.cn.zlff.cn http://www.morning.pcqdf.cn.gov.cn.pcqdf.cn http://www.morning.yqtry.cn.gov.cn.yqtry.cn http://www.morning.yqwrj.cn.gov.cn.yqwrj.cn http://www.morning.ffrys.cn.gov.cn.ffrys.cn http://www.morning.qtzk.cn.gov.cn.qtzk.cn http://www.morning.rlqml.cn.gov.cn.rlqml.cn http://www.morning.mlckd.cn.gov.cn.mlckd.cn http://www.morning.qbjgw.cn.gov.cn.qbjgw.cn http://www.morning.ckfyp.cn.gov.cn.ckfyp.cn http://www.morning.dsmwy.cn.gov.cn.dsmwy.cn http://www.morning.nhgkm.cn.gov.cn.nhgkm.cn http://www.morning.wkgyz.cn.gov.cn.wkgyz.cn http://www.morning.gbrps.cn.gov.cn.gbrps.cn http://www.morning.ctpfq.cn.gov.cn.ctpfq.cn http://www.morning.bsghk.cn.gov.cn.bsghk.cn http://www.morning.bksbx.cn.gov.cn.bksbx.cn