约束(Constraints)

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

Tebiro约束类型

Constraint Description
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:

    Option Description
    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.

    Option Description
    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 View Description
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.














 

你可能感兴趣的:(约束(Constraints))