Creating unique UNIQUE constraints in the database

Source: Internet
Author: User

The requirements of the recent project, to determine the only one piece of data, the original seems to have met, forget, and now record. Implementing a UNIQUE constraint may also not be unique to the primary key.


A UNIQUE constraint in Oracle is a means of ensuring that a class in a table, or a class in a table, is combined to be non-repetitive. We can create a unique constraint in Oracle by modifying the table when the table is created or created.

Here are some examples of creating unique constraints:

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 set up a table unique_test and combine the fname and lname together to create a unique constraint.

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

ALTER TABLE Unique_test

Add constraint email_unique unique (email);

Below we insert data into the table,

Insert into Unique_test (id,fname,lname) VALUES (1, ' Tak Wah ', ' Liu ')

This line can be inserted as normal

Because we used to combine fname and lname as a constraint when we set up the table, because if we want to insert Andy Lau again,

Insert into Unique_test (Id,fname,lname) VALUES (2, ' Tak Wah ', ' Liu ')

The following error will occur:

ORA-00001: Violation of the unique constraint (sys.name_unique)

But if we change to the following values:

Insert into Unique_test (Id,fname,lname) VALUES (2, ' xueyou ', ' Zhang ');

It can also be inserted normally.

Some friends may have questions, we are not a unique constraint for email also established? Why aren't the two rows of data assigned to an email column, or two rows of e-mail columns that are empty values, and that the insert is successful?

This is because a null value (NULL) means that the current state of the column does not exist and that he will never be equal to another null value. So there's no violation of the sole constraint.


This article is from the "Java Program Ape Blog" blog, be sure to keep this source http://chengxuyuan.blog.51cto.com/5789198/1768512

Creating unique UNIQUE constraints in the database

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.