zoukankan      html  css  js  c++  java
  • 数据检查约束类型和语法

    原创:转载请注明出处:
    存储在数据库中的所有数据值均正确的状态。如果数据库中存储有不正确的数据值,则该数据库称为已丧失数据完整性。
    数据完整性(Data Integrity)是指数据的精确性(Accuracy) 和可靠性(Reliability)。它是应防止数据库
    中存在不符合语义规定的数据和防止因错误信息的输入输出造成无效操作或错误信息而提出的。数据完整性分为四类:
    实体完整性(Entity Integrity)、域完整性(Domain Integrity)、参照完整性(Referential Integrity)、
    用户定义的完整性(User-definedIntegrity)。
    数据库采用多种方法来保证数据完整性,包括外键、束约、规则和触发器。系统很好地处理了这四者的关系,并针对
    不同的具体情况用不同的方法进行,相互交叉使用,相补缺点。
    总结了一下对数据约束的sql语法如下:
    14.添加主键
    alter table student
    add constraint pk_student_sid
    primary key(sid);

    15.添加检查约束
    alter table student
    add constraint ck_student_sname
    check(length(sname)<6);

    16.添加唯一约束
    alter table student
    add constraint un_student_scard
    unique(scard);

    17.添加外键
    alter table student
    add constraint fk_student_cid
    foreign key(cid)
    references classes(cid);

    18.添加检查约束
    alter table student
    add constraint ck_student_sex
    check(sex in('男','女'));

    19.添加检查约束
    alter table student
    add constraint ck_student_age
    check(age>0 and age<120);

    20:添加检查约束
    alter table student
    add constraint ck_student_password
    check(length(password)>=6 and length(password)<=16);

    21.查询约束
    select table_name,constraint_name from user_constraints;

    22.删除约束
    alter table student drop constraint ck_student_password;
     

    23.s删除表同时删除该表的所有约束
    drop table student;
  • 相关阅读:
    转 sql 时间转换格式 convert(varchar(10),字段名,转换格式)
    C#页面添加提交数据后跳出小弹窗的功能
    解决粘包问题
    粘包问题
    模拟ssh远程执行命令
    基于TCP协议的socket套接字编程
    Linux和git使用
    osi七层协议
    TCP协议的三次握手和四次挥手
    C/S 和 B/S架构
  • 原文地址:https://www.cnblogs.com/zjdxr-up/p/6634277.html
Copyright © 2011-2022 走看看