Usage constraint

Reprinted from https://blog.csdn.net/shaderdx/article/details/77184924

 

The main constraint is to increase the data.

 

Oracle constraints in brief

Constraints Including Constraints

    Using constraints (constraints) in the database in order to implement the so-called "business rules" in the database is actually to prevent illegal access to the database information, to meet the rules set administrators and application developers to define.

    ORACLE integrity constraints (integrity constraints) to prevent the illegal data written to the database administrators and developers can define integrity rules, enhanced business rules to restrict data in the data table. If any of the results of a DML statement is executed destroyed integrity constraints, ORACLE rollback statement will return an error message.

    Constraint by using the CREATE TABLE statement or the ALTER TABLE generated (or can be modified after the establishment table creating table) if the associated constraints defined in the single row, the constraint can specify that a column defined above;. A plurality of rows constraints must be defined , related columns to be specified in the data table level in parentheses, separated by commas. If you do not provide a name for the constraint, ORACLE will be assigned a unique name of a system-generated, begins with SYS_, you can use the keyword cONSTRAINTS follow behind constraints related to the constraint name specified name.

ORACLE supports five types of integrity constraints
the NOT NULL (non-empty) - prevents NULL value into the specified column is defined on the basis of the single, default, ORACLE NULL values allowed in any column.
The CHECK (check) - - check constraints specified in the conditions have been met.
uNIQUE (unique) - to ensure that no duplicate values in the specified column of each value in the table or each set of values will be unique..
a PRIMARY KEY ( primary key) - uniquely identifies each row in the table, and to prevent occurrence NULL value, a table can have only one primary key constraint.
POREIGN kEY (external key) - to establish a parent-child between tables by using the common column (parent-child) relationship, the foreign key can be defined on a table to the primary key or other unique key table.


Several constraints, and list them:

: 1. The primary key constraint
conditions for a primary key constraint on a column added, then this column must be met is the partial space
as the primary key constraint: that is, one column of the constraint, the constraint is a (non-empty, not repeated)
The following is the code to to add a primary key column, the column named id, name table emp

Format:
the ALTER the Table table name add constraint constraint names added constraint types (column name)

例子:
alter table emp add constraint ppp primary key (id)

————————————————————————————————

2.check constraint:
that the data were a limiting
example, the age column of data must be greater than 20
table (emp) column name (age)

Format:
ALTER TABLE Name Table add constraint constraint constraint types (column names) in the name of increased

例子:
alter table emp add constraint xxx check(age>20)

______________________________________________________________________

3.unique constraint:
this constraint is added to the data string does not overlap constraint type

Format:
the ALTER the Table table name add constraint constraint name constraint types (column name)
for example can give ename columns add a unique, let the data ename column is not repeated
examples:
the ALTER QWE the Table emp add constraint UNIQUE (ename)

————————————————————————————————

4. default constraint:
the meaning was simply to let the data for this column defaults to a certain data

Format:
ALTER TABLE Name Table add constraint constraint constraint Name Type Default) for the column name

For example: gongzi column of the emp table of default is 10000

alter table emp add constraint jfsd default 10000 for gongzi


————————————————————————————————

The foreign key constraint:
This bit difficult to understand, in fact, the foreign key reference
because the primary key to achieve the integrity of the entity,
the foreign key implements referential integrity,
integrity of the application requirements, the referenced data must exist!

In fact, a quote,
say, a table name is dept there are two data is an ID is a ENAME
the above mentioned id: indicate the product number
ename: indicates the name of the product

Another name is emp table there are two data, one is an ID is DID
the above mentioned id: means that the user number
did: represents the number of products purchased

Let the emp table dept did a column to the table reference id

You can use the following method

Format:
ALTER table Table add constraint constraint constraint Table Name Name Type (column names) references cited (column names)

example:

alter table emp add constraint jfkdsj foreign key (did) references dept (id)

 

 

 


Constraint definitions stored in the data dictionary, the query USER_CONSTRAINTS can get information.


定义约束
CREATE TABLE [schema.]table
                  (column datatype [DEFAULT expr]
                  [column_constraint],
                      ...
                  [table_constraint][,...]);
