Oracle field constraints

First Constraint

  Constraints are the means used by the database to ensure that the data meets business rules and restrict the conditions imposed on the data.

Types of constraints

1. The primary key constraint (PRIMARY KEY)

2. Uniqueness constraint (UNIQUE)

3. Not NULL constraint (NOT NULL)

4. Check constraints (CHECK)

5. Foreign key constraint (FOREIGN KEY)

Primary key constraint (PRIMARY KEY)

  1. Non-empty and unique constraints;

  2. A table has only one primary key;

  3. The primary key will be indexed by default;

Uniqueness constraint (UNIQUE)

For UNIQUE constraints, the index is necessary. If it does not exist, it will automatically create one (UNIQUE's uniqueness is essentially guaranteed by the index)

UNIQUE allows null values. UNIQUE constraint columns can have multiple nulls. This is because the uniqueness of Unique is realized by the btree index, and null is not included in the btree index. Therefore, this also causes the use of null values ​​in the where statement to filter will cause a full table scan.

Remove constraints

alter table table_name drop constraint constraint_name;

Non-NULL constraint (NOT NULL)

Columns with non-empty constraints are also called mandatory columns. As the name implies, there must be a value in the mandatory key column. Of course, if you use the default keyword to specify the default value when building the table, you can not enter it.

Foreign key constraint (FOREIGN KEY)

  The foreign key constraint is defined in the child table with the parent-child relationship. The foreign key constraint makes the columns in the child table correspond to the primary key columns of the parent table to maintain the integrity of the database. However, due to performance and later business system expansion considerations, many times, foreign key constraints only appear in the design of the database, and will actually be placed in the business program for processing. Foreign key constraints pay attention to the following points:

  1. The data type of the column in the child table of the foreign key constraint and the column of the corresponding parent table must be the same, the column name can be different
  1. Corresponding parent table column must exist primary key constraint (PRIMARY KEY) or unique constraint (UNIQUE)

 

Guess you like

Origin www.cnblogs.com/kaduoxi1999987/p/12677594.html