Create a unique constraint in oracle (Single Column and multi-column)

Source: Internet
Author: User
The unique constraint in oracle is used to ensure that a type in a table or a type in a table is not repeated. When creating a table or creating a table, you can modify the table to create a unique constraint in oracle. The following are examples of creating a unique constraint: createtableunique_test (idnumber, fname

The unique constraint in oracle is used to ensure that a type in a table or a type in a table is not repeated. When creating a table or creating a table, you can modify the table to create a unique constraint in oracle. The following are examples of creating a unique constraint: create table unique_test (id number, fname

The unique constraint in oracle is used to ensure that a type in a table or a type in a table is not repeated.

When creating a table or creating a table, you can modify the table to create a unique constraint in oracle.

The following are examples of creating a unique constraint:

Create table unique_test (id number, fname varchar2 (20), lname varchar2 (20), address varchar2 (100), email varchar2 (40), constraint name_unique unique (fname, lname ))

Here we create a table unique_test and combine the fname and lname to create a unique constraint.

We can also manually add constraints by modifying the table after the table is created,

Example: alter table unique_test add constraint email_unique unique (email );

Insert into unique_test (id, fname, lname) values (1, 'deve', 'Liu ')

This row can be inserted normally because we used the combination of fname and lname as a constraint when creating the table, because if we want to insert Liu Dehua again,

Insert into unique_test (id, fname, lname) values (2, 'deve', 'Liu ') is prone to the following error: ORA-00001: violation of the unique constraint (SYS. NAME_UNIQUE)

However, if we change it to the following value: insert into unique_test (id, fname, lname) values (2, 'xueyou ', 'zhang'); then we can insert it normally.

Some may have doubts. Aren't we the only constraint for email? Why are the two rows of data not assigned a value to the email column, that is, the email column of the two rows is null, and the insertion is successful? This is because a null value means that the current state of the column does not exist and it will never be equal to another null value. So there is no violation of the unique constraint.

Above from http://wj98127.javaeye.com/blog/360644

If the duplicate keyword is not found,

1. Check whether the constraint exists.

2. Check whether repeated fields exist in the table.

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.