Unique constraints for Oracle

Source: Internet
Author: User

A unique constraint is a uniqueness constraint that repeats a divergence value for an application that requires a column type. You can add a separate column to the unique constraint. Ability to add multiple columns to a unique constraint (). Assuming that a unique constraint is added to multiple columns, it is only necessary to ensure that the values of these columns are not all the same.
When you create a table, add a unique constraint to the column. Forms such as the following:
column_name data_type [constraint constraint_name] unique or
CREATE TABLE table_name (
column_name data_type,[,...]
[Constraint constraint_name] Unique (column_name)
[,...]
)
It is also possible to constrain Lietina in a table that has already been created, and you will need to use alter TALBE. Add statement. Forms such as the following:
ALTER TABLE table_name ADD [constraint constraint_name] Unique (column_name);


Delete a unique constraint
To delete a unique constraint on a column, you can use the ALTER TABLE...DROP statement in the form such as the following:
ALTER TABLE table_name drop unique (column_name)
Assume that the constraint has a name. You can also delete the constraint using the specified name, such as the following statement form:
ALTER TABLE table drop constraint constraint_name;


Detailed actions such as the following:
sql> CREATE TABLE person
2 (PID number (4) is not null unique,
3 pname varchar2 (Ten) Unique,
4 Sex char (2)
5);
The table is created.
Create a person table above and specify a unique constraint for both PID and PName
Sql> INSERT into the person values (1, ' aaa ', ' female ');
1 rows have been created.
Sql> INSERT into the person values (2, ' aaa ', ' female ');
INSERT into person values (2, ' aaa ', ' female ')
*
An error occurred on line 1th:
ORA-00001: Violates the unique constraint (SYSTEM. sys_c0010079)
Sql> INSERT into the person values (2, ' abcd ', ' female ');
1 rows have been created.
As seen above, when adding the name of the same times the wrong.
Sql> INSERT into the person values (2, ' abcd ', ' female ');
1 rows have been created
Change under PName. There is no error at this time.


Another table is created.


Sql> CREATE TABLE P2 (
2 PID Number (4),
3 pname varchar2 (10),
4 Psex char (2),
5 constraint P2_unique unique (pid,pname)
6);
The table is created.
Sql> ALTER TABLE p2 add constraint unique_p2sex unique (psex); --Join unique for Psex
The table has changed.
sql> ALTER TABLE P2 drop unique (psex); --delete unique UNIQUE constraint
The table has changed.
sql> ALTER TABLE P2 drop constraint p2_unique;
Table changes.
Sql>

Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.

Unique constraints for Oracle

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.