zoukankan      html  css  js  c++  java
  • 约束(Constraints)

    约束是一种防止插入、修改或从列中删除不需要的数据的方法。

    Tebiro约束类型

    ConstraintDescription
    Primary Key

    Characteristics of the Unique Key and NOT NULL constraints.

    A column set as the primary key cannot have a NULL value.

    Unique Key Rows in the table cannot have the same value for the column, unless the value is NULL.

    Referential Integrity

    参照完整性

    Refers to the primary key or a unique key of a table.

    指表的主键或唯一键。

    NOT NULL

    Rows in the table cannot have a NULL value for this column.

    Table-level constraints cannot be set.

    CHECK

    Constraint where a certain condition must be met before a value can be inserted or modified.

    A column can have multiple constraints.

    1 声明约束(Declaring Constraints)CREATE TABLE DEPT

      (
          DEPTNO    NUMBER PRIMARY KEY,
          DEPTNAME  VARCHAR(20),
          PDEPTNO   NUMBER
      )
      TABLESPACE my_space
      PCTFREE 5 INITRANS 3;
    
    CREATE TABLE EMP
      (
          EMPNO       NUMBER        PRIMARY KEY,
          ENAME       VARCHAR(16)   NOT NULL,
          ADDR        VARCHAR(24),
          SALARY      NUMBER,
          DEPTNO      NUMBER,
          CONSTRAINT  SALARY_MIN CHECK (SALARY >= 10000),
          CONSTRAINT  DEPTNO_REF FOREIGN KEY (DEPTNO) REFERENCES DEPT(DEPTNO)
    ) TABLESPACE my_space PCTFREE 5 INITRANS 3;

    Declaring Constraints-Column Unit
    CREATE TABLE TEMP_PROD
    (
      PROD_ID NUMBER(6) CONSTRAINT PROD_ID_PK primary key,
      prod_name varchar2(50) constraint prod_name_nn not null,
      prod_cost varchar2(30) constraint prod_cost_nn not null,
      prod_pid number(6),
      prod_date date constraint prod_date not null
    );

    Constraint Declaration - Table Unit

    CREATE TABLE TEMP_PROD
    (
           PROD_ID     NUMBER(6),
           PROD_NAME   VARCHAR2(50)   CONSTRAINT PROD_NAME_NN NOT NULL,
           PROD_COST   VARCHAR2(30)   CONSTRAINT PROD_COST_NN NOT NULL,
           PROD_PID    NUMBER(6),
           PROD_DATE   DATE           CONSTRAINT PROD_DATE_NN NOT NULL,
           CONSTRAINT PROD_ID_PK PRIMARY KEY(PROD_ID, PROD_NAME)

    );

    Changing a Constraint's Name

    ALTER TABLE EMP
            RENAME CONSTRAINT EMP_PRI_KEY TO EMP_KEY;

    Adding New Constraints
    ALTER TABLE EMP ADD CONSTRAINT salary_max CHECK (SALARY >= 5000);
    ALTER TABLE EMP ADD UNIQUE (ename, deptno);

    Constraint Removal

    ALTER TABLE EMP
            DROP PRIMARY KEY;
      ... Removes a constraint set with the primary key. ...
    
    ALTER TABLE EMP
            DROP CONSTRAINT SALARY_MAX;
      ... Removes a constraint whose name is SALARY_MAX. ...

    约束状态(Constraint States)


    Modifying Constraint States(修改约束状态)

     Constraint States

    There are two constraint states.

    • ENABLE

      The declared constraint is applied to all rows which are inserted or updated in the table.

      ENABLE has two additional options:

      OptionDescription
      VALIDATE

      When the constraint state is ENABLE, all rows which are already in the table should satisfy the constraint whenever a row is inserted or updated.

      Checking all rows at the same time can improve database performance.

      NOVALIDATE

      All rows in the table are not checked to determine if they satisfy the constraint. The rows should either be checked later or they should already satisfy the constraint.

      This helps improve database performance because the rows in the table do not need to be checked frequently.

      However, PRIMARY KEY and UNIQUE KEY constraints, due to the characteristics of their indexes, are always checked as if they were using VALIDATE even when the NOVALIDATE option is used.

    • DISABLE

      The declared constraint is not applied to rows. When inserting or updating a large number of rows at the same time, it is helpful to use DISABLE until the work is finished, and then use ENABLE. This helps improve database performance.

      Users can insert or update many rows at once using tbLoader, the tbImport utility, or other batch programs. Because the rows in the table do not need to be checked to determine if they satisfy the constraint, this helps improve database performance.

      DISABLE can be used with the following additional options.

      OptionDescription
      VALIDATE Drops the index on the constraint and does not allow any modification of the constrained column.
      NOVALIDATE Same as not specifying any option.

    Changing a Constraint State - ENABLE

    ALTER TABLE EMP MODIFY CONSTRAINT EMP_UNIQUE ENABLE;
    alter table emp modify constraint emp_unique enable;

    Changing a Constraint State - DISABLE

    ALTER TABLE EMP MODIFY PRIMARY KEY DISABLE;

     Changing a Constraint State - VALIDATE

    ALTER TABLE EMP MODIFY CONSTRAINT SALARY_MIN ENABLE NOVALIDATE; 

    Viewing Constraint Data (查看约束数据)

    Static ViewDescription
    DBA_CONSTRAINTS Information about all constraints in Tibero .
    USER_CONSTRAINTS Information about all constraints that belong to the current user.
    ALL_CONSTRAINTS Information about all constraints that the current user can access.
    DBA_CONS_COLUMNS Information about columns with constraints in Tibero .
    USER_CONS_COLUMNS Information about columns with constraints that belong to the current user.
    ALL_CONS_COLUMNS Information about columns with constraints that can be accessed by the current user.














     

  • 相关阅读:
    day84:luffy:优惠活动策略&用户认证&购物车商品的勾选/结算
    day83:luffy:添加购物车&导航栏购物车数字显示&购物车页面展示
    day82:luffy:课程详情页面显示&章节和课时显示&视频播放组件&CKEditor富文本编辑器
    day81:luffy:课程分类页面&课程信息页面&指定分类显示课程信息&分页显示课程信息
    day80:luffy:短信sdk接入&点击获取验证码&注册功能的实现&Celery实现短信发送功能
    day79:luffy:注册之对手机号的验证&实现基本的注册功能逻辑&点击获取验证码&redis
    day78:luffy:前端对于token的认证&滑动验证码的实现
    day77:luffy:导航栏的实现&DjangoRestFramework JWT&多条件登录
    day76:luffy:项目前端环境搭建&轮播图的实现
    day75:luffy:路飞学城项目后端环境搭建&Git相关知识点
  • 原文地址:https://www.cnblogs.com/zykLove/p/12132929.html
Copyright © 2011-2022 走看看