MySQL数据库~~~~ 完整性约束

1. not null 与 default

not null : 不可空

default : 默认值

例: create table t1(id int not null default 2);

2. unique

unique : 唯一属性

例: create table t1(name varchar(20) unique);

​ create table t1(name varchar(20), constraint uk_name unique(name));

联合唯一:

create table t1(name varchar(20),host varchar(15), unique(name,host));

3. primary key

primary key : 主键,字段不为空切唯一

主键 primary key是 innodb存储引擎组织数据的依据,innodb称之为索引组织表,一张表中必须有且只有一个主键。

create table t1(id int primary key);

4. auto_increment

auto_increment: 自增属性,默认起始位置为1,步长也为1.

create table t1(id int primary key auto_increment);

5. foreign key

foreign key : 外键,标明表和表之间的关系,表和表之间的关系:一对一,多对一,多对多

  1. 一对多

    部门表是被关联表,员工表是关联表,创建表的时候应该先创建部门表(被关联表),再创建员工表(关联表)

    create table dep(id int primary key auto_increment,
                    dep_name char(10),);
    create table emp(id int primary key auto_increment,
                    name char(10),
                    dep_id int,
                    foreign key(dep_id) references dep(id)
                    on update cascade on delete cascade);
    # 加入on update cascade on delete cascade语句可以使员工表中的数据跟着部门数据的改动而变动

    插入数据时,先插入部门数据(被关联表),再插入员工数据(关联表)

  2. 多对多

    先创建作者表和书表,然后创建第三张表,用两个外键字段关联作者表和书表.

    create table author(id int primary key auto_increment,
                       name char(20));
    create table book(id int primary key auto_increment,
                     bname char(20));
    insert into author(name) values('liky'),('小虎');
    insert into book(bname) values('book1'),('book2');
    create table author_book(id int primary key auto_increment,
                            author_id int,
                            book_id int,
                            foreign key(author_id) references author(id) on update cascade on delete cascade,
                            foreign key(book_id) references book(id) on update cascade on delete cascade);
    insert into author_book(author_id,book_id) values(1,2),(2,2);
  3. 一对一

    学生表(student)和客户表(customer),把学生表(关联表)的外键字段设置成唯一属性.

    create table student(
         id int primary key,
         name char(10),
         cid int unique,
         foreign key(cid) references customer(id)
         );
  4. 删除或修改被关联字段

    场景: book表和publish表为多对一关系,book表的pid字段外键关联到了publish表的id字段

1.查看外键关系名称:
    show create table book;
2.删除外键关系
    alter table book drop foreign key book_ibfk_1(外键名称);
3.删除字段
    alter table publish drop id(字段名称);
4.添加字段
    alter table pulish add id(字段名称) int(数据类型) primary key auto_increment(约束条件);
5.创建表完成之后,添加外键关系
    alter table book add foreign key(pid) references publish(id);

创建外键时指定外键名称:
    create table t1(id int,
                   pid int,
                   constraint fk_t1_publish foreign key(pid) references publish(id));
创建表完成之后,添加外键关系
    alter table book add constraint fk_t1_publish foreign key(pid) references publish(id);
  1. 外键约束的三种约束模式(都是针对父表的约束):
1.district严格约束(默认):父表不能删除或者更新已经被子表数据引用的记录
2.cascade级联模式:父表的操作,对应的子表关联的数据也跟着操作
    constraint fk_t1_publish foreign key(pid) references publish(id) on delete cascade on update cascade;
3.set null置空模式:被关联字段删除时,关联它的字段数据会置空成null
注意:删除置空的前提条件是外键字段允许为空,不然外键会创建失败.

猜你喜欢

转载自www.cnblogs.com/lav3nder/p/11985270.html