三.mysql表的完整性约束

mysql表的完整性约束

什么是约束

    not null    不能为空的
    unique      唯一 = 不能重复
    primary key 主键 = 不能为空 且 不能重复
    foreign key 外键约束

为什么要约束

   是因为一个表中的数据要想完整规范,就必须对一些字段有基础的约束
   一般情况下,我们都会根据程序的需求个特点对数据库进行约束

约束一 not null  

  1.  create table t (id int not null,
                                sex enum('male','female') not null default 'male');

 

约束二  unique  唯一,允许多个字段为null

            设置某一个字段的内容必须是唯一的
 1.create table t3 (id int unique);   # 你设置了唯一,就不能插入两个相同的内容 测试:除了null之外

 

2. create table t3 (id int not null unique);  # 你设置了唯一+非空,就不能插入两个相同的内容,也不能插入NULL,就相当于设置了一个主键primary key

  

3.create table t3 (id int, name char(12), unique(id),unique(name));

两个或者多个字段的内容 = 联合唯一
4. create table t4 (pid int, pname char(12),ip char(15),port int,unique(ip,port));
  #设置联合唯一 (ip+端口的联合唯一)


5.create table t4 (pid int, pname char(12),     #设置非空且联合唯一
                            ip char(15) not null,
                            port int not null ,
                            unique(ip,port));

 约束三  主键约束


     主键 : 每一张表只能有一个主键    primary key = not null + unique   主键=非空+唯一
1. create table t5 (id int primary key);     #设置主键
2.能设置多个主键么 ? 不能
               create table t5 (id int primary key,name char(12) primary key);
3. 能不能设置多个非空 + 唯一 ? 能
               create table t6 (id int not null unique,name char(12) not null unique);
4. create table t7 (pid int, pname char(12),    #联合主键
                               ip char(15),
                               port int,
                               primary key(ip,port));

 auto_increment 自增

步长:auto_increment_increment,  起始偏移量:auto_increment_offset

         自增字段必须针对于int数据类型的且必须针对一个unique约束

 1.create table t8 (id int unique auto_increment,name char(12));

2.create table t9 (id int primary key auto_increment,name char(12));  #也可以设置主键也可以自增
3.对于自增id来说,删除数据并不会影响自增,设置为自增,用户最好不要自己插入这个字段

 

约束四  外键

          部门id  部门名称 部门办公室号

          alter table department modify id int unique;

         员工id   name  年龄  工资  部门id(外键)

          create table employee (id int,name char(12),age int,salary int,dep_id int,
                                               foreign key(dep_id) references department(id));

dpt_id外键,关联父表(department主键id),同步更新,同步删除

       create table employee2 (id int,name char(12),age int,salary int,dep_id int,
                                              foreign key(dep_id) references department(id) on delete cascade on update cascade );  

小结

约束 4个
1. not null           # 不允许为空
       # default      # 设置默认值
2. unique # 唯一,不能约束null # 联合唯一 # auto_increment # 自增
3. primary key # 主键 = not null + unique (同一张表不能有两个主键) # 联合主键
4. foreign key # 本表中的字段关联另一张表中的"唯一"字段 ,本表中的字段是 外键,外表中的字段必须唯一/主键
唯一约束: create table 表名 ( 字段名1 字段类型(宽度) not null default 默认值, 字段名2 字段类型(宽度) not null unique, #即非空也有一个唯一 字段名3 字段类型(宽度) primary key,        #设置一个主键 字段名4 int(宽度) unique auto_increment, #自增 ) create table 表名 ( 字段名1 字段类型(宽度) not null, 字段名2 字段类型(宽度) not null, 字段名3 字段类型(宽度), 字段名4 int(宽度), unique(字段名2), primary key(字段名3), unique(字段名4) auto_increment, )
联合主键: create table 表名 ( 字段名1 字段类型(宽度) not null, 字段名2 字段类型(宽度) not null, 字段名3 字段类型(宽度), 字段名4 int(宽度) auto increment, unique(字段名1,字段名2), primary key(字段名3,字段名4), );
外键: create table 表名( 字段名1 字段类型(宽度) not null, 字段名2 字段类型(宽度) not null, 外键名 字段类型(宽度), foreign key (外建) references 外表(外表中的字段) on delete cascade on update cascade, primary key(字段名1) ) 添加主键 alter table 表名 modify 字段名 类型(宽度) primary key;

 

         

原文地址:https://www.cnblogs.com/baoshuang0205/p/10138574.html