zoukankan      html  css  js  c++  java
  • Oracle 约束详解(constraint)

    1 概述

    1. 约束的作用
       (1) 录入 '规范' 的数据
       (2) '定义规则',对数据库中数据进行限制,确保数据正确性、有效性、完整性
    
    2 约束管理
    2.1 约束命名规范

    1. 默认命名:SYS_Cn(n 为正整数)
    2. 指定名称:推荐如下
    3. 若约束名称长度超过 30 个字节,则 "表名" 使用简称 

    约束类型    规范命名    名称说明
    主键约束    PK_表名_列名    Primary Key
    外键约束    FK_表名_列名    Foreign Key
    非空约束    NN_表名_列名    Not Null
    唯一约束    UK_表名_列名    Unique Key
    检查约束    CK_表名_列名    Check
    2.2 约束信息查询
    1. 常用视图 (权限由大到小: dba_* > all_* > user_*)
       (1) dba_constraints : 侧重约束具体信息
       (2) dba_cons_columns: 侧重约束列信息
    
    2. 参考如下
       select *
         from dba_constraints dc
        where dc.owner = 'SCOTT'
          and dc.table_name = 'EMP';
      
       select *
         from dba_cons_columns dcc
        where dcc.owner = 'SCOTT'
          and dcc.table_name = 'EMP';

    2.3 添加约束

    -- 1.唯一性约束
    alter table 表名 add constraint uk_* unique(列名) [not null];
    
    -- 2.检查约束
    alter table 表名 add constraint ck_* check(列名 between 1 and 100); 
    alter table 表名 add constraint ck_* check(列名 in ('值1', '值n')); 
    
    -- 3.非空约束(多个约束中,not null 位于末尾)
    alter table 表名 modify(列名 constraint nk_* not null);

    2.4 删除约束

    alter table 表名 drop constraint 约束名;

    2.5 重命名约束

    alter table 表名 rename constraint 约束名 to new_约束名;

    2.6 禁用启用约束

    -- 1.禁用 disable
    alter table 表名 disable constraint 约束名 [cascade];
    
    -- 2.启用 enable
    alter table 表名 enable constraint 约束名 [cascade];

    3 约束分类

    3.1 主键约束 P

    create table scott.sex (
       sex_code  varchar2(2) constraint pk_sex_code primary key,
       sex_desc  varchar2(10)
    );

    3.2 外键约束 R

    create table scott.student_info (
       sno      number(10) constraint pk_student_info_sno primary key,
       sname    varchar2(30),
       sex_code varchar2(2),
       constraint fk_student_info_sex_code foreign key(sex_code) references scott.sex(sex_code)
    );

    外键约束有以下 3 种情况:子表 引用 父表

    1. 普通外键约束: 删除 父表 记录时,'报错''默认'
    2. 级联外键约束: 删除 父表 记录时,同时 '删除子表记录'
    3. 置空外键约束: 删除 父表 记录时,同时将 '子表记录置为 null'

     3.3 唯一约束 U

    create table scott.temp_u (
       tno number(10) constraint tk_temp_u_tno unique not null
    );

    3.4 检查约束 C

    create table scott.temp_c (
       age number(3) constraint ck_temp_c_age check(age between 1 and 150),
       sex varchar2(2) constraint ck_temp_c_sex check(sex in ('', '', '未说明'))
    );

    3.4 非空约束

    create table scott.temp_n (
       create_date date constraint nn_temp_n_create_date not null
    );





  • 相关阅读:
    Python KNN算法
    Python TF-IDF计算100份文档关键词权重
    Python 结巴分词
    Python 将pdf转换成txt(不处理图片)
    Python小爬虫-自动下载三亿文库文档
    Kubuntu麦克风音频无声音
    adb常用命令
    Ubuntu下adb的安装
    Wamp安装使用+Git for Windows
    TensorFlow使用记录 (九): 模型保存与恢复
  • 原文地址:https://www.cnblogs.com/zouhong/p/15428892.html
Copyright © 2011-2022 走看看