e.g.
CREATE TABLE employees
                  (employee_id NUMBER(6),
                      first_name     VARCHAR2(20),
                      ...
                      job_id           VARCHAR2(10) NOT NULL,
                      CONSTRAINTS emp_emp_id_pk PRIMARY KEY (EMPLOYEE_ID));
列级的约束定义
column [CONSTRAINT constraint_name] constraint_type,
表级约束的定义
column,..
[CONSTRAINT constraint_name] constraint_type (column,...)

NOT NULL constraints
can only be defined at the column level, can not be defined at the table level.
EG
the CREATE TABLE the Employees
                  (employee_id NUMBER (6),
                      last_name VARCHAR2 (25) NOT NULL, // no name is specified, the system will be named with SYS_
                      salary NUMBER ( 8,2),
                      commission_pct NUMBER (2, 2),
                      hire_date DATE CONSTRAINT emp_hire_date_nn the NOT NULL); // user-defined name

UNIQUE constraint
is used to protect one or more columns in a table without any rows two columns having received protected data .ORACLE repeated automatically generate a unique index on the unique key column to achieve uniqueness
EG
the CREATE TABLE the Employees
                  ( NUMBER the employee_id (. 6),
                      last_name VARCHAR2 (25) the NOT NULL, 
                      the salary NUMBER (8,2),
                      a commission_pct NUMBER (2,2 &),
                      the hire_date DATE the NOT NULL,
                      CONSTRAINT emp_email_uk UNIQUE (In Email));

PRIMARY KEY constraints
all features unique keys are applied to the primary key constraint, but does not allow NULL values in a primary key column. A table can have a primary key.
EG
the CREATE TABLE Departments
                  (DEPARTMENT_ID NUMBER (. 4),
                      DEPARTMENT_NAME VARCHAR2 (30) CONSTRAINT the NOT NULL dept_name_nn,
                      the manager_id NUMBER (. 6),
                      location_id NUMBER (. 4),
                      CONSTRAINT dept_id_pk a PRIMARY KEY (DEPARTMENT_ID));

FOREIGN KEY constraints
used to protect a table or a plurality of columns, it will pass a primary key or a unique key to ensure that the primary key for each non-NULL value elsewhere in the database has data available. This key is generated outside this constraint table (child table) of one or more columns in the parent and child tables, the data type of the associated column must match the external key column and a reference key (reference key) column may be located in the same table (from referential integrity constraints).
EG
the CREATE TABLE the Employees
                  (the employee_id NUMBER (. 6),
                      last_name VARCHAR2 (25) the NOT NULL, 
                      the salary NUMBER (8,2),
                      a commission_pct NUMBER (2,2 &),
                      the hire_date DATE the NOT NULL,
                      deparment_id NUMBER (. 4 ),
                      CONSTRAINT EMP_DEPT_FK and a FOREIGN KEY (DEPARTMENT_ID) the REFERENCES Departments (DEPARTMENT_ID),
                      CONSTRAINT emp_email_uk UNIQUE (In Email));

The embodiment is defined at the table level foreign key constraint, if defined at the column level, except that:
the CREATE TABLE the Employees
                  (...,
                      DEPARTMENT_ID NUMBER (. 4) CONSTRAINT emp_deptid_fk the REFERENCES Departments (DEPARTMENT_ID),
                  ...);
                   // kEY keyword is not a FOREIGN
a FOREIGN kEY constraint there are two key
when deleting the parent table referenced records, delete records related to the child table - ON dELETE CASCADE
when ON dELETE SET NULL-- above difference is deleted , the conversion sub-table related records a NULL value
default, if there is no record of any one of the above two, the referenced parent table can not be deleted designated.

