Oracle UNIQUE constraint and unique index ____oracle

Source: Internet
Author: User

Uniqueness Constraint

A uniqueness constraint refers to a constraint in a table in which a field or multiple fields combine to uniquely identify a record. Union field, you can include null values.
Note: In Oracle, uniqueness constraints can have up to 32 columns.
Uniqueness constraints can be created when a table is created or by using the ALTER TABLE statement.

Differences between uniqueness constraints and primary keys
Primary KEY (Primary key): All columns that make up a primary key cannot contain null values. Uniqueness constraint (unique Constraint): If a uniqueness constraint consists of multiple columns, some of the columns can contain null values. It is not allowed in Oracle to create both a primary key and a uniqueness constraint on the same column.

1. Create joint constraints (the following SQL function is staff_id, first_name, last_name not equal at the same time)

Eg:alter Table T_gjcx_emp

Add constraint T_gjcx_emp_joint_key unique (staff_id, first_name, last_name)
(1) Uniqueness constraint based on a single column
ALTER TABLE Tb_supplier
Add Constraint tb_supplier_u1
Unique (supplier_id);

(2) Uniqueness constraint based on multiple columns
ALTER TABLE Tb_products
Add Constraint tb_products_u1
Unique (product_id,product_name);

2. Disable Uniqueness Constraint
(1) Syntax:
ALTER TABLE table_name
DISABLE CONSTRAINT constraint_name;
(2) Example:
ALTER TABLE Tb_supplier
DISABLE CONSTRAINT tb_supplier_u1;

question:

Create unique index idx_ut on t (NVL2 (Nullif (b,3), a,null), NVL2 (Nullif (b,3), b,null), NVL2 (Nullif (b,3), C,null ))】

Similarities and differences in uniqueness constraints for creating tables:
A unique constraint can be used to guarantee that one or more column values are unique when a record is added to the base table. If a unique constraint has been given or defined above, no action is possible to replace the copied value in those columns. Although unique, system-required indexes are used to enforce unique constraints, there is a difference between defining a unique constraint and creating a unique index. Even though both of these can enhance uniqueness, a unique index allows null values and is generally not used in an indicative constraint. In other words, a unique constraint does not allow null values and can be used in the foreign Key specification ("NULL" means that the column value is ambiguous and differs from other values, including other NULL values).

Summary: A unique index is a unique index, which is implemented by a unique index, and a unique index is created when a unique constraint is created if there is no unique index on the constraint column.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.