zoukankan      html  css  js  c++  java
  • Mysql之数据完整性约束

    Mysql之DDL操作


    四、数据完整性约束
    实体完整性、域完整性、引用完整性、自定义完整性

    1、实体完整性
    主键约束、唯一约束、主键自增

    1)主键约束
    特点:唯一的,不能为空
    关键字:primary key
    添加约束语法:alter table 表名 add constraint 约束名 primary key(字段名);
    约束名:PK_字段
    删除约束语法:alter table 表名 drop primary key;

    规定:在每一个表中有且只有一个主键约束

    create table student(
    id int primary key,
    name varchar(20),
    age int
    )

    alter table userinfo add uid int;
    #表已经创建好了,如何添加主键约束
    #alter table 表名 add constraint 约束名 primary key(字段名);
    #约束名:PK_字段
    alter table userinfo add constraint PK_uid primary key(uid);

    #删除约束
    #alter table 表名 drop primary key;
    alter table userinfo drop primary key;

    2)唯一约束
    特点:不能重复,可以为空,可以添加多个
    关键字:unique
    添加约束语法:alter table 表名 add constraint 约束名 unique(字段名);
    约束名:UQ_字段
    删除约束语法:alter table 表名 drop key 约束名;

    create table student(
    id int primary key,
    name varchar(20),
    age int,
    id_card varchar(20) unique
    )

    alter table userinfo add constraint UQ_login_name unique(login_name);

    alter table userinfo drop key UQ_login_name;

    3)主键自增
    特点:从1开始,每次自身加1(在oracle中不能使用)
    关键字: auto_increment
    只能在创建表的时候添加主键自增的约束,而且必须是主键才可以添加

    create table student(
    id int primary key auto_increment,
    name varchar(20),
    age int,
    id_card varchar(20) unique
    )

    -- 删除主键自增约束
    -- 1:去除自增性
    alter table student modify id int;
    -- 2:删除主键约束
    alter table student drop primary key;

    2、域完整性
    域完整性约束保证字段的数据准确的
    域完整性包括类型约束、非空约束、默认值

    1)非空约束
    特点:字段不允许为空
    关键字: not null

    2)默认值
    特点:设置默认的值
    关键字: default

    create table student(
    id int primary key auto_increment,
    name varchar(20) not null,
    age int not null,
    sex char(2) default '男',
    id_card varchar(20) unique
    )

    删除unique约束

    alter table `use` drop index id_card

    删除默认值default约束

    ALTER TABLE `use` CHANGE sex sex varchar(20) DEFAULT NULL;


    3、引用完整性
    一张表中通用列的取值必须参考另外一张表的主键
    引用完整性有外键约束

    1)外键约束
    特点:设置外键的字段的取值只能参考另一张表中同一个字段的值
    关键字: foreign key
    添加外键约束的语法:alter table 表名1 add constraint 约束名 foreign key(字段名) references 表名2(字段名)
    删除外键的语法:alter table 表名 drop foreign key 约束名;


    注意:
    1、主外键关联
    2、外键关联字段名称可以不一样,但是类型必须一致

    --外键约束
    create table student(
    id int primary key auto_increment,
    name varchar(20) not null,
    age int not null,
    sex char(2) default '男',
    id_card varchar(20) unique,
    class_id int #添加外键约束
    )

    create table classroom(
    class_id int PRIMARY key,
    class_name varchar(20)
    )

    -- 添加外键约束的语法:alter table 表名1 add constraint 约束名 foreign key(字段名) references 表名2(字段名)
    -- 约束名 FK_字段名

    alter table student add constraint FK_class_id foreign key(class_id) references classroom(class_id);

    -- 删除外键的语法:alter table 表名 drop foreign key 约束名;
    alter table student drop foreign key FK_class_id;

  • 相关阅读:
    【POJ
    【POJ
    【POJ
    【POJ
    【POJ
    【POJ
    【POJ
    【POJ
    NAT
    OSPF与ACL综合实验
  • 原文地址:https://www.cnblogs.com/gskk/p/13210251.html
Copyright © 2011-2022 走看看