CHECK constraints
[CONSTRAINT <constraint name>] CHECK (<condition>
herein should CHECK CONDITION clause evaluates a boolean result, and may reference the values of other columns in the same row; can not contain subqueries, sequence, function environment and, on a column (SYSDATE, UID, USER, USERENV ) pseudo-column (ROWNUM, LEVEL, CURRVAL, NEXTVAL ) can define multiple CHECK constraints, if the conditions defined for FALSE, then the statement is rolled back.
the CREATE TABLE the Employees
                  ( ...,
                      the salary NUMBER (8,2) CONSTRAINT the EMP_SALARY_MIN the CHECK (the salary> 0),
                      ...);

 


Add constraint
the ALTER TABLE the Employees
the ADD CONSTRAINT emp_manager_fk a FOREIGN KEY (manager_id) the REFERENCES the Employees (employee_id);
delete constraints
the ALTER TABLE the Employees
DROP CONSTRAINT emp_manager_fk;
the ALTER TABLE departments
DROP PRIMARY KEY CASCADE; // Since departments are employees.department_id quoted
NOT NULL constraints for with ALTER TABLE MODIFY clause to remove the
ALTER TABLE employees MODIFY last_name NULL;
closed constraint
the ALTER TABLE the Employees
DISABLE cONSTRAINT EMP_EMP_ID_PK CASCADE; // if not quoted CASCADE keywords you do not need
when you generate a constraint, the constraint is automatically opened (unless you specify DISABLE clause 0, when the DISABLE closed with a uNIQUE or PRIMARY KEY constraint, ORACLE will automatically delete the associated unique index opened again, ORACLE will automatically created.
open the constraints
the ALTER TABLE the Employees
ENABLE cONSTRAINT EMP_EMP_ID_PK; // Note that the primary key constraint is open a previously closed reference does not automatically open the associated foreign key constraint

Constraints can be queried from USER_CONSTRAINTS tables and views information USER_CONS_COLUMNS
the SELECT constraint_name, constraint_type, a search_condition
the FROM USER_CONSTRAINTS
the WHERE table_name = 'the EMPLOYEES';

Constraint Type
C - CHECK NOT NULL and are considered as the TYPE C
P - a PRIMARY KEY
R & lt - INTEGRITY referential constraint is the external key
U - UNIQUE

SELECT constraint_name,column_name
FROM user_cons_columns
WHERE   table_name='EMPLOYEES';

The main constraint is to increase the data.

 

Oracle constraints in brief

Constraints Including Constraints

    Using constraints (constraints) in the database in order to implement the so-called "business rules" in the database is actually to prevent illegal access to the database information, to meet the rules set administrators and application developers to define.

    ORACLE integrity constraints (integrity constraints) to prevent the illegal data written to the database administrators and developers can define integrity rules, enhanced business rules to restrict data in the data table. If any of the results of a DML statement is executed destroyed integrity constraints, ORACLE rollback statement will return an error message.

    Constraint by using the CREATE TABLE statement or the ALTER TABLE generated (or can be modified after the establishment table creating table) if the associated constraints defined in the single row, the constraint can specify that a column defined above;. A plurality of rows constraints must be defined , related columns to be specified in the data table level in parentheses, separated by commas. If you do not provide a name for the constraint, ORACLE will be assigned a unique name of a system-generated, begins with SYS_, you can use the keyword cONSTRAINTS follow behind constraints related to the constraint name specified name.

ORACLE supports five types of integrity constraints
the NOT NULL (non-empty) - prevents NULL value into the specified column is defined on the basis of the single, default, ORACLE NULL values allowed in any column.
The CHECK (check) - - check constraints specified in the conditions have been met.
uNIQUE (unique) - to ensure that no duplicate values in the specified column of each value in the table or each set of values will be unique..
a PRIMARY KEY ( primary key) - uniquely identifies each row in the table, and to prevent occurrence NULL value, a table can have only one primary key constraint.
POREIGN kEY (external key) - to establish a parent-child between tables by using the common column (parent-child) relationship, the foreign key can be defined on a table to the primary key or other unique key table.


Several constraints, and list them:

: 1. The primary key constraint
conditions for a primary key constraint on a column added, then this column must be met is the partial space
as the primary key constraint: that is, one column of the constraint, the constraint is a (non-empty, not repeated)
The following is the code to to add a primary key column, the column named id, name table emp

Format:
the ALTER the Table table name add constraint constraint names added constraint types (column name)

例子:
alter table emp add constraint ppp primary key (id)

————————————————————————————————

2.check constraint:
that the data were a limiting
example, the age column of data must be greater than 20
table (emp) column name (age)

Format:
ALTER TABLE Name Table add constraint constraint constraint types (column names) in the name of increased

例子:
alter table emp add constraint xxx check(age>20)

______________________________________________________________________

3.unique constraint:
this constraint is added to the data string does not overlap constraint type

Format:
the ALTER the Table table name add constraint constraint name constraint types (column name)
for example can give ename columns add a unique, let the data ename column is not repeated
examples:
the ALTER QWE the Table emp add constraint UNIQUE (ename)

————————————————————————————————

4. default constraint:
the meaning was simply to let the data for this column defaults to a certain data

Format:
ALTER TABLE Name Table add constraint constraint constraint Name Type Default) for the column name

For example: gongzi column of the emp table of default is 10000

alter table emp add constraint jfsd default 10000 for gongzi


————————————————————————————————

The foreign key constraint:
This bit difficult to understand, in fact, the foreign key reference
because the primary key to achieve the integrity of the entity,
the foreign key implements referential integrity,
integrity of the application requirements, the referenced data must exist!

In fact, a quote,
say, a table name is dept there are two data is an ID is a ENAME
the above mentioned id: indicate the product number
ename: indicates the name of the product

Another name is emp table there are two data, one is an ID is DID
the above mentioned id: means that the user number
did: represents the number of products purchased

Let the emp table dept did a column to the table reference id

You can use the following method

Format:
ALTER table Table add constraint constraint constraint Table Name Name Type (column names) references cited (column names)

example:

alter table emp add constraint jfkdsj foreign key (did) references dept (id)

 

 

 


Constraint definitions stored in the data dictionary, the query USER_CONSTRAINTS can get information.


定义约束
CREATE TABLE [schema.]table
                  (column datatype [DEFAULT expr]
                  [column_constraint],
                      ...
                  [table_constraint][,...]);
e.g.
CREATE TABLE employees
                  (employee_id NUMBER(6),
                      first_name     VARCHAR2(20),
                      ...
                      job_id           VARCHAR2(10) NOT NULL,
                      CONSTRAINTS emp_emp_id_pk PRIMARY KEY (EMPLOYEE_ID));
列级的约束定义
column [CONSTRAINT constraint_name] constraint_type,
表级约束的定义
column,..
[CONSTRAINT constraint_name] constraint_type (column,...)

NOT NULL constraints
can only be defined at the column level, can not be defined at the table level.
EG
the CREATE TABLE the Employees
                  (employee_id NUMBER (6),
                      last_name VARCHAR2 (25) NOT NULL, // no name is specified, the system will be named with SYS_
                      salary NUMBER ( 8,2),
                      commission_pct NUMBER (2, 2),
                      hire_date DATE CONSTRAINT emp_hire_date_nn the NOT NULL); // user-defined name

UNIQUE constraint
is used to protect one or more columns in a table without any rows two columns having received protected data .ORACLE repeated automatically generate a unique index on the unique key column to achieve uniqueness
EG
the CREATE TABLE the Employees
                  ( NUMBER the employee_id (. 6),
                      last_name VARCHAR2 (25) the NOT NULL, 
                      the salary NUMBER (8,2),
                      a commission_pct NUMBER (2,2 &),
                      the hire_date DATE the NOT NULL,
                      CONSTRAINT emp_email_uk UNIQUE (In Email));

PRIMARY KEY constraints
all features unique keys are applied to the primary key constraint, but does not allow NULL values in a primary key column. A table can have a primary key.
EG
the CREATE TABLE Departments
                  (DEPARTMENT_ID NUMBER (. 4),
                      DEPARTMENT_NAME VARCHAR2 (30) CONSTRAINT the NOT NULL dept_name_nn,
                      the manager_id NUMBER (. 6),
                      location_id NUMBER (. 4),
                      CONSTRAINT dept_id_pk a PRIMARY KEY (DEPARTMENT_ID));

FOREIGN KEY constraints
used to protect a table or a plurality of columns, it will pass a primary key or a unique key to ensure that the primary key for each non-NULL value elsewhere in the database has data available. This key is generated outside this constraint table (child table) of one or more columns in the parent and child tables, the data type of the associated column must match the external key column and a reference key (reference key) column may be located in the same table (from referential integrity constraints).
EG
the CREATE TABLE the Employees
                  (the employee_id NUMBER (. 6),
                      last_name VARCHAR2 (25) the NOT NULL, 
                      the salary NUMBER (8,2),
                      a commission_pct NUMBER (2,2 &),
                      the hire_date DATE the NOT NULL,
                      deparment_id NUMBER (. 4 ),
                      CONSTRAINT EMP_DEPT_FK and a FOREIGN KEY (DEPARTMENT_ID) the REFERENCES Departments (DEPARTMENT_ID),
                      CONSTRAINT emp_email_uk UNIQUE (In Email));

The embodiment is defined at the table level foreign key constraint, if defined at the column level, except that:
the CREATE TABLE the Employees
                  (...,
                      DEPARTMENT_ID NUMBER (. 4) CONSTRAINT emp_deptid_fk the REFERENCES Departments (DEPARTMENT_ID),
                  ...);
                   // kEY keyword is not a FOREIGN
a FOREIGN kEY constraint there are two key
when deleting the parent table referenced records, delete records related to the child table - ON dELETE CASCADE
when ON dELETE SET NULL-- above difference is deleted , the conversion sub-table related records a NULL value
default, if there is no record of any one of the above two, the referenced parent table can not be deleted designated.

CHECK constraints
[CONSTRAINT <constraint name>] CHECK (<condition>
herein should CHECK CONDITION clause evaluates a boolean result, and may reference the values of other columns in the same row; can not contain subqueries, sequence, function environment and, on a column (SYSDATE, UID, USER, USERENV ) pseudo-column (ROWNUM, LEVEL, CURRVAL, NEXTVAL ) can define multiple CHECK constraints, if the conditions defined for FALSE, then the statement is rolled back.
the CREATE TABLE the Employees
                  ( ...,
                      the salary NUMBER (8,2) CONSTRAINT the EMP_SALARY_MIN the CHECK (the salary> 0),
                      ...);

 


Add constraint
the ALTER TABLE the Employees
the ADD CONSTRAINT emp_manager_fk a FOREIGN KEY (manager_id) the REFERENCES the Employees (employee_id);
delete constraints
the ALTER TABLE the Employees
DROP CONSTRAINT emp_manager_fk;
the ALTER TABLE departments
DROP PRIMARY KEY CASCADE; // Since departments are employees.department_id quoted
NOT NULL constraints for with ALTER TABLE MODIFY clause to remove the
ALTER TABLE employees MODIFY last_name NULL;
closed constraint
the ALTER TABLE the Employees
DISABLE cONSTRAINT EMP_EMP_ID_PK CASCADE; // if not quoted CASCADE keywords you do not need
when you generate a constraint, the constraint is automatically opened (unless you specify DISABLE clause 0, when the DISABLE closed with a uNIQUE or PRIMARY KEY constraint, ORACLE will automatically delete the associated unique index opened again, ORACLE will automatically created.
open the constraints
the ALTER TABLE the Employees
ENABLE cONSTRAINT EMP_EMP_ID_PK; // Note that the primary key constraint is open a previously closed reference does not automatically open the associated foreign key constraint

Constraints can be queried from USER_CONSTRAINTS tables and views information USER_CONS_COLUMNS
the SELECT constraint_name, constraint_type, a search_condition
the FROM USER_CONSTRAINTS
the WHERE table_name = 'the EMPLOYEES';

Constraint Type
C - CHECK NOT NULL and are considered as the TYPE C
P - a PRIMARY KEY
R & lt - INTEGRITY referential constraint is the external key
U - UNIQUE

Constraint_name the SELECT, column_name
the FROM USER_CONS_COLUMNS
the WHERE table_name = 'the EMPLOYEES';
---------------------
Author: shaderdx
Source: CSDN
Original: https: //blog.csdn. net / shaderdx / article / details / 77184924
copyright: This article is a blogger original article, reproduced, please attach Bowen link!

Guess you like

Origin www.cnblogs.com/lsswudi/p/10941821.html