思想:数据完整性指存储在数据库中的数据能正确反应实际情况

类型{

  • 实体完整性:标识符或则主键的完整性
  • 域完整性:限制类型  格式  可能值范围
  • 引用完整性:保持原表和引用表数据的一致性
  • 自定义的完整性:用户自定义的业务规则

}

实现方式{

实体完整性:主键约束,唯一值约束

域完整性:检查约束,默认值约束,外键约束

引用完整性:外键约束

}

约束{

用来限制属性或则表的

create table       alter table  ------->对象

行级约束,表级约束   约束效力 行级和表级是一样的

非空约束,唯一约束,主键约束一般写行级

check随便

外键约束:一般就是表级 联合主键 表级约束

}

{

非空约束:确保当前约束的属性不为空值,非空约束只能出现在表对象的列上

}


笔记:

create table student1(id int,name varchar(64),phone varchar(20),
class_no int)engine innodb default charset utf-8;
insert into student1(id) values(1);
insert into student values(2,null,null,null);
create table student2(id int not null,
name varchar(64) not null,
phone varchar(20) ,
class_no int)engine innodb default charset utf-8;
insert into student2(id) values(1);
#指定列添加非空
alter table student2 modify column phone varchar(20) not null;
#指定列删除非空
alter table student2 modify column phone varchar(20);
#唯一约束:指定某列或则某几列数据不能重复
create table student3(id int not null unique,
name varchar(64) not null,
phone varchar(20) unique,
class_no int not null
)engine innodb default charset utf-8;
insert into  student3 values(1,'pika',123451,1001);
insert into  student3 values(2,'pikb',123452,1002);
#指定列添加唯一约束
alter  table student3 values add constraint uc_name unique(name);
insert into student3 values(3,'pika','123453',1002);
#指定列删除唯一约束
alter table student3 drop index uc_name;
#创建复合唯一约束(表级约束)
create table student4(id int not null,
name varchar(64) not null,
phone varchar(20) ,
class_no int not null,
constraint uc_idphone unique(id,phone)
)engine innodb default charset utf-8;
desc student4;
insert into student4 values(1,"pika",123451,1001);
insert into student4 values(1,"pikb",123452,1001);
insert into student4 values(1,"pikc",123453,1001);
insert into student4 values(12345,"pikd",123451,1001);//ID不能和电话一样

主键约束

主键约束,非空+唯一

多列组合的主键约束,组合的值不能重复

每个表只能由一个主键约束

create table student5(
id int ,
name varchar(64) not null,
phone varchar(20) unique,
class_no int not null,
)engine innodb default charset utf-8;
create table student6(
id int ,
name varchar(64) not null,
phone varchar(20) unique,
class_no int not null,
constraint pk_s6 primary key(id,name)
)engine innodb default charset utf-8;
desc student6;
#添加主键约束
alter table student6 add primary key(stu_no,stu_name);
#删除主键约束
alter table student6 drop primary key;
#增加列
create table student7(
id int primary key auto_increment,
name varchar(64) not null,
phone varchar(20) unique,
class_no int not null,
)engine innodb default charset utf-8;
desc student;
insert into student7 values(null,'pika','1231',1001);
insert into student7 (name,phone,class_no)values('pikb','1232',1002);
alter table student7 modify column id int not null;
alter table student7 modify column id int primary key auto _increment;
create table student8(
id int primary key auto_increment,
name varchar(64) not null,
phone varchar(20) unique,
class_no int not null,
)engine innodb default charset utf-8 auto_increment=1000;
desc student8;
insert into student8(name,phone,class_no)values('pikc','1232',1004);
#检查约束
create table student9(
id int primary key auto_increment,
name varchar(64) not null,
phone varchar(20) unique,
age int check(age>0 and age<200),
gender varchar(4) check (gender='男'or gender='女'),
class_no int not null,
)engine innodb default charset utf-8 ;
insert into student9 values(null,'pikd','1234',-20,'男',1002);

 


扩展: